KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 import junit.framework.AssertionFailedError;
11
12 public class OrderedBehavior extends AbstractBehavior {
13
14     private List JavaDoc<ExpectedMethodCall> methodCalls = new ArrayList JavaDoc<ExpectedMethodCall>();
15
16     private List JavaDoc<ResultList> resultLists = new ArrayList JavaDoc<ResultList>();
17
18     private int position = 0;
19
20     public void addExpected(MethodCall methodCall, Result result, Range range) {
21         ExpectedMethodCall matchableMethodCall = new ExpectedMethodCall(
22                 methodCall.getMethod(), methodCall.getArguments(),
23                 getMatcher(methodCall.getMethod()));
24
25         if (!lastMethodCallEqualTo(matchableMethodCall)) {
26             methodCalls.add(matchableMethodCall);
27             resultLists.add(new ResultList());
28         }
29         ResultList resultList = (ResultList) resultLists
30                 .get(resultLists.size() - 1);
31         resultList.add(result, range);
32     }
33
34     private boolean lastMethodCallEqualTo(ExpectedMethodCall matchableMethodCall) {
35         if (methodCalls.isEmpty()) {
36             return false;
37         }
38         ExpectedMethodCall lastMethodCall = methodCalls
39                 .get(methodCalls.size() - 1);
40         return lastMethodCall.equals(matchableMethodCall);
41     }
42
43     public Result doAddActual(MethodCall methodCall) {
44         List JavaDoc<ExpectedMethodCall> matchedCalls = new ArrayList JavaDoc<ExpectedMethodCall>();
45         List JavaDoc<ResultList> matchedResultLists = new ArrayList JavaDoc<ResultList>();
46
47         int tooManyCallsPosition = -1;
48
49         for (; position < methodCalls.size(); position++) {
50             ExpectedMethodCall expectedCall = methodCalls.get(position);
51             ResultList resultList = resultLists.get(position);
52
53             if (expectedCall.matches(methodCall)) {
54                 Result result = resultList.next();
55                 if (result != null) {
56                     return result;
57                 }
58
59                 matchedCalls.add(expectedCall);
60                 matchedResultLists.add(resultList);
61                 tooManyCallsPosition = matchedCalls.size() - 1;
62                 continue;
63             } else {
64                 if (resultList.hasValidCallCount()) {
65                     continue;
66                 }
67
68                 matchedCalls.add(expectedCall);
69                 matchedResultLists.add(resultList);
70                 break;
71             }
72         }
73         Result defaultBehavior = getDefaultResult(methodCall.getMethod());
74         if (defaultBehavior != null) {
75             return defaultBehavior;
76         }
77         throw new AssertionFailedErrorWrapper(new AssertionFailedError(
78                 createFailureMessage(methodCall, matchedCalls,
79                         matchedResultLists, tooManyCallsPosition)));
80     }
81
82     private String JavaDoc createFailureMessage(MethodCall methodCall,
83             List JavaDoc<ExpectedMethodCall> matchedCalls,
84             List JavaDoc<ResultList> matchedResultLists, int tooManyCallsPosition) {
85         String JavaDoc failureMessage = "";
86         if (tooManyCallsPosition == -1) {
87             failureMessage += "\n " + methodCall.toString(getMatcher(methodCall.getMethod())) + ": "
88                     + new Range(2).expectedAndActual(1).replace('2', '0');
89         }
90         for (int i = 0; i < matchedCalls.size(); i++) {
91             ExpectedMethodCall call = matchedCalls.get(i);
92             ResultList list = matchedResultLists.get(i);
93             failureMessage += "\n " + call.toString() + ": ";
94             int count = list.getCallCount()
95                     + (i == tooManyCallsPosition ? 1 : 0);
96             failureMessage += list.getMessage(count);
97         }
98         return failureMessage;
99     }
100
101     public void doVerify() {
102         String JavaDoc failureMessage = "";
103         boolean verifyFailed = false;
104
105         for (int i = position; i < methodCalls.size(); i++) {
106             ExpectedMethodCall call = methodCalls.get(i);
107             ResultList list = resultLists.get(i);
108
109             failureMessage += "\n " + call.toString() + ": "
110                     + list.getMessage();
111
112             if (list.hasValidCallCount()) {
113                 continue;
114             }
115             verifyFailed = true;
116         }
117         if (verifyFailed) {
118             throw new AssertionFailedErrorWrapper(new AssertionFailedError(
119                     failureMessage));
120         }
121     }
122 }
123
Popular Tags