Hi,
I was trying to build the test framework for Jersey, which would
allow tests to be run on the container of choice among - Embedded
Glassfish, Grizzly, HTTPServer, GF v2 and GF v3.
In this regard, I attempted to add this flexibility to the Jersey
HelloWorld-WebApp sample.
Things worked fine with Embedded GF and HTTPServer, but when I run
with Grizzly option, the following error is seen:
====================================================================================
java.lang.NoSuchMethodError:
com.sun.grizzly.http.servlet.ServletAdapter.setContextPath(Ljava/lang/String;)V
at
com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory.create(GrizzlyWebContainerFactory.java:242)
at
com.sun.jersey.qe.tests.helloworld.HelloWorldWebAppTest.startServerAndDeploy(HelloWorldWebAppTest.java:142)
at
com.sun.jersey.qe.tests.helloworld.HelloWorldWebAppTest.setUp(HelloWorldWebAppTest.java:113)
at junit.framework.TestCase.runBare(TestCase.java:128)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
==========================================================================================
Any ideas, as to what might be causing this issue?
Attached are the pom.xml and the source code.
Thanks,
Naresh
/*
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at
https://jersey.dev.java.net/CDDL+GPL.html
* or jersey/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at jersey/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.jersey.qe.tests.helloworld;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.container.ContainerFactory;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.File;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.UriBuilder;
import junit.framework.TestCase;
import org.glassfish.embed.ScatteredWar;
import org.glassfish.embed.GlassFish;
/**
*
* @author Naresh
*/
public class HelloWorldWebAppTest extends TestCase {
private static int getPort(int defaultPort) {
String port = System.getenv("JERSEY_HTTP_PORT");
if (null != port) {
try {
return Integer.parseInt(port);
} catch (NumberFormatException e) {
}
}
return defaultPort;
}
private static URI getBaseURI() {
return UriBuilder.fromUri("
http://localhost/").port(getPort(9998)).
path("helloworld-webapp").build();
}
private static String getContainerType() {
String containerType = System.getProperty("container.type");
return ((containerType != null) ? containerType : EMBEDDED_GF_V3);
}
private static final String CONTAINER_TYPE = getContainerType();
private static final URI BASE_URI = getBaseURI();
private static final String EMBEDDED_GF_V3 = "EmbeddedGF";
private static final String GRIZZLY_WEB_CONTAINER = "Grizzly";
private static final String HTTP_SERVER = "HTTPServer";
private static final String GF_V2 = "GFv2";
private static final String GF_V3 = "GFv3";
private ScatteredWar war;
private GlassFish glassfish;
private Map<String, String> initParams;
private SelectorThread threadSelector;
private Map<String, Object> props;
private ResourceConfig resourceConfig;
private HttpServer server;
private HttpHandler container;
private WebResource r;
public HelloWorldWebAppTest(String testName) {
super(testName);
System.out.println("Run with container :: " + CONTAINER_TYPE);
}
@Override
protected void setUp() throws Exception {
super.setUp();
startServerAndDeploy();
Client c = Client.create();
r = c.resource(BASE_URI);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
stopServer();
}
private void startServerAndDeploy() throws Exception {
if (CONTAINER_TYPE.equals(EMBEDDED_GF_V3)) {
// Create an instance of embedded glassfish and deploy the expanded war file
glassfish = new GlassFish(BASE_URI.getPort());
// Deploy Glassfish referencing the web.xml
war = new ScatteredWar(BASE_URI.getRawPath(),
new File("src/main/webapp"),
new File("src/main/webapp/WEB-INF/web.xml"),
Collections.singleton(new File("target/classes").toURI().toURL()));
glassfish.deploy(war);
} else if (CONTAINER_TYPE.equals(GRIZZLY_WEB_CONTAINER)) {
// Create an instance of grizzly web container
initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages",
"com.sun.jersey.qe.tests.helloworld.resources");
System.out.println("Starting grizzly...");
threadSelector = GrizzlyWebContainerFactory.create(BASE_URI,
com.sun.jersey.spi.container.servlet.ServletContainer.class, initParams);
} else if (CONTAINER_TYPE.equals(HTTP_SERVER)) {
// Create an instance of HTTP Server and start it
props = new HashMap<String, Object>();
props.put( PackagesResourceConfig.PROPERTY_PACKAGES,
"com.sun.jersey.qe.tests.helloworld.resources" );
resourceConfig = new PackagesResourceConfig( props );
container = ContainerFactory.createContainer( HttpHandler.class,
resourceConfig );
server = HttpServerFactory.create(BASE_URI, container);
server.start();
} else if (CONTAINER_TYPE.equals(GF_V2)) {
// do nothing
} else if (CONTAINER_TYPE.equals(GF_V3)) {
// do nothing
} else {
System.out.println("Invalid container.type attribute passed. Expected one of :: ");
System.out.println("====================");
System.out.println(EMBEDDED_GF_V3 + "\n" + GRIZZLY_WEB_CONTAINER +
"\n" + HTTP_SERVER + "\n" + GF_V2 + "\n" + GF_V3);
System.out.println("====================");
System.exit(-1);
}
}
private void stopServer() {
if (CONTAINER_TYPE.equals(EMBEDDED_GF_V3)) {
//stop embedded glassfish
glassfish.stop();
} else if (CONTAINER_TYPE.equals(GRIZZLY_WEB_CONTAINER)) {
// stop the grizzly thread selector instance
threadSelector.stopEndpoint();
} else if (CONTAINER_TYPE.equals(HTTP_SERVER)) {
//stop the http server instance
server.stop(0);
} else if (CONTAINER_TYPE.equals(GF_V2)) {
// do nothing
} else if (CONTAINER_TYPE.equals(GF_V3)) {
// do nothing
}
}
public void testHelloWorld() throws Exception {
String responseMsg = r.path("helloworld").get(String.class);
assertEquals("Hello World", responseMsg);
}
}