1 | |
package org.apache.maven.surefire.report; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.surefire.suite.RunResult; |
23 | |
import org.apache.maven.surefire.util.ReflectionUtils; |
24 | |
import org.apache.maven.surefire.util.SurefireReflectionException; |
25 | |
|
26 | |
import java.lang.reflect.Constructor; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class ReporterManagerFactory |
48 | |
implements ReporterFactory |
49 | |
{ |
50 | |
private final List reportDefinitions; |
51 | |
|
52 | |
private final ClassLoader surefireClassLoader; |
53 | |
|
54 | 0 | private final RunStatistics globalRunStatistics = new RunStatistics(); |
55 | |
|
56 | |
private final ReporterConfiguration reporterConfiguration; |
57 | |
|
58 | |
private RunReporter first; |
59 | |
|
60 | 0 | private final Object lock = new Object(); |
61 | |
|
62 | |
private final ConsoleOutputCapture consoleOutputCapture; |
63 | |
|
64 | |
public ReporterManagerFactory( ClassLoader surefireClassLoader, ReporterConfiguration reporterConfiguration ) |
65 | 0 | { |
66 | 0 | this.reportDefinitions = reporterConfiguration.getReports(); |
67 | 0 | this.surefireClassLoader = surefireClassLoader; |
68 | 0 | this.reporterConfiguration = reporterConfiguration; |
69 | 0 | SystemConsoleOutputReceiver systemConsoleOutputReceiver = |
70 | |
new SystemConsoleOutputReceiver( reporterConfiguration.getOriginalSystemOut(), |
71 | |
reporterConfiguration.getOriginalSystemErr() ); |
72 | |
|
73 | 0 | final DefaultConsoleOutputReceiver target = |
74 | |
new DefaultConsoleOutputReceiver( systemConsoleOutputReceiver, true ); |
75 | 0 | this.consoleOutputCapture = new ConsoleOutputCapture( target ); |
76 | 0 | } |
77 | |
|
78 | |
public RunStatistics getGlobalRunStatistics() |
79 | |
{ |
80 | 0 | return globalRunStatistics; |
81 | |
} |
82 | |
|
83 | |
public RunListener createReporter() |
84 | |
{ |
85 | 0 | return setupReporter( |
86 | |
instantiateReportsNewStyle( reportDefinitions, reporterConfiguration, surefireClassLoader ) ); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
private RunListener setupReporter( List reports ) |
91 | |
{ |
92 | |
|
93 | |
|
94 | 0 | final TestSetRunListener reporterManager = new TestSetRunListener( reports, globalRunStatistics ); |
95 | 0 | if ( first == null ) |
96 | |
{ |
97 | 0 | synchronized ( lock ) |
98 | |
{ |
99 | 0 | if ( first == null ) |
100 | |
{ |
101 | 0 | first = reporterManager; |
102 | 0 | reporterManager.runStarting(); |
103 | |
} |
104 | 0 | } |
105 | |
} |
106 | 0 | return reporterManager; |
107 | |
} |
108 | |
|
109 | |
|
110 | |
public RunResult close() |
111 | |
{ |
112 | 0 | consoleOutputCapture.restoreStreams(); |
113 | 0 | warnIfNoTests(); |
114 | 0 | synchronized ( lock ) |
115 | |
{ |
116 | 0 | if ( first != null ) |
117 | |
{ |
118 | 0 | first.runCompleted(); |
119 | |
} |
120 | 0 | return globalRunStatistics.getRunResult(); |
121 | 0 | } |
122 | |
} |
123 | |
|
124 | |
protected List instantiateReportsNewStyle( List reportDefinitions, ReporterConfiguration reporterConfiguration, |
125 | |
ClassLoader classLoader ) |
126 | |
{ |
127 | 0 | List reports = new ArrayList(); |
128 | |
|
129 | 0 | for ( Iterator i = reportDefinitions.iterator(); i.hasNext(); ) |
130 | |
{ |
131 | |
|
132 | 0 | String className = (String) i.next(); |
133 | |
|
134 | 0 | Reporter report = instantiateReportNewStyle( className, reporterConfiguration, classLoader ); |
135 | |
|
136 | 0 | reports.add( report ); |
137 | 0 | } |
138 | |
|
139 | 0 | return reports; |
140 | |
} |
141 | |
|
142 | |
private static Reporter instantiateReportNewStyle( String className, ReporterConfiguration params, |
143 | |
ClassLoader classLoader ) |
144 | |
{ |
145 | 0 | Class clazz = ReflectionUtils.loadClass( classLoader, className ); |
146 | |
|
147 | 0 | if ( params != null ) |
148 | |
{ |
149 | 0 | Class[] paramTypes = new Class[1]; |
150 | 0 | paramTypes[0] = ReflectionUtils.loadClass( classLoader, ReporterConfiguration.class.getName() ); |
151 | 0 | Constructor constructor = ReflectionUtils.getConstructor( clazz, paramTypes ); |
152 | 0 | return (Reporter) ReflectionUtils.newInstance( constructor, new Object[]{ params } ); |
153 | |
} |
154 | |
else |
155 | |
{ |
156 | |
try |
157 | |
{ |
158 | 0 | return (Reporter) clazz.newInstance(); |
159 | |
} |
160 | 0 | catch ( IllegalAccessException e ) |
161 | |
{ |
162 | 0 | throw new SurefireReflectionException( e ); |
163 | |
} |
164 | 0 | catch ( InstantiationException e ) |
165 | |
{ |
166 | 0 | throw new SurefireReflectionException( e ); |
167 | |
} |
168 | |
} |
169 | |
|
170 | |
} |
171 | |
|
172 | |
public DirectConsoleReporter createConsoleReporter() |
173 | |
{ |
174 | 0 | return new DefaultDirectConsoleReporter( reporterConfiguration.getOriginalSystemOut() ); |
175 | |
} |
176 | |
|
177 | |
private void warnIfNoTests() |
178 | |
{ |
179 | 0 | if ( getGlobalRunStatistics().getRunResult().getCompletedCount() == 0 ) |
180 | |
{ |
181 | 0 | final List target = |
182 | |
instantiateReportsNewStyle( reportDefinitions, reporterConfiguration, surefireClassLoader ); |
183 | 0 | new MulticastingReporter( target ).writeMessage( "There are no tests to run." ); |
184 | |
} |
185 | 0 | } |
186 | |
} |