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 org.apache.maven.surefire.testset.TestFilter;
23
24 import java.io.File;
25 import java.util.Collection;
26
27 import static org.apache.maven.surefire.util.internal.StringUtils.isBlank;
28
29 final class FileScanner
30 {
31 private final File basedir;
32
33 private final String ext;
34
35 FileScanner( File basedir, String ext )
36 {
37 this.basedir = basedir;
38 ext = ext.trim();
39 if ( isBlank( ext ) )
40 {
41 throw new IllegalArgumentException( "No file extension" );
42 }
43 this.ext = ext.startsWith( "." ) ? ext : "." + ext;
44 }
45
46 void scanTo( Collection<String> scannedJavaClassNames, TestFilter<String, String> filter )
47 {
48 scan( scannedJavaClassNames, filter, basedir );
49 }
50
51 private void scan( Collection<String> scannedJavaClassNames,
52 TestFilter<String, String> filter, File basedir, String... subDirectories )
53 {
54 File[] filesAndDirs = basedir.listFiles();
55 if ( filesAndDirs != null )
56 {
57 final String pAckage = toJavaPackage( subDirectories );
58 final String path = toPath( subDirectories );
59 final String ext = this.ext;
60 final boolean hasExtension = ext != null;
61 final int extLength = hasExtension ? ext.length() : 0;
62 for ( File fileOrDir : filesAndDirs )
63 {
64 String name = fileOrDir.getName();
65 if ( name.length() != 0 )
66 {
67 if ( fileOrDir.isFile() )
68 {
69 final int clsLength = name.length() - extLength;
70 if ( clsLength > 0
71 && ( !hasExtension || name.regionMatches( true, clsLength, ext, 0, extLength ) ) )
72 {
73 String simpleClassName = hasExtension ? name.substring( 0, clsLength ) : name;
74 if ( filter.shouldRun( toFile( path, simpleClassName ), null ) )
75 {
76 String fullyQualifiedClassName =
77 pAckage.length() == 0 ? simpleClassName : pAckage + '.' + simpleClassName;
78 scannedJavaClassNames.add( fullyQualifiedClassName );
79 }
80 }
81 }
82 else if ( fileOrDir.isDirectory() )
83 {
84 String[] paths = new String[subDirectories.length + 1];
85 System.arraycopy( subDirectories, 0, paths, 0, subDirectories.length );
86 paths[subDirectories.length] = name;
87 scan( scannedJavaClassNames, filter, fileOrDir, paths );
88 }
89 }
90 }
91 }
92 }
93
94 private static String toJavaPackage( String... subDirectories )
95 {
96 StringBuilder pkg = new StringBuilder();
97 for ( int i = 0; i < subDirectories.length; i++ )
98 {
99 if ( i > 0 && i < subDirectories.length )
100 {
101 pkg.append( '.' );
102 }
103 pkg.append( subDirectories[i] );
104 }
105 return pkg.toString();
106 }
107
108 private static String toPath( String... subDirectories )
109 {
110 StringBuilder pkg = new StringBuilder();
111 for ( int i = 0; i < subDirectories.length; i++ )
112 {
113 if ( i > 0 && i < subDirectories.length )
114 {
115 pkg.append( '/' );
116 }
117 pkg.append( subDirectories[i] );
118 }
119 return pkg.toString();
120 }
121
122 private String toFile( String path, String fileNameWithoutExtension )
123 {
124 String pathWithoutExtension =
125 path.length() == 0 ? fileNameWithoutExtension : path + '/' + fileNameWithoutExtension;
126 return pathWithoutExtension + ext;
127 }
128 }