1 package net.sourceforge.jwebunit; 2 3 import java.io.IOException ; 4 5 import javax.servlet.ServletException ; 6 import javax.servlet.http.HttpServlet ; 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpServletResponse ; 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 { 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 { 41 checkWebConversation(null); 42 } 43 44 public void testNormalContextToHttpUnitDialog() throws Exception { 45 checkWebConversation(getTestContext()); 46 } 47 48 private void checkWebConversation(TestContext whichContext) { 49 defineResource("foo.html", "foo"); 50 51 String 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 { 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 { 67 public void doGet( 68 HttpServletRequest request, 69 HttpServletResponse response) 70 throws ServletException , IOException { 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 |