Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame^] | 1 | import com.sun.jersey.api.client.Client; |
| 2 | import com.sun.jersey.api.client.WebResource; |
| 3 | import com.sun.jersey.api.core.DefaultResourceConfig; |
| 4 | import com.sun.jersey.spi.inject.SingletonTypeInjectableProvider; |
| 5 | import com.sun.jersey.test.framework.AppDescriptor; |
| 6 | import com.sun.jersey.test.framework.LowLevelAppDescriptor; |
| 7 | import com.sun.jersey.test.framework.spi.container.TestContainer; |
| 8 | import com.sun.jersey.test.framework.spi.container.TestContainerFactory; |
| 9 | import com.sun.jersey.test.framework.spi.container.grizzly.GrizzlyTestContainerFactory; |
| 10 | import org.junit.AfterClass; |
| 11 | import org.junit.Before; |
| 12 | import org.junit.BeforeClass; |
| 13 | |
| 14 | import javax.ws.rs.core.Context; |
| 15 | import javax.ws.rs.core.UriBuilder; |
| 16 | import java.net.URI; |
| 17 | |
| 18 | /** |
| 19 | * @author hanl |
| 20 | * @date 29/07/2015 |
| 21 | */ |
| 22 | public abstract class FastJerseyTest { |
| 23 | |
| 24 | protected static String API_VERSION = "v0.1"; |
| 25 | |
| 26 | private static DefaultResourceConfig resourceConfig = new DefaultResourceConfig(); |
| 27 | |
| 28 | private static TestContainerFactory testContainerFactory; |
| 29 | |
| 30 | private static TestContainer testContainer; |
| 31 | |
| 32 | private static Client client; |
| 33 | |
| 34 | public static void addClass(Class<?> resourceClass) { |
| 35 | resourceConfig.getClasses().add(resourceClass); |
| 36 | } |
| 37 | |
| 38 | public static void addSingleton(Object resourceSingleton) { |
| 39 | resourceConfig.getSingletons().add(resourceSingleton); |
| 40 | } |
| 41 | |
| 42 | public static <T> void addProviderForContext(Class<T> contextClass, |
| 43 | T contextObject) { |
| 44 | addSingleton( |
| 45 | new SingletonTypeInjectableProvider<Context, T>(contextClass, |
| 46 | contextObject) { |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | public static void addRequestFilter(Object filter) { |
| 51 | resourceConfig.getContainerRequestFilters().add(filter); |
| 52 | } |
| 53 | |
| 54 | public static void addResponseFilter(Object filter) { |
| 55 | resourceConfig.getContainerResponseFilters().add(filter); |
| 56 | } |
| 57 | |
| 58 | public static void setTestContainerFactory( |
| 59 | TestContainerFactory newTestContainerFactory) { |
| 60 | testContainerFactory = newTestContainerFactory; |
| 61 | } |
| 62 | |
| 63 | @BeforeClass |
| 64 | public static void cleanStaticVariables() { |
| 65 | resourceConfig = new DefaultResourceConfig(); |
| 66 | } |
| 67 | |
| 68 | public static void initServer() { |
| 69 | AppDescriptor ad = new LowLevelAppDescriptor.Builder(resourceConfig) |
| 70 | .build(); |
| 71 | TestContainerFactory tcf = testContainerFactory; |
| 72 | if (tcf == null) { |
| 73 | tcf = new GrizzlyTestContainerFactory(); |
| 74 | } |
| 75 | testContainer = tcf |
| 76 | .create(UriBuilder.fromUri("http://localhost/").port(9998) |
| 77 | .build(), ad); |
| 78 | client = testContainer.getClient(); |
| 79 | if (client == null) { |
| 80 | client = Client.create(ad.getClientConfig()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public static void startServer() { |
| 85 | if (testContainer != null) { |
| 86 | testContainer.start(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @AfterClass |
| 91 | public static void stopServer() { |
| 92 | testContainer.stop(); |
| 93 | testContainer = null; |
| 94 | client = null; |
| 95 | } |
| 96 | |
| 97 | public Client client() { |
| 98 | return client; |
| 99 | } |
| 100 | |
| 101 | public URI getBaseUri() { |
| 102 | return testContainer.getBaseUri(); |
| 103 | } |
| 104 | |
| 105 | public WebResource resource() { |
| 106 | return client.resource(getBaseUri()); |
| 107 | } |
| 108 | |
| 109 | @Before |
| 110 | public void startServerBeforeFirstTestRun() { |
| 111 | if (testContainer == null) { |
| 112 | initServer(); |
| 113 | startServer(); |
| 114 | } |
| 115 | } |
| 116 | } |