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 @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 public TestsToRun locateTestClasses( ClassLoader classLoader, ScannerFilter scannerFilter )
68 {
69 String[] testClassNames = collectTests();
70 Set<Class<?>> result = new LinkedHashSet<Class<?>>();
71
72 String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
73 SpecificTestClassFilter specificTestFilter = new SpecificTestClassFilter( specific );
74
75 for ( String className : testClassNames )
76 {
77 Class<?> testClass = loadClass( classLoader, className );
78
79 if ( !specificTestFilter.accept( testClass ) )
80 {
81
82 continue;
83 }
84
85 if ( scannerFilter == null || scannerFilter.accept( testClass ) )
86 {
87 result.add( testClass );
88 }
89 }
90
91 return new TestsToRun( result );
92 }
93
94 private static Class<?> loadClass( ClassLoader classLoader, String className )
95 {
96 try
97 {
98 return classLoader.loadClass( className );
99 }
100 catch ( ClassNotFoundException e )
101 {
102 throw new RuntimeException( "Unable to create test class '" + className + "'", e );
103 }
104 }
105
106 String[] collectTests()
107 {
108 String[] tests = EMPTY_STRING_ARRAY;
109 if ( basedir.exists() )
110 {
111 org.apache.maven.shared.utils.io.DirectoryScanner scanner =
112 new org.apache.maven.shared.utils.io.DirectoryScanner();
113
114 scanner.setBasedir( basedir );
115
116 if ( includes != null )
117 {
118 scanner.setIncludes( processIncludesExcludes( includes ) );
119 }
120
121 if ( excludes != null )
122 {
123 scanner.setExcludes( processIncludesExcludes( excludes ) );
124 }
125
126 scanner.scan();
127
128 tests = scanner.getIncludedFiles();
129 for ( int i = 0; i < tests.length; i++ )
130 {
131 String test = tests[i];
132 test = test.substring( 0, test.indexOf( "." ) );
133 tests[i] = test.replace( FS.charAt( 0 ), '.' );
134 }
135 }
136 return tests;
137 }
138
139 private static String[] processIncludesExcludes( List<String> list )
140 {
141 List<String> newList = new ArrayList<String>();
142 for ( String include : list )
143 {
144 String[] includes = include.split( "," );
145 Collections.addAll( newList, includes );
146 }
147
148 String[] incs = new String[newList.size()];
149
150 for ( int i = 0; i < incs.length; i++ )
151 {
152 String inc = newList.get( i );
153 if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) )
154 {
155 inc = inc.substring( 0, inc.lastIndexOf( JAVA_SOURCE_FILE_EXTENSION ) ) + JAVA_CLASS_FILE_EXTENSION;
156 }
157 incs[i] = inc;
158
159 }
160 return incs;
161 }
162
163 }