|
14 | 14 |
|
15 | 15 | package org.spockframework.lang; |
16 | 16 |
|
17 | | -import groovy.transform.stc.ClosureParams; |
18 | | -import groovy.transform.stc.SecondParam; |
19 | | -import groovy.transform.stc.ThirdParam; |
20 | | -import org.spockframework.mock.*; |
21 | | -import org.spockframework.mock.runtime.*; |
22 | 17 | import org.spockframework.runtime.*; |
23 | 18 | import org.spockframework.util.*; |
24 | | -import spock.lang.Specification; |
25 | 19 |
|
26 | | -import java.lang.reflect.Type; |
27 | | -import java.util.*; |
28 | | - |
29 | | -import groovy.lang.Closure; |
30 | | -import spock.mock.IMockMakerSettings; |
31 | | - |
32 | | -import static java.util.Collections.emptyMap; |
33 | | -import static java.util.Collections.singletonMap; |
34 | | -import static org.spockframework.util.ObjectUtil.uncheckedCast; |
35 | | - |
36 | | -@SuppressWarnings("UnusedDeclaration") |
37 | 20 | public abstract class SpecInternals { |
38 | | - private static final MockUtil MOCK_UTIL = new MockUtil(); |
39 | 21 | private final ISpecificationContext specificationContext = new SpecificationContext(); |
40 | 22 |
|
41 | 23 | @Beta |
42 | 24 | public ISpecificationContext getSpecificationContext() { |
43 | 25 | return specificationContext; |
44 | 26 | } |
45 | | - |
46 | | - @Beta |
47 | | - public <T> T createMock(@Nullable String name, Type type, MockNature nature, |
48 | | - MockImplementation implementation, Map<String, Object> options, @Nullable Closure<?> closure) { |
49 | | - return createMock(name, null, type, nature, implementation, options, closure); |
50 | | - } |
51 | | - |
52 | | - @Beta |
53 | | - public <T> T createMock(@Nullable String name, T instance, Type type, MockNature nature, |
54 | | - MockImplementation implementation, Map<String, Object> options, @Nullable Closure<?> closure) { |
55 | | - Object mock = CompositeMockFactory.INSTANCE.create( |
56 | | - new MockConfiguration(name, type, instance, nature, implementation, options), (Specification) this); |
57 | | - if (closure != null) { |
58 | | - GroovyRuntimeUtil.invokeClosure(closure, mock); |
59 | | - } |
60 | | - return uncheckedCast(mock); |
61 | | - } |
62 | | - |
63 | | - private void createStaticMock(Type type, MockNature nature, |
64 | | - Map<String, Object> options) { |
65 | | - MockConfiguration configuration = new MockConfiguration(null, type, null, nature, MockImplementation.JAVA, options); |
66 | | - JavaMockFactory.INSTANCE.createStaticMock(configuration, (Specification) this); |
67 | | - } |
68 | | - |
69 | | - <T> T oldImpl(T expression) { |
70 | | - return expression; |
71 | | - } |
72 | | - |
73 | | - <T extends Throwable> T thrownImpl(String inferredName, Class<T> inferredType) { |
74 | | - return inferredType.cast(checkExceptionThrown(inferredType)); |
75 | | - } |
76 | | - |
77 | | - Throwable thrownImpl(String inferredName, Class<? extends Throwable> inferredType, Class<? extends Throwable> specifiedType) { |
78 | | - return checkExceptionThrown(specifiedType == null ? inferredType : specifiedType); |
79 | | - } |
80 | | - |
81 | | - Throwable checkExceptionThrown(Class<? extends Throwable> exceptionType) { |
82 | | - if (exceptionType == null) { |
83 | | - throw new InvalidSpecException("Thrown exception type cannot be inferred automatically. " + |
84 | | - "Please specify a type explicitly (e.g. 'thrown(MyException)')."); |
85 | | - } |
86 | | - |
87 | | - if (!Throwable.class.isAssignableFrom(exceptionType)) |
88 | | - throw new InvalidSpecException( |
89 | | - "Invalid exception condition: '%s' is not a (subclass of) java.lang.Throwable" |
90 | | - ).withArgs(exceptionType.getSimpleName()); |
91 | | - |
92 | | - Throwable actual = specificationContext.getThrownException(); |
93 | | - if (exceptionType.isInstance(actual)) return actual; |
94 | | - |
95 | | - throw new WrongExceptionThrownError(exceptionType, actual); |
96 | | - } |
97 | | - |
98 | | - <T> T MockImpl(String inferredName, Class<T> inferredType) { |
99 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), null, null); |
100 | | - } |
101 | | - |
102 | | - <T> T MockImpl(String inferredName, Class<T> inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure<?> closure) { |
103 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), null, closure); |
104 | | - } |
105 | | - |
106 | | - <T> T MockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options) { |
107 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, null, null); |
108 | | - } |
109 | | - |
110 | | - <T> T MockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Closure<?> closure) { |
111 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, null, closure); |
112 | | - } |
113 | | - |
114 | | - <T> T MockImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType) { |
115 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), specifiedType, null); |
116 | | - } |
117 | | - |
118 | | - <T> T MockImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType, Closure<?> closure) { |
119 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), specifiedType, closure); |
120 | | - } |
121 | | - |
122 | | - <T> T MockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType) { |
123 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, specifiedType, null); |
124 | | - } |
125 | | - |
126 | | - <T> T MockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
127 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, specifiedType, closure); |
128 | | - } |
129 | | - |
130 | | - <T> T StubImpl(String inferredName, Class<T> inferredType) { |
131 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), null, null); |
132 | | - } |
133 | | - |
134 | | - <T> T StubImpl(String inferredName, Class<T> inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure<?> closure) { |
135 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), null, closure); |
136 | | - } |
137 | | - |
138 | | - <T> T StubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options) { |
139 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, null, null); |
140 | | - } |
141 | | - |
142 | | - <T> T StubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Closure<?> closure) { |
143 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, null, closure); |
144 | | - } |
145 | | - |
146 | | - <T> T StubImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType) { |
147 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), specifiedType, null); |
148 | | - } |
149 | | - |
150 | | - <T> T StubImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType, Closure<?> closure) { |
151 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), specifiedType, closure); |
152 | | - } |
153 | | - |
154 | | - <T> T StubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType) { |
155 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, specifiedType, null); |
156 | | - } |
157 | | - |
158 | | - <T> T StubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
159 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, specifiedType, closure); |
160 | | - } |
161 | | - |
162 | | - <T> T SpyImpl(String inferredName, Class<T> inferredType) { |
163 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), null, null); |
164 | | - } |
165 | | - |
166 | | - <T> T SpyImpl(String inferredName, Class<T> inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure<?> closure) { |
167 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), null, closure); |
168 | | - } |
169 | | - |
170 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options) { |
171 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, null, null); |
172 | | - } |
173 | | - |
174 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Closure<?> closure) { |
175 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, null, closure); |
176 | | - } |
177 | | - |
178 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType) { |
179 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), specifiedType, null); |
180 | | - } |
181 | | - |
182 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType, Closure<?> closure) { |
183 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), specifiedType, closure); |
184 | | - } |
185 | | - |
186 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType) { |
187 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, specifiedType, null); |
188 | | - } |
189 | | - |
190 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
191 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, specifiedType, closure); |
192 | | - } |
193 | | - |
194 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, T instance) { |
195 | | - return SpyImpl(inferredName, inferredType, instance, null); |
196 | | - } |
197 | | - |
198 | | - <T> T SpyImpl(String inferredName, Class<?> inferredType, T instance, @ClosureParams(ThirdParam.class) Closure<?> closure) { |
199 | | - if (instance == null) { |
200 | | - throw new SpockException("Spy instance may not be null"); |
201 | | - } |
202 | | - if (MOCK_UTIL.isMock(instance)) { |
203 | | - throw new SpockException("Spy instance may not be another mock object."); |
204 | | - } |
205 | | - return createMockImpl(inferredName, uncheckedCast(instance.getClass()), instance, MockNature.SPY, MockImplementation.JAVA, singletonMap("useObjenesis", true), null, closure); |
206 | | - } |
207 | | - |
208 | | - <T> T GroovyMockImpl(String inferredName, Class<T> inferredType) { |
209 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), null, null); |
210 | | - } |
211 | | - |
212 | | - <T> T GroovyMockImpl(String inferredName, Class<T> inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure<?> closure) { |
213 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), null, closure); |
214 | | - } |
215 | | - |
216 | | - <T> T GroovyMockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options) { |
217 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, null, null); |
218 | | - } |
219 | | - |
220 | | - <T> T GroovyMockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Closure<?> closure) { |
221 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, null, closure); |
222 | | - } |
223 | | - |
224 | | - <T> T GroovyMockImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType) { |
225 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), specifiedType, null); |
226 | | - } |
227 | | - |
228 | | - <T> T GroovyMockImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType, Closure<?> closure) { |
229 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); |
230 | | - } |
231 | | - |
232 | | - <T> T GroovyMockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType) { |
233 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, specifiedType, null); |
234 | | - } |
235 | | - |
236 | | - <T> T GroovyMockImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
237 | | - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, specifiedType, closure); |
238 | | - } |
239 | | - |
240 | | - <T> T GroovyStubImpl(String inferredName, Class<T> inferredType) { |
241 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), null, null); |
242 | | - } |
243 | | - |
244 | | - <T> T GroovyStubImpl(String inferredName, Class<T> inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure<?> closure) { |
245 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), null, closure); |
246 | | - } |
247 | | - |
248 | | - <T> T GroovyStubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options) { |
249 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, null, null); |
250 | | - } |
251 | | - |
252 | | - <T> T GroovyStubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Closure<?> closure) { |
253 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, null, closure); |
254 | | - } |
255 | | - |
256 | | - <T> T GroovyStubImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType) { |
257 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), specifiedType, null); |
258 | | - } |
259 | | - |
260 | | - <T> T GroovyStubImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType, Closure<?> closure) { |
261 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); |
262 | | - } |
263 | | - |
264 | | - <T> T GroovyStubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType) { |
265 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, specifiedType, null); |
266 | | - } |
267 | | - |
268 | | - <T> T GroovyStubImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
269 | | - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, specifiedType, closure); |
270 | | - } |
271 | | - |
272 | | - <T> T GroovySpyImpl(String inferredName, Class<T> inferredType) { |
273 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), null, null); |
274 | | - } |
275 | | - |
276 | | - <T> T GroovySpyImpl(String inferredName, Class<T> inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure<?> closure) { |
277 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), null, closure); |
278 | | - } |
279 | | - |
280 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options) { |
281 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, null, null); |
282 | | - } |
283 | | - |
284 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Closure<?> closure) { |
285 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, null, closure); |
286 | | - } |
287 | | - |
288 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType) { |
289 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), specifiedType, null); |
290 | | - } |
291 | | - |
292 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType, Closure<?> closure) { |
293 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); |
294 | | - } |
295 | | - |
296 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType) { |
297 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, specifiedType, null); |
298 | | - } |
299 | | - |
300 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
301 | | - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, specifiedType, closure); |
302 | | - } |
303 | | - |
304 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, T instance) { |
305 | | - return GroovySpyImpl(inferredName, inferredType, instance, null); |
306 | | - } |
307 | | - |
308 | | - <T> T GroovySpyImpl(String inferredName, Class<?> inferredType, T instance, Closure<?> closure) { |
309 | | - if (instance == null) { |
310 | | - throw new SpockException("GroovySpy instance may not be null"); |
311 | | - } |
312 | | - if (MOCK_UTIL.isMock(instance)) { |
313 | | - throw new SpockException("GroovySpy instance may not be another mock object."); |
314 | | - } |
315 | | - return createMockImpl(inferredName, instance.getClass(), instance, MockNature.SPY, MockImplementation.GROOVY, singletonMap("useObjenesis", true), null, closure); |
316 | | - } |
317 | | - |
318 | | - private <T> T createMockImpl(String inferredName, Class<?> inferredType, MockNature nature, |
319 | | - MockImplementation implementation, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
320 | | - return createMockImpl(inferredName, inferredType, null, nature, implementation, options, specifiedType, closure); |
321 | | - } |
322 | | - |
323 | | - private <T> T createMockImpl(String inferredName, Class<?> inferredType, T instance, MockNature nature, |
324 | | - MockImplementation implementation, Map<String, Object> options, Class<?> specifiedType, Closure<?> closure) { |
325 | | - Type effectiveType = specifiedType != null ? specifiedType : options.containsKey("type") ? (Type) options.get("type") : inferredType; |
326 | | - if (effectiveType == null) { |
327 | | - throw new InvalidSpecException(nature + " object type cannot be inferred automatically. " + |
328 | | - "Please specify a type explicitly (e.g. '" + nature + "(Person)')."); |
329 | | - } |
330 | | - return createMock(inferredName, instance, effectiveType, nature, implementation, options, closure); |
331 | | - } |
332 | | - |
333 | | - void SpyStaticImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType) { |
334 | | - createStaticMockImpl(MockNature.SPY, specifiedType, null); |
335 | | - } |
336 | | - |
337 | | - void SpyStaticImpl(String inferredName, Class<?> inferredType, Class<?> specifiedType, IMockMakerSettings mockMakerSettings) { |
338 | | - createStaticMockImpl(MockNature.SPY, specifiedType, mockMakerSettings); |
339 | | - } |
340 | | - |
341 | | - private void createStaticMockImpl(MockNature nature, Class<?> specifiedType, @Nullable IMockMakerSettings mockMakerSettings) { |
342 | | - if (specifiedType == null) { |
343 | | - throw new InvalidSpecException("The type must not be null."); |
344 | | - } |
345 | | - Map<String, Object> options = emptyMap(); |
346 | | - if (mockMakerSettings != null) { |
347 | | - options = Collections.singletonMap("mockMaker", mockMakerSettings); |
348 | | - } |
349 | | - createStaticMock(specifiedType, nature, options); |
350 | | - } |
351 | 27 | } |
0 commit comments