01: package com.mockrunner.example.servlet;
02:
03: import java.io.BufferedReader;
04:
05: import org.jdom.Element;
06:
07: import com.mockrunner.servlet.BasicServletTestCaseAdapter;
08:
09: /**
10: * Example test for {@link RedirectServlet}.
11: * Demonstrates the usage of {@link com.mockrunner.servlet.ServletTestModule}
12: * and {@link com.mockrunner.servlet.BasicServletTestCaseAdapter}.
13: * Demonstrates the testing of output data as string as well as parsed
14: * HTML data (<code>testServletOutputAsXML</code>).
15: *
16: */
17: public class RedirectServletTest extends BasicServletTestCaseAdapter {
18: protected void setUp() throws Exception {
19: super .setUp();
20: createServlet(RedirectServlet.class);
21: }
22:
23: public void testServletOutput() throws Exception {
24: addRequestParameter("redirecturl", "http://www.mockrunner.com");
25: doPost();
26: BufferedReader reader = getOutputAsBufferedReader();
27: assertEquals("<html>", reader.readLine().trim());
28: assertEquals("<head>", reader.readLine().trim());
29: reader.readLine();
30: assertEquals("</head>", reader.readLine().trim());
31: assertEquals("<body>", reader.readLine().trim());
32: reader.readLine();
33: assertEquals("</body>", reader.readLine().trim());
34: assertEquals("</html>", reader.readLine().trim());
35: verifyOutputContains("URL=http://www.mockrunner.com");
36: }
37:
38: public void testServletOutputAsXML() throws Exception {
39: addRequestParameter("redirecturl", "http://www.mockrunner.com");
40: doPost();
41: Element root = getOutputAsJDOMDocument().getRootElement();
42: assertEquals("html", root.getName());
43: Element head = root.getChild("head");
44: Element meta = head.getChild("meta");
45: assertEquals("refresh", meta.getAttributeValue("http-equiv"));
46: assertEquals("0;URL=http://www.mockrunner.com", meta
47: .getAttributeValue("content"));
48: }
49: }
|