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