1 | |
package org.apache.maven.surefire.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class DefaultDirectoryScanner |
35 | |
implements DirectoryScanner |
36 | |
{ |
37 | |
|
38 | 1 | private static final String FS = System.getProperty( "file.separator" ); |
39 | |
|
40 | 1 | 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 | 1 | private final List classesSkippedByValidation = new ArrayList(); |
55 | |
|
56 | |
public DefaultDirectoryScanner( File basedir, List includes, List excludes, List specificTests ) |
57 | 1 | { |
58 | 1 | this.basedir = basedir; |
59 | 1 | this.includes = includes; |
60 | 1 | this.excludes = excludes; |
61 | 1 | this.specificTests = specificTests; |
62 | 1 | } |
63 | |
|
64 | |
public TestsToRun locateTestClasses( ClassLoader classLoader, ScannerFilter scannerFilter ) |
65 | |
{ |
66 | 0 | String[] testClassNames = collectTests(); |
67 | 0 | List result = new ArrayList(); |
68 | |
|
69 | 0 | String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests ); |
70 | 0 | SpecificTestClassFilter specificTestFilter = new SpecificTestClassFilter( specific ); |
71 | |
|
72 | 0 | for ( int i = 0; i < testClassNames.length; i++ ) |
73 | |
{ |
74 | 0 | String className = testClassNames[i]; |
75 | |
|
76 | 0 | Class testClass = loadClass( classLoader, className ); |
77 | |
|
78 | 0 | if ( !specificTestFilter.accept( testClass ) ) |
79 | |
{ |
80 | |
|
81 | 0 | continue; |
82 | |
} |
83 | |
|
84 | 0 | if ( scannerFilter == null || scannerFilter.accept( testClass ) ) |
85 | |
{ |
86 | 0 | result.add( testClass ); |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | classesSkippedByValidation.add( testClass ); |
91 | |
} |
92 | |
} |
93 | |
|
94 | 0 | return new TestsToRun( result ); |
95 | |
} |
96 | |
|
97 | |
private static Class loadClass( ClassLoader classLoader, String className ) |
98 | |
{ |
99 | |
Class testClass; |
100 | |
try |
101 | |
{ |
102 | 0 | testClass = classLoader.loadClass( className ); |
103 | |
} |
104 | 0 | catch ( ClassNotFoundException e ) |
105 | |
{ |
106 | 0 | throw new NestedRuntimeException( "Unable to create test class '" + className + "'", e ); |
107 | 0 | } |
108 | 0 | return testClass; |
109 | |
} |
110 | |
|
111 | |
String[] collectTests() |
112 | |
{ |
113 | 1 | String[] tests = EMPTY_STRING_ARRAY; |
114 | 1 | if ( basedir.exists() ) |
115 | |
{ |
116 | 1 | org.apache.maven.shared.utils.io.DirectoryScanner scanner = |
117 | |
new org.apache.maven.shared.utils.io.DirectoryScanner(); |
118 | |
|
119 | 1 | scanner.setBasedir( basedir ); |
120 | |
|
121 | 1 | if ( includes != null ) |
122 | |
{ |
123 | 1 | scanner.setIncludes( processIncludesExcludes( includes ) ); |
124 | |
} |
125 | |
|
126 | 1 | if ( excludes != null ) |
127 | |
{ |
128 | 1 | scanner.setExcludes( processIncludesExcludes( excludes ) ); |
129 | |
} |
130 | |
|
131 | 1 | scanner.scan(); |
132 | |
|
133 | 1 | tests = scanner.getIncludedFiles(); |
134 | 5 | for ( int i = 0; i < tests.length; i++ ) |
135 | |
{ |
136 | 4 | String test = tests[i]; |
137 | 4 | test = test.substring( 0, test.indexOf( "." ) ); |
138 | 4 | tests[i] = test.replace( FS.charAt( 0 ), '.' ); |
139 | |
} |
140 | |
} |
141 | 1 | return tests; |
142 | |
} |
143 | |
|
144 | |
private static String[] processIncludesExcludes( List list ) |
145 | |
{ |
146 | 2 | List newList = new ArrayList(); |
147 | 2 | Iterator iter = list.iterator(); |
148 | 3 | while ( iter.hasNext() ) |
149 | |
{ |
150 | 1 | String include = (String) iter.next(); |
151 | 1 | String[] includes = include.split( "," ); |
152 | 2 | for ( int i = 0; i < includes.length; ++i ) |
153 | |
{ |
154 | 1 | newList.add( includes[i] ); |
155 | |
} |
156 | 1 | } |
157 | |
|
158 | 2 | String[] incs = new String[newList.size()]; |
159 | |
|
160 | 3 | for ( int i = 0; i < incs.length; i++ ) |
161 | |
{ |
162 | 1 | String inc = (String) newList.get( i ); |
163 | 1 | if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) ) |
164 | |
{ |
165 | 1 | 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 | 1 | incs[i] = inc; |
171 | |
|
172 | |
} |
173 | 2 | return incs; |
174 | |
} |
175 | |
|
176 | |
public List getClassesSkippedByValidation() |
177 | |
{ |
178 | 0 | return classesSkippedByValidation; |
179 | |
} |
180 | |
|
181 | |
} |