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