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 static org.apache.maven.plugin.surefire.util.ScannerUtil.convertSlashToSystemFileSeparator;
23  import static org.apache.maven.plugin.surefire.util.ScannerUtil.convertToJavaClassName;
24  import static org.apache.maven.plugin.surefire.util.ScannerUtil.processIncludesExcludes;
25  import static org.apache.maven.plugin.surefire.util.ScannerUtil.stripBaseDir;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import org.apache.maven.surefire.util.DefaultScanResult;
32  
33  /**
34   * Scans directories looking for tests.
35   *
36   * @author Karl M. Davis
37   * @author Kristian Rosenvold
38   */
39  public class DirectoryScanner
40  {
41      private final File basedir;
42  
43      private final List<String> includes;
44  
45      private final List<String> excludes;
46  
47      private final List<String> specificTests;
48  
49      public DirectoryScanner( File basedir, List<String> includes, List<String> excludes, List<String> specificTests )
50      {
51          this.basedir = basedir;
52          this.includes = includes;
53          this.excludes = excludes;
54          this.specificTests = specificTests;
55      }
56  
57      public DefaultScanResult scan()
58      {
59          String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
60          SpecificFileFilter specificTestFilter = new SpecificFileFilter( specific );
61  
62          List<String> result = new ArrayList<String>();
63          if ( basedir.exists() )
64          {
65              org.apache.maven.shared.utils.io.DirectoryScanner scanner =
66                  new org.apache.maven.shared.utils.io.DirectoryScanner();
67  
68              scanner.setBasedir( basedir );
69  
70              if ( includes != null )
71              {
72                  scanner.setIncludes( processIncludesExcludes( includes ) );
73              }
74  
75              if ( excludes != null )
76              {
77                  scanner.setExcludes( processIncludesExcludes( excludes ) );
78              }
79  
80              scanner.scan();
81              for ( String test : scanner.getIncludedFiles() )
82              {
83                  if ( specificTestFilter.accept(
84                           convertSlashToSystemFileSeparator(
85                                stripBaseDir( basedir.getAbsolutePath(), test ) ) ) )
86                  {
87                      result.add( convertToJavaClassName( test ) );
88                  }
89              }
90          }
91          return new DefaultScanResult( result );
92      }
93  }