1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.junit4;
20
21 import java.util.Collections;
22 import java.util.Set;
23
24 import junit.framework.TestCase;
25 import junit.framework.TestResult;
26 import org.apache.maven.surefire.api.testset.TestSetFailedException;
27 import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
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 extends TestCase {
40 private final JUnit4TestChecker jUnit4TestChecker =
41 new JUnit4TestChecker(this.getClass().getClassLoader());
42
43 public void testValidJunit4Annotated() throws TestSetFailedException {
44 assertTrue(jUnit4TestChecker.accept(JUnit4TestCheckerTest.class));
45 }
46
47 public void testValidJunit4itsAJunit3Test() throws TestSetFailedException {
48 assertTrue(jUnit4TestChecker.accept(AlsoValid.class));
49 }
50
51 public void testValidJunitSubclassWithoutOwnTestmethods() throws TestSetFailedException {
52 assertTrue(jUnit4TestChecker.accept(SubClassWithoutOwnTestMethods.class));
53 }
54
55 public void testValidSuite() throws TestSetFailedException {
56 assertTrue(jUnit4TestChecker.accept(SuiteValid1.class));
57 }
58
59 public void testValidCustomSuite() throws TestSetFailedException {
60 assertTrue(jUnit4TestChecker.accept(SuiteValid2.class));
61 }
62
63 public void testValidCustomRunner() throws TestSetFailedException {
64 assertTrue(jUnit4TestChecker.accept(SuiteValidCustomRunner.class));
65 }
66
67 public void testInvalidTest() throws TestSetFailedException {
68 assertFalse(jUnit4TestChecker.accept(NotValidTest.class));
69 }
70
71 public void testDontAcceptAbstractClasses() {
72 assertFalse(jUnit4TestChecker.accept(BaseClassWithTest.class));
73 }
74
75 public void testSuiteOnlyTest() {
76 assertTrue(jUnit4TestChecker.accept(SuiteOnlyTest.class));
77 }
78
79 public void testCustomSuiteOnlyTest() {
80 assertTrue(jUnit4TestChecker.accept(CustomSuiteOnlyTest.class));
81 }
82
83 public void testInnerClassNotAutomaticallyTc() {
84 assertTrue(jUnit4TestChecker.accept(NestedTC.class));
85 assertFalse(jUnit4TestChecker.accept(NestedTC.Inner.class));
86 }
87
88 public void testCannotLoadRunWithAnnotation() throws Exception {
89 Class testClass = SimpleJUnit4TestClass.class;
90 ClassLoader testClassLoader = testClass.getClassLoader();
91
92
93
94
95 Set<String> visiblePackages = Collections.singleton("org.junit");
96 PackageFilteringClassLoader filteringTestClassloader =
97 new PackageFilteringClassLoader(testClassLoader, visiblePackages);
98 JUnit4TestChecker checker = new JUnit4TestChecker(filteringTestClassloader);
99 assertTrue(checker.accept(testClass));
100 }
101
102
103
104
105 public static class AlsoValid extends TestCase {
106 public void testSomething() {}
107 }
108
109
110
111
112 public static class SuiteOnlyTest {
113 public static junit.framework.Test suite() {
114 return null;
115 }
116 }
117
118
119
120
121 public static class CustomSuiteOnlyTest {
122 public static MySuite2 suite() {
123 return null;
124 }
125 }
126
127
128
129
130 public static class MySuite2 implements junit.framework.Test {
131 @Override
132 public int countTestCases() {
133 return 0;
134 }
135
136 @Override
137 public void run(TestResult testResult) {}
138 }
139
140
141
142
143 @SuppressWarnings({"UnusedDeclaration"})
144 public static class NotValidTest {
145 public void testSomething() {}
146 }
147
148
149
150
151 public abstract static class BaseClassWithTest {
152 @Test
153 public void weAreAlsoATest() {}
154 }
155
156
157
158
159 public static class SubClassWithoutOwnTestMethods extends BaseClassWithTest {}
160
161
162
163
164 @RunWith(Suite.class)
165 public static class SuiteValid1 {
166 public void testSomething() {}
167 }
168
169 class CustomRunner extends Runner {
170 @Override
171 public Description getDescription() {
172 return Description.createSuiteDescription("CustomRunner");
173 }
174
175 @Override
176 public void run(RunNotifier runNotifier) {}
177 }
178
179
180
181
182 @RunWith(CustomRunner.class)
183 public static class SuiteValidCustomRunner {
184 public void testSomething() {}
185 }
186
187
188
189
190 @RunWith(MySuite.class)
191 public static class SuiteValid2 {
192 public void testSomething() {}
193 }
194
195
196
197
198 public static class SimpleJUnit4TestClass {
199 @Test
200 public void testMethod() {}
201 }
202
203 class MySuite extends Suite {
204 MySuite(Class<?> klass) throws InitializationError {
205 super(klass);
206 }
207 }
208
209 class NestedTC extends TestCase {
210 public class Inner {}
211 }
212 }