1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package org.easymock.internal; |
6 |
| |
7 |
| import org.easymock.ArgumentsMatcher; |
8 |
| import org.easymock.IAnswer; |
9 |
| import org.easymock.IExpectationSetters; |
10 |
| import org.easymock.IMocksControl; |
11 |
| |
12 |
| public class MocksControl implements IMocksControl { |
13 |
| |
14 |
| private IMocksControlState state; |
15 |
| |
16 |
| private IMocksBehavior behavior; |
17 |
| |
18 |
| public enum MockType { |
19 |
| NICE, DEFAULT, STRICT |
20 |
| } |
21 |
| |
22 |
| private final MockType type; |
23 |
| |
24 |
451
| public MocksControl(MockType type) {
|
25 |
451
| this.type = type;
|
26 |
451
| reset();
|
27 |
| } |
28 |
| |
29 |
1968
| public IMocksControlState getState() {
|
30 |
1968
| return state;
|
31 |
| } |
32 |
| |
33 |
367
| public <T> T createMock(Class<T> toMock) {
|
34 |
367
| try {
|
35 |
367
| state.assertRecordState();
|
36 |
366
| IProxyFactory<T> proxyFactory = createProxyFactory(toMock);
|
37 |
366
| return proxyFactory.createProxy(toMock, new ObjectMethodsFilter(
|
38 |
| new MockInvocationHandler(this))); |
39 |
| } catch (RuntimeExceptionWrapper e) { |
40 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
41 |
| } |
42 |
| } |
43 |
| |
44 |
366
| protected <T> IProxyFactory<T> createProxyFactory(Class<T> toMock) {
|
45 |
366
| return new JavaProxyFactory<T>();
|
46 |
| } |
47 |
| |
48 |
460
| public final void reset() {
|
49 |
460
| behavior = new MocksBehavior(type == MockType.NICE);
|
50 |
460
| behavior.checkOrder(type == MockType.STRICT);
|
51 |
460
| state = new RecordState(behavior);
|
52 |
460
| LastControl.reportLastControl(null);
|
53 |
| } |
54 |
| |
55 |
376
| public void replay() {
|
56 |
376
| try {
|
57 |
376
| state.replay();
|
58 |
372
| state = new ReplayState(behavior);
|
59 |
372
| LastControl.reportLastControl(null);
|
60 |
| } catch (RuntimeExceptionWrapper e) { |
61 |
2
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
62 |
| } |
63 |
| } |
64 |
| |
65 |
166
| public void verify() {
|
66 |
166
| try {
|
67 |
166
| state.verify();
|
68 |
| } catch (RuntimeExceptionWrapper e) { |
69 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
70 |
| } catch (AssertionErrorWrapper e) { |
71 |
16
| throw (AssertionError) e.getAssertionError().fillInStackTrace();
|
72 |
| } |
73 |
| } |
74 |
| |
75 |
3
| public void checkOrder(boolean value) {
|
76 |
3
| try {
|
77 |
3
| state.checkOrder(value);
|
78 |
| } catch (RuntimeExceptionWrapper e) { |
79 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
80 |
| } |
81 |
| } |
82 |
| |
83 |
| |
84 |
| |
85 |
235
| public IExpectationSetters andReturn(Object value) {
|
86 |
235
| try {
|
87 |
235
| state.andReturn(value);
|
88 |
216
| return this;
|
89 |
| } catch (RuntimeExceptionWrapper e) { |
90 |
19
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
91 |
| } |
92 |
| } |
93 |
| |
94 |
45
| public IExpectationSetters andThrow(Throwable throwable) {
|
95 |
45
| try {
|
96 |
45
| state.andThrow(throwable);
|
97 |
42
| return this;
|
98 |
| } catch (RuntimeExceptionWrapper e) { |
99 |
3
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
100 |
| } |
101 |
| } |
102 |
| |
103 |
9
| public IExpectationSetters andAnswer(IAnswer answer) {
|
104 |
9
| try {
|
105 |
9
| state.andAnswer(answer);
|
106 |
8
| return this;
|
107 |
| } catch (RuntimeExceptionWrapper e) { |
108 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
109 |
| } |
110 |
| } |
111 |
| |
112 |
24
| public void andStubReturn(Object value) {
|
113 |
24
| try {
|
114 |
24
| state.andStubReturn(value);
|
115 |
| } catch (RuntimeExceptionWrapper e) { |
116 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
117 |
| } |
118 |
| } |
119 |
| |
120 |
7
| public void andStubThrow(Throwable throwable) {
|
121 |
7
| try {
|
122 |
7
| state.andStubThrow(throwable);
|
123 |
| } catch (RuntimeExceptionWrapper e) { |
124 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
125 |
| } |
126 |
| } |
127 |
| |
128 |
3
| public void andStubAnswer(IAnswer answer) {
|
129 |
3
| try {
|
130 |
3
| state.andStubAnswer(answer);
|
131 |
| } catch (RuntimeExceptionWrapper e) { |
132 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
133 |
| } |
134 |
| } |
135 |
| |
136 |
3
| public void asStub() {
|
137 |
3
| try {
|
138 |
3
| state.asStub();
|
139 |
| } catch (RuntimeExceptionWrapper e) { |
140 |
2
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
141 |
| } |
142 |
| } |
143 |
| |
144 |
31
| public IExpectationSetters times(int times) {
|
145 |
31
| try {
|
146 |
31
| state.times(new Range(times));
|
147 |
29
| return this;
|
148 |
| } catch (RuntimeExceptionWrapper e) { |
149 |
2
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
150 |
| } |
151 |
| } |
152 |
| |
153 |
17
| public IExpectationSetters times(int min, int max) {
|
154 |
17
| try {
|
155 |
17
| state.times(new Range(min, max));
|
156 |
14
| return this;
|
157 |
| } catch (RuntimeExceptionWrapper e) { |
158 |
3
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
159 |
| } |
160 |
| } |
161 |
| |
162 |
104
| public IExpectationSetters once() {
|
163 |
104
| try {
|
164 |
104
| state.times(ONCE);
|
165 |
103
| return this;
|
166 |
| } catch (RuntimeExceptionWrapper e) { |
167 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
168 |
| } |
169 |
| } |
170 |
| |
171 |
31
| public IExpectationSetters atLeastOnce() {
|
172 |
31
| try {
|
173 |
31
| state.times(AT_LEAST_ONCE);
|
174 |
30
| return this;
|
175 |
| } catch (RuntimeExceptionWrapper e) { |
176 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
177 |
| } |
178 |
| } |
179 |
| |
180 |
6
| public IExpectationSetters anyTimes() {
|
181 |
6
| try {
|
182 |
6
| state.times(ZERO_OR_MORE);
|
183 |
5
| return this;
|
184 |
| } catch (RuntimeExceptionWrapper e) { |
185 |
1
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
186 |
| } |
187 |
| } |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| public static final Range ONCE = new Range(1); |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| public static final Range AT_LEAST_ONCE = new Range(1, Integer.MAX_VALUE); |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| public static final Range ZERO_OR_MORE = new Range(0, Integer.MAX_VALUE); |
203 |
| |
204 |
8
| public void setLegacyDefaultMatcher(ArgumentsMatcher matcher) {
|
205 |
8
| try {
|
206 |
8
| state.setDefaultMatcher(matcher);
|
207 |
| } catch (RuntimeExceptionWrapper e) { |
208 |
3
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
209 |
| } |
210 |
| } |
211 |
| |
212 |
15
| public void setLegacyMatcher(ArgumentsMatcher matcher) {
|
213 |
15
| try {
|
214 |
15
| state.setMatcher(null, matcher);
|
215 |
| } catch (RuntimeExceptionWrapper e) { |
216 |
4
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
217 |
| } |
218 |
| } |
219 |
| |
220 |
36
| public void setLegacyDefaultReturnValue(Object value) {
|
221 |
36
| try {
|
222 |
36
| state.setDefaultReturnValue(value);
|
223 |
| } catch (RuntimeExceptionWrapper e) { |
224 |
16
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
225 |
| } |
226 |
| } |
227 |
| |
228 |
1
| public void setLegacyDefaultVoidCallable() {
|
229 |
1
| state.setDefaultVoidCallable();
|
230 |
| } |
231 |
| |
232 |
9
| public void setLegacyDefaultThrowable(Throwable throwable) {
|
233 |
9
| try {
|
234 |
9
| state.setDefaultThrowable(throwable);
|
235 |
| } catch (RuntimeExceptionWrapper e) { |
236 |
6
| throw (RuntimeException) e.getRuntimeException().fillInStackTrace();
|
237 |
| } |
238 |
| } |
239 |
| } |