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