Coverage Report - org.apache.maven.surefire.util.DefaultDirectoryScanner
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultDirectoryScanner
68%
37/54
41%
10/24
3,8
 
 1  
 package org.apache.maven.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 org.apache.maven.surefire.SpecificTestClassFilter;
 23  
 
 24  
 import java.io.File;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collections;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * Scans directories looking for tests.
 31  
  *
 32  
  * @author Karl M. Davis
 33  
  * @author Kristian Rosenvold
 34  
  */
 35  
 public class DefaultDirectoryScanner
 36  
     implements DirectoryScanner
 37  
 {
 38  
 
 39  1
     private static final String FS = System.getProperty( "file.separator" );
 40  
 
 41  1
     private static final String[] EMPTY_STRING_ARRAY = new String[0];
 42  
 
 43  
     private static final String JAVA_SOURCE_FILE_EXTENSION = ".java";
 44  
 
 45  
     private static final String JAVA_CLASS_FILE_EXTENSION = ".class";
 46  
 
 47  
     private final File basedir;
 48  
 
 49  
     private final List includes;
 50  
 
 51  
     private final List excludes;
 52  
 
 53  
     private final List specificTests;
 54  
 
 55  1
     private final List<Class> classesSkippedByValidation = new ArrayList<Class>();
 56  
 
 57  
     public DefaultDirectoryScanner( File basedir, List includes, List excludes, List specificTests )
 58  1
     {
 59  1
         this.basedir = basedir;
 60  1
         this.includes = includes;
 61  1
         this.excludes = excludes;
 62  1
         this.specificTests = specificTests;
 63  1
     }
 64  
 
 65  
     public TestsToRun locateTestClasses( ClassLoader classLoader, ScannerFilter scannerFilter )
 66  
     {
 67  0
         String[] testClassNames = collectTests();
 68  0
         List<Class> result = new ArrayList<Class>();
 69  
 
 70  0
         String[] specific = specificTests == null ? new String[0] : processIncludesExcludes( specificTests );
 71  0
         SpecificTestClassFilter specificTestFilter = new SpecificTestClassFilter( specific );
 72  
 
 73  0
         for ( String className : testClassNames )
 74  
         {
 75  0
             Class testClass = loadClass( classLoader, className );
 76  
 
 77  0
             if ( !specificTestFilter.accept( testClass ) )
 78  
             {
 79  
                 // FIXME: Log this somehow!
 80  0
                 continue;
 81  
             }
 82  
 
 83  0
             if ( scannerFilter == null || scannerFilter.accept( testClass ) )
 84  
             {
 85  0
                 result.add( testClass );
 86  
             }
 87  
             else
 88  
             {
 89  0
                 classesSkippedByValidation.add( testClass );
 90  
             }
 91  
         }
 92  
 
 93  0
         return new TestsToRun( result );
 94  
     }
 95  
 
 96  
     private static Class loadClass( ClassLoader classLoader, String className )
 97  
     {
 98  
         Class testClass;
 99  
         try
 100  
         {
 101  0
             testClass = classLoader.loadClass( className );
 102  
         }
 103  0
         catch ( ClassNotFoundException e )
 104  
         {
 105  0
             throw new NestedRuntimeException( "Unable to create test class '" + className + "'", e );
 106  0
         }
 107  0
         return testClass;
 108  
     }
 109  
 
 110  
     String[] collectTests()
 111  
     {
 112  1
         String[] tests = EMPTY_STRING_ARRAY;
 113  1
         if ( basedir.exists() )
 114  
         {
 115  1
             org.apache.maven.shared.utils.io.DirectoryScanner scanner =
 116  
                 new org.apache.maven.shared.utils.io.DirectoryScanner();
 117  
 
 118  1
             scanner.setBasedir( basedir );
 119  
 
 120  1
             if ( includes != null )
 121  
             {
 122  1
                 scanner.setIncludes( processIncludesExcludes( includes ) );
 123  
             }
 124  
 
 125  1
             if ( excludes != null )
 126  
             {
 127  1
                 scanner.setExcludes( processIncludesExcludes( excludes ) );
 128  
             }
 129  
 
 130  1
             scanner.scan();
 131  
 
 132  1
             tests = scanner.getIncludedFiles();
 133  5
             for ( int i = 0; i < tests.length; i++ )
 134  
             {
 135  4
                 String test = tests[i];
 136  4
                 test = test.substring( 0, test.indexOf( "." ) );
 137  4
                 tests[i] = test.replace( FS.charAt( 0 ), '.' );
 138  
             }
 139  
         }
 140  1
         return tests;
 141  
     }
 142  
 
 143  
     private static String[] processIncludesExcludes( List list )
 144  
     {
 145  2
         List<String> newList = new ArrayList<String>();
 146  2
         for ( Object aList : list )
 147  
         {
 148  1
             String include = (String) aList;
 149  1
             String[] includes = include.split( "," );
 150  1
             Collections.addAll( newList, includes );
 151  1
         }
 152  
 
 153  2
         String[] incs = new String[newList.size()];
 154  
 
 155  3
         for ( int i = 0; i < incs.length; i++ )
 156  
         {
 157  1
             String inc = newList.get( i );
 158  1
             if ( inc.endsWith( JAVA_SOURCE_FILE_EXTENSION ) )
 159  
             {
 160  1
                 inc = inc.substring( 0, inc.lastIndexOf( JAVA_SOURCE_FILE_EXTENSION ) ) + JAVA_CLASS_FILE_EXTENSION;
 161  
             }
 162  1
             incs[i] = inc;
 163  
 
 164  
         }
 165  2
         return incs;
 166  
     }
 167  
 
 168  
 }