1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.test.integration; |
21 |
|
|
22 |
|
import java.lang.annotation.ElementType; |
23 |
|
import java.lang.annotation.Retention; |
24 |
|
import java.lang.annotation.RetentionPolicy; |
25 |
|
import java.lang.annotation.Target; |
26 |
|
import java.lang.reflect.Method; |
27 |
|
import java.util.ArrayList; |
28 |
|
import java.util.Collections; |
29 |
|
import java.util.List; |
30 |
|
|
31 |
|
import org.junit.runner.Description; |
32 |
|
import org.junit.runner.Runner; |
33 |
|
import org.junit.runner.notification.Failure; |
34 |
|
import org.junit.runner.notification.RunNotifier; |
35 |
|
import org.junit.runners.BlockJUnit4ClassRunner; |
36 |
|
import org.junit.runners.Suite; |
37 |
|
import org.junit.runners.model.FrameworkMethod; |
38 |
|
import org.junit.runners.model.InitializationError; |
39 |
|
import org.junit.runners.model.Statement; |
40 |
|
import org.xwiki.component.manager.ComponentManager; |
41 |
|
import org.xwiki.test.annotation.AllComponents; |
42 |
|
import org.xwiki.test.annotation.ComponentList; |
43 |
|
import org.xwiki.test.jmock.XWikiComponentInitializer; |
44 |
|
import org.xwiki.test.mockito.MockitoComponentManager; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@version |
76 |
|
@since |
77 |
|
|
|
|
| 95.5% |
Uncovered Elements: 1 (22) |
Complexity: 6 |
Complexity Density: 0.4 |
|
78 |
|
public class RenderingTestSuite extends Suite |
79 |
|
{ |
80 |
|
private static final TestDataGenerator GENERATOR = new TestDataGenerator(); |
81 |
|
|
82 |
|
private static final String DEFAULT_PATTERN = ".*\\.test"; |
83 |
|
|
84 |
|
private final Object klassInstance; |
85 |
|
|
86 |
|
@Retention(RetentionPolicy.RUNTIME) |
87 |
|
@Target(ElementType.METHOD) |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
88 |
|
public static @interface Initialized |
89 |
|
{ |
90 |
|
} |
91 |
|
|
92 |
|
@Retention(RetentionPolicy.RUNTIME) |
93 |
|
@Target(ElementType.TYPE) |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
94 |
|
public static @interface Scope |
95 |
|
{ |
96 |
|
|
97 |
|
@return |
98 |
|
|
99 |
|
String value() default ""; |
100 |
|
|
101 |
|
|
102 |
|
@return |
103 |
|
|
104 |
|
String pattern() default DEFAULT_PATTERN; |
105 |
|
} |
106 |
|
|
|
|
| 95.9% |
Uncovered Elements: 3 (74) |
Complexity: 24 |
Complexity Density: 0.5 |
|
107 |
|
private class TestClassRunnerForParameters extends |
108 |
|
BlockJUnit4ClassRunner |
109 |
|
{ |
110 |
|
private final int parameterSetNumber; |
111 |
|
|
112 |
|
private final MockitoComponentManager mockitoComponentManager = new MockitoComponentManager(); |
113 |
|
|
114 |
|
private final XWikiComponentInitializer componentInitializer = new XWikiComponentInitializer(); |
115 |
|
|
116 |
|
private final List<Object[]> parameterList; |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
118 |
887 |
TestClassRunnerForParameters(Class<?> type,... |
119 |
|
List<Object[]> parameterList, int i) throws InitializationError |
120 |
|
{ |
121 |
887 |
super(type); |
122 |
887 |
this.parameterList = parameterList; |
123 |
887 |
this.parameterSetNumber = i; |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
887 |
@Override... |
127 |
|
public Object createTest() throws Exception |
128 |
|
{ |
129 |
887 |
return getTestClass().getOnlyConstructor().newInstance( |
130 |
|
computeParams()); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
133 |
887 |
private Object[] computeParams() throws Exception... |
134 |
|
{ |
135 |
|
|
136 |
|
|
137 |
887 |
Object[] originalObjects = this.parameterList.get(this.parameterSetNumber); |
138 |
887 |
Object[] newObjects = new Object[originalObjects.length]; |
139 |
887 |
System.arraycopy(originalObjects, 1, newObjects, 0, originalObjects.length - 1); |
140 |
887 |
newObjects[originalObjects.length - 1] = getComponentManager(); |
141 |
887 |
return newObjects; |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
144 |
1774 |
@Override... |
145 |
|
protected String getName() |
146 |
|
{ |
147 |
1774 |
return (String) this.parameterList.get(this.parameterSetNumber)[0]; |
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
150 |
887 |
@Override... |
151 |
|
protected String testName(final FrameworkMethod method) |
152 |
|
{ |
153 |
887 |
return getName(); |
154 |
|
} |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
156 |
887 |
@Override... |
157 |
|
protected void validateConstructor(List<Throwable> errors) |
158 |
|
{ |
159 |
887 |
validateOnlyOneConstructor(errors); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
162 |
887 |
@Override... |
163 |
|
protected Statement classBlock(RunNotifier notifier) |
164 |
|
{ |
165 |
887 |
return childrenInvoker(notifier); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
@link |
170 |
|
|
171 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
172 |
887 |
@Override... |
173 |
|
protected void runChild(FrameworkMethod method, RunNotifier notifier) |
174 |
|
{ |
175 |
887 |
initializeComponentManager(notifier); |
176 |
|
|
177 |
|
|
178 |
887 |
try { |
179 |
887 |
for (Method klassMethod : RenderingTestSuite.this.klassInstance.getClass().getMethods()) { |
180 |
8246 |
Initialized componentManagerAnnotation = klassMethod.getAnnotation(Initialized.class); |
181 |
8246 |
if (componentManagerAnnotation != null) { |
182 |
|
|
183 |
263 |
klassMethod.invoke(RenderingTestSuite.this.klassInstance, getComponentManager()); |
184 |
|
} |
185 |
|
} |
186 |
|
} catch (Exception e) { |
187 |
0 |
notifier.fireTestFailure(new Failure(getDescription(), |
188 |
|
new RuntimeException("Failed to call Component Manager initialization method", e))); |
189 |
|
} |
190 |
|
|
191 |
887 |
try { |
192 |
887 |
super.runChild(method, notifier); |
193 |
|
} finally { |
194 |
887 |
shutdownComponentManager(notifier); |
195 |
|
} |
196 |
|
} |
197 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
198 |
887 |
private void initializeComponentManager(RunNotifier notifier)... |
199 |
|
{ |
200 |
887 |
try { |
201 |
887 |
if (isLegacyMode()) { |
202 |
187 |
this.componentInitializer.initializeConfigurationSource(); |
203 |
187 |
this.componentInitializer.initializeExecution(); |
204 |
|
} else { |
205 |
700 |
this.mockitoComponentManager.initializeTest(RenderingTestSuite.this.klassInstance); |
206 |
700 |
this.mockitoComponentManager.registerMemoryConfigurationSource(); |
207 |
|
} |
208 |
|
} catch (Exception e) { |
209 |
0 |
notifier.fireTestFailure(new Failure(getDescription(), |
210 |
|
new RuntimeException("Failed to initialize Component Manager", e))); |
211 |
|
} |
212 |
|
|
213 |
|
} |
214 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
215 |
887 |
private void shutdownComponentManager(RunNotifier notifier)... |
216 |
|
{ |
217 |
887 |
try { |
218 |
887 |
if (isLegacyMode()) { |
219 |
187 |
this.componentInitializer.shutdown(); |
220 |
|
} else { |
221 |
700 |
this.mockitoComponentManager.shutdownTest(); |
222 |
|
} |
223 |
|
} catch (Exception e) { |
224 |
0 |
notifier.fireTestFailure(new Failure(getDescription(), |
225 |
|
new RuntimeException("Failed to shutdown Component Manager", e))); |
226 |
|
} |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
229 |
1150 |
private ComponentManager getComponentManager() throws Exception... |
230 |
|
{ |
231 |
1150 |
if (isLegacyMode()) { |
232 |
243 |
return this.componentInitializer.getComponentManager(); |
233 |
|
} else { |
234 |
907 |
return this.mockitoComponentManager; |
235 |
|
} |
236 |
|
} |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 6 |
Complexity Density: 0.6 |
|
238 |
2924 |
private boolean isLegacyMode()... |
239 |
|
{ |
240 |
2924 |
boolean isLegacyMode = true; |
241 |
2924 |
for (Method klassMethod : RenderingTestSuite.this.klassInstance.getClass().getMethods()) { |
242 |
17900 |
Initialized componentManagerAnnotation = klassMethod.getAnnotation(Initialized.class); |
243 |
17900 |
if (componentManagerAnnotation != null) { |
244 |
1052 |
if (MockitoComponentManager.class.isAssignableFrom(klassMethod.getParameterTypes()[0])) { |
245 |
828 |
isLegacyMode = false; |
246 |
|
} |
247 |
1052 |
break; |
248 |
|
} |
249 |
|
} |
250 |
|
|
251 |
2924 |
if (isLegacyMode |
252 |
|
&& (RenderingTestSuite.this.klassInstance.getClass().getAnnotation(AllComponents.class) != null |
253 |
|
|| RenderingTestSuite.this.klassInstance.getClass().getAnnotation(ComponentList.class) != null)) |
254 |
|
{ |
255 |
1479 |
isLegacyMode = false; |
256 |
|
} |
257 |
|
|
258 |
2924 |
return isLegacyMode; |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
|
private final ArrayList<Runner> runners = new ArrayList<Runner>(); |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
267 |
31 |
public RenderingTestSuite(Class<?> klass) throws Throwable... |
268 |
|
{ |
269 |
31 |
super(klass, Collections.<Runner>emptyList()); |
270 |
|
|
271 |
31 |
try { |
272 |
31 |
this.klassInstance = klass.newInstance(); |
273 |
|
} catch (Exception e) { |
274 |
0 |
throw new RuntimeException("Failed to construct instance of [" + klass.getName() + "]", e); |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
31 |
Scope scopeAnnotation = klass.getAnnotation(Scope.class); |
279 |
31 |
String packagePrefix = ""; |
280 |
31 |
String pattern = DEFAULT_PATTERN; |
281 |
31 |
if (scopeAnnotation != null) { |
282 |
9 |
packagePrefix = scopeAnnotation.value(); |
283 |
9 |
pattern = scopeAnnotation.pattern(); |
284 |
|
} |
285 |
31 |
List<Object[]> parametersList = (List<Object[]>) GENERATOR.generateData(packagePrefix, pattern); |
286 |
|
|
287 |
918 |
for (int i = 0; i < parametersList.size(); i++) { |
288 |
887 |
this.runners.add(new TestClassRunnerForParameters(RenderingTest.class, parametersList, i)); |
289 |
|
} |
290 |
|
} |
291 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
292 |
31 |
@Override... |
293 |
|
protected List<Runner> getChildren() |
294 |
|
{ |
295 |
31 |
return this.runners; |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
@inheritDoc |
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
304 |
62 |
@Override... |
305 |
|
public Description getDescription() |
306 |
|
{ |
307 |
62 |
return Description.createSuiteDescription(getTestClass().getJavaClass()); |
308 |
|
} |
309 |
|
} |