Coverage Report - org.apache.maven.plugin.surefire.util.DirectoryScanner
 
Classes in this File Line Coverage Branch Coverage Complexity
DirectoryScanner
100%
37/37
66%
12/18
2,8
 
 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  1
     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  1
     {
 54  1
         this.basedir = basedir;
 55  1
         this.includes = includes;
 56  1
         this.excludes = excludes;
 57  1
         this.specificTests = specificTests;
 58  1
     }
 59  
 
 60  
     public DefaultScanResult scan()
 61  
     {
 62  1
         String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
 63  1
         SpecificFileFilter specificTestFilter = new SpecificFileFilter( specific );
 64  
 
 65  1
         List<String> result = new ArrayList<String>();
 66  1
         if ( basedir.exists() )
 67  
         {
 68  1
             org.apache.maven.shared.utils.io.DirectoryScanner scanner =
 69  
                 new org.apache.maven.shared.utils.io.DirectoryScanner();
 70  
 
 71  1
             scanner.setBasedir( basedir );
 72  
 
 73  1
             if ( includes != null )
 74  
             {
 75  1
                 scanner.setIncludes( processIncludesExcludes( includes ) );
 76  
             }
 77  
 
 78  1
             if ( excludes != null )
 79  
             {
 80  1
                 scanner.setExcludes( processIncludesExcludes( excludes ) );
 81  
             }
 82  
 
 83  1
             scanner.scan();
 84  4
             for ( String test : scanner.getIncludedFiles() )
 85  
             {
 86  3
                 if ( specificTestFilter.accept( stripBaseDir( basedir.getAbsolutePath(), test ) ) )
 87  
                 {
 88  3
                     result.add( convertToJavaClassName( test ) );
 89  
                 }
 90  
             }
 91  
         }
 92  1
         return new DefaultScanResult( result );
 93  
     }
 94  
 
 95  
     private String convertToJavaClassName( String test )
 96  
     {
 97  3
         return StringUtils.removeEnd( test, ".class" ).replace( FS, "." );
 98  
     }
 99  
 
 100  
     private String stripBaseDir( String basedir, String test )
 101  
     {
 102  3
         return StringUtils.removeStart( test, basedir );
 103  
     }
 104  
 
 105  
     private static String[] processIncludesExcludes( List<String> list )
 106  
     {
 107  3
         List<String> newList = new ArrayList<String>();
 108  3
         for ( Object aList : list )
 109  
         {
 110  1
             String include = (String) aList;
 111  1
             String[] includes = include.split( "," );
 112  1
             Collections.addAll( newList, includes );
 113  1
         }
 114  
 
 115  3
         String[] incs = new String[newList.size()];
 116  
 
 117  4
         for ( int i = 0; i < incs.length; i++ )
 118  
         {
 119  1
             String inc = newList.get( i );
 120  1
             if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) )
 121  
             {
 122  1
                 inc = StringUtils.removeEnd( inc, JAVA_SOURCE_FILE_EXTENSION ) + JAVA_CLASS_FILE_EXTENSION;
 123  
             }
 124  1
             incs[i] = inc;
 125  
 
 126  
         }
 127  3
         return incs;
 128  
     }
 129  
 }