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