Coverage Report - org.apache.maven.surefire.testng.TestNGDirectoryTestSuite
 
Classes in this File Line Coverage Branch Coverage Complexity
TestNGDirectoryTestSuite
0%
0/99
0%
0/40
2,929
 
 1  
 package org.apache.maven.surefire.testng;
 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.HashMap;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 import java.util.Properties;
 30  
 import java.util.SortedMap;
 31  
 import java.util.TreeMap;
 32  
 import org.apache.maven.surefire.NonAbstractClassFilter;
 33  
 import org.apache.maven.surefire.report.ConsoleOutputCapture;
 34  
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 35  
 import org.apache.maven.surefire.report.ReportEntry;
 36  
 import org.apache.maven.surefire.report.ReporterException;
 37  
 import org.apache.maven.surefire.report.ReporterFactory;
 38  
 import org.apache.maven.surefire.report.RunListener;
 39  
 import org.apache.maven.surefire.report.SimpleReportEntry;
 40  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 41  
 import org.apache.maven.surefire.util.RunOrderCalculator;
 42  
 import org.apache.maven.surefire.util.ScanResult;
 43  
 import org.apache.maven.surefire.util.TestsToRun;
 44  
 
 45  
 /**
 46  
  * Test suite for TestNG based on a directory of Java test classes. Can also execute JUnit tests.
 47  
  *
 48  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 49  
  * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
 50  
  */
 51  
 public class TestNGDirectoryTestSuite
 52  
     implements TestNgTestSuite
 53  
 {
 54  
 
 55  
     private final Map options;
 56  
 
 57  
     private final Map junitOptions;
 58  
 
 59  
     private final String testSourceDirectory;
 60  
 
 61  
     private final File reportsDirectory;
 62  
 
 63  
     private SortedMap<String, TestNGTestSet> testSets;
 64  
 
 65  
     private final ScanResult scanResult;
 66  
 
 67  
     private final String testMethodPattern;
 68  
 
 69  
     private final RunOrderCalculator runOrderCalculator;
 70  
 
 71  
     private final Class junitTestClass;
 72  
 
 73  
     public TestNGDirectoryTestSuite( String testSourceDirectory, Properties confOptions, File reportsDirectory, String testMethodPattern,
 74  
                                      RunOrderCalculator runOrderCalculator, ScanResult scanResult )
 75  0
     {
 76  
 
 77  0
         this.runOrderCalculator = runOrderCalculator;
 78  
 
 79  0
         this.options = confOptions;
 80  
 
 81  0
         this.testSourceDirectory = testSourceDirectory;
 82  0
         this.reportsDirectory = reportsDirectory;
 83  0
         this.scanResult = scanResult;
 84  0
         this.testMethodPattern = testMethodPattern;
 85  0
         this.junitTestClass = findJUnitTestClass();
 86  0
         this.junitOptions = createJUnitOptions();
 87  0
     }
 88  
 
 89  
     public void execute( TestsToRun testsToRun, ReporterFactory reporterManagerFactory )
 90  
         throws ReporterException, TestSetFailedException
 91  
     {
 92  
 
 93  0
         if ( !testsToRun.allowEagerReading() )
 94  
         {
 95  0
             executeLazy( testsToRun, reporterManagerFactory );
 96  
         }
 97  0
         else if ( testsToRun.containsAtLeast( 2 ) )
 98  
         {
 99  0
             executeMulti( testsToRun, reporterManagerFactory );
 100  
         }
 101  0
         else if ( testsToRun.containsAtLeast( 1 ) )
 102  
         {
 103  0
             Class testClass = testsToRun.iterator().next();
 104  0
             executeSingleClass( reporterManagerFactory, testClass );
 105  
         }
 106  0
     }
 107  
 
 108  
     private void executeSingleClass( ReporterFactory reporterManagerFactory, Class testClass )
 109  
         throws TestSetFailedException
 110  
     {
 111  0
         this.options.put( "suitename", testClass.getName() );
 112  
 
 113  0
         RunListener reporter = reporterManagerFactory.createReporter();
 114  0
         ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
 115  
 
 116  0
         startTestSuite( reporter, this );
 117  
 
 118  0
         final Map optionsToUse = isJUnitTest( testClass ) ? junitOptions : options;
 119  
 
 120  0
         TestNGExecutor.run( new Class[]{ testClass }, testSourceDirectory, optionsToUse, reporter, this,
 121  
                             reportsDirectory, testMethodPattern );
 122  
 
 123  0
         finishTestSuite( reporter, this );
 124  0
     }
 125  
 
 126  
     public void executeLazy( TestsToRun testsToRun, ReporterFactory reporterFactory )
 127  
         throws ReporterException, TestSetFailedException
 128  
     {
 129  
 
 130  0
         for ( Class c : testsToRun )
 131  
         {
 132  0
             executeSingleClass( reporterFactory, c );
 133  
         }
 134  0
     }
 135  
 
 136  
     private Class findJUnitTestClass()
 137  
     {
 138  
         Class junitTest;
 139  
         try
 140  
         {
 141  0
             junitTest = Class.forName( "junit.framework.Test" );
 142  
         }
 143  0
         catch ( ClassNotFoundException e )
 144  
         {
 145  0
             junitTest = null;
 146  0
         }
 147  0
         return junitTest;
 148  
     }
 149  
 
 150  
     public void executeMulti( TestsToRun testsToRun, ReporterFactory reporterFactory )
 151  
         throws ReporterException, TestSetFailedException
 152  
     {
 153  0
         List<Class> testNgTestClasses = new ArrayList<Class>();
 154  0
         List<Class> junitTestClasses = new ArrayList<Class>();
 155  0
         for ( Class c : testsToRun )
 156  
         {
 157  0
             if ( isJUnitTest( c ) )
 158  
             {
 159  0
                 junitTestClasses.add( c );
 160  
             }
 161  
             else
 162  
             {
 163  0
                 testNgTestClasses.add( c );
 164  
             }
 165  
         }
 166  
 
 167  0
         File testNgReportsDirectory = reportsDirectory, junitReportsDirectory = reportsDirectory;
 168  
 
 169  0
         if ( junitTestClasses.size() > 0 && testNgTestClasses.size() > 0 )
 170  
         {
 171  0
             testNgReportsDirectory = new File( reportsDirectory, "testng-native-results" );
 172  0
             junitReportsDirectory = new File( reportsDirectory, "testng-junit-results" );
 173  
         }
 174  
 
 175  0
         RunListener reporterManager = reporterFactory.createReporter();
 176  0
         ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporterManager );
 177  0
         startTestSuite( reporterManager, this );
 178  
 
 179  0
         Class[] testClasses = testNgTestClasses.toArray( new Class[testNgTestClasses.size()] );
 180  
 
 181  0
         TestNGExecutor.run( testClasses, this.testSourceDirectory, options, reporterManager, this,
 182  
                             testNgReportsDirectory, testMethodPattern );
 183  
 
 184  0
         if ( junitTestClasses.size() > 0 )
 185  
         {
 186  0
             testClasses = junitTestClasses.toArray( new Class[junitTestClasses.size()] );
 187  
 
 188  0
             TestNGExecutor.run( testClasses, testSourceDirectory, junitOptions, reporterManager, this,
 189  
                                 junitReportsDirectory, testMethodPattern );
 190  
         }
 191  
 
 192  0
         finishTestSuite( reporterManager, this );
 193  0
     }
 194  
 
 195  
     private boolean isJUnitTest( Class c )
 196  
     {
 197  0
         return junitTestClass != null && junitTestClass.isAssignableFrom( c );
 198  
     }
 199  
 
 200  
     private Map createJUnitOptions()
 201  
     {
 202  0
         Map junitOptions = new HashMap( this.options );
 203  0
         junitOptions.put( "junit", Boolean.TRUE );
 204  0
         return junitOptions;
 205  
     }
 206  
 
 207  
     // single class test
 208  
     public void execute( String testSetName, ReporterFactory reporterManagerFactory )
 209  
         throws ReporterException, TestSetFailedException
 210  
     {
 211  0
         if ( testSets == null )
 212  
         {
 213  0
             throw new IllegalStateException( "You must call locateTestSets before calling execute" );
 214  
         }
 215  0
         TestNGTestSet testSet = testSets.get( testSetName );
 216  
 
 217  0
         if ( testSet == null )
 218  
         {
 219  0
             throw new TestSetFailedException( "Unable to find test set '" + testSetName + "' in suite" );
 220  
         }
 221  
 
 222  0
         RunListener reporter = reporterManagerFactory.createReporter();
 223  0
         ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
 224  
 
 225  0
         startTestSuite( reporter, this );
 226  
 
 227  0
         TestNGExecutor.run( new Class[]{ testSet.getTestClass() }, this.testSourceDirectory, this.options, reporter, this, reportsDirectory, testMethodPattern );
 228  
 
 229  0
         finishTestSuite( reporter, this );
 230  0
     }
 231  
 
 232  
     public static void startTestSuite( RunListener reporter, Object suite )
 233  
     {
 234  0
         ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) );
 235  
 
 236  
         try
 237  
         {
 238  0
             reporter.testSetStarting( report );
 239  
         }
 240  0
         catch ( ReporterException e )
 241  
         {
 242  
             // TODO: remove this exception from the report manager
 243  0
         }
 244  0
     }
 245  
 
 246  
     public static void finishTestSuite( RunListener reporterManager, Object suite )
 247  
         throws ReporterException
 248  
     {
 249  0
         ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) );
 250  
 
 251  0
         reporterManager.testSetCompleted( report );
 252  0
     }
 253  
 
 254  
     public String getSuiteName()
 255  
     {
 256  0
         String result = (String) options.get( "suitename" );
 257  0
         if ( result == null )
 258  
         {
 259  0
             result = "TestSuite";
 260  
         }
 261  0
         return result;
 262  
     }
 263  
 
 264  
     private static String getSuiteName( Object suite )
 265  
     {
 266  
         String result;
 267  0
         if ( suite instanceof TestNGDirectoryTestSuite )
 268  
         {
 269  0
             return ( (TestNGDirectoryTestSuite) suite ).getSuiteName();
 270  
         }
 271  0
         else if ( suite instanceof TestNGXmlTestSuite )
 272  
         {
 273  0
             return ( (TestNGXmlTestSuite) suite ).getSuiteName();
 274  
         }
 275  
         else
 276  
         {
 277  0
             result = "TestSuite";
 278  
         }
 279  
 
 280  0
         return result;
 281  
     }
 282  
 
 283  
     public Map locateTestSets( ClassLoader classLoader )
 284  
         throws TestSetFailedException
 285  
     {
 286  0
         if ( testSets != null )
 287  
         {
 288  0
             throw new IllegalStateException( "You can't call locateTestSets twice" );
 289  
         }
 290  0
         testSets = new TreeMap<String, TestNGTestSet>();
 291  
 
 292  0
         final TestsToRun scanned = scanResult.applyFilter( new NonAbstractClassFilter(), classLoader );
 293  
 
 294  0
         final TestsToRun testsToRun = runOrderCalculator.orderTestClasses( scanned );
 295  
 
 296  0
         for ( Class testClass : testsToRun )
 297  
         {
 298  0
             TestNGTestSet testSet = new TestNGTestSet( testClass );
 299  
 
 300  0
             if ( testSets.containsKey( testSet.getName() ) )
 301  
             {
 302  0
                 throw new TestSetFailedException( "Duplicate test set '" + testSet.getName() + "'" );
 303  
             }
 304  0
             testSets.put( testSet.getName(), testSet );
 305  
 
 306  0
         }
 307  
 
 308  0
         return Collections.unmodifiableSortedMap( testSets );
 309  
     }
 310  
 
 311  
 }