Coverage Report - org.apache.maven.surefire.testng.TestNGProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
TestNGProvider
0%
0/38
0%
0/20
2,5
 
 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.util.Iterator;
 23  
 import java.util.Properties;
 24  
 import org.apache.maven.surefire.providerapi.AbstractProvider;
 25  
 import org.apache.maven.surefire.providerapi.ProviderParameters;
 26  
 import org.apache.maven.surefire.report.ReporterConfiguration;
 27  
 import org.apache.maven.surefire.report.ReporterException;
 28  
 import org.apache.maven.surefire.report.ReporterFactory;
 29  
 import org.apache.maven.surefire.suite.RunResult;
 30  
 import org.apache.maven.surefire.testset.TestRequest;
 31  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 32  
 import org.apache.maven.surefire.util.NestedRuntimeException;
 33  
 import org.apache.maven.surefire.util.RunOrderCalculator;
 34  
 import org.apache.maven.surefire.util.ScanResult;
 35  
 import org.apache.maven.surefire.util.TestsToRun;
 36  
 
 37  
 /**
 38  
  * @author Kristian Rosenvold
 39  
  * @noinspection UnusedDeclaration
 40  
  */
 41  
 public class TestNGProvider
 42  
     extends AbstractProvider
 43  
 {
 44  
     private final Properties providerProperties;
 45  
 
 46  
     private final ReporterConfiguration reporterConfiguration;
 47  
 
 48  
     private final ClassLoader testClassLoader;
 49  
 
 50  
     private final ScanResult scanResult;
 51  
 
 52  
     private final TestRequest testRequest;
 53  
 
 54  
     private final ProviderParameters providerParameters;
 55  
 
 56  
     private TestsToRun testsToRun;
 57  
 
 58  
     private final RunOrderCalculator runOrderCalculator;
 59  
 
 60  
     public TestNGProvider( ProviderParameters booterParameters )
 61  0
     {
 62  0
         this.providerParameters = booterParameters;
 63  0
         this.testClassLoader = booterParameters.getTestClassLoader();
 64  0
         this.runOrderCalculator = booterParameters.getRunOrderCalculator();
 65  0
         this.providerProperties = booterParameters.getProviderProperties();
 66  0
         this.testRequest = booterParameters.getTestRequest();
 67  0
         reporterConfiguration = booterParameters.getReporterConfiguration();
 68  0
         this.scanResult = booterParameters.getScanResult();
 69  0
     }
 70  
 
 71  
     public Boolean isRunnable()
 72  
     {
 73  0
         return Boolean.TRUE;
 74  
     }
 75  
 
 76  
     public RunResult invoke( Object forkTestSet )
 77  
         throws TestSetFailedException, ReporterException
 78  
     {
 79  
 
 80  0
         final ReporterFactory reporterFactory = providerParameters.getReporterFactory();
 81  
 
 82  0
         if ( isTestNGXmlTestSuite( testRequest ) )
 83  
         {
 84  0
             TestNGXmlTestSuite testNGXmlTestSuite = getXmlSuite();
 85  0
             testNGXmlTestSuite.locateTestSets( testClassLoader );
 86  0
             if ( forkTestSet != null && testRequest == null )
 87  
             {
 88  0
                 testNGXmlTestSuite.execute( (String) forkTestSet, reporterFactory );
 89  
             }
 90  
             else
 91  
             {
 92  0
                 testNGXmlTestSuite.execute( reporterFactory );
 93  
             }
 94  0
         }
 95  
         else
 96  
         {
 97  0
             if ( testsToRun == null )
 98  
             {
 99  0
                 if ( forkTestSet instanceof TestsToRun )
 100  
                 {
 101  0
                     testsToRun = (TestsToRun) forkTestSet;
 102  
                 }
 103  0
                 else if ( forkTestSet instanceof Class )
 104  
                 {
 105  0
                     testsToRun = TestsToRun.fromClass( (Class) forkTestSet );
 106  
                 }
 107  
                 else
 108  
                 {
 109  0
                     testsToRun = scanClassPath();
 110  
                 }
 111  
             }
 112  0
             TestNGDirectoryTestSuite suite = getDirectorySuite();
 113  0
             suite.execute( testsToRun, reporterFactory );
 114  
         }
 115  
 
 116  0
         return reporterFactory.close();
 117  
     }
 118  
 
 119  
     boolean isTestNGXmlTestSuite( TestRequest testSuiteDefinition )
 120  
     {
 121  0
         return testSuiteDefinition.getSuiteXmlFiles() != null && testSuiteDefinition.getSuiteXmlFiles().size() > 0 &&
 122  
             testSuiteDefinition.getRequestedTest() == null;
 123  
 
 124  
     }
 125  
 
 126  
 
 127  
     private TestNGDirectoryTestSuite getDirectorySuite()
 128  
     {
 129  0
         return new TestNGDirectoryTestSuite( testRequest.getTestSourceDirectory().toString(), providerProperties,
 130  
                                              reporterConfiguration.getReportsDirectory(),
 131  
                                              testRequest.getRequestedTestMethod(), runOrderCalculator, scanResult );
 132  
     }
 133  
 
 134  
     private TestNGXmlTestSuite getXmlSuite()
 135  
     {
 136  0
         return new TestNGXmlTestSuite( testRequest.getSuiteXmlFiles(), testRequest.getTestSourceDirectory().toString(),
 137  
                                        providerProperties,
 138  
                                        reporterConfiguration.getReportsDirectory() );
 139  
     }
 140  
 
 141  
 
 142  
     public Iterator getSuites()
 143  
     {
 144  0
         if ( isTestNGXmlTestSuite( testRequest ) )
 145  
         {
 146  
             try
 147  
             {
 148  0
                 return getXmlSuite().locateTestSets( testClassLoader ).keySet().iterator();
 149  
             }
 150  0
             catch ( TestSetFailedException e )
 151  
             {
 152  0
                 throw new NestedRuntimeException( e );
 153  
             }
 154  
         }
 155  
         else
 156  
         {
 157  0
             testsToRun = scanClassPath();
 158  0
             return testsToRun.iterator();
 159  
         }
 160  
     }
 161  
 
 162  
     private TestsToRun scanClassPath()
 163  
     {
 164  0
         final TestsToRun scanned = scanResult.applyFilter( null, testClassLoader );
 165  0
         return runOrderCalculator.orderTestClasses( scanned );
 166  
     }
 167  
 
 168  
 
 169  
 }