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

File FilterTestSuite.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
39
13
4
226
148
20
0.51
3
3.25
1.54

Classes

Class Line # Actions
FilterTestSuite 48 14 0% 5 1
0.9473684494.7%
FilterTestSuite.Initialized 58 0 - 0 0
-1.0 -
FilterTestSuite.Scope 64 0 - 0 0
-1.0 -
FilterTestSuite.TestClassRunnerForParameters 77 25 0% 15 6
0.8461538684.6%
 

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.filter.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.mockito.MockitoComponentManager;
42   
43    /**
44    *
45    * @version $Id: b4cb4ec653e3f09c62877cc32290abf5e1385a84 $
46    * @since 6.2M1
47    */
 
48    public class FilterTestSuite extends Suite
49    {
50    private static final TestDataGenerator GENERATOR = new TestDataGenerator();
51   
52    private static final String DEFAULT_PATTERN = ".*\\.test";
53   
54    private final Object klassInstance;
55   
56    @Retention(RetentionPolicy.RUNTIME)
57    @Target(ElementType.METHOD)
 
58    public static @interface Initialized
59    {
60    }
61   
62    @Retention(RetentionPolicy.RUNTIME)
63    @Target(ElementType.TYPE)
 
64    public static @interface Scope
65    {
66    /**
67    * @return the classpath prefix to search in
68    */
69    String value() default "";
70   
71    /**
72    * @return the regex pattern to filter *.test files to execute
73    */
74    String pattern() default DEFAULT_PATTERN;
75    }
76   
 
77    private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner
78    {
79    private final MockitoComponentManager mockitoComponentManager = new MockitoComponentManager();
80   
81    private final TestConfiguration configuration;
82   
 
83  32 toggle TestClassRunnerForParameters(Class< ? > type, TestConfiguration configuration) throws InitializationError
84    {
85  32 super(type);
86   
87  32 this.configuration = configuration;
88    }
89   
 
90  32 toggle @Override
91    public Object createTest() throws Exception
92    {
93  32 return getTestClass().getOnlyConstructor().newInstance(
94    new Object[] {this.configuration, getComponentManager()});
95    }
96   
 
97  64 toggle @Override
98    protected String getName()
99    {
100  64 return this.configuration.name != null ? this.configuration.name : super.getName();
101    }
102   
 
103  32 toggle @Override
104    protected String testName(final FrameworkMethod method)
105    {
106  32 return getName();
107    }
108   
 
109  32 toggle @Override
110    protected void validateConstructor(List<Throwable> errors)
111    {
112  32 validateOnlyOneConstructor(errors);
113    }
114   
 
115  32 toggle @Override
116    protected Statement classBlock(RunNotifier notifier)
117    {
118  32 return childrenInvoker(notifier);
119    }
120   
121    /**
122    * Initialize the Component Manager and call all methods annotated with {@link Initialized} in the suite, before
123    * each test is executed, to ensure test isolation.
124    */
 
125  32 toggle @Override
126    protected void runChild(FrameworkMethod method, RunNotifier notifier)
127    {
128  32 initializeComponentManager(notifier);
129   
130    // Check all methods for a ComponentManager annotation and call the found ones.
131  32 try {
132  32 for (Method klassMethod : klassInstance.getClass().getMethods()) {
133  289 Initialized componentManagerAnnotation = klassMethod.getAnnotation(Initialized.class);
134  289 if (componentManagerAnnotation != null) {
135    // Call it!
136  0 klassMethod.invoke(klassInstance, getComponentManager());
137    }
138    }
139    } catch (Exception e) {
140  0 notifier.fireTestFailure(new Failure(getDescription(), new RuntimeException(
141    "Failed to call Component Manager initialization method", e)));
142    }
143   
144  32 try {
145  32 super.runChild(method, notifier);
146    } finally {
147  32 shutdownComponentManager(notifier);
148    }
149    }
150   
 
151  32 toggle private void initializeComponentManager(RunNotifier notifier)
152    {
153  32 try {
154  32 this.mockitoComponentManager.initializeTest(klassInstance);
155  32 this.mockitoComponentManager.registerMemoryConfigurationSource();
156    } catch (Exception e) {
157  0 notifier.fireTestFailure(new Failure(getDescription(), new RuntimeException(
158    "Failed to initialize Component Manager", e)));
159    }
160   
161    }
162   
 
163  32 toggle private void shutdownComponentManager(RunNotifier notifier)
164    {
165  32 try {
166  32 this.mockitoComponentManager.shutdownTest();
167    } catch (Exception e) {
168  0 notifier.fireTestFailure(new Failure(getDescription(), new RuntimeException(
169    "Failed to shutdown Component Manager", e)));
170    }
171    }
172   
 
173  32 toggle private ComponentManager getComponentManager() throws Exception
174    {
175  32 return this.mockitoComponentManager;
176    }
177    }
178   
179    private final ArrayList<Runner> runners = new ArrayList<Runner>();
180   
181    /**
182    * Only called reflectively. Do not use programmatically.
183    */
 
184  3 toggle public FilterTestSuite(Class< ? > klass) throws Throwable
185    {
186  3 super(klass, Collections.<Runner> emptyList());
187   
188  3 try {
189  3 this.klassInstance = klass.newInstance();
190    } catch (Exception e) {
191  0 throw new RuntimeException("Failed to construct instance of [" + klass.getName() + "]", e);
192    }
193   
194    // If a Scope Annotation is present then use it to define the scope
195  3 Scope scopeAnnotation = klass.getAnnotation(Scope.class);
196  3 String packagePrefix = "";
197  3 String pattern = DEFAULT_PATTERN;
198  3 if (scopeAnnotation != null) {
199  2 packagePrefix = scopeAnnotation.value();
200  2 pattern = scopeAnnotation.pattern();
201    }
202   
203  3 for (TestConfiguration testConfiguration : GENERATOR.generateData(packagePrefix, pattern)) {
204  32 this.runners.add(new TestClassRunnerForParameters(FilterTest.class, testConfiguration));
205    }
206    }
207   
 
208  3 toggle @Override
209    protected List<Runner> getChildren()
210    {
211  3 return this.runners;
212    }
213   
214    /**
215    * {@inheritDoc}
216    * <p>
217    * We override this method so that the JUnit results are not displayed in a test hierarchy with a
218    * single test result for each node (as it would be otherwise since RenderingTest has a single test method).
219    * </p>
220    */
 
221  6 toggle @Override
222    public Description getDescription()
223    {
224  6 return Description.createSuiteDescription(getTestClass().getJavaClass());
225    }
226    }