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         public int countTestCases()
152         {
153             return 0;
154         }
155 
156         public void run( TestResult testResult )
157         {
158         }
159     }
160 
161 
162     @SuppressWarnings( { "UnusedDeclaration" } )
163     public static class NotValidTest
164     {
165         public void testSomething()
166         {
167         }
168     }
169 
170     public abstract static class BaseClassWithTest
171     {
172         @Test
173         public void weAreAlsoATest()
174         {
175         }
176     }
177 
178     public static class SubClassWithoutOwnTestMethods
179         extends BaseClassWithTest
180     {
181     }
182 
183     @RunWith( Suite.class )
184     public static class SuiteValid1
185     {
186         public void testSomething()
187         {
188 
189         }
190     }
191 
192     class CustomRunner
193         extends Runner
194     {
195         @Override
196         public Description getDescription()
197         {
198             return Description.createSuiteDescription( "CustomRunner" );
199         }
200 
201         @Override
202         public void run( RunNotifier runNotifier )
203         {
204         }
205     }
206 
207     @RunWith( CustomRunner.class )
208     public static class SuiteValidCustomRunner
209     {
210         public void testSomething()
211         {
212 
213         }
214     }
215 
216 
217     @RunWith( MySuite.class )
218     public static class SuiteValid2
219     {
220         public void testSomething()
221         {
222 
223         }
224     }
225 
226     public static class SimpleJUnit4TestClass
227     {
228         @Test
229         public void testMethod()
230         {
231         }
232     }
233 
234     class MySuite
235         extends Suite
236     {
237         MySuite( Class<?> klass )
238             throws InitializationError
239         {
240             super( klass );
241         }
242     }
243 
244     class NestedTC
245         extends TestCase
246     {
247         public class Inner
248         {
249 
250         }
251     }
252 
253 }