1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
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
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 }