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