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