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