View Javadoc

1   package org.apache.maven.surefire.junit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.Test;
23  import org.apache.maven.surefire.suite.AbstractDirectoryTestSuite;
24  import org.apache.maven.surefire.testset.PojoTestSet;
25  import org.apache.maven.surefire.testset.SurefireTestSet;
26  import org.apache.maven.surefire.testset.TestSetFailedException;
27  
28  import java.io.File;
29  import java.util.ArrayList;
30  
31  /**
32   * Test suite for JUnit tests based on a directory of Java test classes.
33   *
34   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
35   */
36  public class JUnitDirectoryTestSuite
37      extends AbstractDirectoryTestSuite
38  {
39      public JUnitDirectoryTestSuite( File basedir, ArrayList includes, ArrayList excludes )
40      {
41          super( basedir, includes, excludes );
42      }
43  
44      protected SurefireTestSet createTestSet( Class testClass, ClassLoader classLoader )
45          throws TestSetFailedException
46      {
47          Class junitClass = null;
48          try
49          {
50              junitClass = classLoader.loadClass( Test.class.getName() );
51          }
52          catch ( NoClassDefFoundError e)
53          {
54              // ignore this
55          }
56          catch ( ClassNotFoundException e )
57          {
58              // ignore this
59          }
60  
61          SurefireTestSet testSet;
62          if ( junitClass != null && junitClass.isAssignableFrom( testClass ) )
63          {
64              testSet = new JUnitTestSet( testClass );
65          }
66          else if (classHasPublicNoArgConstructor( testClass ))
67          {
68              testSet = new PojoTestSet( testClass );
69          }
70          else
71          {
72              testSet = null;
73          }
74          return testSet;
75      }
76  
77      private boolean classHasPublicNoArgConstructor( Class testClass )
78      {
79          try
80          {
81              testClass.getConstructor( new Class[0] );
82              return true;
83          }
84          catch ( Exception e )
85          {
86              return false;
87          }
88      }
89  }