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