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
37
38
39 public class JUnit4TestCheckerTest
40 extends TestCase
41 {
42 private final JUnit4TestChecker jUnit4TestChecker = new JUnit4TestChecker( this.getClass().getClassLoader() );
43
44 public void testValidJunit4Annotated()
45 throws TestSetFailedException
46 {
47 assertTrue( jUnit4TestChecker.accept( JUnit4TestCheckerTest.class ) );
48 }
49
50 public void testValidJunit4itsAJunit3Test()
51 throws TestSetFailedException
52 {
53 assertTrue( jUnit4TestChecker.accept( AlsoValid.class ) );
54 }
55
56 public void testValidJunitSubclassWithoutOwnTestmethods()
57 throws TestSetFailedException
58 {
59 assertTrue( jUnit4TestChecker.accept( SubClassWithoutOwnTestMethods.class ) );
60 }
61
62 public void testValidSuite()
63 throws TestSetFailedException
64 {
65 assertTrue( jUnit4TestChecker.accept( SuiteValid1.class ) );
66 }
67
68 public void testValidCustomSuite()
69 throws TestSetFailedException
70 {
71 assertTrue( jUnit4TestChecker.accept( SuiteValid2.class ) );
72 }
73
74 public void testValidCustomRunner()
75 throws TestSetFailedException
76 {
77 assertTrue( jUnit4TestChecker.accept( SuiteValidCustomRunner.class ) );
78 }
79
80 public void testInvalidTest()
81 throws TestSetFailedException
82 {
83 assertFalse( jUnit4TestChecker.accept( NotValidTest.class ) );
84 }
85
86 public void testDontAcceptAbstractClasses()
87 {
88 assertFalse( jUnit4TestChecker.accept( BaseClassWithTest.class ) );
89 }
90
91 public void testSuiteOnlyTest()
92 {
93 assertTrue( jUnit4TestChecker.accept( SuiteOnlyTest.class ) );
94 }
95
96 public void testCustomSuiteOnlyTest()
97 {
98 assertTrue( jUnit4TestChecker.accept( CustomSuiteOnlyTest.class ) );
99 }
100
101 public void testInnerClassNotAutomaticallyTc()
102 {
103 assertTrue( jUnit4TestChecker.accept( NestedTC.class ) );
104 assertFalse( jUnit4TestChecker.accept( NestedTC.Inner.class ) );
105 }
106
107 public void testCannotLoadRunWithAnnotation()
108 throws Exception
109 {
110 Class testClass = SimpleJUnit4TestClass.class;
111 ClassLoader testClassLoader = testClass.getClassLoader();
112
113
114
115
116 Set<String> visiblePackages = Collections.singleton( "org.junit" );
117 PackageFilteringClassLoader filteringTestClassloader =
118 new PackageFilteringClassLoader( testClassLoader, visiblePackages );
119 JUnit4TestChecker checker = new JUnit4TestChecker( filteringTestClassloader );
120 assertTrue( checker.accept( testClass ) );
121 }
122
123 public static class AlsoValid
124 extends TestCase
125 {
126 public void testSomething()
127 {
128
129 }
130 }
131
132 public static class SuiteOnlyTest
133 {
134 public static junit.framework.Test suite()
135 {
136 return null;
137 }
138 }
139
140 public static class CustomSuiteOnlyTest
141 {
142 public static MySuite2 suite()
143 {
144 return null;
145 }
146 }
147
148 public static class MySuite2
149 implements junit.framework.Test
150 {
151 @Override
152 public int countTestCases()
153 {
154 return 0;
155 }
156
157 @Override
158 public void run( TestResult testResult )
159 {
160 }
161 }
162
163
164 @SuppressWarnings( { "UnusedDeclaration" } )
165 public static class NotValidTest
166 {
167 public void testSomething()
168 {
169 }
170 }
171
172 public abstract static class BaseClassWithTest
173 {
174 @Test
175 public void weAreAlsoATest()
176 {
177 }
178 }
179
180 public static class SubClassWithoutOwnTestMethods
181 extends BaseClassWithTest
182 {
183 }
184
185 @RunWith( Suite.class )
186 public static class SuiteValid1
187 {
188 public void testSomething()
189 {
190
191 }
192 }
193
194 class CustomRunner
195 extends Runner
196 {
197 @Override
198 public Description getDescription()
199 {
200 return Description.createSuiteDescription( "CustomRunner" );
201 }
202
203 @Override
204 public void run( RunNotifier runNotifier )
205 {
206 }
207 }
208
209 @RunWith( CustomRunner.class )
210 public static class SuiteValidCustomRunner
211 {
212 public void testSomething()
213 {
214
215 }
216 }
217
218
219 @RunWith( MySuite.class )
220 public static class SuiteValid2
221 {
222 public void testSomething()
223 {
224
225 }
226 }
227
228 public static class SimpleJUnit4TestClass
229 {
230 @Test
231 public void testMethod()
232 {
233 }
234 }
235
236 class MySuite
237 extends Suite
238 {
239 MySuite( Class<?> klass )
240 throws InitializationError
241 {
242 super( klass );
243 }
244 }
245
246 class NestedTC
247 extends TestCase
248 {
249 public class Inner
250 {
251
252 }
253 }
254
255 }