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