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  import org.apache.maven.surefire.util.NestedRuntimeException;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.List;
34  
35  /**
36   * Reports data for a single test set.
37   * <p/>
38   *
39   * @author Kristian Rosenvold
40   */
41  public class TestSetRunListener
42      implements RunListener, ConsoleOutputReceiver, ConsoleLogger
43  {
44      private final RunStatistics globalStatistics;
45  
46      private final TestSetStats detailsForThis;
47  
48      private Utf8RecodingDeferredFileOutputStream testStdOut = initDeferred( "stdout" );
49  
50      private Utf8RecodingDeferredFileOutputStream testStdErr = initDeferred( "stderr" );
51  
52      private Utf8RecodingDeferredFileOutputStream initDeferred( String channel )
53      {
54          return new Utf8RecodingDeferredFileOutputStream( channel );
55      }
56  
57      private final TestcycleConsoleOutputReceiver consoleOutputReceiver;
58  
59      private final boolean briefOrPlainFormat;
60  
61      private final StatelessXmlReporter simpleXMLReporter;
62  
63      private final ConsoleReporter consoleReporter;
64  
65      private final FileReporter fileReporter;
66  
67      private final StatisticsReporter statisticsReporter;
68  
69      public TestSetRunListener( ConsoleReporter consoleReporter, FileReporter fileReporter,
70                                 StatelessXmlReporter simpleXMLReporter,
71                                 TestcycleConsoleOutputReceiver consoleOutputReceiver,
72                                 StatisticsReporter statisticsReporter, RunStatistics globalStats, boolean trimStackTrace,
73                                 boolean isPlainFormat, boolean briefOrPlainFormat )
74      {
75          this.consoleReporter = consoleReporter;
76          this.fileReporter = fileReporter;
77          this.statisticsReporter = statisticsReporter;
78          this.simpleXMLReporter = simpleXMLReporter;
79          this.consoleOutputReceiver = consoleOutputReceiver;
80          this.briefOrPlainFormat = briefOrPlainFormat;
81          this.detailsForThis = new TestSetStats( trimStackTrace, isPlainFormat );
82          this.globalStatistics = globalStats;
83      }
84  
85      public void info( String message )
86      {
87          if ( consoleReporter != null )
88          {
89              consoleReporter.writeMessage( message );
90          }
91      }
92  
93      public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
94      {
95          try
96          {
97              if ( stdout )
98              {
99                  testStdOut.write( buf, off, len );
100             }
101             else
102             {
103                 testStdErr.write( buf, off, len );
104             }
105         }
106         catch ( IOException e )
107         {
108             throw new NestedRuntimeException( e );
109         }
110         consoleOutputReceiver.writeTestOutput( buf, off, len, stdout );
111     }
112 
113     public void testSetStarting( ReportEntry report )
114     {
115         detailsForThis.testSetStart();
116         if ( consoleReporter != null )
117         {
118             consoleReporter.testSetStarting( report );
119         }
120         consoleOutputReceiver.testSetStarting( report );
121     }
122 
123     public void clearCapture()
124     {
125         testStdOut = initDeferred( "stdout" );
126         testStdErr = initDeferred( "stderr" );
127     }
128 
129     public void testSetCompleted( ReportEntry report )
130     {
131         WrappedReportEntry wrap = wrapTestSet( report );
132         List<String> testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : null;
133         if ( consoleReporter != null )
134         {
135             consoleReporter.testSetCompleted( wrap, detailsForThis, testResults );
136         }
137         consoleOutputReceiver.testSetCompleted( wrap );
138         if ( fileReporter != null )
139         {
140             fileReporter.testSetCompleted( wrap, detailsForThis, testResults );
141         }
142         if ( simpleXMLReporter != null )
143         {
144             simpleXMLReporter.testSetCompleted( wrap, detailsForThis );
145         }
146         if ( statisticsReporter != null )
147         {
148             statisticsReporter.testSetCompleted();
149         }
150         if ( consoleReporter != null )
151         {
152             consoleReporter.reset();
153         }
154 
155         globalStatistics.add( detailsForThis );
156         detailsForThis.reset();
157 
158     }
159 
160     // ----------------------------------------------------------------------
161     // Test
162     // ----------------------------------------------------------------------
163 
164     public void testStarting( ReportEntry report )
165     {
166         detailsForThis.testStart();
167 
168     }
169 
170     public void testSucceeded( ReportEntry reportEntry )
171     {
172         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.success );
173         detailsForThis.testSucceeded( wrapped );
174         if ( statisticsReporter != null )
175         {
176             statisticsReporter.testSucceeded( reportEntry );
177         }
178         clearCapture();
179     }
180 
181     public void testError( ReportEntry reportEntry )
182     {
183         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.error );
184         detailsForThis.testError( wrapped );
185         if ( statisticsReporter != null )
186         {
187             statisticsReporter.testError( reportEntry );
188         }
189         globalStatistics.addErrorSource( reportEntry.getStackTraceWriter() );
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         globalStatistics.addFailureSource( reportEntry.getStackTraceWriter() );
202         clearCapture();
203     }
204 
205     // ----------------------------------------------------------------------
206     // Counters
207     // ----------------------------------------------------------------------
208 
209     public void testSkipped( ReportEntry reportEntry )
210     {
211         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.skipped );
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 }