|
From: <pir...@ya...> - 2001-11-19 20:46:47
|
Hi fellow mockers,
I've had earlier version of these changes pending in the sourceforge for sometime. Not having access to the CVS, I'm emailing these updates in.
Here are differences for MockDynamoHttpServletRequest.java and a new file for MockDynamoServlet.java.
First, com.mockobjects.atg.MockDynamoHttpServletRequest.java. The changes are to call MockDynamoServlet if possible, and to fix a bug in getLocalParameter()
1a2
> import atg.servlet.DynamoServlet;
2a4
> import atg.servlet.DynamoHttpServletResponse;
9c11,14
< import com.mockobjects.util.Verifier;public class MockDynamoHttpServletRequest
extends DynamoHttpServletRequest {
---
> import com.mockobjects.util.Verifier;
>
> public class MockDynamoHttpServletRequest extends DynamoHttpServletRequest {
>
42a48,51
> public Object getOutputParameter(String key) {
> return myOutputParameters.get(key);
> }
>
51c60,62
< return true;
---
> DynamoServlet servlet = (DynamoServlet) this.getLocalParameter(name);
> if (servlet != null)
> servlet.service((DynamoHttpServletRequest)request, (DynamoHttpServ
letResponse)response);
52a64
> return true;
56c68
< return (String) myInputParameters.get(parameterName);
---
> return myInputParameters.get(parameterName);
Second, com.mockobjects.atg.MockDynamoServlet is a new class which mocks oparams in calls. The current implementation is very simple, counting its calls or checking its parameters. However, you can override it to do more of your own checks.
package com.mockobjects.atg;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import atg.servlet.DynamoServlet;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import com.mockobjects.Verifiable;
import com.mockobjects.util.Verifier;
import com.mockobjects.ExpectationSet;
import com.mockobjects.ExpectationCounter;
import com.mockobjects.MapEntry;
import com.mockobjects.atg.MockDynamoHttpServletRequest;
public class MockDynamoServlet extends DynamoServlet implements Verifiable {
private ExpectationCounter myServiceCalls = new ExpectationCounter("MockDynamoServlet.service");
private ExpectationSet myExpectedOutputParameters = new ExpectationSet("Output Parameters");
private Set myExpectedParamNames = new HashSet();
public void setExpectedServiceCalls(int callCount) {
myServiceCalls.setExpected(callCount);
}
public void setExpectedParameter(String name, Object value) {
myExpectedOutputParameters.addExpected(new MapEntry(name, value));
myExpectedParamNames.add(name);
}
public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
myServiceCalls.inc();
if (request instanceof MockDynamoHttpServletRequest) {
MockDynamoHttpServletRequest mockRequest = (MockDynamoHttpServletRequest) request;
for ( Iterator i = myExpectedParamNames.iterator(); i.hasNext(); ) {
String key = (String) i.next();
Object value = mockRequest.getOutputParameter(key);
myExpectedOutputParameters.addActual(new MapEntry(key, value));
}
}
}
public void verify() {
Verifier.verifyObject(this);
}
}
Below is a simple droplet Size that gets the size of something, and after that it's junit test case:
package com.philips.utils;import java.io.IOException;import atg.servlet.*;import javax.servlet.*;import java.util.Collection;import java.util.Map;/** * Get the size of an array, collection, string or map. * * param <code>value</code>: the value to find the size of<br> * oparam <code>output</code>: passed the param <code>size</code> with the size of <code>value</code><br> * oparam <code>error</code>: passed the param <code>errorMsg</code> in case of an error * * For strings, the size is the length of the string
* For collections, the size is the number of elements
* For arrays, the size is the array length
* For maps, the size is the number of elements
* * @author Piran Montford * @version $Revision: 1.2 $ */public class Size extends DynamoServlet { private static final String PARAM_VALUE = "value"; private static final String PARAM_SIZE = "size"; private static final String PARAM_OUTPUT = "output"; private static final String PARAM_ERROR = "error"; private static final String PARAM_ERROR_MSG = "errorMsg"; /** Creates new Size */ public Size() {} /** * Find the size of the parameter. */ public void service(DynamoHttpServletRequest request, DynamoHttpServletResponse response) throws ServletException, IOException { int size; Object value = request.getLocalParameter(PARAM_VALUE); if (value instanceof Collection) { size = ((Collection) value).size(); } else if (value instanceof Object[]) { size = ((Object[]) value).length; } else if (value instanceof Map) { size = ((Map) value).size(); } else if (value instanceof String) { size = ((String) value).length(); } else { request.setParameter(PARAM_ERROR_MSG, "unknown type of value"); request.serviceLocalParameter(PARAM_ERROR, request, response); return; } request.setParameter(PARAM_SIZE, new Integer(size)); request.serviceLocalParameter(PARAM_OUTPUT, request, response); }}import java.io.IOException;import atg.servlet.*;import javax.servlet.*;import java.util.Collection;import java.util.Map;import java.util.HashMap;import java.util.List;import java.util.ArrayList;import java.util.Set;import java.util.HashSet;import junit.framework.TestCase;import junit.framework.TestSuite;import com.mockobjects.atg.MockDynamoServlet;import com.mockobjects.atg.MockDynamoHttpServletRequest;import com.mockobjects.atg.MockDynamoHttpServletResponse;/** * JUnit Test class for Size * * @author Piran Montford * @version $Revision: 1.1 $ */public class TestSize extends TestCase { public TestSize(String name) { super(name); } /** TestSuite of tests in this class */ public static TestSuite suite() { return new TestSuite(TestSize.class); } Size mTarget; MockDynamoHttpServletRequest mRequest; MockDynamoHttpServletResponse mResponse; MockDynamoServlet mOutput, mError; public void setUp() { mTarget = new Size(); mRequest = new MockDynamoHttpServletRequest(); mResponse = new MockDynamoHttpServletResponse(); mOutput = new MockDynamoServlet(); mError = new MockDynamoServlet(); mRequest.setupInputParameter("output", mOutput); mRequest.setupInputParameter("error", mError); } /** Verify all the default variables */ public void verify() { mRequest.verify(); mResponse.verify(); mOutput.verify(); mError.verify(); } /** Test we get the right length of a string */ public void testString() throws Throwable { doOutputCall( "Size5", 5 ); } /** Test we get the right length of a list */ public void testList() throws Throwable { List test = new ArrayList(); test.add( "one" ); test.add( "two" ); test.add( "three" ); doOutputCall(test, 3); } /** Test we get the right airity of a set */ public void testSet() throws Throwable { Set test = new HashSet(); test.add("ragtag"); test.add("bobtail"); doOutputCall(test, 2); } /** Test we get the right size of a map */ public void testMap() throws Throwable { Map test = new HashMap(); test.put("pupil", "teacher"); test.put("student", "lecturer"); test.put("ward", "nurse"); test.put("charge", "police"); doOutputCall(test, 4); } /** Test we get the right lenght of an array */ public void testStringArray() throws Throwable { String[] test = {"Enie", "Meenie", "Minie", "Moe"}; doOutputCall(test, 4); } /** Test we get an error for a non-collection type */ public void testBadType() throws Throwable { Integer test = new Integer(3); mError.setExpectedServiceCalls(1); mOutput.setExpectedServiceCalls(0); mRequest.setupInputParameter("value", test); mTarget.service(mRequest, mResponse); } /** Test we get an error for a null value */ public void testNullValue() throws Throwable { mError.setExpectedServiceCalls(1); mOutput.setExpectedServiceCalls(0); mRequest.setupInputParameter("value", null); mTarget.service(mRequest, mResponse); } /** Call the service() method expecting success with the test value, and the predicted size */ void doOutputCall(Object test, int size) throws Throwable { mError.setExpectedServiceCalls(0); mOutput.setExpectedServiceCalls(1); mOutput.setExpectedParameter("size", new Integer(size)); mRequest.setupInputParameter("value", test); mTarget.service(mRequest, mResponse); verify(); } }
BCNU, Piran.
I came here for a reason, and I don't intend to leave without one
Piran Montford mailto:pi...@po... http://www.pobox.com/piran
---------------------------------
Do You Yahoo!?
Get personalised at My Yahoo!. |