From: Steve F. <sm...@us...> - 2002-09-08 15:19:01
|
Update of /cvsroot/mockobjects/no-stone-unturned/src/java/nostone/addressbook In directory usw-pr-cvs1:/tmp/cvs-serv18399/src/java/nostone/addressbook Modified Files: AddressBookServletTest.java AddressBookServlet.java Log Message: Reworked servlet tests. Realised that testNoEntries is testMissingRequest Added ExpectationValue for parameterName Extracted some instance variables in the test case Index: AddressBookServletTest.java =================================================================== RCS file: /cvsroot/mockobjects/no-stone-unturned/src/java/nostone/addressbook/AddressBookServletTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AddressBookServletTest.java 2 Sep 2002 22:40:11 -0000 1.2 +++ AddressBookServletTest.java 8 Sep 2002 15:18:57 -0000 1.3 @@ -6,38 +6,64 @@ import javax.servlet.ServletException; import java.io.*; +import com.mockobjects.ExpectationValue; + public class AddressBookServletTest extends TestCase { + private final StringWriter page = new StringWriter(); + private final ExpectationValue requestParameterName = new ExpectationValue("Request parameter name"); + private HttpServletResponse mockResponse = new NullHttpServletResponse() { + private String contentType; + public void setContentType(String aContentType) { + assertEquals("Content type", "text/plain", aContentType); + contentType = aContentType; + } + + public PrintWriter getWriter() throws IOException { + assertNotNull("Should have content type", contentType); + return new PrintWriter(page); + } + }; + private AddressBookServlet servlet = new AddressBookServlet(); + public AddressBookServletTest(String name) { super(name); } public void testNoEntries() throws ServletException, IOException { - final StringWriter page = new StringWriter(); + requestParameterName.setExpected("name"); + HttpServletRequest mockRequest = new NullHttpServletRequest() { + public String getMethod() { + return "GET"; + } + public String getProtocol() { + return "HTTP/1.1"; + } + }; + servlet.service(mockRequest, mockResponse); + requestParameterName.verify(); + assertEquals("Should be empty response", "No address found", page.toString().trim()); + } + + public void testFindOneAddress() throws ServletException, IOException { + requestParameterName.setExpected("name"); HttpServletRequest mockRequest = new NullHttpServletRequest() { - public String getMethod() { - return "GET"; - } - public String getProtocol() { - return "HTTP/1.1"; - } - }; - HttpServletResponse mockResponse = new NullHttpServletResponse() { - private String contentType; - public void setContentType(String aContentType) { - assertEquals("Content type", "text/plain", aContentType); - contentType = aContentType; - } - - public PrintWriter getWriter() throws IOException { - assertNotNull("Should have content type", contentType); - return new PrintWriter(page); - } - }; + public String getMethod() { + return "GET"; + } + public String getProtocol() { + return "HTTP/1.1"; + } - AddressBookServlet servlet = new AddressBookServlet(); + public String getParameter(String parameterName) { + requestParameterName.setActual(parameterName); + return "surname"; + } + }; servlet.service(mockRequest, mockResponse); - assertEquals("Should be empty response", "No address found", page.toString().trim()); + requestParameterName.verify(); + assertEquals("Should be empty response", "surname@domain", page.toString().trim()); } + } Index: AddressBookServlet.java =================================================================== RCS file: /cvsroot/mockobjects/no-stone-unturned/src/java/nostone/addressbook/AddressBookServlet.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AddressBookServlet.java 2 Sep 2002 22:40:11 -0000 1.2 +++ AddressBookServlet.java 8 Sep 2002 15:18:57 -0000 1.3 @@ -11,9 +11,11 @@ } protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException - { + throws ServletException, IOException { + String name = request.getParameter("name"); + response.setContentType("text/plain"); - response.getWriter().println( "No address found" ); + response.getWriter().println( + name == null ? "No address found" : "surname@domain"); } } |