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.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.apache.maven.surefire.report.ConsoleOutputCapture;
28 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
29 import org.apache.maven.surefire.report.ReporterFactory;
30 import org.apache.maven.surefire.report.RunListener;
31 import org.apache.maven.surefire.testset.TestSetFailedException;
32
33
34
35
36
37
38
39 public class TestNGXmlTestSuite
40 implements TestNgTestSuite
41 {
42 private final List suiteFiles;
43
44 private List<String> suiteFilePaths;
45
46 private final String testSourceDirectory;
47
48 private final Map<String, String> options;
49
50 private final File reportsDirectory;
51
52 private final int skipAfterFailureCount;
53
54
55 private Map<File, String> testSets;
56
57
58
59
60
61 public TestNGXmlTestSuite( List<File> suiteFiles, String testSourceDirectory, Map<String, String> confOptions,
62 File reportsDirectory, int skipAfterFailureCount )
63 {
64 this.suiteFiles = suiteFiles;
65 this.options = confOptions;
66 this.testSourceDirectory = testSourceDirectory;
67 this.reportsDirectory = reportsDirectory;
68 this.skipAfterFailureCount = skipAfterFailureCount;
69 }
70
71 public void execute( ReporterFactory reporterManagerFactory )
72 throws TestSetFailedException
73 {
74 if ( testSets == null )
75 {
76 throw new IllegalStateException( "You must call locateTestSets before calling execute" );
77 }
78
79 RunListener reporter = reporterManagerFactory.createReporter();
80 ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
81
82 TestNGDirectoryTestSuite.startTestSuite( reporter, this );
83 TestNGExecutor.run( suiteFilePaths, testSourceDirectory, options, reporter, this, reportsDirectory,
84 skipAfterFailureCount );
85 TestNGDirectoryTestSuite.finishTestSuite( reporter, this );
86 }
87
88 public void execute( String testSetName, ReporterFactory reporterManagerFactory )
89 throws TestSetFailedException
90 {
91 throw new TestSetFailedException( "Cannot run individual test when suite files are specified" );
92 }
93
94 public Map locateTestSets( ClassLoader classLoader )
95 throws TestSetFailedException
96 {
97 if ( testSets != null )
98 {
99 throw new IllegalStateException( "You can't call locateTestSets twice" );
100 }
101
102 if ( this.suiteFiles == null )
103 {
104 throw new IllegalStateException( "No suite files were specified" );
105 }
106
107 this.testSets = new HashMap<File, String>();
108 this.suiteFilePaths = new ArrayList<String>();
109
110 for ( Object suiteFile : suiteFiles )
111 {
112 File file = (File) suiteFile;
113 if ( !file.exists() || !file.isFile() )
114 {
115 throw new TestSetFailedException( "Suite file " + file + " is not a valid file" );
116 }
117 this.testSets.put( file, file.getAbsolutePath() );
118 this.suiteFilePaths.add( file.getAbsolutePath() );
119 }
120
121 return this.testSets;
122 }
123
124 public String getSuiteName()
125 {
126 String result = options.get( "suitename" );
127 return result == null ? "TestSuite" : result;
128 }
129 }