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