|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package org.easymock.internal; |
|
6 |
| |
|
7 |
| import java.lang.reflect.Method; |
|
8 |
| import java.util.ArrayList; |
|
9 |
| import java.util.List; |
|
10 |
| |
|
11 |
| import org.easymock.ArgumentsMatcher; |
|
12 |
| import org.easymock.MockControl; |
|
13 |
| |
|
14 |
| public class MocksBehavior implements IMocksBehavior { |
|
15 |
| |
|
16 |
| private final List<UnorderedBehavior> behaviorLists = new ArrayList<UnorderedBehavior>(); |
|
17 |
| |
|
18 |
| private List<ExpectedInvocationAndResult> stubResults = new ArrayList<ExpectedInvocationAndResult>(); |
|
19 |
| |
|
20 |
| private final boolean nice; |
|
21 |
| |
|
22 |
| private boolean checkOrder; |
|
23 |
| |
|
24 |
| private int position = 0; |
|
25 |
| |
|
26 |
481
| public MocksBehavior(boolean nice) {
|
|
27 |
481
| this.nice = nice;
|
|
28 |
| } |
|
29 |
| |
|
30 |
56
| public final void addStub(ExpectedInvocation expected, Result result) {
|
|
31 |
56
| stubResults.add(new ExpectedInvocationAndResult(expected, result));
|
|
32 |
| } |
|
33 |
| |
|
34 |
385
| public void addExpected(ExpectedInvocation expected, Result result,
|
|
35 |
| Range count) { |
|
36 |
385
| if (legacyMatcherProvider != null) {
|
|
37 |
14
| expected = expected.withMatcher(legacyMatcherProvider
|
|
38 |
| .getMatcher(expected.getMethod())); |
|
39 |
| } |
|
40 |
385
| addBehaviorListIfNecessary(expected);
|
|
41 |
385
| lastBehaviorList().addExpected(expected, result, count);
|
|
42 |
| } |
|
43 |
| |
|
44 |
148
| private final Result getStubResult(Invocation actual) {
|
|
45 |
148
| for (ExpectedInvocationAndResult each : stubResults) {
|
|
46 |
137
| if (each.getExpectedInvocation().matches(actual)) {
|
|
47 |
97
| return each.getResult();
|
|
48 |
| } |
|
49 |
| } |
|
50 |
51
| return null;
|
|
51 |
| } |
|
52 |
| |
|
53 |
385
| private void addBehaviorListIfNecessary(ExpectedInvocation expected) {
|
|
54 |
385
| if (behaviorLists.isEmpty()
|
|
55 |
| || !lastBehaviorList().allowsExpectedInvocation(expected, |
|
56 |
| checkOrder)) { |
|
57 |
228
| behaviorLists.add(new UnorderedBehavior(checkOrder));
|
|
58 |
| } |
|
59 |
| } |
|
60 |
| |
|
61 |
597
| private UnorderedBehavior lastBehaviorList() {
|
|
62 |
597
| return behaviorLists.get(behaviorLists.size() - 1);
|
|
63 |
| } |
|
64 |
| |
|
65 |
548
| public final Result addActual(Invocation actual) {
|
|
66 |
548
| int tempPosition = position;
|
|
67 |
548
| String errorMessage = "";
|
|
68 |
548
| while (position < behaviorLists.size()) {
|
|
69 |
554
| Result result = behaviorLists.get(position).addActual(actual);
|
|
70 |
554
| if (result != null) {
|
|
71 |
400
| return result;
|
|
72 |
| } |
|
73 |
154
| errorMessage += behaviorLists.get(position).toString(actual);
|
|
74 |
154
| if (!behaviorLists.get(position).verify()) {
|
|
75 |
52
| break;
|
|
76 |
| } |
|
77 |
102
| position++;
|
|
78 |
| } |
|
79 |
148
| Result stubOrNice = getStubResult(actual);
|
|
80 |
148
| if (stubOrNice == null && nice) {
|
|
81 |
13
| stubOrNice = Result.createReturnResult(RecordState
|
|
82 |
| .emptyReturnValueFor(actual.getMethod().getReturnType())); |
|
83 |
| } |
|
84 |
148
| if (stubOrNice != null) {
|
|
85 |
110
| position = tempPosition;
|
|
86 |
110
| return stubOrNice;
|
|
87 |
| } |
|
88 |
38
| throw new AssertionErrorWrapper(new AssertionError(
|
|
89 |
| "\n Unexpected method call " |
|
90 |
| + actual.toString(MockControl.EQUALS_MATCHER) + ":" |
|
91 |
| + errorMessage.toString())); |
|
92 |
| } |
|
93 |
| |
|
94 |
165
| public void verify() {
|
|
95 |
165
| boolean verified = true;
|
|
96 |
165
| StringBuffer errorMessage = new StringBuffer();
|
|
97 |
| |
|
98 |
165
| for (UnorderedBehavior behaviorList : behaviorLists.subList(position,
|
|
99 |
| behaviorLists.size())) { |
|
100 |
145
| errorMessage.append(behaviorList.toString());
|
|
101 |
144
| if (!behaviorList.verify()) {
|
|
102 |
18
| verified = false;
|
|
103 |
| } |
|
104 |
| } |
|
105 |
164
| if (verified) {
|
|
106 |
148
| return;
|
|
107 |
| } |
|
108 |
| |
|
109 |
16
| throw new AssertionErrorWrapper(new AssertionError(
|
|
110 |
| "\n Expectation failure on verify:" + errorMessage.toString())); |
|
111 |
| } |
|
112 |
| |
|
113 |
462
| public void checkOrder(boolean value) {
|
|
114 |
462
| this.checkOrder = value;
|
|
115 |
| } |
|
116 |
| |
|
117 |
| private LegacyMatcherProvider legacyMatcherProvider; |
|
118 |
| |
|
119 |
19
| public LegacyMatcherProvider getLegacyMatcherProvider() {
|
|
120 |
19
| if (legacyMatcherProvider == null) {
|
|
121 |
13
| legacyMatcherProvider = new LegacyMatcherProvider();
|
|
122 |
| } |
|
123 |
19
| return legacyMatcherProvider;
|
|
124 |
| } |
|
125 |
| |
|
126 |
6
| public void setDefaultMatcher(ArgumentsMatcher matcher) {
|
|
127 |
6
| getLegacyMatcherProvider().setDefaultMatcher(matcher);
|
|
128 |
| } |
|
129 |
| |
|
130 |
13
| public void setMatcher(Method method, ArgumentsMatcher matcher) {
|
|
131 |
13
| getLegacyMatcherProvider().setMatcher(method, matcher);
|
|
132 |
| } |
|
133 |
| } |