1 package org.apache.maven.plugin.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.Collections;
25 import java.util.List;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.surefire.util.DefaultScanResult;
28
29
30
31
32
33
34
35 public class DirectoryScanner
36 {
37
38 private static final String FS = System.getProperty( "file.separator" );
39
40 private static final String JAVA_SOURCE_FILE_EXTENSION = ".java";
41
42 private static final String JAVA_CLASS_FILE_EXTENSION = ".class";
43
44 private final File basedir;
45
46 private final List<String> includes;
47
48 private final List<String> excludes;
49
50 private final List<String> specificTests;
51
52 public DirectoryScanner( File basedir, List<String> includes, List<String> excludes, List<String> specificTests )
53 {
54 this.basedir = basedir;
55 this.includes = includes;
56 this.excludes = excludes;
57 this.specificTests = specificTests;
58 }
59
60 public DefaultScanResult scan()
61 {
62 String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
63 SpecificFileFilter specificTestFilter = new SpecificFileFilter( specific );
64
65 List<String> result = new ArrayList<String>();
66 if ( basedir.exists() )
67 {
68 org.apache.maven.shared.utils.io.DirectoryScanner scanner =
69 new org.apache.maven.shared.utils.io.DirectoryScanner();
70
71 scanner.setBasedir( basedir );
72
73 if ( includes != null )
74 {
75 scanner.setIncludes( processIncludesExcludes( includes ) );
76 }
77
78 if ( excludes != null )
79 {
80 scanner.setExcludes( processIncludesExcludes( excludes ) );
81 }
82
83 scanner.scan();
84 for ( String test : scanner.getIncludedFiles() )
85 {
86 if ( specificTestFilter.accept( stripBaseDir( basedir.getAbsolutePath(), test ) ) )
87 {
88 result.add( convertToJavaClassName( test ) );
89 }
90 }
91 }
92 return new DefaultScanResult( result );
93 }
94
95 private String convertToJavaClassName( String test )
96 {
97 return StringUtils.removeEnd( test, ".class" ).replace( FS, "." );
98 }
99
100 private String stripBaseDir( String basedir, String test )
101 {
102 return StringUtils.removeStart( test, basedir );
103 }
104
105 private static String[] processIncludesExcludes( List<String> list )
106 {
107 List<String> newList = new ArrayList<String>();
108 for ( Object aList : list )
109 {
110 String include = (String) aList;
111 String[] includes = include.split( "," );
112 Collections.addAll( newList, includes );
113 }
114
115 String[] incs = new String[newList.size()];
116
117 for ( int i = 0; i < incs.length; i++ )
118 {
119 String inc = newList.get( i );
120 if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) )
121 {
122 inc = StringUtils.removeEnd( inc, JAVA_SOURCE_FILE_EXTENSION ) + JAVA_CLASS_FILE_EXTENSION;
123 }
124 incs[i] = inc;
125
126 }
127 return incs;
128 }
129 }