1 5 package org.easymock.internal; 6 7 import java.lang.reflect.Method ; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 11 import junit.framework.AssertionFailedError; 12 13 public class UnorderedBehavior extends AbstractBehavior { 14 15 private Map <Method , ResultListMap> methodBehaviors = new HashMap <Method , ResultListMap>(); 16 17 private ResultListMap getMethodBehavior(Method method) { 18 if (!methodBehaviors.containsKey(method)) { 19 methodBehaviors.put(method, new ResultListMap(method, 20 getMatcher(method))); 21 } 22 return methodBehaviors.get(method); 23 } 24 25 public void addExpected(MethodCall call, Result returnValue, Range range) { 26 ResultListMap behaviors = getMethodBehavior(call.getMethod()); 27 behaviors.addExpected(call.getArguments(), returnValue, range); 28 } 29 30 protected Result returnValueForUnexpected(Method method) { 31 return null; 32 } 33 34 public Result doAddActual(MethodCall methodCall) { 35 ResultListMap behavior = getMethodBehavior(methodCall.getMethod()); 36 try { 37 return behavior.addActual(methodCall.getArguments()); 38 } catch (AssertionFailedErrorWrapper e) { 39 Result defaultBehavior = getDefaultResult(methodCall.getMethod()); 40 if (defaultBehavior != null) { 41 return defaultBehavior; 42 } 43 Result niceBehavior = returnValueForUnexpected(methodCall 44 .getMethod()); 45 if (niceBehavior != null) { 46 return niceBehavior; 47 } 48 throw e; 49 } 50 51 } 52 53 public void doVerify() { 54 String failureMessage = ""; 55 boolean verifyFailed = false; 56 57 for (Method method : methodBehaviors.keySet()) { 58 try { 59 getMethodBehavior(method).verify(); 60 } catch (AssertionFailedError e) { 61 verifyFailed = true; 62 failureMessage += e.getMessage(); 63 } 64 } 65 66 if (!verifyFailed) 67 return; 68 throw new AssertionFailedErrorWrapper(new AssertionFailedError( 69 failureMessage)); 70 } 71 } | Popular Tags |