View Javadoc

1   package org.apache.maven.surefire.util;
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 org.apache.maven.surefire.SpecificTestClassFilter;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  
29  /**
30   * Scans directories looking for tests.
31   *
32   * @author Karl M. Davis
33   * @author Kristian Rosenvold
34   */
35  public class DefaultDirectoryScanner
36      implements DirectoryScanner
37  {
38  
39      private static final String FS = System.getProperty( "file.separator" );
40  
41      private static final String[] EMPTY_STRING_ARRAY = new String[0];
42  
43      private static final String JAVA_SOURCE_FILE_EXTENSION = ".java";
44  
45      private static final String JAVA_CLASS_FILE_EXTENSION = ".class";
46  
47      private final File basedir;
48  
49      private final List includes;
50  
51      private final List excludes;
52  
53      private final List specificTests;
54  
55      private final List<Class> classesSkippedByValidation = new ArrayList<Class>();
56  
57      public DefaultDirectoryScanner( File basedir, List includes, List excludes, List specificTests )
58      {
59          this.basedir = basedir;
60          this.includes = includes;
61          this.excludes = excludes;
62          this.specificTests = specificTests;
63      }
64  
65      public TestsToRun locateTestClasses( ClassLoader classLoader, ScannerFilter scannerFilter )
66      {
67          String[] testClassNames = collectTests();
68          List<Class> result = new ArrayList<Class>();
69  
70          String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
71          SpecificTestClassFilter specificTestFilter = new SpecificTestClassFilter( specific );
72  
73          for ( String className : testClassNames )
74          {
75              Class testClass = loadClass( classLoader, className );
76  
77              if ( !specificTestFilter.accept( testClass ) )
78              {
79                  // FIXME: Log this somehow!
80                  continue;
81              }
82  
83              if ( scannerFilter == null || scannerFilter.accept( testClass ) )
84              {
85                  result.add( testClass );
86              }
87              else
88              {
89                  classesSkippedByValidation.add( testClass );
90              }
91          }
92  
93          return new TestsToRun( result );
94      }
95  
96      private static Class loadClass( ClassLoader classLoader, String className )
97      {
98          Class testClass;
99          try
100         {
101             testClass = classLoader.loadClass( className );
102         }
103         catch ( ClassNotFoundException e )
104         {
105             throw new NestedRuntimeException( "Unable to create test class '" + className + "'", e );
106         }
107         return testClass;
108     }
109 
110     String[] collectTests()
111     {
112         String[] tests = EMPTY_STRING_ARRAY;
113         if ( basedir.exists() )
114         {
115             org.apache.maven.shared.utils.io.DirectoryScanner scanner =
116                 new org.apache.maven.shared.utils.io.DirectoryScanner();
117 
118             scanner.setBasedir( basedir );
119 
120             if ( includes != null )
121             {
122                 scanner.setIncludes( processIncludesExcludes( includes ) );
123             }
124 
125             if ( excludes != null )
126             {
127                 scanner.setExcludes( processIncludesExcludes( excludes ) );
128             }
129 
130             scanner.scan();
131 
132             tests = scanner.getIncludedFiles();
133             for ( int i = 0; i < tests.length; i++ )
134             {
135                 String test = tests[i];
136                 test = test.substring( 0, test.indexOf( "." ) );
137                 tests[i] = test.replace( FS.charAt( 0 ), '.' );
138             }
139         }
140         return tests;
141     }
142 
143     private static String[] processIncludesExcludes( List list )
144     {
145         List<String> newList = new ArrayList<String>();
146         for ( Object aList : list )
147         {
148             String include = (String) aList;
149             String[] includes = include.split( "," );
150             Collections.addAll( newList, includes );
151         }
152 
153         String[] incs = new String[newList.size()];
154 
155         for ( int i = 0; i < incs.length; i++ )
156         {
157             String inc = newList.get( i );
158             if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) )
159             {
160                 inc = inc.substring( 0, inc.lastIndexOf( JAVA_SOURCE_FILE_EXTENSION ) ) + JAVA_CLASS_FILE_EXTENSION;
161             }
162             incs[i] = inc;
163 
164         }
165         return incs;
166     }
167 
168 }