KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > examples > dynamic > SimpleServletTest


1 package com.mockobjects.examples.dynamic;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.io.StringWriter JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import junit.framework.TestCase;
12
13 import com.mockobjects.dynamic.C;
14 import com.mockobjects.dynamic.Mock;
15 import com.mockobjects.dynamic.OrderedMock;
16
17 /**
18  * A sample test case showing usage of the dynamic mock library.
19  * It has also been used as an acceptance test for the implementation
20  * of the library, and so uses different variations of some of the calls
21  */

22
23 public class SimpleServletTest extends TestCase {
24     final String JavaDoc SUBJECT = "Mail Subject";
25     final String JavaDoc[] RECIPIENTS = new String JavaDoc[]{ "tim@hotmail.com", "steve@yahoo.com"};
26     final String JavaDoc BODY = "Mail Body";
27     
28     public SimpleServletTest(String JavaDoc name) {
29         super(name);
30     }
31 /*
32     public void testDoGetOldStyle() throws ServletException, IOException {
33         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
34         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
35         
36         CallSequence params = new CallSequence();
37         params.expectAndReturn( new Constraint[] { C.eq("body") }, "this is the body" );
38         params.expectAndReturn( new Constraint[] { C.eq("subject") }, "mail from tim" );
39         params.expectAndReturn( new Constraint[] { C.eq("recipients") }, new String[] { "nat.pryce@b13media.com", "steve@m3p.com" } );
40         
41         mockHttpServletRequest.expect( "getParameter", params );
42         
43         SimpleServlet aServlet = new SimpleServlet();
44         aServlet.doGet((HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy());
45         
46         mockHttpServletRequest.verify();
47         mockHttpServletResponse.verify();
48     }
49 */

50     public void testDoGet() throws ServletException JavaDoc, IOException JavaDoc {
51       
52         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
53         Mock mockHttpServletResponse = new OrderedMock(HttpServletResponse JavaDoc.class, "Response with non-default name");
54         Mock mockTimer = new Mock(Timer.class);
55         Mock mockMailSender = new Mock(MailSender.class);
56         
57         // Set no expectations but answer a value for the specified method
58
mockHttpServletRequest.matchAndReturn( "getParameter", C.eq("browser-identifier"), "MSIE-5.0" );
59         mockHttpServletRequest.matchAndReturn("getIntHeader", C.ANY_ARGS, 20);
60         
61         mockHttpServletRequest.expectAndReturn( "getParameter", "subject", SUBJECT );
62         mockHttpServletRequest.expectAndReturn("getParameterValues", "recipients", RECIPIENTS);
63         mockHttpServletRequest.expectAndReturn( "getParameter", C.args(C.eq("body")), BODY );
64         mockTimer.expectAndReturn("getTime", 10000);
65         mockTimer.expectAndReturn("getTime", 20000);
66         mockMailSender.expectAndReturn("sendMail", C.eq(SUBJECT, RECIPIENTS, BODY),true);
67         
68         final PrintWriter JavaDoc contentWriter = new PrintWriter JavaDoc(new StringWriter JavaDoc());
69         
70         mockHttpServletResponse.expect( "setContentType", "text/html");
71         mockHttpServletResponse.expectAndReturn( "getWriter", contentWriter );
72
73 // Proposed enhancement to allow individual ordering
74
//
75
// CallSignature m1 = mockHttpServletResponse.expect( "setContentType", "text/html");
76
// CallSignature m2 = mockHttpServletResponse.expectAndReturn( "getWriter", C.args(), contentWriter );
77
// m1.expectBefore(m2);
78

79         SimpleServlet aServlet = new SimpleServlet((Timer)mockTimer.proxy(), (MailSender)mockMailSender.proxy());
80         aServlet.doGet((HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy());
81         
82         mockHttpServletRequest.verify();
83         mockHttpServletResponse.verify();
84         mockTimer.verify();
85         mockMailSender.verify();
86     }
87 }
88
Popular Tags