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