Skip to content

Instantly share code, notes, and snippets.

@Synesso
Created February 28, 2013 07:09
Show Gist options
  • Save Synesso/5054853 to your computer and use it in GitHub Desktop.
Save Synesso/5054853 to your computer and use it in GitHub Desktop.
package org.togglz.spring.test.proxy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.DependencyResolvers;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.togglz.core.context.FeatureContext;
import org.togglz.core.repository.FeatureState;
import org.togglz.test.Deployments;
@RunWith(Arquillian.class)
public class FeatureProxyTest {
@Deployment
public static WebArchive createDeployment() {
return Deployments.getBasicWebArchive()
.addAsLibrary(Deployments.getTogglzSpringArchive())
.addAsLibraries(
DependencyResolvers.use(MavenDependencyResolver.class)
.artifact("org.springframework:spring-web:3.0.7.RELEASE")
.resolveAs(JavaArchive.class))
.addAsWebInfResource("applicationContext.xml")
.addAsWebInfResource("applicationContext-proxy.xml")
.setWebXML("spring-web.xml")
.addClass(ProxyFeatures.class)
.addClass(FeatureProxyConfiguration.class)
.addClass(SomeService.class)
.addClass(SomeServiceActive.class)
.addClass(SomeServiceInactive.class);
}
@Test
public void testProxyWithManuallySetProxyType() throws IOException {
// obtain the service
SomeService someService = getSpringBeanFromXml("someServiceManuallySetProxyType");
assertNotNull(someService);
// disable the feature flag
FeatureContext.getFeatureManager().setFeatureState(new FeatureState(ProxyFeatures.SERVICE_TOGGLE, false));
// first the inactive Service is invoked
assertEquals("I'm SomeServiceInactive", someService.whoAreYou());
// enable the feature flag
FeatureContext.getFeatureManager().setFeatureState(new FeatureState(ProxyFeatures.SERVICE_TOGGLE, true));
// calls are now delegated to the other service implementation
assertEquals("I'm SomeServiceActive", someService.whoAreYou());
}
@Test
public void testProxyFromXmlWithAutoDetectedProxyType() throws IOException {
// obtain the service
SomeService someService = getSpringBeanFromXml("someServiceAutoDetectProxyType");
assertNotNull(someService);
// disable the feature flag
FeatureContext.getFeatureManager().setFeatureState(new FeatureState(ProxyFeatures.SERVICE_TOGGLE, false));
// first the inactive Service is invoked
assertEquals("I'm SomeServiceInactive", someService.whoAreYou());
// enable the feature flag
FeatureContext.getFeatureManager().setFeatureState(new FeatureState(ProxyFeatures.SERVICE_TOGGLE, true));
// calls are now delegated to the other service implementation
assertEquals("I'm SomeServiceActive", someService.whoAreYou());
}
@Test
public void testProxyFromConfigurationBeanWithAutoDetectedProxyType() throws IOException {
// obtain the service
SomeService someService = getSpringBeanFromConfigurationBean();
assertNotNull(someService);
// disable the feature flag
FeatureContext.getFeatureManager().setFeatureState(new FeatureState(ProxyFeatures.SERVICE_TOGGLE, false));
// first the inactive Service is invoked
assertEquals("I'm SomeServiceInactive", someService.whoAreYou());
// enable the feature flag
FeatureContext.getFeatureManager().setFeatureState(new FeatureState(ProxyFeatures.SERVICE_TOGGLE, true));
// calls are now delegated to the other service implementation
assertEquals("I'm SomeServiceActive", someService.whoAreYou());
}
private SomeService getSpringBeanFromXml(String name) {
WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext();
return (SomeService) applicationContext.getBean(name);
}
private SomeService getSpringBeanFromConfigurationBean() {
ApplicationContext context = new AnnotationConfigApplicationContext(SomeServiceConfiguration.class);
return context.getBean(SomeService.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment