1 package org.apache.maven.surefire.junit4;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.util.Collections;
22 import java.util.Set;
23 import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
24 import org.apache.maven.surefire.testset.TestSetFailedException;
25
26 import junit.framework.TestCase;
27 import junit.framework.TestResult;
28 import org.junit.Test;
29 import org.junit.internal.runners.InitializationError;
30 import org.junit.runner.Description;
31 import org.junit.runner.RunWith;
32 import org.junit.runner.Runner;
33 import org.junit.runner.notification.RunNotifier;
34 import org.junit.runners.Suite;
35
36 import static org.junit.Assert.assertFalse;
37 import static org.junit.Assert.assertTrue;
38
39
40
41
42 public class JUnit4TestCheckerTest
43 {
44 private final JUnit4TestChecker jUnit4TestChecker = new JUnit4TestChecker( this.getClass().getClassLoader() );
45
46 @Test
47 public void validJunit4Annotated()
48 throws TestSetFailedException
49 {
50 assertTrue( jUnit4TestChecker.accept( JUnit4TestCheckerTest.class ) );
51 }
52
53 @Test
54 public void validJunit4itsAJunit3Test()
55 throws TestSetFailedException
56 {
57 assertTrue( jUnit4TestChecker.accept( AlsoValid.class ) );
58 }
59
60 @Test
61 public void validJunitSubclassWithoutOwnTestmethods()
62 throws TestSetFailedException
63 {
64 assertTrue( jUnit4TestChecker.accept( SubClassWithoutOwnTestMethods.class ) );
65 }
66
67 @Test
68 public void validSuite()
69 throws TestSetFailedException
70 {
71 assertTrue( jUnit4TestChecker.accept( SuiteValid1.class ) );
72 }
73
74 @Test
75 public void validCustomSuite()
76 throws TestSetFailedException
77 {
78 assertTrue( jUnit4TestChecker.accept( SuiteValid2.class ) );
79 }
80
81 @Test
82 public void validCustomRunner()
83 throws TestSetFailedException
84 {
85 assertTrue( jUnit4TestChecker.accept( SuiteValidCustomRunner.class ) );
86 }
87
88 @Test
89 public void invalidTest()
90 throws TestSetFailedException
91 {
92 assertFalse( jUnit4TestChecker.accept( NotValidTest.class ) );
93 }
94
95 @Test
96 public void dontAcceptAbstractClasses()
97 {
98 assertFalse( jUnit4TestChecker.accept( BaseClassWithTest.class ) );
99 }
100
101 @Test
102 public void suiteOnlyTest()
103 {
104 assertTrue( jUnit4TestChecker.accept( SuiteOnlyTest.class ) );
105 }
106
107 @Test
108 public void customSuiteOnlyTest()
109 {
110 assertTrue( jUnit4TestChecker.accept( CustomSuiteOnlyTest.class ) );
111 }
112
113 @Test
114 public void innerClassNotAutomaticallyTc()
115 {
116 assertTrue( jUnit4TestChecker.accept( NestedTC.class ) );
117 assertFalse( jUnit4TestChecker.accept( NestedTC.Inner.class ) );
118 }
119
120 @Test
121 public void testCannotLoadRunWithAnnotation()
122 throws Exception
123 {
124 Class testClass = SimpleJUnit4TestClass.class;
125 ClassLoader testClassLoader = testClass.getClassLoader();
126
127
128
129
130 Set<String> visiblePackages = Collections.singleton( "org.junit" );
131 PackageFilteringClassLoader filteringTestClassloader =
132 new PackageFilteringClassLoader( testClassLoader, visiblePackages );
133 JUnit4TestChecker checker = new JUnit4TestChecker( filteringTestClassloader );
134 assertTrue( checker.accept( testClass ) );
135 }
136
137 public static class AlsoValid
138 extends TestCase
139 {
140 public void testSomething()
141 {
142
143 }
144 }
145
146 public static class SuiteOnlyTest
147 {
148 public static junit.framework.Test suite()
149 {
150 return null;
151 }
152 }
153
154 public static class CustomSuiteOnlyTest
155 {
156 public static MySuite2 suite()
157 {
158 return null;
159 }
160 }
161
162 public static class MySuite2
163 implements junit.framework.Test
164 {
165 public int countTestCases()
166 {
167 return 0;
168 }
169
170 public void run( TestResult testResult )
171 {
172 }
173 }
174
175
176 @SuppressWarnings( { "UnusedDeclaration" } )
177 public static class NotValidTest
178 {
179 public void testSomething()
180 {
181 }
182 }
183
184 public abstract static class BaseClassWithTest
185 {
186 @Test
187 public void weAreAlsoATest()
188 {
189 }
190 }
191
192 public static class SubClassWithoutOwnTestMethods
193 extends BaseClassWithTest
194 {
195 }
196
197 @RunWith( Suite.class )
198 public static class SuiteValid1
199 {
200 public void testSomething()
201 {
202
203 }
204 }
205
206 class CustomRunner
207 extends Runner
208 {
209 @Override
210 public Description getDescription()
211 {
212 return Description.createSuiteDescription( "CustomRunner" );
213 }
214
215 @Override
216 public void run( RunNotifier runNotifier )
217 {
218 }
219 }
220
221 @RunWith( CustomRunner.class )
222 public static class SuiteValidCustomRunner
223 {
224 public void testSomething()
225 {
226
227 }
228 }
229
230
231 @RunWith( MySuite.class )
232 public static class SuiteValid2
233 {
234 public void testSomething()
235 {
236
237 }
238 }
239
240 public static class SimpleJUnit4TestClass
241 {
242 @Test
243 public void testMethod()
244 {
245 }
246 }
247
248 class MySuite
249 extends Suite
250 {
251 MySuite( Class<?> klass )
252 throws InitializationError
253 {
254 super( klass );
255 }
256 }
257
258 class NestedTC
259 extends TestCase
260 {
261 public class Inner
262 {
263
264 }
265 }
266
267 }