1 package org.apache.maven.plugin.surefire;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
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 Properties testVmSystemProperties = new Properties();
68
69 public static final String BRIEF_REPORT_FORMAT = ConsoleReporter.BRIEF;
70
71 public static final String PLAIN_REPORT_FORMAT = ConsoleReporter.PLAIN;
72
73 public StartupReportConfiguration( boolean useFile, boolean printSummary, String reportFormat,
74 boolean redirectTestOutputToFile, boolean disableXmlReport,
75 @Nonnull File reportsDirectory,
76 boolean trimStackTrace, String reportNameSuffix,
77 String configurationHash, boolean requiresRunHistory )
78 {
79 this.useFile = useFile;
80 this.printSummary = printSummary;
81 this.reportFormat = reportFormat;
82 this.redirectTestOutputToFile = redirectTestOutputToFile;
83 this.disableXmlReport = disableXmlReport;
84 this.reportsDirectory = reportsDirectory;
85 this.trimStackTrace = trimStackTrace;
86 this.reportNameSuffix = reportNameSuffix;
87 this.configurationHash = configurationHash;
88 this.requiresRunHistory = requiresRunHistory;
89 this.originalSystemOut = System.out;
90 this.originalSystemErr = System.err;
91 }
92
93 public static StartupReportConfiguration defaultValue()
94 {
95 File target = new File( "./target" );
96 return new StartupReportConfiguration( true, true, "PLAIN", false, false, target, false, null, "TESTHASH",
97 false );
98 }
99
100 public static StartupReportConfiguration defaultNoXml()
101 {
102 File target = new File( "./target" );
103 return new StartupReportConfiguration( true, true, "PLAIN", false, true, target, false, null, "TESTHASHxXML",
104 false );
105 }
106
107 public boolean isUseFile()
108 {
109 return useFile;
110 }
111
112 public boolean isPrintSummary()
113 {
114 return printSummary;
115 }
116
117 public String getReportFormat()
118 {
119 return reportFormat;
120 }
121
122 public String getReportNameSuffix()
123 {
124 return reportNameSuffix;
125 }
126
127 public boolean isRedirectTestOutputToFile()
128 {
129 return redirectTestOutputToFile;
130 }
131
132 public boolean isDisableXmlReport()
133 {
134 return disableXmlReport;
135 }
136
137 public File getReportsDirectory()
138 {
139 return reportsDirectory;
140 }
141
142 public StatelessXmlReporter instantiateStatelessXmlReporter()
143 {
144 if ( !isDisableXmlReport() )
145 {
146 return new StatelessXmlReporter( reportsDirectory, reportNameSuffix, trimStackTrace );
147 }
148 return null;
149 }
150
151 public FileReporter instantiateFileReporter()
152 {
153 if ( isUseFile() && isBriefOrPlainFormat() )
154 {
155 return new FileReporter( reportsDirectory, getReportNameSuffix() );
156 }
157 return null;
158 }
159
160 public boolean isBriefOrPlainFormat()
161 {
162 String fmt = getReportFormat();
163 return BRIEF_REPORT_FORMAT.equals( fmt ) || PLAIN_REPORT_FORMAT.equals( fmt );
164 }
165
166 public ConsoleReporter instantiateConsoleReporter()
167 {
168 return shouldReportToConsole() ? new ConsoleReporter( originalSystemOut ) : null;
169 }
170
171 private boolean shouldReportToConsole()
172 {
173 return isUseFile() ? isPrintSummary() : isRedirectTestOutputToFile() || isBriefOrPlainFormat();
174 }
175
176 public TestcycleConsoleOutputReceiver instantiateConsoleOutputFileReporter()
177 {
178 if ( isRedirectTestOutputToFile() )
179 {
180 return new ConsoleOutputFileReporter( reportsDirectory, getReportNameSuffix() );
181 }
182 else
183 {
184 return new DirectConsoleOutput( originalSystemOut, originalSystemErr );
185 }
186 }
187
188 public StatisticsReporter instantiateStatisticsReporter()
189 {
190 if ( requiresRunHistory )
191 {
192 final File target = getStatisticsFile();
193 return new StatisticsReporter( target );
194 }
195 return null;
196 }
197
198 public File getStatisticsFile()
199 {
200 return new File( reportsDirectory.getParentFile().getParentFile(), ".surefire-" + this.configurationHash );
201 }
202
203
204 public Properties getTestVmSystemProperties()
205 {
206 return testVmSystemProperties;
207 }
208
209
210 public boolean isTrimStackTrace()
211 {
212 return trimStackTrace;
213 }
214
215 public String getConfigurationHash()
216 {
217 return configurationHash;
218 }
219
220 public boolean isRequiresRunHistory()
221 {
222 return requiresRunHistory;
223 }
224
225 public PrintStream getOriginalSystemOut()
226 {
227 return originalSystemOut;
228 }
229
230 }