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 java.io.File;
23  import java.io.PrintStream;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter;
31  import org.apache.maven.plugin.surefire.report.ConsoleReporter;
32  import org.apache.maven.plugin.surefire.report.DirectConsoleOutput;
33  import org.apache.maven.plugin.surefire.report.FileReporter;
34  import org.apache.maven.plugin.surefire.report.StatelessXmlReporter;
35  import org.apache.maven.plugin.surefire.report.TestcycleConsoleOutputReceiver;
36  import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
37  import org.apache.maven.plugin.surefire.runorder.StatisticsReporter;
38  
39  import javax.annotation.Nonnull;
40  
41  /**
42   * All the parameters used to construct reporters
43   * <p/>
44   *
45   * @author Kristian Rosenvold
46   */
47  public class StartupReportConfiguration
48  {
49      private final PrintStream originalSystemOut;
50  
51      private final PrintStream originalSystemErr;
52  
53      private final boolean useFile;
54  
55      private final boolean printSummary;
56  
57      private final String reportFormat;
58  
59      private final String reportNameSuffix;
60  
61      private final String configurationHash;
62  
63      private final boolean requiresRunHistory;
64  
65      private final boolean redirectTestOutputToFile;
66  
67      private final boolean disableXmlReport;
68  
69      private final File reportsDirectory;
70  
71      private final boolean trimStackTrace;
72  
73      private final int rerunFailingTestsCount;
74  
75      private final Properties testVmSystemProperties = new Properties();
76  
77      public static final String BRIEF_REPORT_FORMAT = ConsoleReporter.BRIEF;
78  
79      public static final String PLAIN_REPORT_FORMAT = ConsoleReporter.PLAIN;
80  
81      private final Map<String, Map<String, List<WrappedReportEntry>>> testClassMethodRunHistoryMap;
82  
83      @SuppressWarnings( "checkstyle:parameternumber" )
84      public StartupReportConfiguration( boolean useFile, boolean printSummary, String reportFormat,
85                                         boolean redirectTestOutputToFile, boolean disableXmlReport,
86                                         @Nonnull File reportsDirectory, boolean trimStackTrace, String reportNameSuffix,
87                                         String configurationHash, boolean requiresRunHistory,
88                                         int rerunFailingTestsCount )
89      {
90          this.useFile = useFile;
91          this.printSummary = printSummary;
92          this.reportFormat = reportFormat;
93          this.redirectTestOutputToFile = redirectTestOutputToFile;
94          this.disableXmlReport = disableXmlReport;
95          this.reportsDirectory = reportsDirectory;
96          this.trimStackTrace = trimStackTrace;
97          this.reportNameSuffix = reportNameSuffix;
98          this.configurationHash = configurationHash;
99          this.requiresRunHistory = requiresRunHistory;
100         this.originalSystemOut = System.out;
101         this.originalSystemErr = System.err;
102         this.rerunFailingTestsCount = rerunFailingTestsCount;
103         this.testClassMethodRunHistoryMap =
104                         Collections.synchronizedMap(
105                              new HashMap<String, Map<String, List<WrappedReportEntry>>>() );
106     }
107 
108     public static StartupReportConfiguration defaultValue()
109     {
110         File target = new File( "./target" );
111         return new StartupReportConfiguration( true, true, "PLAIN", false, false, target, false, null, "TESTHASH",
112                                                false, 0 );
113     }
114 
115     public static StartupReportConfiguration defaultNoXml()
116     {
117         File target = new File( "./target" );
118         return new StartupReportConfiguration( true, true, "PLAIN", false, true, target, false, null, "TESTHASHxXML",
119                                                false, 0 );
120     }
121 
122     public boolean isUseFile()
123     {
124         return useFile;
125     }
126 
127     public boolean isPrintSummary()
128     {
129         return printSummary;
130     }
131 
132     public String getReportFormat()
133     {
134         return reportFormat;
135     }
136 
137     public String getReportNameSuffix()
138     {
139         return reportNameSuffix;
140     }
141 
142     public boolean isRedirectTestOutputToFile()
143     {
144         return redirectTestOutputToFile;
145     }
146 
147     public boolean isDisableXmlReport()
148     {
149         return disableXmlReport;
150     }
151 
152     public File getReportsDirectory()
153     {
154         return reportsDirectory;
155     }
156 
157     public int getRerunFailingTestsCount()
158     {
159         return rerunFailingTestsCount;
160     }
161 
162     public StatelessXmlReporter instantiateStatelessXmlReporter()
163     {
164         if ( !isDisableXmlReport() )
165         {
166             return new StatelessXmlReporter( reportsDirectory, reportNameSuffix, trimStackTrace,
167                                              rerunFailingTestsCount, testClassMethodRunHistoryMap );
168         }
169         return null;
170     }
171 
172     public FileReporter instantiateFileReporter()
173     {
174         if ( isUseFile() && isBriefOrPlainFormat() )
175         {
176             return new FileReporter( reportsDirectory, getReportNameSuffix() );
177         }
178         return null;
179     }
180 
181     public boolean isBriefOrPlainFormat()
182     {
183         String fmt = getReportFormat();
184         return BRIEF_REPORT_FORMAT.equals( fmt ) || PLAIN_REPORT_FORMAT.equals( fmt );
185     }
186 
187     public ConsoleReporter instantiateConsoleReporter()
188     {
189         return shouldReportToConsole() ? new ConsoleReporter( originalSystemOut ) : null;
190     }
191 
192     private boolean shouldReportToConsole()
193     {
194         return isUseFile() ? isPrintSummary() : isRedirectTestOutputToFile() || isBriefOrPlainFormat();
195     }
196 
197     public TestcycleConsoleOutputReceiver instantiateConsoleOutputFileReporter()
198     {
199         if ( isRedirectTestOutputToFile() )
200         {
201             return new ConsoleOutputFileReporter( reportsDirectory, getReportNameSuffix() );
202         }
203         else
204         {
205             return new DirectConsoleOutput( originalSystemOut, originalSystemErr );
206         }
207     }
208 
209     public StatisticsReporter instantiateStatisticsReporter()
210     {
211         if ( requiresRunHistory )
212         {
213             final File target = getStatisticsFile();
214             return new StatisticsReporter( target );
215         }
216         return null;
217     }
218 
219     public File getStatisticsFile()
220     {
221         return new File( reportsDirectory.getParentFile().getParentFile(), ".surefire-" + this.configurationHash );
222     }
223 
224 
225     public Properties getTestVmSystemProperties()
226     {
227         return testVmSystemProperties;
228     }
229 
230 
231     public boolean isTrimStackTrace()
232     {
233         return trimStackTrace;
234     }
235 
236     public String getConfigurationHash()
237     {
238         return configurationHash;
239     }
240 
241     public boolean isRequiresRunHistory()
242     {
243         return requiresRunHistory;
244     }
245 
246     public PrintStream getOriginalSystemOut()
247     {
248         return originalSystemOut;
249     }
250 
251 }