From: Barry K. <bk...@in...> - 2003-05-14 15:09:12
|
I had a need for a CallBag, except I do not want to limit the number of calls. Mainly to support getters. Below is what I did. Is there another way? If not, could this be incorporated into the dynamic package? Notice that I had to override the field 'numcalls'. This is smelly. But I could not see any other way. And I'm not too happy about the class name. Also, I noticed that the comments in CallBag that the class was temporary, and would be pushed-up into CallSequence. If that is the case, I would like to see the ability to support unspecified number of calls. (Isn't there other support for unspecified number of calls in the Mock class?) ---- package mockobjects; import com.mockobjects.dynamic.CallCollection; import com.mockobjects.dynamic.MockCall; public class CallSet extends CallCollection { private int numCalls = 0; public Object call(Object[] args) throws Throwable { numCalls++; int callIndex = findCallIndexAndCheckArguments(args, numCalls); MockCall call = (MockCall)_expected_calls.get(callIndex); _hasBeenCalled.add(callIndex, Boolean.TRUE); return call.call(args); } protected int findCallIndexAndCheckArguments( Object[] args, int callNumber) { for (int i = 0; i < _expected_constraints.size(); i++) { if (checkArguments(i, args)) { return i; } } fail(errorMessage(args)); return -1; } public void verify() { assertTrue(errorMessage( null), _expected_calls.size() <= numCalls); } } ---- |