Coverage Report - org.apache.maven.surefire.util.SurefireDirectoryScanner
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireDirectoryScanner
0%
0/52
0%
0/22
3.143
SurefireDirectoryScanner$TestSetCreator
N/A
N/A
3.143
 
 1  
 package org.apache.maven.surefire.util;
 2  
 /*
 3  
  * Licensed to the Apache Software Foundation (ASF) under one
 4  
  * or more contributor license agreements.  See the NOTICE file
 5  
  * distributed with this work for additional information
 6  
  * regarding copyright ownership.  The ASF licenses this file
 7  
  * to you under the Apache License, Version 2.0 (the
 8  
  * "License"); you may not use this file except in compliance
 9  
  * with the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *     http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing,
 14  
  * software distributed under the License is distributed on an
 15  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16  
  * KIND, either express or implied.  See the License for the
 17  
  * specific language governing permissions and limitations
 18  
  * under the License.
 19  
  */
 20  
 
 21  
 import org.codehaus.plexus.util.StringUtils;
 22  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 23  
 import org.apache.maven.surefire.testset.SurefireTestSet;
 24  
 
 25  
 import java.util.*;
 26  
 import java.io.File;
 27  
 import java.lang.reflect.Modifier;
 28  
 
 29  
 /**
 30  
  * Scans directories looking for tests.
 31  
  * @author Karl M. Davis
 32  
  * @author Kristian Rosenvold
 33  
  */
 34  
 public class SurefireDirectoryScanner {
 35  
 
 36  0
     private static final String FS = System.getProperty( "file.separator" );
 37  0
     private static final String[] EMPTY_STRING_ARRAY = new String[0];
 38  
 
 39  
     private final File basedir;
 40  
 
 41  
     private final List includes;
 42  
 
 43  
     private final List excludes;
 44  
 
 45  
     protected Map testSets;
 46  
 
 47  
     private int totalTests;
 48  
 
 49  
 
 50  
     public Map getTestSets() {
 51  0
         return testSets;
 52  
     }
 53  
 
 54  0
     public SurefireDirectoryScanner(File basedir, List includes, List excludes) {
 55  0
         this.basedir = basedir;
 56  0
         this.includes = includes;
 57  0
         this.excludes = excludes;
 58  0
     }
 59  
 
 60  
     public interface TestSetCreator{
 61  
         SurefireTestSet createTestSet(Class clazz);
 62  
     }
 63  
 
 64  
     public Map locateTestSets( ClassLoader classLoader, TestSetCreator testSetCreator )
 65  
         throws TestSetFailedException
 66  
     {
 67  0
         if ( testSets != null )
 68  
         {
 69  0
             throw new IllegalStateException( "You can't call locateTestSets twice" );
 70  
         }
 71  0
         testSets = new HashMap();
 72  
 
 73  0
         Class[] locatedClasses = locateTestClasses( classLoader);
 74  
 
 75  0
         for ( int i = 0; i < locatedClasses.length; i++ )
 76  
         {
 77  0
             Class testClass = locatedClasses[i];
 78  0
             SurefireTestSet testSet = testSetCreator.createTestSet( testClass);
 79  
 
 80  0
                 if ( testSet == null )
 81  
                 {
 82  0
                     continue;
 83  
                 }
 84  
 
 85  0
                 if ( testSets.containsKey( testSet.getName() ) )
 86  
                 {
 87  0
                     throw new TestSetFailedException( "Duplicate test set '" + testSet.getName() + "'" );
 88  
                 }
 89  0
                 testSets.put( testSet.getName(), testSet );
 90  
 
 91  0
                 totalTests++;
 92  
         }
 93  
 
 94  0
         return Collections.unmodifiableMap( testSets );
 95  
     }
 96  
 
 97  
     public Class[] locateTestClasses( ClassLoader classLoader)
 98  
         throws TestSetFailedException
 99  
     {
 100  0
         String[] testClassNames =   collectTests( );
 101  0
         List result = new ArrayList();
 102  
 
 103  0
         for ( int i = 0; i < testClassNames.length; i++ )
 104  
         {
 105  0
             String className = testClassNames[i];
 106  
 
 107  
             Class testClass;
 108  
             try
 109  
             {
 110  0
                 testClass = classLoader.loadClass( className );
 111  
             }
 112  0
             catch ( ClassNotFoundException e )
 113  
             {
 114  0
                 throw new TestSetFailedException( "Unable to create test class '" + className + "'", e );
 115  0
             }
 116  
 
 117  0
             if ( !Modifier.isAbstract( testClass.getModifiers() ) )
 118  
             {
 119  
 
 120  0
                 result.add( testClass);
 121  
             }
 122  
         }
 123  0
         return (Class[]) result.toArray(new Class[result.size()]);
 124  
     }
 125  
 
 126  
 
 127  
     String[] collectTests( )
 128  
     {
 129  0
         String[] tests = EMPTY_STRING_ARRAY;
 130  0
         if ( basedir.exists() )
 131  
         {
 132  0
             org.codehaus.plexus.util.DirectoryScanner scanner = new org.codehaus.plexus.util.DirectoryScanner();
 133  
 
 134  0
             scanner.setBasedir( basedir );
 135  
 
 136  0
             if ( includes != null )
 137  
             {
 138  0
                 scanner.setIncludes( processIncludesExcludes( includes ) );
 139  
             }
 140  
 
 141  0
             if ( excludes != null )
 142  
             {
 143  0
                 scanner.setExcludes( processIncludesExcludes( excludes ) );
 144  
             }
 145  
 
 146  0
             scanner.scan();
 147  
 
 148  0
             tests = scanner.getIncludedFiles();
 149  0
             for ( int i = 0; i < tests.length; i++ )
 150  
             {
 151  0
                 String test = tests[i];
 152  0
                 test = test.substring( 0, test.indexOf( "." ) );
 153  0
                 tests[i] = test.replace( FS.charAt( 0 ), '.' );
 154  
             }
 155  
         }
 156  0
         return tests;
 157  
     }
 158  
 
 159  
     private static String[] processIncludesExcludes( List list )
 160  
     {
 161  0
         String[] incs = new String[list.size()];
 162  
 
 163  0
         for ( int i = 0; i < incs.length; i++ )
 164  
         {
 165  0
             incs[i] = StringUtils.replace( (String) list.get( i ), "java", "class" );
 166  
 
 167  
         }
 168  0
         return incs;
 169  
     }
 170  
 
 171  
 
 172  
 
 173  
 }