View Javadoc
1   package org.apache.maven.plugin.surefire;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter;
23  import org.apache.maven.plugin.surefire.report.DirectConsoleOutput;
24  import org.apache.maven.plugin.surefire.report.FileReporter;
25  import org.apache.maven.plugin.surefire.report.StatelessXmlReporter;
26  import org.apache.maven.plugin.surefire.report.TestcycleConsoleOutputReceiver;
27  import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
28  import org.apache.maven.plugin.surefire.runorder.StatisticsReporter;
29  
30  import javax.annotation.Nonnull;
31  import java.io.File;
32  import java.io.PrintStream;
33  import java.nio.charset.Charset;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.concurrent.ConcurrentHashMap;
37  
38  import static org.apache.maven.plugin.surefire.SurefireHelper.replaceForkThreadsInPath;
39  import static org.apache.maven.plugin.surefire.report.ConsoleReporter.BRIEF;
40  import static org.apache.maven.plugin.surefire.report.ConsoleReporter.PLAIN;
41  import static org.apache.commons.lang3.StringUtils.trimToNull;
42  
43  /**
44   * All the parameters used to construct reporters
45   * <br>
46   *
47   * @author Kristian Rosenvold
48   */
49  public final class StartupReportConfiguration
50  {
51      private final PrintStream originalSystemOut;
52  
53      private final PrintStream originalSystemErr;
54  
55      private final boolean useFile;
56  
57      private final boolean printSummary;
58  
59      private final String reportFormat;
60  
61      private final String reportNameSuffix;
62  
63      private final File statisticsFile;
64  
65      private final boolean requiresRunHistory;
66  
67      private final boolean redirectTestOutputToFile;
68  
69      private final boolean disableXmlReport;
70  
71      private final File reportsDirectory;
72  
73      private final boolean trimStackTrace;
74  
75      private final int rerunFailingTestsCount;
76  
77      private final String xsdSchemaLocation;
78  
79      private final Map<String, Map<String, List<WrappedReportEntry>>> testClassMethodRunHistory
80          = new ConcurrentHashMap<String, Map<String, List<WrappedReportEntry>>>();
81  
82      private final Charset encoding;
83  
84      private boolean isForkMode;
85  
86      private StatisticsReporter statisticsReporter;
87  
88      @SuppressWarnings( "checkstyle:parameternumber" )
89      public StartupReportConfiguration( boolean useFile, boolean printSummary, String reportFormat,
90                                         boolean redirectTestOutputToFile, boolean disableXmlReport,
91                                         @Nonnull File reportsDirectory, boolean trimStackTrace, String reportNameSuffix,
92                                         File statisticsFile, boolean requiresRunHistory, int rerunFailingTestsCount,
93                                         String xsdSchemaLocation, String encoding, boolean isForkMode )
94      {
95          this.useFile = useFile;
96          this.printSummary = printSummary;
97          this.reportFormat = reportFormat;
98          this.redirectTestOutputToFile = redirectTestOutputToFile;
99          this.disableXmlReport = disableXmlReport;
100         this.reportsDirectory = reportsDirectory;
101         this.trimStackTrace = trimStackTrace;
102         this.reportNameSuffix = reportNameSuffix;
103         this.statisticsFile = statisticsFile;
104         this.requiresRunHistory = requiresRunHistory;
105         this.originalSystemOut = System.out;
106         this.originalSystemErr = System.err;
107         this.rerunFailingTestsCount = rerunFailingTestsCount;
108         this.xsdSchemaLocation = xsdSchemaLocation;
109         String charset = trimToNull( encoding );
110         this.encoding = charset == null ? Charset.defaultCharset() : Charset.forName( charset );
111         this.isForkMode = isForkMode;
112     }
113 
114     public boolean isUseFile()
115     {
116         return useFile;
117     }
118 
119     public boolean isPrintSummary()
120     {
121         return printSummary;
122     }
123 
124     public String getReportFormat()
125     {
126         return reportFormat;
127     }
128 
129     public String getReportNameSuffix()
130     {
131         return reportNameSuffix;
132     }
133 
134     public boolean isRedirectTestOutputToFile()
135     {
136         return redirectTestOutputToFile;
137     }
138 
139     public boolean isDisableXmlReport()
140     {
141         return disableXmlReport;
142     }
143 
144     public File getReportsDirectory()
145     {
146         return reportsDirectory;
147     }
148 
149     public int getRerunFailingTestsCount()
150     {
151         return rerunFailingTestsCount;
152     }
153 
154     @Deprecated // rename to stateful
155     public StatelessXmlReporter instantiateStatelessXmlReporter( Integer forkNumber )
156     {
157         assert forkNumber == null || isForkMode;
158 
159         // If forking TestNG the suites have same name 'TestSuite' and tend to override report statistics in stateful
160         // reporter, see Surefire1535TestNGParallelSuitesIT. The testClassMethodRunHistory should be isolated.
161         // In the in-plugin execution of parallel JUnit4.7 with rerun the map must be shared because reports and
162         // listeners are in ThreadLocal, see Surefire1122ParallelAndFlakyTestsIT.
163         Map<String, Map<String, List<WrappedReportEntry>>> testClassMethodRunHistory
164                 = isForkMode
165                 ? new ConcurrentHashMap<String, Map<String, List<WrappedReportEntry>>>()
166                 : this.testClassMethodRunHistory;
167 
168         return isDisableXmlReport()
169             ? null
170             : new StatelessXmlReporter( resolveReportsDirectory( forkNumber ), reportNameSuffix, trimStackTrace,
171                 rerunFailingTestsCount, testClassMethodRunHistory, xsdSchemaLocation );
172     }
173 
174     public FileReporter instantiateFileReporter( Integer forkNumber )
175     {
176         return isUseFile() && isBriefOrPlainFormat()
177             ? new FileReporter( resolveReportsDirectory( forkNumber ), reportNameSuffix, encoding )
178             : null;
179     }
180 
181     public boolean isBriefOrPlainFormat()
182     {
183         String fmt = getReportFormat();
184         return BRIEF.equals( fmt ) || PLAIN.equals( fmt );
185     }
186 
187     public TestcycleConsoleOutputReceiver instantiateConsoleOutputFileReporter( Integer forkNumber )
188     {
189         return isRedirectTestOutputToFile()
190             ? new ConsoleOutputFileReporter( resolveReportsDirectory( forkNumber ), reportNameSuffix, forkNumber )
191             : new DirectConsoleOutput( originalSystemOut, originalSystemErr );
192     }
193 
194     public synchronized StatisticsReporter getStatisticsReporter()
195     {
196         if ( statisticsReporter == null )
197         {
198             statisticsReporter = requiresRunHistory ? new StatisticsReporter( statisticsFile ) : null;
199         }
200         return statisticsReporter;
201     }
202 
203     public File getStatisticsFile()
204     {
205         return statisticsFile;
206     }
207 
208     public boolean isTrimStackTrace()
209     {
210         return trimStackTrace;
211     }
212 
213     public boolean isRequiresRunHistory()
214     {
215         return requiresRunHistory;
216     }
217 
218     public PrintStream getOriginalSystemOut()
219     {
220         return originalSystemOut;
221     }
222 
223     public String getXsdSchemaLocation()
224     {
225         return xsdSchemaLocation;
226     }
227 
228     public Charset getEncoding()
229     {
230         return encoding;
231     }
232 
233     public boolean isForkMode()
234     {
235         return isForkMode;
236     }
237 
238     private File resolveReportsDirectory( Integer forkNumber )
239     {
240         return forkNumber == null ? reportsDirectory : replaceForkThreadsInPath( reportsDirectory, forkNumber );
241     }
242 }