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