Superceeded Article: Embedded Web Integration Testing with Jetty

Do you speak test? In that case: Hello web application:

This code runs with no application server, no separate deployment step, just like that.

If this looks interesting, see my full-sized article on java.net

About Johannes Brodwall

Johannes is Principal Software Engineer in SopraSteria. In his spare time he likes to coach teams and developers on better coding, collaboration, planning and product understanding.
This entry was posted in Java, Software Development. Bookmark the permalink.
  • Thank you Mr. Johannes for this article

  • Thank you Mr. Johannes for this article

  • Martin Gilday

    Thanks for the article, been very useful in setting up our end to end tests.
    I am however having problems with getting JNID working within Jetty. Can you confirm the quoted code is correct?

  • Hi, Martin

    I am pretty sure it is correct. But make sure that you use the latest version of this article: http://www.brodwall.com/johannes/blog/2007/02/0

    The code for this particular bit is here: http://svn.brodwall.com/demo/insanejava/trunk/w

    If you still don't have any success, feel free to post the problem here, or send me an email.

  • Martin Gilday

    Thanks for the article, been very useful in setting up our end to end tests.
    I am however having problems with getting JNID working within Jetty. Can you confirm the quoted code is correct?

  • Hi, Martin

    I am pretty sure it is correct. But make sure that you use the latest version of this article: http://www.brodwall.com/johannes/blog/2007/02/04/updated-article-embedded-web-integration-testing-with-jetty/

    The code for this particular bit is here: http://svn.brodwall.com/demo/insanejava/trunk/web-demo/src/test/java/no/brodwall/web/integration/JettyTestServer.java

    If you still don’t have any success, feel free to post the problem here, or send me an email.

  • suresh

    Thanks for the article. I ran into an issue with Jetty classloading when running on a linux platform.

    The code which retrieves Springs WebApplicatonContext using the jetty server's servlet context was failing. Specifically, the following code was throwing a ClassCastException:

    ApplicationContext appctx = WebApplicationContextUtils
    .getRequiredWebApplicationContext(servletContext);

    The reason being that the Spring's application context was loaded by Jetty's own classloader, hence cannot be used here.

    The work around was to set the parentLoaderPriority to true while creating the Jetty server as described here: http://docs.codehaus.org/display/JETTY/Classloa

    eg:
    WebAppContext context = new WebAppContext();
    context.setParentLoaderPriority(true);
    context.setContextPath(“/”);
    context.setWar(“path/to/war/or/exploded/war”);
    jettyServer.setHandler(context);

    The errors happens didnt happen on a Windows XP box. But while running our CI builds on a Redhat EL4 box, the classcast exception was thrown.

    Redhat EL4
    JDK 1.5_07-b3
    Jetty-6.1.1
    Spring-Framework-2.0.2

    Hope this might help someone who runs into the same problem.

    — suresh —

  • suresh

    Thanks for the article. I ran into an issue with Jetty classloading when running on a linux platform.

    The code which retrieves Springs WebApplicatonContext using the jetty server’s servlet context was failing. Specifically, the following code was throwing a ClassCastException:

    ApplicationContext appctx = WebApplicationContextUtils
    .getRequiredWebApplicationContext(servletContext);

    The reason being that the Spring’s application context was loaded by Jetty’s own classloader, hence cannot be used here.

    The work around was to set the parentLoaderPriority to true while creating the Jetty server as described here: http://docs.codehaus.org/display/JETTY/Classloading

    eg:
    WebAppContext context = new WebAppContext();
    context.setParentLoaderPriority(true);
    context.setContextPath(“/”);
    context.setWar(“path/to/war/or/exploded/war”);
    jettyServer.setHandler(context);

    The errors happens didnt happen on a Windows XP box. But while running our CI builds on a Redhat EL4 box, the classcast exception was thrown.

    Redhat EL4
    JDK 1.5_07-b3
    Jetty-6.1.1
    Spring-Framework-2.0.2

    Hope this might help someone who runs into the same problem.

    — suresh —

  • Hi, Suresh.

    I was not aware of this issue. Thanks for the heads-up and the good feedback. I will have to work on how to integrate it into the text, but I have updated the source code in SVN.

    ~Johannes

  • Hi, Suresh.

    I was not aware of this issue. Thanks for the heads-up and the good feedback. I will have to work on how to integrate it into the text, but I have updated the source code in SVN.

    ~Johannes

  • zp bappi

    hi,

    i am new to jetty. may be thats why i could not make the datasource work :(. well, i tried everything i clould. please help me.

    here is my detail:
    OS: windows vista ultimate
    jetty : 7.0.0.pre5
    web application base location: $jetty.home/webapps-plus/LSRTest1
    database: mysql
    command line: java -DOPTIONS=plus,jsp,ext,default -jar start.jar etc/jetty.xml etc/jetty-plus.xml

    my jetty-plus.xml:
    <?xml version=”1.0″?>
    <!DOCTYPE Configure PUBLIC “-//Mort Bay Consulting//DTD Configure//EN” “http://jetty.mortbay.org/configure.dtd”>
    <Configure id=”Server” class=”org.mortbay.jetty.Server”>
    <Array id=”plusConfig” type=”java.lang.String”>
    <Item>org.mortbay.jetty.webapp.WebInfConfiguration</Item>
    <Item>org.mortbay.jetty.plus.webapp.EnvConfiguration</Item>
    <Item>org.mortbay.jetty.plus.webapp.Configuration</Item>
    <Item>org.mortbay.jetty.webapp.JettyWebXmlConfiguration</Item>
    <Item>org.mortbay.jetty.webapp.TagLibConfiguration</Item>
    </Array>
    <Call name=”addLifeCycle”>
    <Arg>
    <New class=”org.mortbay.jetty.deployer.WebAppDeployer”>
    <Set name=”contexts”><Ref id=”Contexts”/></Set>
    <Set name=”webAppDir”><SystemProperty name=”jetty.home” default=”.”/>/webapps-plus</Set>
    <Set name=”parentLoaderPriority”>false</Set>
    <Set name=”extract”>true</Set>
    <Set name=”allowDuplicates”>false</Set>
    <Set name=”defaultsDescriptor”><SystemProperty name=”jetty.home” default=”.”/>/etc/webdefault.xml</Set>
    <Set name=”configurationClasses”><Ref id=”plusConfig”/></Set>
    </New>
    </Arg>
    </Call>
    </Configure>

    my context file ($jetty.home/context/LSRTest1.xml):
    <?xml version=”1.0″ encoding=”ISO-8859-1″?>
    <!DOCTYPE Configure PUBLIC “-//Mort Bay Consulting//DTD Configure//EN” “http://jetty.mortbay.org/configure.dtd”>
    <Configure id='wac' class=”org.mortbay.jetty.webapp.WebAppContext”>
    <Set name=”contextPath”>/LSRTest1</Set>
    <Set name=”war”><SystemProperty name=”jetty.home” default=”.”/>/webapps-plus/LSRTest1</Set>
    <Set name=”descriptor”><SystemProperty name=”jetty.home” default=”.”/>/webapps-plus/LSRTest1/WEB-INF/web.xml</Set>
    <Set name=”extractWAR”>false</Set>
    <Set name=”copyWebDir”>false</Set>
    <New id=”lsrdb” class=”org.mortbay.jetty.plus.naming.Resource”>
    <Arg><Ref id='wac'/></Arg>
    <Arg>java:/comp/env/jdbc/LSRDB</Arg>
    <Arg>
    <New class=”com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource”>
    <Set name=”Url”>jdbc:mysql://localhost:3306/lsrdb?autoReconnect=true</Set>
    <Set name=”User”>root</Set>
    <Set name=”Password”>zp123</Set>
    </New>
    </Arg>
    </New>
    </Configure>

    my web.xml file ($jetty.home/webapps-plus/LSRTest1/WEB-INF/web.xml):
    <?xml version=”1.0″ encoding=”ISO-8859-1″?>
    <web-app
    xmlns=”http://java.sun.com/xml/ns/javaee”
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
    xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
    version=”2.5″>
    <display-name>LSR Test1 WebApp</display-name>
    <context-param>
    <param-name>org.mortbay.jetty.servlet.ManagedAttributes</param-name>
    <param-value>org.cometd.bayeux,dojox.cometd.bayeux</param-value>
    </context-param>
    <listener>
    <listener-class>lsr.cometd.listener.BayeuxServicesListener</listener-class>
    </listener>
    <servlet>
    <servlet-name>prechat</servlet-name>
    <servlet-class>lsr.prechat.PreChatServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
    <servlet-name>initchat</servlet-name>
    <servlet-class>lsr.initchat.InitChatServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
    <servlet-name>chat</servlet-name>
    <servlet-class>org.mortbay.cometd.SuspendingCometdServlet</servlet-class>
    <init-param>
    <param-name>timeout</param-name>
    <param-value>120000</param-value>
    </init-param>
    <init-param>
    <param-name>interval</param-name>
    <param-value>0</param-value>
    </init-param>
    <init-param>
    <param-name>maxInterval</param-name>
    <param-value>10000</param-value>
    </init-param>
    <init-param>
    <param-name>multiFrameInterval</param-name>
    <param-value>2000</param-value>
    </init-param>
    <init-param>
    <param-name>logLevel</param-name>
    <param-value>0</param-value>
    </init-param>
    <init-param>
    <param-name>directDeliver</param-name>
    <param-value>true</param-value>
    </init-param>
    <init-param>
    <param-name>refsThreshold</param-name>
    <param-value>10</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>prechat</servlet-name>
    <url-pattern>/prechat/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>initchat</servlet-name>
    <url-pattern>/init/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>chat</servlet-name>
    <url-pattern>/chat/*</url-pattern>
    </servlet-mapping>
    <resource-ref>
    <res-ref-name>jdbc/LSRDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>
    </web-app>

    i did not modify any other file like etc/jetty.xml. also, i dont have any jetty-web.xml or jetty-env.xml file in $jetty.home/webapps-plus/LSRTest1/WEB-INF directory.

    when i use java code like:
    InitialContext ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup(“java:comp/env/jdbc/LSRDB”);

    i get the following exception:
    javax.naming.NameNotFoundException; remaining name 'env/jdbc/LSRDB'
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:634)
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:665)
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:680)
    at org.mortbay.naming.java.javaRootURLContext.lookup(javaRootURLContext.java:112)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at lsr.util.DBConnection.<init>(DBConnection.java:26)
    at lsr.initchat.InitChatServlet.getDomainInfo(InitChatServlet.java:126)
    at lsr.initchat.InitChatServlet.doGet(InitChatServlet.java:43)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:365)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:822)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:229)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:550)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:876)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:535)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:209)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:407)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:421)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:520)

    according to any documentation i find on internet, the above setup is correct. but, i dont know what is wrong here. i am stuck here for 3 days… trying different methods. but, failed in all. please help.

    regards,
    zp bappi.

  • Hi, zp bappi

    My experience is that JNDI is something that is a little tricky to get right, and I've never used this approach with pre-released versions of Jetty. I recommend that you try out using Jetty 6.1 or that you post a question to the Jetty mailing lists, which you can find on http://docs.codehaus.org/display/JETTY/Jetty+Wiki. I would also recommend using Java rather than XML to set up Jetty. This will not necessarily solve you problem, but your cycle time can be quicker.

  • Hi, zp bappi

    My experience is that JNDI is something that is a little tricky to get
    right, and I’ve never used this approach with pre-released versions of
    Jetty. I recommend that you try out using Jetty 6.1 or that you post a
    question to the Jetty mailing lists, which you can find on
    http://docs.codehaus.org/display/JETTY/Jetty+Wiki. I would also
    recommend using Java rather than XML to set up Jetty. This will not
    necessarily solve you problem, but your cycle time can be quicker.

    Good luck.

    ~Johannes

  • OxfordjdlkKayla

    In ffice right now will go through this latter.

    Thanks
    marshel
    ______________________________________________
    aion gold | Aion Powerleveling

  • kaili

    You have just won $15. Regardless of the experience that you are looking to get you will be able to find it on this website. This should be obvious but most people who wow gold go into a casino never do this. A lot of people who have been waiting for the launch of Chromebooks are now thinking to replace their computers with wow gold fast delivery Chrome Netbooks. The odds are not in your favor and if you look at people who have won big jackpots, they didn’t manage the money well and eventually lost it all or spent it all.