View Javadoc
1   package org.apache.maven.plugin.surefire.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.isEmpty() )
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.isEmpty() ? 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.isEmpty() ? fileNameWithoutExtension : path + '/' + fileNameWithoutExtension;
126         return pathWithoutExtension + ext;
127     }
128 }