22
22
import java.io.File;
23
23
import java.lang.reflect.Constructor;
24
import java.util.ArrayList;
24
import java.lang.reflect.InvocationTargetException;
25
import java.lang.reflect.Method;
25
26
import java.util.List;
26
27
import java.util.Map;
28
29
import org.apache.maven.artifact.versioning.ArtifactVersion;
29
30
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
30
31
import org.apache.maven.artifact.versioning.VersionRange;
31
import org.apache.maven.surefire.report.ReporterManager;
32
import org.apache.maven.surefire.suite.SurefireTestSuite;
32
import org.apache.maven.surefire.report.RunListener;
33
33
import org.apache.maven.surefire.testng.conf.Configurator;
34
34
import org.apache.maven.surefire.testng.conf.TestNG4751Configurator;
35
35
import org.apache.maven.surefire.testng.conf.TestNG52Configurator;
36
36
import org.apache.maven.surefire.testng.conf.TestNGMapConfigurator;
37
37
import org.apache.maven.surefire.testset.TestSetFailedException;
38
import org.testng.IReporter;
38
import org.apache.maven.surefire.util.NestedRuntimeException;
39
import org.apache.maven.surefire.util.internal.StringUtils;
39
40
import org.testng.TestNG;
47
48
public class TestNGExecutor
49
52
private TestNGExecutor()
53
57
public static void run( Class[] testClasses, String testSourceDirectory, Map options, ArtifactVersion version,
54
String classifier, ReporterManager reportManager, SurefireTestSuite suite, File reportsDirectory )
58
RunListener reportManager, TestNgTestSuite suite, File reportsDirectory, final String methodNamePattern )
55
59
throws TestSetFailedException
57
61
TestNG testng = new TestNG( true );
62
if (!StringUtils.isBlank( methodNamePattern ))
64
applyMethodNameFiltering( testng, methodNamePattern );
58
66
Configurator configurator = getConfigurator( version );
59
67
configurator.configure( testng, options );
60
postConfigure( testng, testSourceDirectory, classifier, reportManager, suite, reportsDirectory );
68
postConfigure( testng, testSourceDirectory, reportManager, suite, reportsDirectory );
61
69
testng.setTestClasses( testClasses );
73
private static void applyMethodNameFiltering(TestNG testng, String methodNamePattern)
74
throws TestSetFailedException
76
// the class is available in the testClassPath
77
String clazzName = "org.apache.maven.surefire.testng.utils.MethodSelector";
78
// looks to need a high value
79
testng.addMethodSelector( clazzName , 10000 );
82
Class clazz = Class.forName( clazzName );
84
Method method = clazz.getMethod( "setMethodName", new Class[]{String.class} );
85
method.invoke( null, new Object[]{methodNamePattern} );
87
catch ( ClassNotFoundException e )
89
throw new TestSetFailedException(e.getMessage(), e);
91
catch ( SecurityException e )
93
throw new TestSetFailedException(e.getMessage(), e);
95
catch ( NoSuchMethodException e )
97
throw new TestSetFailedException(e.getMessage(), e);
99
catch ( IllegalArgumentException e )
101
throw new TestSetFailedException(e.getMessage(), e);
103
catch ( IllegalAccessException e )
105
throw new TestSetFailedException(e.getMessage(), e);
107
catch ( InvocationTargetException e )
109
throw new TestSetFailedException(e.getMessage(), e);
65
113
public static void run( List suiteFiles, String testSourceDirectory, Map options, ArtifactVersion version,
66
String classifier, ReporterManager reportManager, SurefireTestSuite suite, File reportsDirectory )
114
RunListener reportManager, TestNgTestSuite suite, File reportsDirectory )
67
115
throws TestSetFailedException
69
117
TestNG testng = new TestNG( true );
70
118
Configurator configurator = getConfigurator( version );
71
119
configurator.configure( testng, options );
72
postConfigure( testng, testSourceDirectory, classifier, reportManager, suite, reportsDirectory );
120
postConfigure( testng, testSourceDirectory, reportManager, suite, reportsDirectory );
73
121
testng.setTestSuites( suiteFiles );
77
private static Configurator getConfigurator( ArtifactVersion version ) throws TestSetFailedException
125
private static Configurator getConfigurator( ArtifactVersion version )
126
throws TestSetFailedException
125
174
// If we have access to IResultListener, return a ConfigurationAwareTestNGReporter
126
175
// But don't cause NoClassDefFoundErrors if it isn't available; just return a regular TestNGReporter instead
127
private static TestNGReporter createTestNGReporter( ReporterManager reportManager, SurefireTestSuite suite ) {
176
private static TestNGReporter createTestNGReporter( RunListener reportManager, TestNgTestSuite suite )
129
180
Class.forName( "org.testng.internal.IResultListener" );
130
181
Class c = Class.forName( "org.apache.maven.surefire.testng.ConfigurationAwareTestNGReporter" );
133
Constructor ctor = c.getConstructor( new Class[] { ReporterManager.class, SurefireTestSuite.class } );
134
return (TestNGReporter) ctor.newInstance( new Object[] { reportManager, suite } );
184
Constructor ctor = c.getConstructor( new Class[]{ RunListener.class, TestNgTestSuite.class } );
185
return (TestNGReporter) ctor.newInstance( new Object[]{ reportManager, suite } );
136
187
catch ( Exception e )
138
throw new RuntimeException("Bug in ConfigurationAwareTestNGReporter", e);
189
throw new NestedRuntimeException( "Bug in ConfigurationAwareTestNGReporter", e );
140
} catch (ClassNotFoundException e) {
192
catch ( ClassNotFoundException e )
141
194
return new TestNGReporter( reportManager );
145
private static void attachNonStandardReporter( TestNG testNG, String className )
149
Class c = Class.forName( className );
150
if (IReporter.class.isAssignableFrom( c )) {
151
testNG.addListener( c.newInstance() );
154
catch ( Exception e ) {} // ignore