1 | |
package org.apache.maven.plugin.surefire.report; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.PrintStream; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import org.apache.maven.plugin.surefire.runorder.StatisticsReporter; |
27 | |
import org.apache.maven.surefire.booter.StartupReportConfiguration; |
28 | |
import org.apache.maven.surefire.report.ConsoleLogger; |
29 | |
import org.apache.maven.surefire.report.DefaultDirectConsoleReporter; |
30 | |
import org.apache.maven.surefire.report.MulticastingReporter; |
31 | |
import org.apache.maven.surefire.report.Reporter; |
32 | |
import org.apache.maven.surefire.report.ReporterConfiguration; |
33 | |
import org.apache.maven.surefire.report.ReporterFactory; |
34 | |
import org.apache.maven.surefire.report.RunListener; |
35 | |
import org.apache.maven.surefire.report.RunStatistics; |
36 | |
import org.apache.maven.surefire.report.TestSetRunListener; |
37 | |
import org.apache.maven.surefire.suite.RunResult; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class FileReporterFactory |
47 | |
implements ReporterFactory |
48 | |
{ |
49 | |
|
50 | |
private final ReporterConfiguration reporterConfiguration; |
51 | |
|
52 | 0 | private final RunStatistics globalStats = new RunStatistics(); |
53 | |
|
54 | |
private final MulticastingReporter multicastingReporter; |
55 | |
|
56 | |
private final StartupReportConfiguration reportConfiguration; |
57 | |
|
58 | |
private final StatisticsReporter statisticsReporter; |
59 | |
|
60 | |
public FileReporterFactory( StartupReportConfiguration reportConfiguration ) |
61 | 0 | { |
62 | 0 | this.reportConfiguration = reportConfiguration; |
63 | 0 | this.reporterConfiguration = getReporterConfiguration(); |
64 | 0 | multicastingReporter = new MulticastingReporter( instantiateReports() ); |
65 | 0 | this.statisticsReporter = reportConfiguration.instantiateStatisticsReporter(); |
66 | 0 | runStarting(); |
67 | 0 | } |
68 | |
|
69 | |
private ReporterConfiguration getReporterConfiguration() |
70 | |
{ |
71 | |
|
72 | 0 | return new ReporterConfiguration( reportConfiguration.getReportsDirectory(), |
73 | |
new Boolean( reportConfiguration.isTrimStackTrace() ) ); |
74 | |
} |
75 | |
|
76 | |
public RunListener createReporter() |
77 | |
{ |
78 | 0 | final PrintStream sout = reporterConfiguration.getOriginalSystemOut(); |
79 | 0 | return new TestSetRunListener( reportConfiguration.instantiateConsoleReporter(), |
80 | |
reportConfiguration.instantiateFileReporter(), |
81 | |
reportConfiguration.instantiateXmlReporter(), |
82 | |
reportConfiguration.instantiateConsoleOutputFileReporter( sout ), |
83 | |
statisticsReporter, globalStats ); |
84 | |
} |
85 | |
|
86 | |
private List instantiateReports() |
87 | |
{ |
88 | 0 | final PrintStream sout = reporterConfiguration.getOriginalSystemOut(); |
89 | 0 | List result = new ArrayList(); |
90 | 0 | addIfNotNull( result, reportConfiguration.instantiateConsoleReporter() ); |
91 | 0 | addIfNotNull( result, reportConfiguration.instantiateFileReporter() ); |
92 | 0 | addIfNotNull( result, reportConfiguration.instantiateXmlReporter() ); |
93 | 0 | addIfNotNull( result, reportConfiguration.instantiateConsoleOutputFileReporter( sout ) ); |
94 | 0 | addIfNotNull( result, statisticsReporter ); |
95 | 0 | return result; |
96 | |
} |
97 | |
|
98 | |
private void addIfNotNull( List result, Reporter reporter ) |
99 | |
{ |
100 | 0 | if ( reporter != null ) |
101 | |
{ |
102 | 0 | result.add( reporter ); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
public RunResult close() |
107 | |
{ |
108 | 0 | runCompleted(); |
109 | 0 | return globalStats.getRunResult(); |
110 | |
} |
111 | |
|
112 | |
private ConsoleLogger createConsoleLogger() |
113 | |
{ |
114 | 0 | return new DefaultDirectConsoleReporter( reporterConfiguration.getOriginalSystemOut() ); |
115 | |
} |
116 | |
|
117 | |
public void runStarting() |
118 | |
{ |
119 | 0 | final ConsoleLogger consoleReporter = createConsoleLogger(); |
120 | 0 | consoleReporter.info( "" ); |
121 | 0 | consoleReporter.info( "-------------------------------------------------------" ); |
122 | 0 | consoleReporter.info( " T E S T S" ); |
123 | 0 | consoleReporter.info( "-------------------------------------------------------" ); |
124 | 0 | } |
125 | |
|
126 | |
private void runCompleted() |
127 | |
{ |
128 | 0 | final ConsoleLogger logger = createConsoleLogger(); |
129 | 0 | logger.info( "" ); |
130 | 0 | logger.info( "Results :" ); |
131 | 0 | logger.info( "" ); |
132 | 0 | if ( globalStats.hadFailures() ) |
133 | |
{ |
134 | 0 | multicastingReporter.writeMessage( "Failed tests: " ); |
135 | 0 | for ( Iterator iterator = this.globalStats.getFailureSources().iterator(); iterator.hasNext(); ) |
136 | |
{ |
137 | 0 | logger.info( " " + iterator.next() ); |
138 | |
} |
139 | 0 | logger.info( "" ); |
140 | |
} |
141 | 0 | if ( globalStats.hadErrors() ) |
142 | |
{ |
143 | 0 | logger.info( "Tests in error: " ); |
144 | 0 | for ( Iterator iterator = this.globalStats.getErrorSources().iterator(); iterator.hasNext(); ) |
145 | |
{ |
146 | 0 | logger.info( " " + iterator.next() ); |
147 | |
} |
148 | 0 | logger.info( "" ); |
149 | |
} |
150 | 0 | logger.info( globalStats.getSummary() ); |
151 | 0 | logger.info( "" ); |
152 | 0 | } |
153 | |
|
154 | |
public RunStatistics getGlobalRunStatistics() |
155 | |
{ |
156 | 0 | return globalStats; |
157 | |
} |
158 | |
|
159 | |
public static FileReporterFactory defaultNoXml(){ |
160 | 0 | return new FileReporterFactory( StartupReportConfiguration.defaultNoXml() ); |
161 | |
} |
162 | |
} |