Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../../img/srcFileCovDistChart10.png 0% of files have more coverage
70   204   31   5,38
32   139   0,44   13
13     2,38  
1    
 
  MocksBehavior       Line # 28 70 0% 31 0 100% 1.0
 
  (720)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.internal;
18   
19    import java.io.Serializable;
20    import java.util.ArrayList;
21    import java.util.List;
22   
23    import org.easymock.EasyMock;
24   
25    /**
26    * @author OFFIS, Tammo Freese
27    */
 
28    public class MocksBehavior implements IMocksBehavior, Serializable {
29   
30    private static final long serialVersionUID = 3265727009370529027L;
31   
32    private final List<UnorderedBehavior> behaviorLists = new ArrayList<UnorderedBehavior>();
33   
34    private final List<ExpectedInvocationAndResult> stubResults = new ArrayList<ExpectedInvocationAndResult>();
35   
36    private final boolean nice;
37   
38    private boolean checkOrder;
39   
40    private boolean isThreadSafe;
41   
42    private boolean shouldBeUsedInOneThread;
43   
44    private int position = 0;
45   
46    private transient volatile Thread lastThread;
47   
 
48  1168 toggle public MocksBehavior(final boolean nice) {
49  1168 this.nice = nice;
50  1168 this.isThreadSafe = !Boolean.valueOf(EasyMockProperties.getInstance().getProperty(
51    EasyMock.NOT_THREAD_SAFE_BY_DEFAULT));
52  1168 this.shouldBeUsedInOneThread = Boolean.valueOf(EasyMockProperties.getInstance().getProperty(
53    EasyMock.ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT));
54    }
55   
 
56  132 toggle public final void addStub(final ExpectedInvocation expected, final Result result) {
57  132 stubResults.add(new ExpectedInvocationAndResult(expected, result));
58    }
59   
 
60  1266 toggle public void addExpected(final ExpectedInvocation expected, final Result result, final Range count) {
61  1266 addBehaviorListIfNecessary(expected);
62  1266 lastBehaviorList().addExpected(expected, result, count);
63    }
64   
 
65  492 toggle private Result getStubResult(final Invocation actual) {
66  492 for (final ExpectedInvocationAndResult each : stubResults) {
67  302 if (each.getExpectedInvocation().matches(actual)) {
68  218 return each.getResult();
69    }
70    }
71  274 return null;
72    }
73   
 
74  1266 toggle private void addBehaviorListIfNecessary(final ExpectedInvocation expected) {
75  1266 if (behaviorLists.isEmpty() || !lastBehaviorList().allowsExpectedInvocation(expected, checkOrder)) {
76  790 behaviorLists.add(new UnorderedBehavior(checkOrder));
77    }
78    }
79   
 
80  1916 toggle private UnorderedBehavior lastBehaviorList() {
81  1916 return behaviorLists.get(behaviorLists.size() - 1);
82    }
83   
 
84  1754 toggle public final Result addActual(final Invocation actual) {
85  1754 final int initialPosition = position;
86   
87  2070 while (position < behaviorLists.size()) {
88  1762 final Result result = behaviorLists.get(position).addActual(actual);
89  1762 if (result != null) {
90  1262 return result;
91    }
92  500 if (!behaviorLists.get(position).verify()) {
93  184 break;
94    }
95  316 position++;
96    }
97  492 Result stubOrNice = getStubResult(actual);
98  492 if (stubOrNice == null && nice) {
99  80 stubOrNice = Result.createReturnResult(RecordState.emptyReturnValueFor(actual.getMethod()
100    .getReturnType()));
101    }
102   
103  492 int endPosition = position;
104   
105    // Do not move the cursor in case of stub, nice or error
106  492 position = initialPosition;
107   
108  492 if (stubOrNice != null) {
109  298 actual.validateCaptures();
110  298 actual.clearCaptures();
111  298 return stubOrNice;
112    }
113   
114    // Case where the loop was exited at the end of the behaviorLists
115  194 if (endPosition == behaviorLists.size()) {
116  86 endPosition--;
117    }
118   
119    // Loop all around the behaviors left to generate the message
120  194 final StringBuilder errorMessage = new StringBuilder(70 * (endPosition - initialPosition + 1)); // rough approximation of the length
121  194 errorMessage.append("\n Unexpected method call ").append(actual.toString());
122   
123  192 final List<ErrorMessage> messages = new ArrayList<ErrorMessage>();
124   
125  192 int matches = 0;
126   
127    // First find how many match we have
128  390 for (int i = initialPosition; i <= endPosition; i++) {
129  198 final List<ErrorMessage> thisListMessages = behaviorLists.get(i).getMessages(actual);
130  198 messages.addAll(thisListMessages);
131  198 for (final ErrorMessage m : thisListMessages) {
132  182 if (m.isMatching()) {
133  42 matches++;
134    }
135    }
136    }
137   
138  192 if (matches > 1) {
139  2 errorMessage.append(". Possible matches are marked with (+1):");
140    } else {
141  190 errorMessage.append(":");
142    }
143   
144  192 for (final ErrorMessage m : messages) {
145  182 m.appendTo(errorMessage, matches);
146    }
147   
148    // And finally throw the error
149  192 throw new AssertionErrorWrapper(new AssertionError(errorMessage));
150    }
151   
 
152  576 toggle public void verify() {
153  576 boolean verified = true;
154   
155  576 for (final UnorderedBehavior behaviorList : behaviorLists.subList(position, behaviorLists.size())) {
156  496 if (!behaviorList.verify()) {
157  44 verified = false;
158    }
159    }
160  576 if (verified) {
161  536 return;
162    }
163   
164  40 final StringBuilder errorMessage = new StringBuilder(70 * (behaviorLists.size() - position + 1));
165   
166  40 errorMessage.append("\n Expectation failure on verify:");
167  40 for (final UnorderedBehavior behaviorList : behaviorLists.subList(position, behaviorLists.size())) {
168  52 for (final ErrorMessage m : behaviorList.getMessages(null)) {
169  52 m.appendTo(errorMessage, 0);
170    }
171    }
172   
173  38 throw new AssertionErrorWrapper(new AssertionError(errorMessage.toString()));
174    }
175   
 
176  1130 toggle public void checkOrder(final boolean value) {
177  1130 this.checkOrder = value;
178    }
179   
 
180  4 toggle public void makeThreadSafe(final boolean isThreadSafe) {
181  4 this.isThreadSafe = isThreadSafe;
182    }
183   
 
184  4 toggle public void shouldBeUsedInOneThread(final boolean shouldBeUsedInOneThread) {
185  4 this.shouldBeUsedInOneThread = shouldBeUsedInOneThread;
186    }
187   
 
188  1756 toggle public boolean isThreadSafe() {
189  1756 return this.isThreadSafe;
190    }
191   
 
192  1776 toggle public void checkThreadSafety() {
193  1776 if (!shouldBeUsedInOneThread) {
194  1748 return;
195    }
196  28 if (lastThread == null) {
197  6 lastThread = Thread.currentThread();
198  22 } else if (lastThread != Thread.currentThread()) {
199  20 throw new AssertionErrorWrapper(new AssertionError(
200    "\n Mock isn't supposed to be called from multiple threads. Last: " + lastThread
201    + " Current: " + Thread.currentThread()));
202    }
203    }
204    }