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