From: markt Date: Thu, 30 Jul 2009 18:10:35 +0000 (+0000) Subject: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=47569 X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=d1f04ca2b3cc07895a63b6489816441e313fea4c;p=tomcat7.0 Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=47569 Use the new base class so tests clean up after themselves git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@799392 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/test/org/apache/catalina/connector/TestRequest.java b/test/org/apache/catalina/connector/TestRequest.java index 7166c7877..26e590699 100644 --- a/test/org/apache/catalina/connector/TestRequest.java +++ b/test/org/apache/catalina/connector/TestRequest.java @@ -38,14 +38,23 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.core.StandardContext; +import org.apache.catalina.startup.TestTomcatBase; import org.apache.catalina.startup.Tomcat; -import junit.framework.TestCase; - /** * Test case for {@link Request}. */ -public class TestRequest extends TestCase { +public class TestRequest extends TestTomcatBase { + @Override + public void setUp() throws Exception { + super.setUp(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + } + /** * Test case for https://issues.apache.org/bugzilla/show_bug.cgi?id=37794 * POST parameters are not returned from a call to @@ -54,6 +63,7 @@ public class TestRequest extends TestCase { */ public void testBug37794() throws Exception { Bug37794Client client = new Bug37794Client(); + client.setPort(getPort()); // Edge cases around zero client.doRequest(-1, false); // Unlimited @@ -97,6 +107,8 @@ public class TestRequest extends TestCase { private static class Bug37794Servlet extends HttpServlet { + private static final long serialVersionUID = 1L; + /** * Only interested in the parameters and values for POST requests. */ @@ -120,15 +132,28 @@ public class TestRequest extends TestCase { /** * Bug 37794 test client. */ - private static class Bug37794Client extends SimpleHttpClient { + private class Bug37794Client extends SimpleHttpClient { + + private boolean init; + + private synchronized void init() throws Exception { + if (init) return; + + Tomcat tomcat = getTomcatInstance(); + StandardContext root = tomcat.addContext("", TEMP_DIR); + Tomcat.addServlet(root, "Bug37794", new Bug37794Servlet()); + root.addServletMapping("/test", "Bug37794"); + tomcat.start(); + + init = true; + } + private Exception doRequest(int postLimit, boolean ucChunkedHead) { - Tomcat tomcat = new Tomcat(); + Tomcat tomcat = getTomcatInstance(); + try { - StandardContext root = tomcat.addContext("", TEMP_DIR); - Tomcat.addServlet(root, "Bug37794", new Bug37794Servlet()); - root.addServletMapping("/test", "Bug37794"); + init(); tomcat.getConnector().setMaxPostSize(postLimit); - tomcat.start(); // Open connection connect(); @@ -167,12 +192,6 @@ public class TestRequest extends TestCase { disconnect(); } catch (Exception e) { return e; - } finally { - try { - tomcat.stop(); - } catch (Exception e) { - // Ignore - } } return null; } @@ -209,6 +228,7 @@ public class TestRequest extends TestCase { private Socket socket; private Writer writer; private BufferedReader reader; + private int port = 8080; private String[] request; private int requestPause = 1000; @@ -217,6 +237,10 @@ public class TestRequest extends TestCase { private List responseHeaders = new ArrayList(); private String responseBody; + public void setPort(int thePort) { + port = thePort; + } + public void setRequest(String[] theRequest) { request = theRequest; } @@ -238,7 +262,7 @@ public class TestRequest extends TestCase { } public void connect() throws UnknownHostException, IOException { - socket = new Socket("localhost", 8080); + socket = new Socket("localhost", port); OutputStream os = socket.getOutputStream(); writer = new OutputStreamWriter(os); InputStream is = socket.getInputStream(); diff --git a/test/org/apache/catalina/startup/TestTomcat.java b/test/org/apache/catalina/startup/TestTomcat.java index ee87cc56f..db9251d9f 100644 --- a/test/org/apache/catalina/startup/TestTomcat.java +++ b/test/org/apache/catalina/startup/TestTomcat.java @@ -29,19 +29,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import junit.framework.TestCase; - import org.apache.catalina.core.StandardContext; import org.apache.tomcat.util.buf.ByteChunk; -public class TestTomcat extends TestCase { - Tomcat tomcat; - // if you run in eclipse - or tomcat src dir - String base = "./"; - File tempDir; - static int port = 8001; - long t0; - +public class TestTomcat extends TestTomcatBase { + /** * Simple servlet to test in-line registration */ @@ -55,28 +47,6 @@ public class TestTomcat extends TestCase { } } - public void setUp() throws Exception { - t0 = System.currentTimeMillis(); - tempDir = new File(base + "output/tmp"); - tempDir.mkdir(); - - tomcat = new Tomcat(); - tomcat.setBaseDir(tempDir.getAbsolutePath()); - tomcat.getHost().setAppBase(tempDir.getAbsolutePath() + "/webapps"); - - // If each test is running on same port - they - // may interfere with each other (on unix at least) - port++; - tomcat.setPort(port); - } - - public void tearDown() throws Exception { - tomcat.stop(); - System.err.println("Test time: " + - (System.currentTimeMillis() - t0)); - ExpandWar.delete(tempDir); - } - /** * Start tomcat with a single context and one * servlet - all programmatic, no server.xml or @@ -85,10 +55,11 @@ public class TestTomcat extends TestCase { * @throws Exception */ public void testProgrammatic() throws Exception { + Tomcat tomcat = getTomcatInstance(); + // Must have a real docBase - just use temp StandardContext ctx = - tomcat.addContext("/", - tempDir.getAbsolutePath()); + tomcat.addContext("/", System.getProperty("java.io.tmpdir")); // You can customize the context by calling // its API @@ -97,27 +68,34 @@ public class TestTomcat extends TestCase { tomcat.start(); - ByteChunk res = getUrl("http://localhost:" + port + "/"); + ByteChunk res = getUrl("http://localhost:" + getPort() + "/"); assertEquals(res.toString(), "Hello world"); } public void testSingleWebapp() throws Exception { + Tomcat tomcat = getTomcatInstance(); + // Currently in sandbox/tomcat-lite File appDir = - new File(base + "output/build/webapps/examples"); + new File("output/build/webapps/examples"); // app dir is relative to server home tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath()); tomcat.start(); - ByteChunk res = getUrl("http://localhost:" + port + "/examples/servlets/servlet/HelloWorldExample"); + ByteChunk res = getUrl("http://localhost:" + getPort() + + "/examples/servlets/servlet/HelloWorldExample"); assertTrue(res.toString().indexOf("

Hello World!

") > 0); } public void testLaunchTime() throws Exception { - tomcat.addContext(null, "/", base); + Tomcat tomcat = getTomcatInstance(); + long t0 = System.currentTimeMillis(); + tomcat.addContext(null, "/", "."); tomcat.start(); - } + System.err.println("Test time: " + + (System.currentTimeMillis() - t0)); + } /** * Wrapper for getting the response.