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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.maven.surefire.util.DefaultScanResult;
28  
29  /**
30   * Scans directories looking for tests.
31   *
32   * @author Karl M. Davis
33   * @author Kristian Rosenvold
34   */
35  public class DirectoryScanner
36  {
37  
38      private static final String FS = System.getProperty( "file.separator" );
39  
40      private static final String JAVA_SOURCE_FILE_EXTENSION = ".java";
41  
42      private static final String JAVA_CLASS_FILE_EXTENSION = ".class";
43  
44      private final File basedir;
45  
46      private final List<String> includes;
47  
48      private final List<String> excludes;
49  
50      private final List<String> specificTests;
51  
52      public DirectoryScanner( File basedir, List<String> includes, List<String> excludes, List<String> specificTests )
53      {
54          this.basedir = basedir;
55          this.includes = includes;
56          this.excludes = excludes;
57          this.specificTests = specificTests;
58      }
59  
60      public DefaultScanResult scan()
61      {
62          String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
63          SpecificFileFilter specificTestFilter = new SpecificFileFilter( specific );
64  
65          List<String> result = new ArrayList<String>();
66          if ( basedir.exists() )
67          {
68              org.apache.maven.shared.utils.io.DirectoryScanner scanner =
69                  new org.apache.maven.shared.utils.io.DirectoryScanner();
70  
71              scanner.setBasedir( basedir );
72  
73              if ( includes != null )
74              {
75                  scanner.setIncludes( processIncludesExcludes( includes ) );
76              }
77  
78              if ( excludes != null )
79              {
80                  scanner.setExcludes( processIncludesExcludes( excludes ) );
81              }
82  
83              scanner.scan();
84              for ( String test : scanner.getIncludedFiles() )
85              {
86                  if ( specificTestFilter.accept( stripBaseDir( basedir.getAbsolutePath(), test ) ) )
87                  {
88                      result.add( convertToJavaClassName( test ) );
89                  }
90              }
91          }
92          return new DefaultScanResult( result );
93      }
94  
95      private String convertToJavaClassName( String test )
96      {
97          return StringUtils.removeEnd( test, ".class" ).replace( FS, "." );
98      }
99  
100     private String stripBaseDir( String basedir, String test )
101     {
102         return StringUtils.removeStart( test, basedir );
103     }
104 
105     private static String[] processIncludesExcludes( List<String> list )
106     {
107         List<String> newList = new ArrayList<String>();
108         for ( Object aList : list )
109         {
110             String include = (String) aList;
111             String[] includes = include.split( "," );
112             Collections.addAll( newList, includes );
113         }
114 
115         String[] incs = new String[newList.size()];
116 
117         for ( int i = 0; i < incs.length; i++ )
118         {
119             String inc = newList.get( i );
120             if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) )
121             {
122                 inc = StringUtils.removeEnd( inc, JAVA_SOURCE_FILE_EXTENSION ) + JAVA_CLASS_FILE_EXTENSION;
123             }
124             incs[i] = inc;
125 
126         }
127         return incs;
128     }
129 }