added HTTP listener to Jetty to test UI

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11474 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2008-08-14 21:04:21 +00:00
1 parent 2f31b0f20e
commit 73d1b237ef
1 file changed
+47 -2
@@ -1,15 +1,21 @@
package org.jvnet.hudson.test;
import com.meterware.httpunit.WebResponse;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.model.Item;
import junit.framework.TestCase;
import org.jvnet.hudson.test.HudsonHomeLoader.CopyExisting;
import org.kohsuke.stapler.Stapler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.xml.sax.SAXException;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
/**
* Base class for all Hudson test cases.
@@ -21,6 +27,16 @@ public abstract class HudsonTestCase extends TestCase {
protected final TestEnvironment env = new TestEnvironment();
protected HudsonHomeLoader homeLoader;
/**
* TCP/IP port that the server is listening on.
*/
protected int localPort;
protected Server server;
/**
* Where in the {@link Server} is Hudson deploed?
*/
protected String contextPath = "/";
protected HudsonTestCase(String name) {
super(name);
@@ -30,11 +46,13 @@ public abstract class HudsonTestCase extends TestCase {
env.pin();
recipe();
hudson = newHudson();
hudson.servletContext.setAttribute("app",hudson);
}
protected void tearDown() throws Exception {
hudson.cleanUp();
env.dispose();
server.stop();
}
protected Hudson newHudson() throws Exception {
@@ -46,14 +64,31 @@ public abstract class HudsonTestCase extends TestCase {
* that we need for testing.
*/
protected ServletContext createWebServer() throws Exception {
Server server = new Server();
Context root = new Context(server, "/", Context.SESSIONS);
server = new Server();
Context root = new Context(server, contextPath, Context.SESSIONS);
ServletHolder holder = new ServletHolder(new Stapler());
root.addServlet(holder, "/");
SocketConnector connector = new SocketConnector();
server.addConnector(connector);
server.start();
localPort = connector.getLocalPort();
return holder.getServletHandler().getServletContext();
}
//
// Convenience methods
//
protected FreeStyleProject createFreeStyleProject() throws IOException {
return createFreeStyleProject("test");
}
protected FreeStyleProject createFreeStyleProject(String name) throws IOException {
return (FreeStyleProject)hudson.createProject(FreeStyleProject.DESCRIPTOR,name);
}
//
// recipe methods. Control the test environments.
//
@@ -78,4 +113,14 @@ public abstract class HudsonTestCase extends TestCase {
homeLoader = new CopyExisting(source);
return this;
}
/**
* Extends {@link com.meterware.httpunit.WebConversation} and provide convenience methods
* for accessing Hudson.
*/
public class WebConversation extends com.meterware.httpunit.WebConversation {
public WebResponse getResponse(Item item) throws IOException, SAXException {
return getResponse("http://localhost:"+localPort+contextPath+item.getUrl());
}
}
}