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