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
*/
public void testBug37794() throws Exception {
Bug37794Client client = new Bug37794Client();
+ client.setPort(getPort());
// Edge cases around zero
client.doRequest(-1, false); // Unlimited
private static class Bug37794Servlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
/**
* Only interested in the parameters and values for POST requests.
*/
/**
* 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();
disconnect();
} catch (Exception e) {
return e;
- } finally {
- try {
- tomcat.stop();
- } catch (Exception e) {
- // Ignore
- }
}
return null;
}
private Socket socket;
private Writer writer;
private BufferedReader reader;
+ private int port = 8080;
private String[] request;
private int requestPause = 1000;
private List<String> responseHeaders = new ArrayList<String>();
private String responseBody;
+ public void setPort(int thePort) {
+ port = thePort;
+ }
+
public void setRequest(String[] theRequest) {
request = theRequest;
}
}
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();
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
*/
}
}
- 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
* @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
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("<h1>Hello World!</h1>") > 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.