View Javadoc
1   package org.apache.maven.plugin.surefire.report;
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.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.plugin.surefire.runorder.StatisticsReporter;
27  import org.apache.maven.surefire.report.ConsoleLogger;
28  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
29  import org.apache.maven.surefire.report.ReportEntry;
30  import org.apache.maven.surefire.report.RunListener;
31  
32  /**
33   * Reports data for a single test set.
34   * <p/>
35   *
36   * @author Kristian Rosenvold
37   */
38  public class TestSetRunListener
39      implements RunListener, ConsoleOutputReceiver, ConsoleLogger
40  {
41      private final TestSetStats detailsForThis;
42  
43      private List<TestMethodStats> testMethodStats;
44  
45      private Utf8RecodingDeferredFileOutputStream testStdOut = initDeferred( "stdout" );
46  
47      private Utf8RecodingDeferredFileOutputStream testStdErr = initDeferred( "stderr" );
48  
49      private Utf8RecodingDeferredFileOutputStream initDeferred( String channel )
50      {
51          return new Utf8RecodingDeferredFileOutputStream( channel );
52      }
53  
54      private final TestcycleConsoleOutputReceiver consoleOutputReceiver;
55  
56      private final boolean briefOrPlainFormat;
57  
58      private final StatelessXmlReporter simpleXMLReporter;
59  
60      private final ConsoleReporter consoleReporter;
61  
62      private final FileReporter fileReporter;
63  
64      private final StatisticsReporter statisticsReporter;
65  
66      @SuppressWarnings( "checkstyle:parameternumber" )
67      public TestSetRunListener( ConsoleReporter consoleReporter, FileReporter fileReporter,
68                                 StatelessXmlReporter simpleXMLReporter,
69                                 TestcycleConsoleOutputReceiver consoleOutputReceiver,
70                                 StatisticsReporter statisticsReporter, boolean trimStackTrace,
71                                 boolean isPlainFormat, boolean briefOrPlainFormat )
72      {
73          this.consoleReporter = consoleReporter;
74          this.fileReporter = fileReporter;
75          this.statisticsReporter = statisticsReporter;
76          this.simpleXMLReporter = simpleXMLReporter;
77          this.consoleOutputReceiver = consoleOutputReceiver;
78          this.briefOrPlainFormat = briefOrPlainFormat;
79          this.detailsForThis = new TestSetStats( trimStackTrace, isPlainFormat );
80          this.testMethodStats = new ArrayList<TestMethodStats>(  );
81      }
82  
83      public void info( String message )
84      {
85          if ( consoleReporter != null )
86          {
87              consoleReporter.writeMessage( message );
88          }
89      }
90  
91      public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
92      {
93          try
94          {
95              if ( stdout )
96              {
97                  testStdOut.write( buf, off, len );
98              }
99              else
100             {
101                 testStdErr.write( buf, off, len );
102             }
103         }
104         catch ( IOException e )
105         {
106             throw new RuntimeException( e );
107         }
108         consoleOutputReceiver.writeTestOutput( buf, off, len, stdout );
109     }
110 
111     public void testSetStarting( ReportEntry report )
112     {
113         detailsForThis.testSetStart();
114         if ( consoleReporter != null )
115         {
116             consoleReporter.testSetStarting( report );
117         }
118         consoleOutputReceiver.testSetStarting( report );
119     }
120 
121     public void clearCapture()
122     {
123         testStdOut = initDeferred( "stdout" );
124         testStdErr = initDeferred( "stderr" );
125     }
126 
127     public void testSetCompleted( ReportEntry report )
128     {
129         WrappedReportEntry wrap = wrapTestSet( report );
130         List<String> testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : null;
131         if ( fileReporter != null )
132         {
133             fileReporter.testSetCompleted( wrap, detailsForThis, testResults );
134         }
135         if ( simpleXMLReporter != null )
136         {
137             simpleXMLReporter.testSetCompleted( wrap, detailsForThis );
138         }
139         if ( statisticsReporter != null )
140         {
141             statisticsReporter.testSetCompleted();
142         }
143         if ( consoleReporter != null )
144         {
145             consoleReporter.testSetCompleted( wrap, detailsForThis, testResults );
146         }
147         consoleOutputReceiver.testSetCompleted( wrap );
148         if ( consoleReporter != null )
149         {
150             consoleReporter.reset();
151         }
152 
153         wrap.getStdout().free();
154         wrap.getStdErr().free();
155 
156         addTestMethodStats();
157         detailsForThis.reset();
158 
159     }
160 
161     // ----------------------------------------------------------------------
162     // Test
163     // ----------------------------------------------------------------------
164 
165     public void testStarting( ReportEntry report )
166     {
167         detailsForThis.testStart();
168 
169     }
170 
171     public void testSucceeded( ReportEntry reportEntry )
172     {
173         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.SUCCESS );
174         detailsForThis.testSucceeded( wrapped );
175         if ( statisticsReporter != null )
176         {
177             statisticsReporter.testSucceeded( reportEntry );
178         }
179         clearCapture();
180     }
181 
182     public void testError( ReportEntry reportEntry )
183     {
184         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.ERROR );
185         detailsForThis.testError( wrapped );
186         if ( statisticsReporter != null )
187         {
188             statisticsReporter.testError( reportEntry );
189         }
190         clearCapture();
191     }
192 
193     public void testFailed( ReportEntry reportEntry )
194     {
195         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.FAILURE );
196         detailsForThis.testFailure( wrapped );
197         if ( statisticsReporter != null )
198         {
199             statisticsReporter.testFailed( reportEntry );
200         }
201         clearCapture();
202     }
203 
204     // ----------------------------------------------------------------------
205     // Counters
206     // ----------------------------------------------------------------------
207 
208     public void testSkipped( ReportEntry reportEntry )
209     {
210         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.SKIPPED );
211 
212         detailsForThis.testSkipped( wrapped );
213         if ( statisticsReporter != null )
214         {
215             statisticsReporter.testSkipped( reportEntry );
216         }
217         clearCapture();
218     }
219 
220     public void testAssumptionFailure( ReportEntry report )
221     {
222         testSkipped( report );
223     }
224 
225     private WrappedReportEntry wrap( ReportEntry other, ReportEntryType reportEntryType )
226     {
227         final int estimatedElapsed;
228         if ( reportEntryType != ReportEntryType.SKIPPED )
229         {
230             if ( other.getElapsed() != null )
231             {
232                 estimatedElapsed = other.getElapsed();
233             }
234             else
235             {
236                 estimatedElapsed = detailsForThis.getElapsedSinceLastStart();
237             }
238         }
239         else
240         {
241             estimatedElapsed = 0;
242         }
243 
244         return new WrappedReportEntry( other, reportEntryType, estimatedElapsed, testStdOut, testStdErr );
245     }
246 
247     private WrappedReportEntry wrapTestSet( ReportEntry other )
248     {
249         return new WrappedReportEntry( other, null, other.getElapsed() != null
250             ? other.getElapsed()
251             : detailsForThis.getElapsedSinceTestSetStart(), testStdOut, testStdErr );
252     }
253 
254     public void close()
255     {
256         if ( consoleOutputReceiver != null )
257         {
258             consoleOutputReceiver.close();
259         }
260     }
261 
262     public void  addTestMethodStats()
263     {
264         for ( WrappedReportEntry reportEntry : detailsForThis.getReportEntries() )
265         {
266             TestMethodStats methodStats =
267                 new TestMethodStats( reportEntry.getClassMethodName(), reportEntry.getReportEntryType(),
268                                      reportEntry.getStackTraceWriter() );
269             testMethodStats.add( methodStats );
270         }
271     }
272 
273     public List<TestMethodStats> getTestMethodStats()
274     {
275         return testMethodStats;
276     }
277 }