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