EasyMock 2.3

Clover coverage report - EasyMock 2.3
Coverage timestamp: mar. juil. 10 2007 02:46:04 CEST
FRAMES NO FRAMES

file stats: | LOC: | 133 |  | Methods: | 12
| NCLOC: | 106 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

MocksBehavior.java

100%

100%

100%

100%

1 |  | /*
2 |  | * Copyright (c) 2001-2007 OFFIS, Tammo Freese.
3 |  | * This program is made available under the terms of the MIT License.
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 | 491 | public MocksBehavior(boolean nice) {
27 | 491 | 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 | 401 | public void addExpected(ExpectedInvocation expected, Result result,
35 |  | Range count) {
36 | 401 | if (legacyMatcherProvider != null) {
37 | 14 | expected = expected.withMatcher(legacyMatcherProvider
38 |  | .getMatcher(expected.getMethod()));
39 |  | }
40 | 401 | addBehaviorListIfNecessary(expected);
41 | 401 | lastBehaviorList().addExpected(expected, result, count);
42 |  | }
43 |  |
44 | 158 | private final Result getStubResult(Invocation actual) {
45 | 158 | for (ExpectedInvocationAndResult each : stubResults) {
46 | 137 | if (each.getExpectedInvocation().matches(actual)) {
47 | 97 | return each.getResult();
48 |  | }
49 |  | }
50 | 61 | return null;
51 |  | }
52 |  |
53 | 401 | private void addBehaviorListIfNecessary(ExpectedInvocation expected) {
54 | 401 | if (behaviorLists.isEmpty()
55 |  | || !lastBehaviorList().allowsExpectedInvocation(expected,
56 |  | checkOrder)) {
57 | 240 | behaviorLists.add(new UnorderedBehavior(checkOrder));
58 |  | }
59 |  | }
60 |  |
61 | 623 | private UnorderedBehavior lastBehaviorList() {
62 | 623 | return behaviorLists.get(behaviorLists.size() - 1);
63 |  | }
64 |  |
65 | 570 | public final Result addActual(Invocation actual) {
66 | 570 | int tempPosition = position;
67 | 570 | String errorMessage = "";
68 | 570 | while (position < behaviorLists.size()) {
69 | 582 | Result result = behaviorLists.get(position).addActual(actual);
70 | 582 | if (result != null) {
71 | 412 | return result;
72 |  | }
73 | 170 | errorMessage += behaviorLists.get(position).toString(actual);
74 | 170 | if (!behaviorLists.get(position).verify()) {
75 | 62 | break;
76 |  | }
77 | 108 | position++;
78 |  | }
79 | 158 | Result stubOrNice = getStubResult(actual);
80 | 158 | if (stubOrNice == null && nice) {
81 | 13 | stubOrNice = Result.createReturnResult(RecordState
82 |  | .emptyReturnValueFor(actual.getMethod().getReturnType()));
83 |  | }
84 | 158 | if (stubOrNice != null) {
85 | 110 | position = tempPosition;
86 | 110 | return stubOrNice;
87 |  | }
88 | 48 | throw new AssertionErrorWrapper(new AssertionError(
89 |  | "\n Unexpected method call "
90 |  | + actual.toString(MockControl.EQUALS_MATCHER) + ":"
91 |  | + errorMessage.toString()));
92 |  | }
93 |  |
94 | 171 | public void verify() {
95 | 171 | boolean verified = true;
96 | 171 | StringBuffer errorMessage = new StringBuffer();
97 |  |
98 | 171 | for (UnorderedBehavior behaviorList : behaviorLists.subList(position,
99 |  | behaviorLists.size())) {
100 | 151 | errorMessage.append(behaviorList.toString());
101 | 150 | if (!behaviorList.verify()) {
102 | 22 | verified = false;
103 |  | }
104 |  | }
105 | 170 | if (verified) {
106 | 150 | return;
107 |  | }
108 |  |
109 | 20 | throw new AssertionErrorWrapper(new AssertionError(
110 |  | "\n Expectation failure on verify:" + errorMessage.toString()));
111 |  | }
112 |  |
113 | 473 | public void checkOrder(boolean value) {
114 | 473 | 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 |  | }