~ubuntu-branches/ubuntu/precise/surefire/precise

« back to all changes in this revision

Viewing changes to surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2011-10-10 20:42:16 UTC
  • mfrom: (2.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20111010204216-cemva69wkagf4fay
Tags: 2.10-1
* Team upload.
* New upstream release.
* Refresh and remove unneccesary patches.
* Add Build-Depends on libsurefire-java and
  libmaven-common-artifact-filters-java.
* Drop outdated Maven artifact surefire-junit.
* Provide new Maven artifacts: surefire-junit3, maven-surefire-common,
  common-junit3, common-junit4, surefire-junit47 and surefire-testng-utils.
* Fix clean target to allow "two in a row" builds.
* Update Vcs-Browser field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
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;
27
28
 
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;
40
41
 
41
42
/**
46
47
 */
47
48
public class TestNGExecutor
48
49
{
 
50
    
 
51
   
49
52
    private TestNGExecutor()
50
53
    {
 
54
        // noop
51
55
    }
52
56
 
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
56
60
    {
57
61
        TestNG testng = new TestNG( true );
 
62
        if (!StringUtils.isBlank( methodNamePattern ))
 
63
        {
 
64
            applyMethodNameFiltering( testng, methodNamePattern );
 
65
        } 
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 );
62
70
        testng.run();
63
71
    }
64
72
 
 
73
    private static void applyMethodNameFiltering(TestNG testng, String methodNamePattern)
 
74
        throws TestSetFailedException
 
75
    {
 
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 );
 
80
        try
 
81
        {
 
82
            Class clazz = Class.forName( clazzName );
 
83
 
 
84
            Method method = clazz.getMethod( "setMethodName", new Class[]{String.class} );
 
85
            method.invoke( null, new Object[]{methodNamePattern} );
 
86
        }
 
87
        catch ( ClassNotFoundException e )
 
88
        {
 
89
            throw new TestSetFailedException(e.getMessage(), e);
 
90
        }
 
91
        catch ( SecurityException e )
 
92
        {
 
93
            throw new TestSetFailedException(e.getMessage(), e);
 
94
        }
 
95
        catch ( NoSuchMethodException e )
 
96
        {
 
97
            throw new TestSetFailedException(e.getMessage(), e);
 
98
        }
 
99
        catch ( IllegalArgumentException e )
 
100
        {
 
101
            throw new TestSetFailedException(e.getMessage(), e);
 
102
        }
 
103
        catch ( IllegalAccessException e )
 
104
        {
 
105
            throw new TestSetFailedException(e.getMessage(), e);
 
106
        }
 
107
        catch ( InvocationTargetException e )
 
108
        {
 
109
            throw new TestSetFailedException(e.getMessage(), e);
 
110
        }        
 
111
    }
 
112
    
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
68
116
    {
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 );
74
122
        testng.run();
75
123
    }
76
124
 
77
 
    private static Configurator getConfigurator( ArtifactVersion version ) throws TestSetFailedException
 
125
    private static Configurator getConfigurator( ArtifactVersion version )
 
126
        throws TestSetFailedException
78
127
    {
79
128
        try
80
129
        {
103
152
    }
104
153
 
105
154
 
106
 
    private static void postConfigure( TestNG testNG, String sourcePath, String classifier, 
107
 
                                       ReporterManager reportManager, SurefireTestSuite suite, File reportsDirectory )
 
155
    private static void postConfigure( TestNG testNG, String sourcePath, RunListener reportManager, TestNgTestSuite suite,
 
156
                                       File reportsDirectory )
108
157
        throws TestSetFailedException
109
158
    {
110
159
        // turn off all TestNG output
112
161
 
113
162
        TestNGReporter reporter = createTestNGReporter( reportManager, suite );
114
163
        testNG.addListener( (Object) reporter );
115
 
        
 
164
 
116
165
        // FIXME: use classifier to decide if we need to pass along the source dir (onyl for JDK14)
117
166
        if ( sourcePath != null )
118
167
        {
124
173
 
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 ) {
128
 
        try {
 
176
    private static TestNGReporter createTestNGReporter( RunListener reportManager, TestNgTestSuite suite )
 
177
    {
 
178
        try
 
179
        {
129
180
            Class.forName( "org.testng.internal.IResultListener" );
130
181
            Class c = Class.forName( "org.apache.maven.surefire.testng.ConfigurationAwareTestNGReporter" );
131
182
            try
132
183
            {
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 } );
135
186
            }
136
187
            catch ( Exception e )
137
188
            {
138
 
                throw new RuntimeException("Bug in ConfigurationAwareTestNGReporter", e);
 
189
                throw new NestedRuntimeException( "Bug in ConfigurationAwareTestNGReporter", e );
139
190
            }
140
 
        } catch (ClassNotFoundException e) {
 
191
        }
 
192
        catch ( ClassNotFoundException e )
 
193
        {
141
194
            return new TestNGReporter( reportManager );
142
195
        }
143
196
    }
144
 
 
145
 
    private static void attachNonStandardReporter( TestNG testNG, String className )
146
 
    {
147
 
        try
148
 
        {
149
 
            Class c = Class.forName( className );
150
 
            if (IReporter.class.isAssignableFrom( c )) {
151
 
                testNG.addListener( c.newInstance() );
152
 
            }
153
 
        }
154
 
        catch ( Exception e ) {} // ignore
155
 
    }
156
 
 
 
197
    
157
198
}