View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.util;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.surefire.api.filter.SpecificTestClassFilter;
29  
30  /**
31   * Scans directories looking for tests.
32   *
33   * @author Karl M. Davis
34   * @author Kristian Rosenvold
35   */
36  @Deprecated
37  public class DefaultDirectoryScanner implements DirectoryScanner {
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<String> includes;
50  
51      private final List<String> excludes;
52  
53      private final List<String> specificTests;
54  
55      public DefaultDirectoryScanner(
56              File basedir, List<String> includes, List<String> excludes, List<String> specificTests) {
57          this.basedir = basedir;
58          this.includes = includes;
59          this.excludes = excludes;
60          this.specificTests = specificTests;
61      }
62  
63      @Override
64      public TestsToRun locateTestClasses(ClassLoader classLoader, ScannerFilter scannerFilter) {
65          String[] testClassNames = collectTests();
66          Set<Class<?>> result = new LinkedHashSet<>();
67  
68          String[] specific = specificTests == null ? new String[0] : processIncludesExcludes(specificTests);
69          SpecificTestClassFilter specificTestFilter = new SpecificTestClassFilter(specific);
70  
71          for (String className : testClassNames) {
72              Class<?> testClass = loadClass(classLoader, className);
73  
74              if (!specificTestFilter.accept(testClass)) {
75                  // FIXME: Log this somehow!
76                  continue;
77              }
78  
79              if (scannerFilter == null || scannerFilter.accept(testClass)) {
80                  result.add(testClass);
81              }
82          }
83  
84          return new TestsToRun(result);
85      }
86  
87      private static Class<?> loadClass(ClassLoader classLoader, String className) {
88          try {
89              return classLoader.loadClass(className);
90          } catch (ClassNotFoundException e) {
91              throw new RuntimeException("Unable to create test class '" + className + "'", e);
92          }
93      }
94  
95      String[] collectTests() {
96          String[] tests = EMPTY_STRING_ARRAY;
97          if (basedir.exists()) {
98              org.apache.maven.surefire.shared.utils.io.DirectoryScanner scanner =
99                      new org.apache.maven.surefire.shared.utils.io.DirectoryScanner();
100 
101             scanner.setBasedir(basedir);
102 
103             if (includes != null) {
104                 scanner.setIncludes(processIncludesExcludes(includes));
105             }
106 
107             if (excludes != null) {
108                 scanner.setExcludes(processIncludesExcludes(excludes));
109             }
110 
111             scanner.scan();
112 
113             tests = scanner.getIncludedFiles();
114             for (int i = 0; i < tests.length; i++) {
115                 String test = tests[i];
116                 test = test.substring(0, test.indexOf("."));
117                 tests[i] = test.replace(FS.charAt(0), '.');
118             }
119         }
120         return tests;
121     }
122 
123     private static String[] processIncludesExcludes(List<String> list) {
124         List<String> newList = new ArrayList<>();
125         for (String include : list) {
126             String[] includes = include.split(",");
127             Collections.addAll(newList, includes);
128         }
129 
130         String[] incs = new String[newList.size()];
131 
132         for (int i = 0; i < incs.length; i++) {
133             String inc = newList.get(i);
134             if (inc.endsWith(JAVA_SOURCE_FILE_EXTENSION)) {
135                 inc = inc.substring(0, inc.lastIndexOf(JAVA_SOURCE_FILE_EXTENSION)) + JAVA_CLASS_FILE_EXTENSION;
136             }
137             incs[i] = inc;
138         }
139         return incs;
140     }
141 }