001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone.testCase;
008:
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.util.HashMap;
012: import java.util.Map;
013:
014: import junit.framework.Test;
015: import junit.framework.TestCase;
016: import junit.framework.TestSuite;
017:
018: import org.xml.sax.SAXException;
019:
020: import winstone.Launcher;
021: import winstone.Logger;
022:
023: import com.meterware.httpunit.GetMethodWebRequest;
024: import com.meterware.httpunit.WebConversation;
025: import com.meterware.httpunit.WebImage;
026: import com.meterware.httpunit.WebRequest;
027: import com.meterware.httpunit.WebResponse;
028:
029: /**
030: * Test case for the Http Connector to Winstone. Simulates a simple connect and
031: * retrieve case, then a keep-alive connection case.
032: *
033: * @author mailto: <a href="rick_knowles@hotmail.com">Rick Knowles</a>
034: * @version $Id: HttpConnectorTest.java,v 1.8 2007/04/23 15:06:22 rickknowles Exp $
035: */
036: public class HttpConnectorTest extends TestCase {
037: public static Test suite() {
038: return (new TestSuite(HttpConnectorTest.class));
039: }
040:
041: /**
042: * Constructor
043: */
044: public HttpConnectorTest(String name) {
045: super (name);
046: }
047:
048: /**
049: * Test the simple case of connecting, retrieving and disconnecting
050: */
051: public void testSimpleConnection() throws IOException,
052: SAXException, InterruptedException {
053: // Initialise container
054: Map args = new HashMap();
055: args.put("webroot", "target/testwebapp");
056: args.put("prefix", "/examples");
057: args.put("httpPort", "10003");
058: args.put("ajp13Port", "-1");
059: args.put("controlPort", "-1");
060: args.put("debug", "8");
061: args.put("logThrowingLineNo", "true");
062: Logger.init(Logger.FULL_DEBUG, System.out, true);
063: Launcher winstone = new Launcher(args);
064:
065: // Check for a simple connection
066: WebConversation wc = new WebConversation();
067: WebRequest wreq = new GetMethodWebRequest(
068: "http://localhost:10003/examples/CountRequestsServlet");
069: WebResponse wresp = wc.getResponse(wreq);
070: InputStream content = wresp.getInputStream();
071: assertTrue("Loading CountRequestsServlet",
072: content.available() > 0);
073: content.close();
074: winstone.shutdown();
075: Thread.sleep(500);
076: }
077:
078: /**
079: * Test the keep alive case
080: */
081: public void testKeepAliveConnection() throws IOException,
082: InterruptedException, SAXException {
083: // Initialise container
084: Map args = new HashMap();
085: args.put("webroot", "target/testwebapp");
086: args.put("prefix", "/examples");
087: args.put("httpPort", "10004");
088: args.put("ajp13Port", "-1");
089: args.put("controlPort", "-1");
090: args.put("debug", "8");
091: args.put("logThrowingLineNo", "true");
092: Logger.init(Logger.FULL_DEBUG, System.out, true);
093: Launcher winstone = new Launcher(args);
094:
095: // Check for a simple connection
096: WebConversation wc = new WebConversation();
097: WebRequest wreq = new GetMethodWebRequest(
098: "http://localhost:10004/examples/CountRequestsServlet");
099: WebResponse wresp1 = wc.getResponse(wreq);
100: WebImage img[] = wresp1.getImages();
101: for (int n = 0; n < img.length; n++)
102: wc.getResponse(img[n].getRequest());
103: // Thread.sleep(2000);
104: // WebResponse wresp2 = wc.getResponse(wreq);
105: // Thread.sleep(2000);
106: //WebResponse wresp3 = wc.getResponse(wreq);
107: InputStream content = wresp1.getInputStream();
108: assertTrue("Loading CountRequestsServlet + child images",
109: content.available() > 0);
110: content.close();
111: winstone.shutdown();
112: Thread.sleep(500);
113: }
114:
115: /**
116: * Test the keep alive case
117: */
118: public void testWriteAfterServlet() throws IOException,
119: InterruptedException, SAXException {
120: // Initialise container
121: Map args = new HashMap();
122: args.put("webroot", "target/testwebapp");
123: args.put("prefix", "/examples");
124: args.put("httpPort", "10005");
125: args.put("ajp13Port", "-1");
126: args.put("controlPort", "-1");
127: args.put("debug", "8");
128: args.put("logThrowingLineNo", "true");
129: Logger.init(Logger.FULL_DEBUG, System.out, true);
130: Launcher winstone = new Launcher(args);
131:
132: // Check for a simple connection
133: WebConversation wc = new WebConversation();
134: WebRequest wreq = new GetMethodWebRequest(
135: "http://localhost:10005/examples/TestWriteAfterServlet");
136: WebResponse wresp1 = wc.getResponse(wreq);
137: Logger.logDirectMessage(Logger.INFO, "log", "Output: "
138: + wresp1.getText(), null);
139: assertTrue(wresp1.getText().endsWith("Hello"));
140: winstone.shutdown();
141: Thread.sleep(500);
142: }
143: }
|