KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > internal > UnorderedBehavior


1 /*
2  * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.internal;
6
7 import java.lang.reflect.Method JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import junit.framework.AssertionFailedError;
12
13 public class UnorderedBehavior extends AbstractBehavior {
14
15     private Map JavaDoc<Method JavaDoc, ResultListMap> methodBehaviors = new HashMap JavaDoc<Method JavaDoc, ResultListMap>();
16
17     private ResultListMap getMethodBehavior(Method JavaDoc 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 JavaDoc 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 JavaDoc failureMessage = "";
55         boolean verifyFailed = false;
56
57         for (Method JavaDoc 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