1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.test.integration

File RenderingTestSuite.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

18
63
15
4
309
198
30
0.48
4.2
3.75
2

Classes

Class Line # Actions
RenderingTestSuite 78 15 0% 6 1
0.9545454495.5%
RenderingTestSuite.Initialized 88 0 - 0 0
-1.0 -
RenderingTestSuite.Scope 94 0 - 0 0
-1.0 -
RenderingTestSuite.TestClassRunnerForParameters 107 48 0% 24 3
0.959459595.9%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Run all tests found in {@code *.test} files located in the classpath. These {@code *.test} files must follow the
48    * conventions described in {@link org.xwiki.rendering.test.integration.TestDataParser}.
49    * <p>Usage Example</p>
50    * <pre>
51    * <code>
52    * {@literal @}RunWith(RenderingTestSuite.class)
53    * public class IntegrationTests
54    * {
55    * }
56    * </code>
57    * </pre>
58    * <p>It's also possible to get access to the underlying Component Manager used, for example in order to register
59    * Mock implementations of components. For example:</p>
60    * <pre>
61    * <code>
62    * {@literal @}RunWith(RenderingTestSuite.class)
63    * {@literal @}AllComponents
64    * public class IntegrationTests
65    * {
66    * {@literal @}RenderingTestSuite.Initialized
67    * public void initialize(MockitoComponentManager componentManager)
68    * {
69    * // Init mocks here for example
70    * }
71    * }
72    * </code>
73    * </pre>
74    *
75    * @version $Id: 09aa1a334660babe7513c063723ffe91af653f6d $
76    * @since 3.0RC1
77    */
 
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)
 
88    public static @interface Initialized
89    {
90    }
91   
92    @Retention(RetentionPolicy.RUNTIME)
93    @Target(ElementType.TYPE)
 
94    public static @interface Scope
95    {
96    /**
97    * @return the classpath prefix to search in
98    */
99    String value() default "";
100   
101    /**
102    * @return the regex pattern to filter *.test files to execute
103    */
104    String pattern() default DEFAULT_PATTERN;
105    }
106   
 
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   
 
118  887 toggle 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   
 
126  887 toggle @Override
127    public Object createTest() throws Exception
128    {
129  887 return getTestClass().getOnlyConstructor().newInstance(
130    computeParams());
131    }
132   
 
133  887 toggle private Object[] computeParams() throws Exception
134    {
135    // Add the Component Manager as the last parameter in order to pass it to the Test constructor
136    // Remove the first parameter which is the test name and that is not needed in RenderingTest
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   
 
144  1774 toggle @Override
145    protected String getName()
146    {
147  1774 return (String) this.parameterList.get(this.parameterSetNumber)[0];
148    }
149   
 
150  887 toggle @Override
151    protected String testName(final FrameworkMethod method)
152    {
153  887 return getName();
154    }
155   
 
156  887 toggle @Override
157    protected void validateConstructor(List<Throwable> errors)
158    {
159  887 validateOnlyOneConstructor(errors);
160    }
161   
 
162  887 toggle @Override
163    protected Statement classBlock(RunNotifier notifier)
164    {
165  887 return childrenInvoker(notifier);
166    }
167   
168    /**
169    * Initialize the Component Manager and call all methods annotated with {@link Initialized} in the suite,
170    * before each test is executed, to ensure test isolation.
171    */
 
172  887 toggle @Override
173    protected void runChild(FrameworkMethod method, RunNotifier notifier)
174    {
175  887 initializeComponentManager(notifier);
176   
177    // Check all methods for a ComponentManager annotation and call the found ones.
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    // Call it!
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   
 
198  887 toggle 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   
 
215  887 toggle 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   
 
229  1150 toggle 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   
 
238  2924 toggle 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    // If the class is using either @AllComponents or @ComponentList then consider we're not in legacy.
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    * Only called reflectively. Do not use programmatically.
266    */
 
267  31 toggle 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    // If a Scope Annotation is present then use it to define the scope
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   
 
292  31 toggle @Override
293    protected List<Runner> getChildren()
294    {
295  31 return this.runners;
296    }
297   
298    /**
299    * {@inheritDoc}
300    *
301    * We override this method so that the JUnit results are not displayed in a test hierarchy with a single test
302    * result for each node (as it would be otherwise since RenderingTest has a single test method).
303    */
 
304  62 toggle @Override
305    public Description getDescription()
306    {
307  62 return Description.createSuiteDescription(getTestClass().getJavaClass());
308    }
309    }