Clover Coverage Report - EasyMock 2.4
Coverage timestamp: mer. juil. 2 2008 02:17:38 CEST
51   162   32   3,4
26   127   0,63   15
15     2,13  
1    
 
 
  MocksBehavior       Line # 12 51 32 100% 1.0
 
  (223)
 
1    /*
2    * Copyright (c) 2001-2008 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.io.Serializable;
8    import java.lang.reflect.Method;
9    import java.util.ArrayList;
10    import java.util.List;
11   
 
12    public class MocksBehavior implements IMocksBehavior, Serializable {
13   
14    private static final long serialVersionUID = 3265727009370529027L;
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 boolean isThreadSafe;
25   
26    private int position = 0;
27   
28    private transient volatile Thread lastThread;
29   
 
30  520 toggle public MocksBehavior(boolean nice) {
31  520 this.nice = nice;
32    }
33   
 
34  57 toggle public final void addStub(ExpectedInvocation expected, Result result) {
35  57 stubResults.add(new ExpectedInvocationAndResult(expected, result));
36    }
37   
 
38  439 toggle public void addExpected(ExpectedInvocation expected, Result result,
39    Range count) {
40  439 if (legacyMatcherProvider != null) {
41  14 expected = expected.withMatcher(legacyMatcherProvider
42    .getMatcher(expected.getMethod()));
43    }
44  439 addBehaviorListIfNecessary(expected);
45  439 lastBehaviorList().addExpected(expected, result, count);
46    }
47   
 
48  163 toggle private final Result getStubResult(Invocation actual) {
49  163 for (ExpectedInvocationAndResult each : stubResults) {
50  138 if (each.getExpectedInvocation().matches(actual)) {
51  98 return each.getResult();
52    }
53    }
54  65 return null;
55    }
56   
 
57  439 toggle private void addBehaviorListIfNecessary(ExpectedInvocation expected) {
58  439 if (behaviorLists.isEmpty()
59    || !lastBehaviorList().allowsExpectedInvocation(expected,
60    checkOrder)) {
61  261 behaviorLists.add(new UnorderedBehavior(checkOrder));
62    }
63    }
64   
 
65  680 toggle private UnorderedBehavior lastBehaviorList() {
66  680 return behaviorLists.get(behaviorLists.size() - 1);
67    }
68   
 
69  620 toggle @SuppressWarnings("deprecation")
70    public final Result addActual(Invocation actual) {
71  620 int tempPosition = position;
72  620 String errorMessage = "";
73  742 while (position < behaviorLists.size()) {
74  643 Result result = behaviorLists.get(position).addActual(actual);
75  643 if (result != null) {
76  457 return result;
77    }
78  186 errorMessage += behaviorLists.get(position).toString(actual);
79  186 if (!behaviorLists.get(position).verify()) {
80  64 break;
81    }
82  122 position++;
83    }
84  163 Result stubOrNice = getStubResult(actual);
85  163 if (stubOrNice == null && nice) {
86  15 stubOrNice = Result.createReturnResult(RecordState
87    .emptyReturnValueFor(actual.getMethod().getReturnType()));
88    }
89   
90    // Do not move the cursor in case of stub, nice or error
91  163 position = tempPosition;
92   
93  163 if (stubOrNice != null) {
94  113 return stubOrNice;
95    }
96  50 throw new AssertionErrorWrapper(
97    new AssertionError(
98    "\n Unexpected method call "
99    + actual
100    .toString(org.easymock.MockControl.EQUALS_MATCHER)
101    + ":" + errorMessage.toString()));
102    }
103   
 
104  185 toggle public void verify() {
105  185 boolean verified = true;
106  185 StringBuffer errorMessage = new StringBuffer();
107   
108  185 for (UnorderedBehavior behaviorList : behaviorLists.subList(position,
109    behaviorLists.size())) {
110  165 errorMessage.append(behaviorList.toString());
111  164 if (!behaviorList.verify()) {
112  21 verified = false;
113    }
114    }
115  184 if (verified) {
116  165 return;
117    }
118   
119  19 throw new AssertionErrorWrapper(new AssertionError(
120    "\n Expectation failure on verify:" + errorMessage.toString()));
121    }
122   
 
123  501 toggle public void checkOrder(boolean value) {
124  501 this.checkOrder = value;
125    }
126   
 
127  499 toggle public void makeThreadSafe(boolean isThreadSafe) {
128  499 this.isThreadSafe = isThreadSafe;
129    }
130   
 
131  629 toggle public boolean isThreadSafe() {
132  629 return this.isThreadSafe;
133    }
134   
 
135  619 toggle public void checkCurrentThreadSameAsLastThread() {
136  619 if (lastThread == null) {
137  204 lastThread = Thread.currentThread();
138  415 } else if(lastThread != Thread.currentThread()) {
139  9 throw new AssertionErrorWrapper(new AssertionError(
140    "\n Un-thread-safe mock called from multiple threads"));
141    }
142    }
143   
144    private LegacyMatcherProvider legacyMatcherProvider;
145   
 
146  19 toggle public LegacyMatcherProvider getLegacyMatcherProvider() {
147  19 if (legacyMatcherProvider == null) {
148  13 legacyMatcherProvider = new LegacyMatcherProvider();
149    }
150  19 return legacyMatcherProvider;
151    }
152   
 
153  6 toggle @SuppressWarnings("deprecation")
154    public void setDefaultMatcher(org.easymock.ArgumentsMatcher matcher) {
155  6 getLegacyMatcherProvider().setDefaultMatcher(matcher);
156    }
157   
 
158  13 toggle @SuppressWarnings("deprecation")
159    public void setMatcher(Method method, org.easymock.ArgumentsMatcher matcher) {
160  13 getLegacyMatcherProvider().setMatcher(method, matcher);
161    }
162    }