|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MockBuilder | Line # 37 | 68 | 0% | 35 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(72) | |||
Result | |||
0.3
|
org.easymock.tests2.MockBuilderTest.testAddMockedMethod
![]() |
1 PASS | |
0.3
|
org.easymock.tests2.MockBuilderTest.testAddMockedMethod
![]() |
1 PASS | |
0.29090908
|
org.easymock.tests2.SerializationTest.testClass
![]() |
1 PASS | |
0.29090908
|
org.easymock.tests2.EasyMockClassExtensionTest.testCreateMockBuilder
![]() |
1 PASS | |
0.29090908
|
org.easymock.tests2.SerializationTest.testClass
![]() |
1 PASS | |
0.29090908
|
org.easymock.tests2.EasyMockClassExtensionTest.testCreateMockBuilder
![]() |
1 PASS | |
0.25454545
|
org.easymock.tests2.EasyMockSupportClassTest.testAllMockBuilderFlavors
![]() |
1 PASS | |
0.25454545
|
org.easymock.tests2.EasyMockSupportClassTest.testAllMockBuilderFlavors
![]() |
1 PASS | |
0.24545455
|
org.easymock.tests2.MockBuilderTest.testWithConstructorParams
![]() |
1 PASS | |
0.24545455
|
org.easymock.tests2.MockBuilderTest.testWithConstructorParams
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.PartialMockingTest.testPartialMock_PublicConstructor
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.PartialMockingTest.testPartialMock_ProtectedConstructor
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.MockBuilderTest.testWithConstructor
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.PartialMockingTest.testPartialMock_ProtectedConstructor
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.PartialMockingTest.testPartialMock_ExceptionInConstructor
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.PartialMockingTest.testPartialMock_PublicConstructor
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.MockBuilderTest.testWithConstructor
![]() |
1 PASS | |
0.23636363
|
org.easymock.tests2.PartialMockingTest.testPartialMock_ExceptionInConstructor
![]() |
1 PASS | |
0.21818182
|
org.easymock.tests2.EasyMockClassExtensionTest.testNamedMock
![]() |
1 PASS | |
0.21818182
|
org.easymock.tests2.EasyMockClassExtensionTest.testNamedMock
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.EasyMockClassExtensionTest.testMock_Partial
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.MockBuilderTest.testWithEmptyConstructor
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.MockBuilderTest.testWithConstructorWithArgs
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.EasyMockClassExtensionTest.testStrictMock_Partial
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.EasyMockClassExtensionTest.testMock_Partial
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.MockBuilderTest.testWithConstructorWithArgs
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.EasyMockClassExtensionTest.testNiceMock_Partial
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.EasyMockClassExtensionTest.testNiceMock_Partial
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.EasyMockClassExtensionTest.testStrictMock_Partial
![]() |
1 PASS | |
0.19090909
|
org.easymock.tests2.MockBuilderTest.testWithEmptyConstructor
![]() |
1 PASS | |
0.18181819
|
org.easymock.tests2.MockBuilderTest.testCreateMock_ConstructorWithoutArgs
![]() |
1 PASS | |
0.18181819
|
org.easymock.tests2.MockBuilderTest.testWithConstructorConstructorArgs
![]() |
1 PASS | |
0.18181819
|
org.easymock.tests2.MockBuilderTest.testCreateMock_ConstructorWithoutArgs
![]() |
1 PASS | |
0.18181819
|
org.easymock.tests2.MockBuilderTest.testWithConstructorConstructorArgs
![]() |
1 PASS | |
0.15454546
|
org.easymock.tests2.MockBuilderTest.testWithArgsTwice
![]() |
1 PASS | |
0.15454546
|
org.easymock.tests2.MockBuilderTest.testWithArgsTwice
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.EasyMockSupportClassTest.testCreateMockBuilder
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.MockBuilderTest.testCreateNiceMock
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.MockBuilderTest.testCreateStrictMock
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.EasyMockSupportClassTest.testCreateMockBuilder
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.MockBuilderTest.testCreateMock
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.MockBuilderTest.testCreateNiceMock
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.MockBuilderTest.testCreateMock
![]() |
1 PASS | |
0.14545454
|
org.easymock.tests2.MockBuilderTest.testCreateStrictMock
![]() |
1 PASS | |
0.13636364
|
org.easymock.tests2.MockedExceptionTest.testNotMockedFillInStackTrace
![]() |
1 PASS | |
0.13636364
|
org.easymock.tests2.MockedExceptionTest.testNotMockedFillInStackTrace
![]() |
1 PASS | |
0.12727273
|
org.easymock.tests2.MockBuilderTest.testCreateStrictMockString
![]() |
1 PASS | |
0.12727273
|
org.easymock.tests2.MockBuilderTest.testCreateMockString
![]() |
1 PASS | |
0.12727273
|
org.easymock.tests2.MockBuilderTest.testCreateMockString
![]() |
1 PASS | |
0.12727273
|
org.easymock.tests2.MockBuilderTest.testCreateNiceMockString
![]() |
1 PASS | |
0.12727273
|
org.easymock.tests2.MockBuilderTest.testCreateStrictMockString
![]() |
1 PASS | |
0.12727273
|
org.easymock.tests2.MockBuilderTest.testCreateNiceMockString
![]() |
1 PASS | |
0.11818182
|
org.easymock.tests2.MockBuilderTest.testWithEmptyConstructor_NoEmptyConstructor
![]() |
1 PASS | |
0.11818182
|
org.easymock.tests2.MockBuilderTest.testWithEmptyConstructor_NoEmptyConstructor
![]() |
1 PASS | |
0.10909091
|
org.easymock.tests2.EasyMockSupportClassTest.testCreateMockBuilder_existingControl
![]() |
1 PASS | |
0.10909091
|
org.easymock.tests2.EasyMockSupportClassTest.testCreateMockBuilder_existingControl
![]() |
1 PASS | |
0.09090909
|
org.easymock.tests2.MockBuilderTest.testWithConstructor_Twice
![]() |
1 PASS | |
0.09090909
|
org.easymock.tests2.MockBuilderTest.testWithConstructor_Twice
![]() |
1 PASS | |
0.09090909
|
org.easymock.tests2.MockBuilderTest.testCreateMockStringIMocksControl
![]() |
1 PASS | |
0.09090909
|
org.easymock.tests2.MockBuilderTest.testCreateMockStringIMocksControl
![]() |
1 PASS | |
0.07272727
|
org.easymock.tests2.MockBuilderTest.testWithConstructorWithArgs_NotExisting
![]() |
1 PASS | |
0.07272727
|
org.easymock.tests2.MockBuilderTest.testWithConstructor_WrongClass
![]() |
1 PASS | |
0.07272727
|
org.easymock.tests2.MockBuilderTest.testWithConstructorWithArgs_NotExisting
![]() |
1 PASS | |
0.07272727
|
org.easymock.tests2.MockBuilderTest.testWithConstructor_WrongClass
![]() |
1 PASS | |
0.054545455
|
org.easymock.tests2.MockBuilderTest.testCreateMockIMocksControl
![]() |
1 PASS | |
0.054545455
|
org.easymock.tests2.MockBuilderTest.testCreateMockIMocksControl
![]() |
1 PASS | |
0.045454547
|
org.easymock.tests2.MockBuilderTest.testAddMethodWithParams_NotExisting
![]() |
1 PASS | |
0.045454547
|
org.easymock.tests2.MockBuilderTest.testAddMethod_NotExisting
![]() |
1 PASS | |
0.045454547
|
org.easymock.tests2.MockBuilderTest.testAddMethod_NotExisting
![]() |
1 PASS | |
0.045454547
|
org.easymock.tests2.MockBuilderTest.testAddMethodWithParams_NotExisting
![]() |
1 PASS | |
0.036363635
|
org.easymock.tests2.MockBuilderTest.testWithArgs_WithoutConstructor
![]() |
1 PASS | |
0.036363635
|
org.easymock.tests2.MockBuilderTest.testWithArgs_WithoutConstructor
![]() |
1 PASS | |
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.lang.reflect.Constructor; | |
20 | import java.lang.reflect.Method; | |
21 | import java.util.HashSet; | |
22 | import java.util.Set; | |
23 | ||
24 | import org.easymock.*; | |
25 | ||
26 | /** | |
27 | * Default implementation of IMockBuilder. | |
28 | * <p> | |
29 | * <i>The original idea and part of the code where contributed by Rodrigo | |
30 | * Damazio and Bruno Fonseca at <a href="http://www.google.com">Google</a></i> | |
31 | * | |
32 | * @param <T> | |
33 | * type of the mock created | |
34 | * | |
35 | * @author Henri Tremblay | |
36 | */ | |
37 | public class MockBuilder<T> implements IMockBuilder<T> { | |
38 | ||
39 | private final Class<T> toMock; | |
40 | ||
41 | private final Set<Method> mockedMethods = new HashSet<Method>(); | |
42 | ||
43 | private Constructor<T> constructor; | |
44 | ||
45 | private ConstructorArgs constructorArgs; | |
46 | ||
47 | private final EasyMockSupport support; | |
48 | ||
49 | 72 |
![]() |
50 | 72 | this(toMock, null); |
51 | } | |
52 | ||
53 | /** | |
54 | * Used by EasyMockSupport to allow the mock registration in the list of | |
55 | * controls | |
56 | * | |
57 | * @param toMock | |
58 | * The class of the mock to create | |
59 | * @param support | |
60 | * The EasyMockSupport used to create mocks. Null if none | |
61 | */ | |
62 | 88 |
![]() |
63 | 88 | this.toMock = toMock; |
64 | 88 | this.support = support; |
65 | } | |
66 | ||
67 | 22 |
![]() |
68 | 22 | mockedMethods.add(method); |
69 | 22 | return this; |
70 | } | |
71 | ||
72 | 46 |
![]() |
73 | 46 | final Method m = ReflectionUtils.findMethod(toMock, methodName); |
74 | 46 | if (m == null) { |
75 | 2 | throw new IllegalArgumentException("Method not found (or private): " + methodName); |
76 | } | |
77 | 44 | mockedMethods.add(m); |
78 | 44 | return this; |
79 | } | |
80 | ||
81 | 8 |
![]() |
82 | 8 | final Method m = ReflectionUtils.findMethod(toMock, methodName, parameterTypes); |
83 | 8 | if (m == null) { |
84 | 2 | throw new IllegalArgumentException("Method not found (or private): " + methodName); |
85 | } | |
86 | 6 | mockedMethods.add(m); |
87 | 6 | return this; |
88 | } | |
89 | ||
90 | 4 |
![]() |
91 | 4 | for (final String methodName : methodNames) { |
92 | 8 | addMockedMethod(methodName); |
93 | } | |
94 | 4 | return this; |
95 | } | |
96 | ||
97 | 2 |
![]() |
98 | 2 | for (final Method method : methods) { |
99 | 4 | addMockedMethod(method); |
100 | } | |
101 | 2 | return this; |
102 | } | |
103 | ||
104 | 2 |
![]() |
105 | public IMockBuilder<T> withConstructor(final Constructor<?> constructor) { | |
106 | 2 | checkConstructorNotInitialized(); |
107 | 2 | this.constructor = (Constructor<T>) constructor; |
108 | 2 | return this; |
109 | } | |
110 | ||
111 | 2 |
![]() |
112 | public IMockBuilder<T> withConstructor(final ConstructorArgs constructorArgs) { | |
113 | 2 | checkConstructorNotInitialized(); |
114 | 2 | this.constructor = (Constructor<T>) constructorArgs.getConstructor(); |
115 | 2 | this.constructorArgs = constructorArgs; |
116 | 2 | return this; |
117 | } | |
118 | ||
119 | 6 |
![]() |
120 | 6 | checkConstructorNotInitialized(); |
121 | 6 | try { |
122 | 6 | constructor = ReflectionUtils.getConstructor(toMock); |
123 | } catch (final NoSuchMethodException e) { | |
124 | 2 | throw new IllegalArgumentException("No empty constructor can be found", e); |
125 | } | |
126 | 4 | constructorArgs = new ConstructorArgs(constructor); |
127 | 4 | return this; |
128 | } | |
129 | ||
130 | 10 |
![]() |
131 | 10 | checkConstructorNotInitialized(); |
132 | 10 | try { |
133 | 10 | constructor = ReflectionUtils.getConstructor(toMock, initArgs); |
134 | } catch (final NoSuchMethodException e) { | |
135 | 2 | throw new IllegalArgumentException("No constructor matching arguments can be found", e); |
136 | } | |
137 | 8 | constructorArgs = new ConstructorArgs(constructor, initArgs); |
138 | 8 | return this; |
139 | } | |
140 | ||
141 | 18 |
![]() |
142 | 18 | checkConstructorNotInitialized(); |
143 | ||
144 | 16 | try { |
145 | 16 | constructor = toMock.getDeclaredConstructor(argTypes); |
146 | } catch (final NoSuchMethodException e) { | |
147 | 2 | throw new IllegalArgumentException("No constructor matching arguments can be found", e); |
148 | } | |
149 | 14 | return this; |
150 | } | |
151 | ||
152 | 16 |
![]() |
153 | 16 | if (constructor == null) { |
154 | 2 | throw new IllegalStateException( |
155 | "Trying to define constructor arguments without first setting their type."); | |
156 | } | |
157 | 14 | if (constructorArgs != null) { |
158 | 2 | throw new IllegalStateException("Trying to define the constructor arguments more than once."); |
159 | } | |
160 | ||
161 | 12 | constructorArgs = new ConstructorArgs(constructor, initArgs); |
162 | 12 | return this; |
163 | } | |
164 | ||
165 | 4 |
![]() |
166 | 4 | return createMock(null, control); |
167 | } | |
168 | ||
169 | 38 |
![]() |
170 | 38 | return createMock((String) null); |
171 | } | |
172 | ||
173 | 8 |
![]() |
174 | 8 | return createNiceMock(null); |
175 | } | |
176 | ||
177 | 6 |
![]() |
178 | 6 | return createStrictMock(null); |
179 | } | |
180 | ||
181 | 70 |
![]() |
182 | public T createMock(final String name, final IMocksControl control) { | |
183 | // Create a mock with no default {@code withConstructor} was not called. | |
184 | 70 | if (constructor == null) { |
185 | 44 | return control.createMock(name, toMock, mockedMethods.toArray(new Method[mockedMethods.size()])); |
186 | } | |
187 | ||
188 | // If the constructor is defined, so must be its arguments | |
189 | 26 | if (constructorArgs == null) { |
190 | 2 | throw new IllegalStateException("Picked a constructor but didn't pass arguments to it"); |
191 | } | |
192 | ||
193 | 24 | return control.createMock(name, toMock, constructorArgs, mockedMethods |
194 | .toArray(new Method[mockedMethods.size()])); | |
195 | } | |
196 | ||
197 | 42 |
![]() |
198 | 42 | final IMocksControl control = (support == null ? EasyMock.createControl() : support.createControl()); |
199 | 42 | return createMock(name, control); |
200 | } | |
201 | ||
202 | 12 |
![]() |
203 | 12 | final IMocksControl control = (support == null ? EasyMock.createNiceControl() : support |
204 | .createNiceControl()); | |
205 | 12 | return createMock(name, control); |
206 | } | |
207 | ||
208 | 10 |
![]() |
209 | 10 | final IMocksControl control = (support == null ? EasyMock.createStrictControl() : support |
210 | .createStrictControl()); | |
211 | 10 | return createMock(name, control); |
212 | } | |
213 | ||
214 | 38 |
![]() |
215 | 38 | if (constructor != null) { |
216 | 2 | throw new IllegalStateException("Trying to define the constructor call more than once."); |
217 | } | |
218 | } | |
219 | } |
|