KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > ServletUnitTest


1 package net.sourceforge.jwebunit;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.ServletException JavaDoc;
6 import javax.servlet.http.HttpServlet JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9
10 import com.meterware.httpunit.WebClient;
11 import com.meterware.httpunit.WebConversation;
12 import com.meterware.httpunit.WebResponse;
13 import com.meterware.servletunit.ServletRunner;
14 import com.meterware.servletunit.ServletUnitClient;
15
16 public class ServletUnitTest extends JWebUnitTest {
17
18     public void setUpWebClient() {
19         ServletRunner runner = new ServletRunner();
20         runner.registerServlet("myServlet.html", MyServlet.class.getName());
21
22         WebClient client = runner.newClient();
23         getTestContext().setWebClient(client);
24         getTestContext().setBaseUrl("http://dummy/");
25     }
26
27     public void testSetUpWithoutJwebunit() throws Exception JavaDoc {
28         setUpWebClient();
29
30         WebClient client = getTestContext().getWebClient();
31         WebResponse response =
32             client.getResponse("http://dummy/myServlet.html");
33         assertEquals("text/html", response.getContentType());
34         assertEquals(
35             "<html><head><title>myServlet</title></head>\n"
36                 + "<body></body></html>\n",
37             response.getText());
38     }
39
40     public void testNullContextToHttpUnitDialog() throws Exception JavaDoc {
41         checkWebConversation(null);
42     }
43
44     public void testNormalContextToHttpUnitDialog() throws Exception JavaDoc {
45         checkWebConversation(getTestContext());
46     }
47
48     private void checkWebConversation(TestContext whichContext) {
49         defineResource("foo.html", "foo");
50
51         String JavaDoc url = getTestContext().getBaseUrl() + "foo.html";
52         HttpUnitDialog dialog = new HttpUnitDialog(url, whichContext);
53
54         assertTrue(dialog.getWebClient() instanceof WebConversation);
55     }
56
57     public void testServletUnit() throws Exception JavaDoc {
58         setUpWebClient();
59
60         beginAt("/myServlet.html");
61         assertTitleEquals("myServlet");
62
63         assertTrue(getDialog().getWebClient() instanceof ServletUnitClient);
64     }
65
66     public static class MyServlet extends HttpServlet JavaDoc {
67         public void doGet(
68             HttpServletRequest JavaDoc request,
69             HttpServletResponse JavaDoc response)
70             throws ServletException JavaDoc, IOException JavaDoc {
71             response.setContentType("text/html; charset=UTF-8");
72             response.getWriter().print(
73                 "<html><head><title>myServlet</title></head>\n"
74                     + "<body></body></html>\n");
75         }
76     }
77 }
78
Popular Tags