Created
April 9, 2012 16:42
-
-
Save valotas/2344641 to your computer and use it in GitHub Desktop.
Better JerseyTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BetterJerseyTest extends JerseyTest { | |
//Fire up jersey with Guice | |
private static final AppDescriptor APP_DESCRIPTOR = new WebAppDescriptor.Builder("com.some.package.name") | |
.filterClass(GuiceFilter.class) | |
.contextPath("jersey-ctx-path") | |
.servletPath("/") | |
.clientConfig(new DefaultClientConfig(JacksonJaxbJsonProvider.class)) | |
.build(); | |
public JerseyTestNG() { | |
super(APP_DESCRIPTOR); | |
} | |
@Override | |
protected TestContainerFactory getTestContainerFactory() throws TestContainerException { | |
return new OnePerAppDescriptorTestContainerFactory(super.getTestContainerFactory()); | |
} | |
@BeforeSuite | |
public void initTestContainer() throws Exception { | |
setUp(); | |
} | |
@AfterSuite | |
public void tearDownTestContainer() throws Exception { | |
tearDown(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OnePerAppDescriptorTestContainerFactory implements TestContainerFactory { | |
private static final ConcurrentMap<AppDescriptor, TestContainer> cache = new ConcurrentHashMap<AppDescriptor, TestContainer>(); | |
private final TestContainerFactory tcf; | |
public OnePerAppDescriptorTestContainerFactory(TestContainerFactory tcf) { | |
this.tcf = tcf; | |
} | |
@Override | |
public <T extends AppDescriptor> Class<T> supports() { | |
return tcf.supports(); | |
} | |
@Override | |
public TestContainer create(URI baseUri, AppDescriptor ad) { | |
if (cache.get(ad) == null) { | |
cache.putIfAbsent(ad, tcf.create(baseUri, ad)); | |
} | |
return cache.get(ad); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment