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.io.File;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.SortedMap;
31 import java.util.TreeMap;
32
33 import org.apache.maven.artifact.versioning.ArtifactVersion;
34 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
35 import org.apache.maven.surefire.NonAbstractClassFilter;
36 import org.apache.maven.surefire.report.ConsoleOutputCapture;
37 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
38 import org.apache.maven.surefire.report.ReportEntry;
39 import org.apache.maven.surefire.report.ReporterException;
40 import org.apache.maven.surefire.report.ReporterFactory;
41 import org.apache.maven.surefire.report.RunListener;
42 import org.apache.maven.surefire.report.SimpleReportEntry;
43 import org.apache.maven.surefire.testset.TestSetFailedException;
44 import org.apache.maven.surefire.util.DefaultDirectoryScanner;
45 import org.apache.maven.surefire.util.DirectoryScanner;
46 import org.apache.maven.surefire.util.RunOrder;
47 import org.apache.maven.surefire.util.TestsToRun;
48
49
50
51
52
53
54
55 public class TestNGDirectoryTestSuite
56 implements TestNgTestSuite
57 {
58 private final ArtifactVersion version;
59
60 private final Map options;
61
62 private final String testSourceDirectory;
63
64 private final File reportsDirectory;
65
66 private SortedMap testSets;
67
68 private final DirectoryScanner surefireDirectoryScanner;
69
70 private final String testMethodPattern;
71
72 public TestNGDirectoryTestSuite( File basedir, ArrayList includes, ArrayList excludes, String testSourceDirectory,
73 String artifactVersion, Properties confOptions, File reportsDirectory,
74 String testMethodPattern )
75 {
76
77 this.surefireDirectoryScanner = new DefaultDirectoryScanner( basedir, includes, excludes, RunOrder.FILESYSTEM );
78
79 this.options = confOptions;
80
81 this.testSourceDirectory = testSourceDirectory;
82 this.reportsDirectory = reportsDirectory;
83 this.version = new DefaultArtifactVersion( artifactVersion );
84 this.testMethodPattern = testMethodPattern;
85 }
86
87 public void execute( TestsToRun testsToRun, ReporterFactory reporterManagerFactory )
88 throws ReporterException, TestSetFailedException
89 {
90
91 if ( testsToRun.size() == 0 )
92 {
93 return;
94 }
95
96 if ( testsToRun.size() > 1 )
97 {
98 executeMulti( testsToRun, reporterManagerFactory );
99 return;
100 }
101
102 this.options.put( "suitename", testsToRun.getLocatedClasses()[0].getName() );
103
104 RunListener reporter = reporterManagerFactory.createReporter();
105 ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
106
107 startTestSuite( reporter, this );
108
109 TestNGExecutor.run( new Class[]{ (Class) testsToRun.iterator().next() }, this.testSourceDirectory, this.options,
110 this.version, reporter, this, reportsDirectory, testMethodPattern );
111
112 finishTestSuite( reporter, this );
113 }
114
115 public void executeMulti( TestsToRun testsToRun, ReporterFactory reporterFactory )
116 throws ReporterException, TestSetFailedException
117 {
118 Class junitTest;
119 try
120 {
121 junitTest = Class.forName( "junit.framework.Test" );
122 }
123 catch ( ClassNotFoundException e )
124 {
125 junitTest = null;
126 }
127
128 List testNgTestClasses = new ArrayList();
129 List junitTestClasses = new ArrayList();
130 for ( Iterator it = testsToRun.iterator(); it.hasNext(); )
131 {
132 Class c = (Class) it.next();
133 if ( junitTest != null && junitTest.isAssignableFrom( c ) )
134 {
135 junitTestClasses.add( c );
136 }
137 else
138 {
139 testNgTestClasses.add( c );
140 }
141 }
142
143 File testNgReportsDirectory = reportsDirectory, junitReportsDirectory = reportsDirectory;
144
145 if ( junitTestClasses.size() > 0 && testNgTestClasses.size() > 0 )
146 {
147 testNgReportsDirectory = new File( reportsDirectory, "testng-native-results" );
148 junitReportsDirectory = new File( reportsDirectory, "testng-junit-results" );
149 }
150
151
152 RunListener reporterManager = reporterFactory.createReporter();
153 ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporterManager );
154 startTestSuite( reporterManager, this );
155
156 Class[] testClasses = (Class[]) testNgTestClasses.toArray( new Class[testNgTestClasses.size()] );
157
158 TestNGExecutor.run( testClasses, this.testSourceDirectory, this.options, this.version, reporterManager, this,
159 testNgReportsDirectory, testMethodPattern );
160
161 if ( junitTestClasses.size() > 0 )
162 {
163 testClasses = (Class[]) junitTestClasses.toArray( new Class[junitTestClasses.size()] );
164
165 Map junitOptions = new HashMap();
166 for ( Iterator it = this.options.keySet().iterator(); it.hasNext(); )
167 {
168 Object key = it.next();
169 junitOptions.put( key, options.get( key ) );
170 }
171
172 junitOptions.put( "junit", Boolean.TRUE );
173
174 TestNGExecutor.run( testClasses, this.testSourceDirectory, junitOptions, this.version, reporterManager,
175 this, junitReportsDirectory, testMethodPattern );
176 }
177
178
179 finishTestSuite( reporterManager, this );
180 }
181
182
183 public void execute( String testSetName, ReporterFactory reporterManagerFactory )
184 throws ReporterException, TestSetFailedException
185 {
186 if ( testSets == null )
187 {
188 throw new IllegalStateException( "You must call locateTestSets before calling execute" );
189 }
190 TestNGTestSet testSet = (TestNGTestSet) testSets.get( testSetName );
191
192 if ( testSet == null )
193 {
194 throw new TestSetFailedException( "Unable to find test set '" + testSetName + "' in suite" );
195 }
196
197 RunListener reporter = reporterManagerFactory.createReporter();
198 ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
199
200 startTestSuite( reporter, this );
201
202 TestNGExecutor.run( new Class[]{ testSet.getTestClass() }, this.testSourceDirectory, this.options, this.version,
203 reporter, this, reportsDirectory, testMethodPattern );
204
205 finishTestSuite( reporter, this );
206 }
207
208 public static void startTestSuite( RunListener reporter, Object suite )
209 {
210 ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) );
211
212 try
213 {
214 reporter.testSetStarting( report );
215 }
216 catch ( ReporterException e )
217 {
218
219 }
220 }
221
222 public static void finishTestSuite( RunListener reporterManager, Object suite )
223 throws ReporterException
224 {
225 ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) );
226
227 reporterManager.testSetCompleted( report );
228 }
229
230 public String getSuiteName()
231 {
232 String result = (String) options.get( "suitename" );
233 if ( result == null )
234 {
235 result = "TestSuite";
236 }
237 return result;
238 }
239
240 private static String getSuiteName( Object suite )
241 {
242 String result;
243 if ( suite instanceof TestNGDirectoryTestSuite )
244 {
245 return ( (TestNGDirectoryTestSuite) suite ).getSuiteName();
246 }
247 else if ( suite instanceof TestNGXmlTestSuite )
248 {
249 return ( (TestNGXmlTestSuite) suite ).getSuiteName();
250 }
251 else
252 {
253 result = "TestSuite";
254 }
255
256 return result;
257 }
258
259 public Map locateTestSets( ClassLoader classLoader )
260 throws TestSetFailedException
261 {
262 if ( testSets != null )
263 {
264 throw new IllegalStateException( "You can't call locateTestSets twice" );
265 }
266 testSets = new TreeMap();
267
268 final TestsToRun testsToRun =
269 surefireDirectoryScanner.locateTestClasses( classLoader, new NonAbstractClassFilter() );
270 Class[] locatedClasses = testsToRun.getLocatedClasses();
271
272 for ( int i = 0; i < locatedClasses.length; i++ )
273 {
274 Class testClass = locatedClasses[i];
275 TestNGTestSet testSet = new TestNGTestSet( testClass );
276
277 if ( testSets.containsKey( testSet.getName() ) )
278 {
279 throw new TestSetFailedException( "Duplicate test set '" + testSet.getName() + "'" );
280 }
281 testSets.put( testSet.getName(), testSet );
282
283 }
284
285 return Collections.unmodifiableSortedMap( testSets );
286 }
287
288 }