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