Superceeded Article: Embedded Web Integration Testing with Jetty
Do you speak test? In that case: Hello web application:
public class WebIntegrationTest extends net.sourceforge.jwebunit.WebTestCase {
public void testIndex() {
beginAt("/index.html");
assertTextPresent("Hello world");
}
private org.mortbay.jetty.Server server;
protected void setUp() throws Exception {
server = new org.mortbay.jetty.Server(0);
server.addHandler(
new org.mortbay.jetty.webapp.WebAppContext("src/main/webapp", "/my-context"));
server.start();
int actualPort = server.getConnectors()[0].getLocalPort();
getTestContext().setBaseUrl("http://localhost:" + actualPort + "/my-context");
}
}
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
Comments:
Niraj Manandhar - Jan 20, 2007
Thank you Mr. Johannes for this article
[Martin Gilday] - Feb 28, 2007
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?
Johannes Brodwall - Feb 28, 2007
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] - Mar 14, 2007
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 –
Johannes Brodwall - Mar 15, 2007
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] - Jan 8, 2009
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:
my context file ($jetty.home/context/LSRTest1.xml):
my web.xml file ($jetty.home/webapps-plus/LSRTest1/WEB-INF/web.xml):
<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”>
LSR Test1 WebApp
org.mortbay.jetty.servlet.ManagedAttributes
org.cometd.bayeux,dojox.cometd.bayeux
lsr.cometd.listener.BayeuxServicesListener
prechat
lsr.prechat.PreChatServlet
1
initchat
lsr.initchat.InitChatServlet
1
chat
org.mortbay.cometd.SuspendingCometdServlet
timeout
120000
interval
0
maxInterval
10000
multiFrameInterval
2000
logLevel
0
directDeliver
true
refsThreshold
10
1
prechat
/prechat/
initchat
/init/
chat
/chat/*
jdbc/LSRDB
javax.sql.DataSource
Container
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.(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.
jhannes - Jan 8, 2009
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.
Johannes Brodwall - Jan 8, 2009
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