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