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 private final RunStatistics globalRunStatistics = new RunStatistics();
55
56 private final ReporterConfiguration reporterConfiguration;
57
58 private RunReporter first;
59
60 private final Object lock = new Object();
61
62 private final ConsoleOutputCapture consoleOutputCapture;
63
64 public ReporterManagerFactory( ClassLoader surefireClassLoader, ReporterConfiguration reporterConfiguration )
65 {
66 this.reportDefinitions = reporterConfiguration.getReports();
67 this.surefireClassLoader = surefireClassLoader;
68 this.reporterConfiguration = reporterConfiguration;
69 SystemConsoleOutputReceiver systemConsoleOutputReceiver =
70 new SystemConsoleOutputReceiver( reporterConfiguration.getOriginalSystemOut(),
71 reporterConfiguration.getOriginalSystemErr() );
72
73 final DefaultConsoleOutputReceiver target =
74 new DefaultConsoleOutputReceiver( systemConsoleOutputReceiver, true );
75 this.consoleOutputCapture = new ConsoleOutputCapture( target );
76 }
77
78 public RunStatistics getGlobalRunStatistics()
79 {
80 return globalRunStatistics;
81 }
82
83 public RunListener createReporter()
84 {
85 return setupReporter(
86 instantiateReportsNewStyle( reportDefinitions, reporterConfiguration, surefireClassLoader ) );
87 }
88
89
90 private RunListener setupReporter( List reports )
91 {
92
93
94 final TestSetRunListener reporterManager = new TestSetRunListener( reports, globalRunStatistics );
95 if ( first == null )
96 {
97 synchronized ( lock )
98 {
99 if ( first == null )
100 {
101 first = reporterManager;
102 reporterManager.runStarting();
103 }
104 }
105 }
106 return reporterManager;
107 }
108
109
110 public RunResult close()
111 {
112 consoleOutputCapture.restoreStreams();
113 warnIfNoTests();
114 synchronized ( lock )
115 {
116 if ( first != null )
117 {
118 first.runCompleted();
119 }
120 return globalRunStatistics.getRunResult();
121 }
122 }
123
124 protected List instantiateReportsNewStyle( List reportDefinitions, ReporterConfiguration reporterConfiguration,
125 ClassLoader classLoader )
126 {
127 List reports = new ArrayList();
128
129 for ( Iterator i = reportDefinitions.iterator(); i.hasNext(); )
130 {
131
132 String className = (String) i.next();
133
134 Reporter report = instantiateReportNewStyle( className, reporterConfiguration, classLoader );
135
136 reports.add( report );
137 }
138
139 return reports;
140 }
141
142 private static Reporter instantiateReportNewStyle( String className, ReporterConfiguration params,
143 ClassLoader classLoader )
144 {
145 Class clazz = ReflectionUtils.loadClass( classLoader, className );
146
147 if ( params != null )
148 {
149 Class[] paramTypes = new Class[1];
150 paramTypes[0] = ReflectionUtils.loadClass( classLoader, ReporterConfiguration.class.getName() );
151 Constructor constructor = ReflectionUtils.getConstructor( clazz, paramTypes );
152 return (Reporter) ReflectionUtils.newInstance( constructor, new Object[]{ params } );
153 }
154 else
155 {
156 try
157 {
158 return (Reporter) clazz.newInstance();
159 }
160 catch ( IllegalAccessException e )
161 {
162 throw new SurefireReflectionException( e );
163 }
164 catch ( InstantiationException e )
165 {
166 throw new SurefireReflectionException( e );
167 }
168 }
169
170 }
171
172 public DirectConsoleReporter createConsoleReporter()
173 {
174 return new DefaultDirectConsoleReporter( reporterConfiguration.getOriginalSystemOut() );
175 }
176
177 private void warnIfNoTests()
178 {
179 if ( getGlobalRunStatistics().getRunResult().getCompletedCount() == 0 )
180 {
181 final List target =
182 instantiateReportsNewStyle( reportDefinitions, reporterConfiguration, surefireClassLoader );
183 new MulticastingReporter( target ).writeMessage( "There are no tests to run." );
184 }
185 }
186 }