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  import static org.junit.Assert.assertFalse;
37  import static org.junit.Assert.assertTrue;
38  
39  /**
40   * @author Kristian Rosenvold
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         // Emulate an OSGi classloader which filters on package level.
127         // Use a classloader which can only load classes in package org.junit,
128         // e.g. org.junit.Test, but no classes from other packages,
129         // in particular org.junit.runner.RunWith can't be loaded
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 }