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