Jersey 2: Migrate test factories
A few APIs have changed in the class JerseyTest from Jersey 1 to Jersey 2,
but direct replacements are available for most cases albeit the setup is
slightly different.
References:
https://eclipse-ee4j.github.io/jersey.github.io/apidocs/2.32/jersey/org/glassfish/jersey/test/JerseyTest.html
https://eclipse-ee4j.github.io/jersey.github.io/apidocs/2.32/jersey/org/glassfish/jersey/test/ServletDeploymentContext.html
diff --git a/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java b/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
index ac11283..0ce1c11 100644
--- a/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
@@ -1,9 +1,5 @@
package de.ids_mannheim.korap.config;
-import java.io.IOException;
-import java.net.ServerSocket;
-import java.util.concurrent.ThreadLocalRandom;
-
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
@@ -11,15 +7,14 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.support.GenericWebApplicationContext;
-import com.sun.jersey.api.client.config.ClientConfig;
-import com.sun.jersey.api.client.config.DefaultClientConfig;
-import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
-import com.sun.jersey.test.framework.AppDescriptor;
-import com.sun.jersey.test.framework.JerseyTest;
-import com.sun.jersey.test.framework.WebAppDescriptor;
-import com.sun.jersey.test.framework.spi.container.TestContainerException;
-import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
-import com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.servlet.ServletContainer;
+import org.glassfish.jersey.test.DeploymentContext;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.test.ServletDeploymentContext;
+import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
+import org.glassfish.jersey.test.spi.TestContainerException;
+import org.glassfish.jersey.test.spi.TestContainerFactory;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:test-config.xml")
@@ -55,32 +50,11 @@
}
@Override
- protected AppDescriptor configure () {
- // Simulation of the production server
- // Indicate to use codehaus jackson
- ClientConfig config = new DefaultClientConfig();
- config.getFeatures().put("com.sun.jersey.api.json.POJOMappingFeature", true);
-
- return new WebAppDescriptor.Builder(classPackages)
- .servletClass(SpringServlet.class)
- .contextListenerClass(StaticContextLoaderListener.class)
+ protected DeploymentContext configureDeployment() {
+ return ServletDeploymentContext
+ .forServlet(new ServletContainer(new ResourceConfig().packages(classPackages)))
+ .addListener(StaticContextLoaderListener.class)
.contextParam("adminToken", "secret")
.build();
}
-
- @Override
- protected int getPort (int defaultPort) {
- int port = ThreadLocalRandom.current().nextInt(5000, 8000 + 1);
- try {
- ServerSocket socket = new ServerSocket(port);
- socket.close();
- }
- catch (IOException e) {
-// e.printStackTrace();
- System.out.println("[WARNING] " + e.getMessage());
- port = getPort(port);
- }
- return port;
- }
-
}
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/FastJerseyTest.java b/full/src/test/java/de/ids_mannheim/korap/web/FastJerseyTest.java
index a22ded4..ae9a2ef 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/FastJerseyTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/FastJerseyTest.java
@@ -3,6 +3,7 @@
import java.net.URI;
import java.util.concurrent.ThreadLocalRandom;
+import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.UriBuilder;
import org.junit.After;
@@ -10,17 +11,16 @@
import org.junit.BeforeClass;
import org.springframework.web.context.ContextLoaderListener;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.api.core.DefaultResourceConfig;
-import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
-import com.sun.jersey.test.framework.AppDescriptor;
-import com.sun.jersey.test.framework.LowLevelAppDescriptor;
-import com.sun.jersey.test.framework.WebAppDescriptor;
-import com.sun.jersey.test.framework.spi.container.TestContainer;
-import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
-import com.sun.jersey.test.framework.spi.container.grizzly.GrizzlyTestContainerFactory;
-import com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.servlet.ServletContainer;
+import org.glassfish.jersey.test.DeploymentContext;
+import org.glassfish.jersey.test.ServletDeploymentContext;
+import org.glassfish.jersey.test.spi.TestContainer;
+import org.glassfish.jersey.test.spi.TestContainerFactory;
+import org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory;
+import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import de.ids_mannheim.korap.config.BeanConfigTest;
@@ -33,14 +33,14 @@
public final static String API_VERSION = "v0.1";
- private static DefaultResourceConfig resourceConfig =
- new DefaultResourceConfig();
+ private static ResourceConfig resourceConfig =
+ new ResourceConfig();
private static TestContainerFactory testContainerFactory;
protected static TestContainer testContainer;
- protected static Client client;
+ protected static javax.ws.rs.client.Client client;
protected static int PORT = 8089; // FB, was: 9000;
protected static int PORT_IT = 1;
@@ -58,18 +58,18 @@
@BeforeClass
public static void cleanStaticVariables () {
- resourceConfig = new DefaultResourceConfig();
+ resourceConfig = new ResourceConfig();
}
protected static void initServer (int port, String[] classPackages) {
- AppDescriptor ad;
+ DeploymentContext dc;
if (classPackages == null)
- ad = new LowLevelAppDescriptor.Builder(resourceConfig).build();
+ dc = DeploymentContext.builder(resourceConfig).build();
else
- ad = new WebAppDescriptor.Builder(classPackages)
- .servletClass(SpringServlet.class)
- .contextListenerClass(ContextLoaderListener.class)
+ dc = ServletDeploymentContext
+ .forServlet(new ServletContainer(resourceConfig.packages(classPackages)))
+ .addListener(ContextLoaderListener.class)
.contextParam("contextConfigLocation", "classpath:test-config.xml")
.build();
@@ -82,10 +82,10 @@
}
testContainer = tcf.create(
- UriBuilder.fromUri(containerURI).port(port).build(), ad);
- client = testContainer.getClient();
+ UriBuilder.fromUri(containerURI).port(port).build(), dc);
+ client = testContainer.getClientConfig().getClient();
if (client == null) {
- client = Client.create(ad.getClientConfig());
+ client = ClientBuilder.newClient(testContainer.getClientConfig());
}
}
@@ -108,8 +108,8 @@
}
- public WebResource resource () {
- return client.resource(getBaseUri());
+ public WebTarget target () {
+ return client.target(getBaseUri());
}
// protected TestHelper helper () {
diff --git a/lite/src/test/java/de/ids_mannheim/korap/config/LiteJerseyTest.java b/lite/src/test/java/de/ids_mannheim/korap/config/LiteJerseyTest.java
index e6536f5..882efce 100644
--- a/lite/src/test/java/de/ids_mannheim/korap/config/LiteJerseyTest.java
+++ b/lite/src/test/java/de/ids_mannheim/korap/config/LiteJerseyTest.java
@@ -1,9 +1,5 @@
package de.ids_mannheim.korap.config;
-import java.io.IOException;
-import java.net.ServerSocket;
-import java.util.concurrent.ThreadLocalRandom;
-
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
@@ -11,13 +7,14 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.support.GenericWebApplicationContext;
-import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
-import com.sun.jersey.test.framework.AppDescriptor;
-import com.sun.jersey.test.framework.JerseyTest;
-import com.sun.jersey.test.framework.WebAppDescriptor;
-import com.sun.jersey.test.framework.spi.container.TestContainerException;
-import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
-import com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.servlet.ServletContainer;
+import org.glassfish.jersey.test.DeploymentContext;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.test.ServletDeploymentContext;
+import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
+import org.glassfish.jersey.test.spi.TestContainerException;
+import org.glassfish.jersey.test.spi.TestContainerFactory;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:test-config.xml")
@@ -53,26 +50,11 @@
}
@Override
- protected AppDescriptor configure () {
- return new WebAppDescriptor.Builder(classPackages)
- .servletClass(SpringServlet.class)
- .contextListenerClass(StaticContextLoaderListener.class)
+ protected DeploymentContext configureDeployment() {
+ return ServletDeploymentContext
+ .forServlet(new ServletContainer(new ResourceConfig().packages(classPackages)))
+ .addListener(StaticContextLoaderListener.class)
.contextParam("adminToken", "secret")
.build();
}
-
- @Override
- protected int getPort (int defaultPort) {
- int port = ThreadLocalRandom.current().nextInt(5000, 8000 + 1);
- try {
- ServerSocket socket = new ServerSocket(port);
- socket.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- port = getPort(port);
- }
- return port;
- }
-
}