Coverage Report - org.apache.maven.surefire.testng.TestNGExecutor
 
Classes in this File Line Coverage Branch Coverage Complexity
TestNGExecutor
0%
0/92
0%
0/26
6,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.io.File;
 23  
 import java.lang.reflect.Constructor;
 24  
 import java.lang.reflect.InvocationTargetException;
 25  
 import java.lang.reflect.Method;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 29  
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 30  
 import org.apache.maven.artifact.versioning.VersionRange;
 31  
 import org.apache.maven.surefire.booter.ProviderParameterNames;
 32  
 import org.apache.maven.surefire.report.RunListener;
 33  
 import org.apache.maven.surefire.testng.conf.Configurator;
 34  
 import org.apache.maven.surefire.testng.conf.TestNG4751Configurator;
 35  
 import org.apache.maven.surefire.testng.conf.TestNG52Configurator;
 36  
 import org.apache.maven.surefire.testng.conf.TestNG652Configurator;
 37  
 import org.apache.maven.surefire.testng.conf.TestNGMapConfigurator;
 38  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 39  
 import org.apache.maven.surefire.util.NestedRuntimeException;
 40  
 import org.apache.maven.surefire.util.internal.StringUtils;
 41  
 
 42  
 import org.testng.TestNG;
 43  
 
 44  
 /**
 45  
  * Contains utility methods for executing TestNG.
 46  
  *
 47  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 48  
  * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
 49  
  */
 50  
 public class TestNGExecutor
 51  
 {
 52  
 
 53  
 
 54  
     private TestNGExecutor()
 55  0
     {
 56  
         // noop
 57  0
     }
 58  
 
 59  
     public static void run( Class[] testClasses, String testSourceDirectory, Map options, ArtifactVersion version,
 60  
                             RunListener reportManager, TestNgTestSuite suite, File reportsDirectory,
 61  
                             final String methodNamePattern )
 62  
         throws TestSetFailedException
 63  
     {
 64  0
         TestNG testng = new TestNG( true );
 65  
 
 66  0
         applyGroupMatching( testng, options );
 67  0
         if ( !StringUtils.isBlank( methodNamePattern ) )
 68  
         {
 69  0
             applyMethodNameFiltering( testng, methodNamePattern );
 70  
         }
 71  
 
 72  0
         Configurator configurator = getConfigurator( version );
 73  0
         System.out.println( "Configuring TestNG with: " + configurator.getClass().getSimpleName() );
 74  0
         configurator.configure( testng, options );
 75  0
         postConfigure( testng, testSourceDirectory, reportManager, suite, reportsDirectory );
 76  0
         testng.setTestClasses( testClasses );
 77  0
         testng.run();
 78  0
     }
 79  
 
 80  
     private static void applyMethodNameFiltering( TestNG testng, String methodNamePattern )
 81  
         throws TestSetFailedException
 82  
     {
 83  
         // the class is available in the testClassPath
 84  0
         String clazzName = "org.apache.maven.surefire.testng.utils.MethodSelector";
 85  
         // looks to need a high value 
 86  0
         testng.addMethodSelector( clazzName, 10000 );
 87  
         try
 88  
         {
 89  0
             Class clazz = Class.forName( clazzName );
 90  
 
 91  0
             Method method = clazz.getMethod( "setMethodName", new Class[]{ String.class } );
 92  0
             method.invoke( null, new Object[]{ methodNamePattern } );
 93  
         }
 94  0
         catch ( ClassNotFoundException e )
 95  
         {
 96  0
             throw new TestSetFailedException( e.getMessage(), e );
 97  
         }
 98  0
         catch ( SecurityException e )
 99  
         {
 100  0
             throw new TestSetFailedException( e.getMessage(), e );
 101  
         }
 102  0
         catch ( NoSuchMethodException e )
 103  
         {
 104  0
             throw new TestSetFailedException( e.getMessage(), e );
 105  
         }
 106  0
         catch ( IllegalArgumentException e )
 107  
         {
 108  0
             throw new TestSetFailedException( e.getMessage(), e );
 109  
         }
 110  0
         catch ( IllegalAccessException e )
 111  
         {
 112  0
             throw new TestSetFailedException( e.getMessage(), e );
 113  
         }
 114  0
         catch ( InvocationTargetException e )
 115  
         {
 116  0
             throw new TestSetFailedException( e.getMessage(), e );
 117  0
         }
 118  0
     }
 119  
 
 120  
     private static void applyGroupMatching( TestNG testng, Map options )
 121  
         throws TestSetFailedException
 122  
     {
 123  0
         String groups = (String) options.get( ProviderParameterNames.TESTNG_GROUPS_PROP );
 124  0
         String excludedGroups = (String) options.get( ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP );
 125  
 
 126  0
         if ( groups == null && excludedGroups == null )
 127  
         {
 128  0
             return;
 129  
         }
 130  
 
 131  
         // the class is available in the testClassPath
 132  0
         String clazzName = "org.apache.maven.surefire.testng.utils.GroupMatcherMethodSelector";
 133  
         // looks to need a high value
 134  0
         testng.addMethodSelector( clazzName, 9999 );
 135  
         try
 136  
         {
 137  0
             Class clazz = Class.forName( clazzName );
 138  
 
 139  
             // HORRIBLE hack, but TNG doesn't allow us to setup a method selector instance directly.
 140  0
             Method method = clazz.getMethod( "setGroups", new Class[]{ String.class, String.class } );
 141  0
             method.invoke( null, new Object[]{ groups, excludedGroups } );
 142  
         }
 143  0
         catch ( ClassNotFoundException e )
 144  
         {
 145  0
             throw new TestSetFailedException( e.getMessage(), e );
 146  
         }
 147  0
         catch ( SecurityException e )
 148  
         {
 149  0
             throw new TestSetFailedException( e.getMessage(), e );
 150  
         }
 151  0
         catch ( NoSuchMethodException e )
 152  
         {
 153  0
             throw new TestSetFailedException( e.getMessage(), e );
 154  
         }
 155  0
         catch ( IllegalArgumentException e )
 156  
         {
 157  0
             throw new TestSetFailedException( e.getMessage(), e );
 158  
         }
 159  0
         catch ( IllegalAccessException e )
 160  
         {
 161  0
             throw new TestSetFailedException( e.getMessage(), e );
 162  
         }
 163  0
         catch ( InvocationTargetException e )
 164  
         {
 165  0
             throw new TestSetFailedException( e.getMessage(), e );
 166  0
         }
 167  0
     }
 168  
 
 169  
     public static void run( List suiteFiles, String testSourceDirectory, Map options, ArtifactVersion version,
 170  
                             RunListener reportManager, TestNgTestSuite suite, File reportsDirectory )
 171  
         throws TestSetFailedException
 172  
     {
 173  0
         TestNG testng = new TestNG( true );
 174  0
         Configurator configurator = getConfigurator( version );
 175  0
         configurator.configure( testng, options );
 176  0
         postConfigure( testng, testSourceDirectory, reportManager, suite, reportsDirectory );
 177  0
         testng.setTestSuites( suiteFiles );
 178  0
         testng.run();
 179  0
     }
 180  
 
 181  
     private static Configurator getConfigurator( ArtifactVersion version )
 182  
         throws TestSetFailedException
 183  
     {
 184  
         try
 185  
         {
 186  0
             VersionRange range = VersionRange.createFromVersionSpec( "[4.7,5.1]" );
 187  0
             if ( range.containsVersion( version ) )
 188  
             {
 189  0
                 return new TestNG4751Configurator();
 190  
             }
 191  0
             range = VersionRange.createFromVersionSpec( "[5.2]" );
 192  0
             if ( range.containsVersion( version ) )
 193  
             {
 194  0
                 return new TestNG52Configurator();
 195  
             }
 196  0
             range = VersionRange.createFromVersionSpec( "[5.3,6.4]" );
 197  0
             if ( range.containsVersion( version ) )
 198  
             {
 199  0
                 return new TestNGMapConfigurator();
 200  
             }
 201  0
             range = VersionRange.createFromVersionSpec( "[6.5,)" );
 202  0
             if ( range.containsVersion( version ) )
 203  
             {
 204  0
                 return new TestNG652Configurator();
 205  
             }
 206  
 
 207  0
             throw new TestSetFailedException( "Unknown TestNG version " + version );
 208  
         }
 209  0
         catch ( InvalidVersionSpecificationException invsex )
 210  
         {
 211  0
             throw new TestSetFailedException( "Bug in plugin. Please report it with the attached stacktrace", invsex );
 212  
         }
 213  
     }
 214  
 
 215  
 
 216  
     private static void postConfigure( TestNG testNG, String sourcePath, RunListener reportManager,
 217  
                                        TestNgTestSuite suite, File reportsDirectory )
 218  
         throws TestSetFailedException
 219  
     {
 220  
         // turn off all TestNG output
 221  0
         testNG.setVerbose( 0 );
 222  
 
 223  0
         TestNGReporter reporter = createTestNGReporter( reportManager, suite );
 224  0
         testNG.addListener( (Object) reporter );
 225  
 
 226  
         // FIXME: use classifier to decide if we need to pass along the source dir (onyl for JDK14)
 227  0
         if ( sourcePath != null )
 228  
         {
 229  0
             testNG.setSourcePath( sourcePath );
 230  
         }
 231  
 
 232  0
         testNG.setOutputDirectory( reportsDirectory.getAbsolutePath() );
 233  0
     }
 234  
 
 235  
     // If we have access to IResultListener, return a ConfigurationAwareTestNGReporter
 236  
     // But don't cause NoClassDefFoundErrors if it isn't available; just return a regular TestNGReporter instead
 237  
     private static TestNGReporter createTestNGReporter( RunListener reportManager, TestNgTestSuite suite )
 238  
     {
 239  
         try
 240  
         {
 241  0
             Class.forName( "org.testng.internal.IResultListener" );
 242  0
             Class c = Class.forName( "org.apache.maven.surefire.testng.ConfigurationAwareTestNGReporter" );
 243  
             try
 244  
             {
 245  0
                 Constructor ctor = c.getConstructor( new Class[]{ RunListener.class, TestNgTestSuite.class } );
 246  0
                 return (TestNGReporter) ctor.newInstance( new Object[]{ reportManager, suite } );
 247  
             }
 248  0
             catch ( Exception e )
 249  
             {
 250  0
                 throw new NestedRuntimeException( "Bug in ConfigurationAwareTestNGReporter", e );
 251  
             }
 252  
         }
 253  0
         catch ( ClassNotFoundException e )
 254  
         {
 255  0
             return new TestNGReporter( reportManager );
 256  
         }
 257  
     }
 258  
 
 259  
 }