|
From: Steve F. <sm...@us...> - 2002-10-20 23:28:57
|
Update of /cvsroot/mockobjects/no-stone-unturned/src/java/nostone/addressbook
In directory usw-pr-cvs1:/tmp/cvs-serv30492/src/java/nostone/addressbook
Modified Files:
AddressBookServlet.java AddressBookServletTest.java
Log Message:
added more tests for one entry
Index: AddressBookServlet.java
===================================================================
RCS file: /cvsroot/mockobjects/no-stone-unturned/src/java/nostone/addressbook/AddressBookServlet.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AddressBookServlet.java 8 Sep 2002 15:18:57 -0000 1.3
+++ AddressBookServlet.java 20 Oct 2002 23:28:54 -0000 1.4
@@ -7,15 +7,22 @@
import java.io.IOException;
public class AddressBookServlet extends HttpServlet {
- public AddressBookServlet() {
- }
-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
response.setContentType("text/plain");
response.getWriter().println(
- name == null ? "No address found" : "surname@domain");
+ lookupName(name));
+ }
+
+ private String lookupName(String name) {
+ if (name == null) {
+ return "No name";
+ } else if (name.equals("somebody")) {
+ return "somebody@domain";
+ } else {
+ return "No address found";
+ }
}
}
Index: AddressBookServletTest.java
===================================================================
RCS file: /cvsroot/mockobjects/no-stone-unturned/src/java/nostone/addressbook/AddressBookServletTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AddressBookServletTest.java 8 Sep 2002 15:18:57 -0000 1.3
+++ AddressBookServletTest.java 20 Oct 2002 23:28:54 -0000 1.4
@@ -10,35 +10,54 @@
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 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 String requestParameter;
+ private final ExpectationValue requestParameterName = new ExpectationValue("Request parameter name");
private AddressBookServlet servlet = new AddressBookServlet();
+ private HttpServletRequest mockRequest = new NullHttpServletRequest() {
+ public String getMethod() {
+ return "GET";
+ }
+
+ public String getProtocol() {
+ return "HTTP/1.1";
+ }
+
+ public String getParameter(String parameterName) {
+ requestParameterName.setActual(parameterName);
+ return requestParameter;
+ }
+ };
public AddressBookServletTest(String name) {
super(name);
}
- public void testNoEntries() throws ServletException, IOException {
+ public void testMissingName() throws ServletException, IOException {
+ requestParameterName.setExpected("name");
+
+ servlet.service(mockRequest, mockResponse);
+
+ requestParameterName.verify();
+ assertEquals("Should be error message", "No name", page.toString().trim());
+ }
+
+ public void testFindNoAddress() throws ServletException, IOException {
requestParameterName.setExpected("name");
- HttpServletRequest mockRequest = new NullHttpServletRequest() {
- public String getMethod() {
- return "GET";
- }
- public String getProtocol() {
- return "HTTP/1.1";
- }
- };
+
+ requestParameter = "nobody";
servlet.service(mockRequest, mockResponse);
requestParameterName.verify();
@@ -47,23 +66,12 @@
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";
- }
-
- public String getParameter(String parameterName) {
- requestParameterName.setActual(parameterName);
- return "surname";
- }
- };
+
+ requestParameter = "somebody";
servlet.service(mockRequest, mockResponse);
requestParameterName.verify();
- assertEquals("Should be empty response", "surname@domain", page.toString().trim());
+ assertEquals("Should be expected address", "somebody@domain", page.toString().trim());
}
}
|