View Javadoc
1   package org.apache.maven.surefire.junit4;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
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   * @author Kristian Rosenvold
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         // Emulate an OSGi classloader which filters on package level.
113         // Use a classloader which can only load classes in package org.junit,
114         // e.g. org.junit.Test, but no classes from other packages,
115         // in particular org.junit.runner.RunWith can't be loaded
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 }