| version 1.1.1.5, 1999/09/30 11:12:37 |
version 1.1.1.6, 1999/09/30 11:31:20 |
|
|
|
|
| |
|
| public class TestServlets extends TestCase { |
public class TestServlets extends TestCase { |
| private static final String PORT_STRING = "8081"; |
private static final String PORT_STRING = "8081"; |
| |
private static final String URL_BASE = "http://127.0.0.1:"; |
| |
|
| public TestServlets (String name) { |
public TestServlets (String name) { |
| |
|
|
|
|
|
| } |
} |
| public static void main (String[] args) { |
public static void main (String[] args) { |
| TestRunner.run (suite()); |
TestRunner.run (suite()); |
| |
stopServletRunner(); |
| } |
} |
| protected static void startServletRunner() { |
protected static void startServletRunner() { |
| class ServerThread extends Thread |
class ServerThread extends Thread |
|
|
|
|
| Thread serverThread = new ServerThread("webserver"); |
Thread serverThread = new ServerThread("webserver"); |
| serverThread.start(); |
serverThread.start(); |
| } |
} |
| public static Test suite() { |
protected static void stopServletRunner() { |
| startServletRunner(); |
|
| |
|
| return new TestSuite(TestServlets.class); |
|
| } |
|
| /** |
|
| * Exercise the PageSaver |
|
| * Send a request and check the response contains the expected String. |
|
| */ |
|
| public void testDisplayingAKnownString() |
|
| { |
|
| |
|
| try { |
try { |
| String result = "My known input"; |
URL servlet = new URL( URL_BASE + PORT_STRING + "/servlet/" + Terminator.class.getName() ); |
| PageSaver.save("AnyName", result); |
URLConnection connx = servlet.openConnection(); |
| |
|
| String servletName = PageSaver.class.getName(); |
connx.getInputStream(); |
| String actualResponse = |
|
| getServerResponse("http://127.0.0.1:"+PORT_STRING+"/servlet/"+servletName |
|
| + "?name=AnyName"); |
|
| assert(actualResponse.equals(result)); |
|
| } |
} |
| catch (Exception exception) { |
catch (Exception exception) { |
| exception.printStackTrace(); |
|
| assert("General exceptions should not occur.",false); |
|
| } |
} |
| } |
} |
| /** |
public static Test suite() { |
| * Exercise the PageSaver |
startServletRunner(); |
| * Send a request and check the response contains the expected String. |
|
| */ |
|
| public void testGetEditingUrl() { |
|
| try { |
|
| String result = "Myknowninput"; |
|
| |
|
| String servletName = PageSaver.class.getName(); |
|
| |
|
| String actualResponse = |
return new TestSuite(TestServlets.class); |
| getServerResponse("http://127.0.0.1:" + PORT_STRING |
|
| + "/servlet/" + servletName |
|
| + "?name=AnyName" |
|
| +"&edit="); |
|
| assert(actualResponse.equals(PageSaver.makeEditForm(result))); |
|
| } |
|
| catch (Exception exception) { |
|
| exception.printStackTrace(); |
|
| assert("General exceptions should not occur.", false); |
|
| } |
|
| } |
} |
| /** |
/** |
| * Exercise the PageSaver |
* Exercise the PageSaver |
| * Send a request and check the response contains the expected String. |
* Send a request and check the response contains the expected String. |
| */ |
*/ |
| public void testSaving() |
public void testDisplayingAKnownString() |
| { |
{ |
| |
|
| try { |
try { |
| String result = "Myknowninput"; |
String result = "Myknowninput"; |
| |
PageSaver.save(result); |
| |
|
| String servletName = PageSaver.class.getName(); |
String servletName = PageSaver.class.getName(); |
| getServerResponse("http://127.0.0.1:"+PORT_STRING+"/servlet/"+servletName |
String actualResponse = getServerResponse(URL_BASE+PORT_STRING+"/servlet/"+servletName); |
| + "?name=AnyName" |
|
| +"&save="+result); |
|
| |
|
| String actualResponse = |
|
| getServerResponse("http://127.0.0.1:"+PORT_STRING+"/servlet/"+servletName |
|
| + "?name=AnyName"); |
|
| assert(actualResponse.equals(result)); |
assert(actualResponse.equals(result)); |
| } |
} |
| catch (Exception exception) { |
catch (Exception exception) { |
|
|
|
|
| assert("General exceptions should not occur.",false); |
assert("General exceptions should not occur.",false); |
| } |
} |
| } |
} |
| public void testSavingNamedPage() { |
|
| try { |
|
| String result = "Myknowninput"; |
|
| String pageName = "PageOne"; |
|
| String servletName = PageSaver.class.getName(); |
|
| getServerResponse("http://127.0.0.1:" + PORT_STRING + "/servlet/" + servletName |
|
| + "?name=" + pageName |
|
| + "&save=" + result); |
|
| |
|
| String result2 = "Myknowninput2"; |
|
| String pageName2 = "PageTwo"; |
|
| getServerResponse("http://127.0.0.1:" + PORT_STRING + "/servlet/" + servletName |
|
| + "?name=" + pageName2 |
|
| + "&save=" + result2); |
|
| |
|
| String actualResponse = getServerResponse("http://127.0.0.1:" + PORT_STRING + "/servlet/" |
|
| + servletName |
|
| + "?name=" + pageName); |
|
| assert("Expected first page response",actualResponse.equals(result)); |
|
| } |
|
| catch (Exception exception) { |
|
| exception.printStackTrace(); |
|
| assert("General exceptions should not occur.", false); |
|
| } |
|
| } |
|
| } |
} |