Hi Naresh,
Tests in the jersey-tests module and the samples work fine using the
same versions. Jersey would fail to build which such an error as
reported. It is as if you are picking up a different grizzly
implementation.
BTW i hope you are going to encpasulate the if/else if/else loop as
separate classes. For example there is the direct Grizzly container,
if you need to support that in your current design you will have to
add another else if statement to all blocks. It is better to
encapsulate the code into separate classes with a common interface.
Paul.
On Nov 6, 2008, at 4:02 PM, Srinivas Naresh Bhimisetty wrote:
> 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
> <?xml version="1.0" encoding="UTF-8"?>
> <!--
>
> DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
>
> Copyright 1997-2007 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.
> -->
> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
> " xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
> ">
> <modelVersion>4.0.0</modelVersion>
> <groupId>com.sun.jersey.qe.tests</groupId>
> <version>1.0.1-SNAPSHOT</version>
> <artifactId>helloworld-webapp</artifactId>
> <name>Helloworld WebApp - Jersey QE Tests</name>
> <packaging>war</packaging>
> <profiles>
> <profile>
> <id>jdk-1.5</id>
> <activation>
> <jdk>1.5</jdk>
> </activation>
> <dependencies>
> <dependency>
> <groupId>com.sun.xml.bind</groupId>
> <artifactId>jaxb-impl</artifactId>
> <version>2.1</version>
> </dependency>
> </dependencies>
> </profile>
> </profiles>
> <dependencies>
> <dependency>
> <groupId>com.sun.jersey</groupId>
> <artifactId>jersey-server</artifactId>
> <version>${project.version}</version>
> </dependency>
> <dependency>
> <groupId>junit</groupId>
> <artifactId>junit</artifactId>
> <version>3.8.2</version>
> <scope>test</scope>
> </dependency>
> <dependency>
> <groupId>org.glassfish.distributions</groupId>
> <artifactId>web-all</artifactId>
> <version>10.0-build-20080430</version>
> <scope>test</scope>
> </dependency>
> <dependency>
> <groupId>org.glassfish.embedded</groupId>
> <artifactId>gf-embedded-api</artifactId>
> <version>1.0-alpha-4</version>
> <scope>test</scope>
> </dependency>
> <dependency>
> <groupId>com.sun.grizzly</groupId>
> <artifactId>grizzly-servlet-webserver</artifactId>
> <version>1.8.6.3</version>
> <scope>test</scope>
> </dependency>
> <dependency>
> <groupId>com.sun.jersey</groupId>
> <artifactId>jersey-client</artifactId>
> <version>${project.version}</version>
> <scope>test</scope>
> </dependency>
> </dependencies>
> <build>
> <finalName>helloworld-webapp</finalName>
> <plugins>
> <plugin>
> <artifactId>maven-compiler-plugin</artifactId>
> <inherited>true</inherited>
> <configuration>
> <source>1.5</source>
> <target>1.5</target>
> </configuration>
> </plugin>
> <!-- Run the application using "mvn glassfish:run" -->
> <plugin>
> <groupId>org.glassfish</groupId>
> <artifactId>maven-glassfish-plugin</artifactId>
> </plugin>
> <plugin>
> <artifactId>maven-assembly-plugin</artifactId>
> <configuration>
> <descriptors>
> <descriptor>src/main/assembly/src.xml</
> descriptor>
> </descriptors>
> </configuration>
> <executions>
> <execution>
> <id>make-assembly</id> <!-- this is used for
> inheritance merges -->
> <phase>package</phase> <!-- append to the
> packaging phase. -->
> <goals>
> <goal>attached</goal> <!-- goals == mojos
> -->
> </goals>
> </execution>
> </executions>
> </plugin>
> </plugins>
> </build>
> <pluginRepositories>
> <pluginRepository>
> <id>maven2-repository.dev.java.net</id>
> <name>Java.net Repository for Maven</name>
> <url>http://download.java.net/maven/2/</url>
> <layout>default</layout>
> </pluginRepository>
> <pluginRepository>
> <id>maven-repository.dev.java.net</id>
> <name>Java.net Maven 1 Repository (legacy)</name>
> <url>http://download.java.net/maven/1</url>
> <layout>legacy</layout>
> </pluginRepository>
> </pluginRepositories>
> <repositories>
> <repository>
> <id>maven2-repository.dev.java.net</id>
> <name>Java.net Repository for Maven</name>
> <url>http://download.java.net/maven/2/</url>
> <layout>default</layout>
> </repository>
> <repository>
> <id>maven-repository.dev.java.net</id>
> <name>Java.net Maven 1 Repository (legacy)</name>
> <url>http://download.java.net/maven/1</url>
> <layout>legacy</layout>
> </repository>
> <repository>
> <id>glassfish-repository</id>
> <name>Java.net Repository for Glassfish</name>
> <url>http://download.java.net/maven/glassfish</url>
> </repository>
> </repositories>
> <properties>
> <netbeans.hint.deploy.server>gfv3</netbeans.hint.deploy.server>
> </properties>
> <distributionManagement>
> <repository>
> <uniqueVersion>false</uniqueVersion>
> <id>java.net-m2-dist-repository</id>
> <name>java.net-m2-repository</name>
> <url>java-net:/maven2-repository/trunk/www/repository/</
> url>
> </repository>
> </distributionManagement>
> </project>
> /*
> *
> * 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);
> }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net