1 | package clarin.cmdi.componentregistry.rest; |
---|
2 | |
---|
3 | import java.net.URI; |
---|
4 | |
---|
5 | import com.sun.jersey.test.framework.AppDescriptor; |
---|
6 | import com.sun.jersey.test.framework.JerseyTest; |
---|
7 | import com.sun.jersey.test.framework.spi.container.TestContainer; |
---|
8 | import com.sun.jersey.test.framework.spi.container.TestContainerFactory; |
---|
9 | |
---|
10 | /** |
---|
11 | * Factory for {@link JerseyTest}s that returns reusable servlet container testing contexts thus reducing startup time |
---|
12 | * @author george.georgovassilis@mpi.nl |
---|
13 | * |
---|
14 | */ |
---|
15 | public class SingletonTestContainerFactory implements TestContainerFactory { |
---|
16 | |
---|
17 | private TestContainerFactory targetFactory; |
---|
18 | private TestContainer testContainer; |
---|
19 | private boolean testContainerStarted = false; |
---|
20 | |
---|
21 | public void startTestContainer() { |
---|
22 | testContainer.start(); |
---|
23 | testContainerStarted = true; |
---|
24 | } |
---|
25 | |
---|
26 | public void stopTestContainer() { |
---|
27 | testContainer.stop(); |
---|
28 | testContainerStarted = false; |
---|
29 | } |
---|
30 | |
---|
31 | public boolean isTestContainerRunning() { |
---|
32 | return testContainerStarted; |
---|
33 | } |
---|
34 | |
---|
35 | public SingletonTestContainerFactory(TestContainerFactory factory) { |
---|
36 | this.targetFactory = factory; |
---|
37 | } |
---|
38 | |
---|
39 | @Override |
---|
40 | public <T extends AppDescriptor> Class<T> supports() { |
---|
41 | return targetFactory.supports(); |
---|
42 | } |
---|
43 | |
---|
44 | @Override |
---|
45 | public TestContainer create(URI baseUri, AppDescriptor ad) |
---|
46 | throws IllegalArgumentException { |
---|
47 | if (testContainer == null) |
---|
48 | testContainer = targetFactory.create(baseUri, ad); |
---|
49 | return testContainer; |
---|
50 | } |
---|
51 | } |
---|