[Mockobject-checkins] mockobject2/input/javasrc/biz/xsoftware/mock CalledMethod.java,1.1,1.2 ExpectF
Brought to you by:
fastdragon
From: Dean H. <dea...@us...> - 2004-12-08 18:38:26
|
Update of /cvsroot/mockobject/mockobject2/input/javasrc/biz/xsoftware/mock In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17916/input/javasrc/biz/xsoftware/mock Modified Files: CalledMethod.java ExpectFailedException.java MockObject.java MockSuperclass.java Log Message: add more info into ExpectFailedExceptions. Index: MockSuperclass.java =================================================================== RCS file: /cvsroot/mockobject/mockobject2/input/javasrc/biz/xsoftware/mock/MockSuperclass.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** MockSuperclass.java 13 Nov 2004 15:48:40 -0000 1.6 --- MockSuperclass.java 8 Dec 2004 18:38:13 -0000 1.7 *************** *** 4,8 **** import java.io.StringWriter; import java.util.ArrayList; - import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; --- 4,7 ---- *************** *** 72,75 **** --- 71,77 ---- */ private Map methodToDefaultRetVal = new HashMap(); + + private String[] ignoredMethods = new String[0]; + /** * Default wait time to wait for a method to be called once expectCall is *************** *** 103,106 **** --- 105,115 ---- return waitTime; } + + public void setIgnoredMethods(String[] methods) { + if(methods == null) + ignoredMethods = new String[0]; + ignoredMethods = methods; + } + /** * Subclasses should call this method when a method on their interface *************** *** 150,154 **** //without cloning the object so it can't be changed. parameters = clone(parameters); ! methodsCalled.add(new MethodAndParam(method, parameters, new Throwable().fillInStackTrace())); this.notifyAll(); --- 159,163 ---- //without cloning the object so it can't be changed. parameters = clone(parameters); ! methodsCalled.add(new CalledMethod(method, parameters, new Throwable().fillInStackTrace())); this.notifyAll(); *************** *** 184,195 **** * @see biz.xsoftware.mock.MockObject#expectUnorderedCalls(java.lang.String[]) */ ! public CalledMethod[] expectUnorderedCalls(String[] methods) { ! return expectUnorderedCalls(methods, null); ! } ! ! /** ! * @see biz.xsoftware.mock.MockObject#expectUnorderedCalls(java.lang.String[], java.lang.String[]) ! */ ! synchronized public CalledMethod[] expectUnorderedCalls(String[] methods, String[] ignorableMethods) { if(methods == null) throw new IllegalArgumentException("methods cannot be null"); --- 193,197 ---- * @see biz.xsoftware.mock.MockObject#expectUnorderedCalls(java.lang.String[]) */ ! synchronized public CalledMethod[] expectUnorderedCalls(String[] methods) { if(methods == null) throw new IllegalArgumentException("methods cannot be null"); *************** *** 206,210 **** } ! Map ignorables = createIgnorableMap(ignorableMethods); CalledMethod[] retVal = new CalledMethod[methods.length]; --- 208,212 ---- } ! Map ignorables = createIgnorableMap(ignoredMethods); CalledMethod[] retVal = new CalledMethod[methods.length]; *************** *** 214,230 **** throw new IllegalArgumentException("The parameter 'methods' in expectUnorderedCalls cannot contain MockSuperclass.ANY(use expectOrderedCalls instead)"); ! MethodAndParam o = expectUnignoredCall(ANY, ignorables, methodsCalledList); if(o == null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, "timed out on next expected method\n"); ! throw new ExpectFailedException("Timed out waiting for a method call\n"+reason, ExpectFailedException.TIMED_OUT); } ! Integer index = (Integer)expectedMethods.remove(o.getMethod()); if(index == null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, null); ! throw new ExpectFailedException(reason, ExpectFailedException.UNEXPECTED_CALL_BEFORE); } ! ! retVal[index.intValue()] = new CalledMethod(o.method, o.getParams()); } --- 216,231 ---- throw new IllegalArgumentException("The parameter 'methods' in expectUnorderedCalls cannot contain MockSuperclass.ANY(use expectOrderedCalls instead)"); ! CalledMethod o = expectUnignoredCall(ANY, ignorables, methodsCalledList); if(o == null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, "timed out on next expected method\n"); ! throw new ExpectFailedException("Timed out waiting for a method call\n"+reason, retVal, ExpectFailedException.TIMED_OUT); } ! Integer index = (Integer)expectedMethods.remove(o.getMethodName()); if(index == null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, null); ! throw new ExpectFailedException(reason, retVal, ExpectFailedException.UNEXPECTED_CALL_BEFORE); } ! retVal[index.intValue()] = o; } *************** *** 232,236 **** if(leftOver != null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, "extra method(s)="+leftOver+"\n"); ! throw new ExpectFailedException("There was a method called after your expected methods.\n"+reason , ExpectFailedException.UNEXPECTED_CALL_AFTER); } --- 233,237 ---- if(leftOver != null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, "extra method(s)="+leftOver+"\n"); ! throw new ExpectFailedException("There was a method called after your expected methods.\n"+reason, retVal , ExpectFailedException.UNEXPECTED_CALL_AFTER); } *************** *** 239,267 **** } ! private String getHowMethodWasCalled(MethodAndParam method) { Throwable t = method.getHowItWasCalled(); ! String retVal = "\nThe last method was="+method.getMethod(); retVal += "\nHere is a stack trace showing "; retVal += "you how it was called...\n"; ! retVal += "--------BEGIN="+method.getMethod()+"---------------\n"; StringWriter s = new StringWriter(); PrintWriter p = new PrintWriter(s); t.printStackTrace(p); retVal += s.toString(); ! retVal += "--------END="+method.getMethod() +"---------------\n"; return retVal; } /** * @see biz.xsoftware.mock.MockObject#expectCall(java.lang.String) */ public CalledMethod expectCall(String method) { ! return expectCall(method, null); ! } ! /** ! * @see biz.xsoftware.mock.MockObject#expectCall(java.lang.String, java.lang.String[]) ! */ ! public CalledMethod expectCall(String method, String[] ignorableMethods) { ! return expectOrderedCalls(new String[] {method}, ignorableMethods)[0]; } --- 240,270 ---- } ! private String getHowMethodWasCalled(CalledMethod method) { Throwable t = method.getHowItWasCalled(); ! String retVal = "\nThe last method was="+method.getMethodName(); retVal += "\nHere is a stack trace showing "; retVal += "you how it was called...\n"; ! retVal += "--------BEGIN="+method.getMethodName()+"---------------\n"; StringWriter s = new StringWriter(); PrintWriter p = new PrintWriter(s); t.printStackTrace(p); retVal += s.toString(); ! retVal += "--------END="+method.getMethodName() +"---------------\n"; return retVal; } + private CalledMethod[] cleanup(CalledMethod[] methods, int size) { + CalledMethod[] retVal = new CalledMethod[size]; + for(int i = 0; i < retVal.length; i++) { + retVal[i] = methods[i]; + } + return retVal; + } + /** * @see biz.xsoftware.mock.MockObject#expectCall(java.lang.String) */ public CalledMethod expectCall(String method) { ! return expectOrderedCalls(new String[] {method})[0]; } *************** *** 269,280 **** * @see biz.xsoftware.mock.MockObject#expectOrderedCalls(java.lang.String[]) */ ! public CalledMethod[] expectOrderedCalls(String[] methods) { ! return expectOrderedCalls(methods, null); ! } ! ! /** ! * @see biz.xsoftware.mock.MockObject#expectOrderedCalls(java.lang.String[], java.lang.String[]) ! */ ! synchronized public CalledMethod[] expectOrderedCalls(String[] methods, String[] ignorableMethods) { if(methods == null) throw new IllegalArgumentException("methods cannot be null"); --- 272,276 ---- * @see biz.xsoftware.mock.MockObject#expectOrderedCalls(java.lang.String[]) */ ! synchronized public CalledMethod[] expectOrderedCalls(String[] methods) { if(methods == null) throw new IllegalArgumentException("methods cannot be null"); *************** *** 286,306 **** } ! Map ignorables = createIgnorableMap(ignorableMethods); List methodsCalledList = new ArrayList(); CalledMethod[] retVal = new CalledMethod[methods.length]; ! for(int i = 0; i < methods.length; i++) { String method = methods[i]; ! MethodAndParam o = expectUnignoredCall(method, ignorables, methodsCalledList); if(o == null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, "timed out on next expected method\n"); ! throw new ExpectFailedException("Timed out waiting for call="+method+"\n"+reason, ExpectFailedException.TIMED_OUT); } ! retVal[i] = new CalledMethod(o.method, o.getParams()); ! if(!method.equals(o.method) && !ANY.equals(method)) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, null); reason += getHowMethodWasCalled(o); ! throw new ExpectFailedException(reason, null); } } --- 282,308 ---- } ! Map ignorables = createIgnorableMap(ignoredMethods); List methodsCalledList = new ArrayList(); CalledMethod[] retVal = new CalledMethod[methods.length]; ! int i = 0; ! for(; i < methods.length; i++) { String method = methods[i]; ! CalledMethod o = null; ! try { ! o = expectUnignoredCall(method, ignorables, methodsCalledList); ! } catch(ExpectFailedException e) { ! throw new ExpectFailedException(e.getMessage(), cleanup(retVal, i), e.getReason()); ! } if(o == null) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, "timed out on next expected method\n"); ! throw new ExpectFailedException("Timed out waiting for call="+method+"\n"+reason, cleanup(retVal, i), ExpectFailedException.TIMED_OUT); } ! retVal[i] = o; ! if(!method.equals(o.getMethodName()) && !ANY.equals(method)) { String reason = putTogetherReason(methods, ignorables, methodsCalledList, null); reason += getHowMethodWasCalled(o); ! throw new ExpectFailedException(reason, cleanup(retVal, i), null); } } *************** *** 310,314 **** String reason = putTogetherReason(methods, ignorables, methodsCalledList, "extra method(s)="+leftOver+"\n"); reason += getHowMethodWasCalled(leftOver.getMethods()[0]); ! throw new ExpectFailedException("There was a method called after your expected methods.\n"+reason , ExpectFailedException.UNEXPECTED_CALL_AFTER); } --- 312,316 ---- String reason = putTogetherReason(methods, ignorables, methodsCalledList, "extra method(s)="+leftOver+"\n"); reason += getHowMethodWasCalled(leftOver.getMethods()[0]); ! throw new ExpectFailedException("There was a method called after your expected methods.\n"+reason, cleanup(retVal, i) , ExpectFailedException.UNEXPECTED_CALL_AFTER); } *************** *** 346,350 **** } ! synchronized protected MethodAndParam expectUnignoredCall(String method, Map ignorables, List calledMethods) { if(method == null) --- 348,352 ---- } ! synchronized protected CalledMethod expectUnignoredCall(String method, Map ignorables, List calledMethods) { if(method == null) *************** *** 361,366 **** if(leftOver != null) throw new ExpectFailedException("A method that wasn't expected(nor ignored) was\n" ! +"called earlier. method(s)="+leftOver, ExpectFailedException.UNEXPECTED_ON_NONE); ! return new MethodAndParam(NONE, null, null); } --- 363,368 ---- if(leftOver != null) throw new ExpectFailedException("A method that wasn't expected(nor ignored) was\n" ! +"called earlier. method(s)="+leftOver, null, ExpectFailedException.UNEXPECTED_ON_NONE); ! return new CalledMethod(NONE, null, null); } *************** *** 372,376 **** } ! private MethodAndParam waitForUnignoredCall(String logM, Map ignorables, List calledMethods) throws InterruptedException { long waitTime2 = waitTime+50; --- 374,378 ---- } ! private CalledMethod waitForUnignoredCall(String logM, Map ignorables, List calledMethods) throws InterruptedException { long waitTime2 = waitTime+50; *************** *** 382,386 **** currentTime = System.currentTimeMillis(); if (log.isLoggable(Level.FINE)) ! log.fine("method expected(not called yet-waiting)="+logM+" on obj="+this); this.wait(waitTime2); long waitedOnWait = System.currentTimeMillis() - currentTime; --- 384,388 ---- currentTime = System.currentTimeMillis(); if (log.isLoggable(Level.FINE)) ! log.fine("method expected(not called yet-waiting)="+logM+" on obj="+this+" wait="+waitTime2); this.wait(waitTime2); long waitedOnWait = System.currentTimeMillis() - currentTime; *************** *** 391,397 **** //continue while loop. for(int i = 0; i < methodsCalled.size(); i++) { ! MethodAndParam calledMethod = (MethodAndParam)methodsCalled.remove(0); calledMethods.add(calledMethod); ! if(ignorables.get(calledMethod.getMethod()) == null) { if(log.isLoggable(Level.FINE)) log.fine("method expected and was called="+logM); --- 393,399 ---- //continue while loop. for(int i = 0; i < methodsCalled.size(); i++) { ! CalledMethod calledMethod = (CalledMethod)methodsCalled.remove(0); calledMethods.add(calledMethod); ! if(ignorables.get(calledMethod.getMethodName()) == null) { if(log.isLoggable(Level.FINE)) log.fine("method expected and was called="+logM); *************** *** 418,423 **** List leftOver = new ArrayList(); for(int i = 0; i < methodsCalled.size(); i++) { ! MethodAndParam o = (MethodAndParam)methodsCalled.get(i); ! if(ignorables.get(o.getMethod()) == null && o != null) { leftOver.add(o); } --- 420,425 ---- List leftOver = new ArrayList(); for(int i = 0; i < methodsCalled.size(); i++) { ! CalledMethod o = (CalledMethod)methodsCalled.get(i); ! if(ignorables.get(o.getMethodName()) == null && o != null) { leftOver.add(o); } *************** *** 426,431 **** return null; ! MethodAndParam[] m = new MethodAndParam[0]; ! m = (MethodAndParam[])leftOver.toArray(m); return new LeftOverMethods(m); } --- 428,433 ---- return null; ! CalledMethod[] m = new CalledMethod[0]; ! m = (CalledMethod[])leftOver.toArray(m); return new LeftOverMethods(m); } *************** *** 433,438 **** private class LeftOverMethods { ! private MethodAndParam[] leftOver; ! public LeftOverMethods(MethodAndParam[] m) { leftOver = m; } --- 435,440 ---- private class LeftOverMethods { ! private CalledMethod[] leftOver; ! public LeftOverMethods(CalledMethod[] m) { leftOver = m; } *************** *** 440,444 **** * @return */ ! public MethodAndParam[] getMethods() { return leftOver; } --- 442,446 ---- * @return */ ! public CalledMethod[] getMethods() { return leftOver; } *************** *** 455,458 **** --- 457,464 ---- */ synchronized public void addThrowException(String method, Throwable e) { + if(method == null) + throw new IllegalArgumentException("method parameter cannot be null"); + else if(e == null) + throw new IllegalArgumentException("e parameter to this method cannot be null"); List l = (List)methodToException.get(method); if(l == null) { *************** *** 467,470 **** --- 473,478 ---- */ synchronized public void addReturnValue(String method, Object o) { + if(method == null) + throw new IllegalArgumentException("method parameter cannot be null"); List l = (List)methodToReturnVal.get(method); if(l == null) { *************** *** 491,520 **** return params; } - - private class MethodAndParam { - private String method; - private Object[] param; - private Throwable howItWasCalled; - public MethodAndParam(String method, Object[] param, Throwable howItWasCalled) { - this.method = method; - this.param = param; - this.howItWasCalled = howItWasCalled; - } - public String getMethod() { - return method; - } - public Object[] getParams() { - return param; - } - public Throwable getHowItWasCalled() { - return howItWasCalled; - } - public String toString() { - List params = null; - if(param != null) - params = Arrays.asList(param); - - return "[method="+method+" params="+params+"]"; - } - } } --- 499,501 ---- Index: CalledMethod.java =================================================================== RCS file: /cvsroot/mockobject/mockobject2/input/javasrc/biz/xsoftware/mock/CalledMethod.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CalledMethod.java 12 Sep 2004 07:14:10 -0000 1.1 --- CalledMethod.java 8 Dec 2004 18:38:13 -0000 1.2 *************** *** 7,10 **** --- 7,13 ---- package biz.xsoftware.mock; + import java.util.Arrays; + import java.util.List; + /** * @author Dean Hiller *************** *** 17,27 **** private String method; private Object[] params; ! public CalledMethod(String method, Object[] params) { if(params == null) this.params = new Object[0]; else ! this.params = params; ! this.method = method; } --- 20,32 ---- private String method; private Object[] params; + private Throwable howItWasCalled; ! public CalledMethod(String method, Object[] params, Throwable howItWasCalled) { ! this.method = method; if(params == null) this.params = new Object[0]; else ! this.params = params; ! this.howItWasCalled = howItWasCalled; } *************** *** 37,43 **** return params[index]; } ! public Object[] getAllParams() { return params; } } --- 42,62 ---- return params[index]; } ! /** ! * ! * @return ! */ public Object[] getAllParams() { return params; } + + public Throwable getHowItWasCalled() { + return howItWasCalled; + } + public String toString() { + List paramList = null; + if(params != null) + paramList = Arrays.asList(params); + + return "[method="+method+" params="+paramList+"]"; + } } Index: MockObject.java =================================================================== RCS file: /cvsroot/mockobject/mockobject2/input/javasrc/biz/xsoftware/mock/MockObject.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MockObject.java 13 Nov 2004 14:29:41 -0000 1.2 --- MockObject.java 8 Dec 2004 18:38:13 -0000 1.3 *************** *** 25,34 **** public static String ANY = "'Any method'"; ! //public CalledMethod expectCalls(int numberMethodsToBeCalled); ! public void setExpectTimeout(int timeout); ! ! public int getExpectTimeout(); /** --- 25,37 ---- public static String ANY = "'Any method'"; ! //possibly for any.... //public CalledMethod expectCalls(int numberMethodsToBeCalled); ! /** ! * When calling expectCall, the MockObject will ignore the methods ! * in 'methods' variable so if one of the methods in this array is ! * called, it will not result in an exception. ! */ ! public void setIgnoredMethods(String[] methods); /** *************** *** 44,64 **** /** - * Waits for one and only one method to be called. If any methods are - * called before or after this one(after can sometimes be caught by - * the MockObject when the threading is not synchronous), then - * this call will throw an ExpectFailedException. - * <br/><br/> - * If any of the ignoredMethods are called, they will be ignored and not - * verified at all that they were called or not called. They are like the - * toString on object which will be ignored every time so you can write - * a test case that is less brittle. - * - * @param method The expected method. - * @param ignoredMethods methods to ignore(don't throw an exception if one of these accidentally happens) - * @return An array of params that were passed to the methods called - */ - public CalledMethod expectCall(String method, String[] ignoredMethods); - - /** * Waits for all the methods to be called. If any of the methods * are not called or are called out of order, this method throws --- 47,50 ---- *************** *** 72,93 **** */ public CalledMethod[] expectOrderedCalls(String[] methods); - /** - * Waits for all the methods to be called. If any of the methods - * are not called or are called out of order, this method throws - * an exception which will fail the test case. For each method, - * this will wait WAIT_TIME milliseconds for each method to be called. - * If the method is not called within this period, an exception will - * be thrown saying 'method' was not called within timeout period. - * <br/><br/> - * If any of the ignoredMethods are called, they will be ignored and not - * verified at all that they were called or not called. They are like the - * toString on object which will be ignored every time so you can write - * a test case that is less brittle. - * - * @param methods The expected methods in the correct order. - * @param ignoredMethods methods to ignore(don't throw an exception if one of these accidentally happens) - * @return An array of params that were passed to the methods called - */ - public CalledMethod[] expectOrderedCalls(String[] methods, String[] ignoredMethods); /** --- 58,61 ---- *************** *** 100,117 **** */ public CalledMethod[] expectUnorderedCalls(String[] methods); - /** - * Expect many methods to be called irrelevant of the order in which they are - * called. - * <br/><br/> - * If any of the ignoredMethods are called, they will be ignored and not - * verified at all that they were called or not called. They are like the - * toString on object which will be ignored every time so you can write - * a test case that is less brittle. - * - * @param methods The methods to be called in no particular order - * @return An array of params that were passed to the methods called. This - * array lines up with the methods array passed in. - */ - public CalledMethod[] expectUnorderedCalls(String[] methods, String[] ignoredMethods); /** --- 68,71 ---- *************** *** 157,159 **** --- 111,117 ---- */ public void setDefaultReturnValue(String method, Object o); + + public void setExpectTimeout(int timeout); + + public int getExpectTimeout(); } Index: ExpectFailedException.java =================================================================== RCS file: /cvsroot/mockobject/mockobject2/input/javasrc/biz/xsoftware/mock/ExpectFailedException.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ExpectFailedException.java 12 Sep 2004 07:14:10 -0000 1.1 --- ExpectFailedException.java 8 Dec 2004 18:38:13 -0000 1.2 *************** *** 37,41 **** private String reason; ! --- 37,41 ---- private String reason; ! private CalledMethod[] methods; *************** *** 45,57 **** * @param message */ ! public ExpectFailedException(String message, String reason) { super(message); this.reason = reason; } - - // public ExpectFailedException(String message, Throwable e, String reason) { - // super(message, e); - // this.reason = reason; - // } /** --- 45,53 ---- * @param message */ ! public ExpectFailedException(String message, CalledMethod[] methods, String reason) { super(message); this.reason = reason; + this.methods = methods; } /** *************** *** 70,72 **** --- 66,79 ---- return reason; } + + /** + * Gives you back the CalledMethods giving you access to the + * parameters that were passed in and the stack traces of how + * they were called. + * + * @return + */ + public CalledMethod[] getCalledMethods() { + return methods; + } } |