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