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 DeferredFileOutputStream testStdOut = initDeferred( "stdout" );
49  
50      private DeferredFileOutputStream testStdErr = initDeferred( "stderr" );
51  
52      private DeferredFileOutputStream initDeferred( String channel )
53      {
54          return new DeferredFileOutputStream( 1000000, channel , "deferred", null );
55      }
56  
57  
58      private final TestcycleConsoleOutputReceiver consoleOutputReceiver;
59  
60      private final boolean briefOrPlainFormat;
61  
62      private final StatelessXmlReporter simpleXMLReporter;
63  
64      private final ConsoleReporter consoleReporter;
65  
66      private final FileReporter fileReporter;
67  
68      private final StatisticsReporter statisticsReporter;
69  
70      public TestSetRunListener( ConsoleReporter consoleReporter, FileReporter fileReporter,
71                                 StatelessXmlReporter simpleXMLReporter,
72                                 TestcycleConsoleOutputReceiver consoleOutputReceiver,
73                                 StatisticsReporter statisticsReporter, RunStatistics globalStats, boolean trimStackTrace,
74                                 boolean isPlainFormat, boolean briefOrPlainFormat )
75      {
76          this.consoleReporter = consoleReporter;
77          this.fileReporter = fileReporter;
78          this.statisticsReporter = statisticsReporter;
79          this.simpleXMLReporter = simpleXMLReporter;
80          this.consoleOutputReceiver = consoleOutputReceiver;
81          this.briefOrPlainFormat = briefOrPlainFormat;
82          this.detailsForThis = new TestSetStats( trimStackTrace, isPlainFormat );
83          this.globalStatistics = globalStats;
84      }
85  
86      public void info( String message )
87      {
88          if ( consoleReporter != null )
89          {
90              consoleReporter.writeMessage( message );
91          }
92      }
93  
94      public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
95      {
96          try
97          {
98              if ( stdout )
99              {
100                 testStdOut.write( buf, off, len );
101             }
102             else
103             {
104                 testStdErr.write( buf, off, len );
105             }
106         }
107         catch ( IOException e )
108         {
109             throw new NestedRuntimeException( e );
110         }
111         consoleOutputReceiver.writeTestOutput( buf, off, len, stdout );
112     }
113 
114     public void testSetStarting( ReportEntry report )
115     {
116         detailsForThis.testSetStart();
117         if ( consoleReporter != null )
118         {
119             consoleReporter.testSetStarting( report );
120         }
121         consoleOutputReceiver.testSetStarting( report );
122     }
123 
124     public void clearCapture()
125     {
126         testStdOut = initDeferred( "stdout" );
127         testStdErr = initDeferred( "stderr" );
128     }
129 
130     public void testSetCompleted( ReportEntry report )
131     {
132         WrappedReportEntry wrap = wrapTestSet( report );
133         List<String> testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : null;
134         if ( consoleReporter != null )
135         {
136             consoleReporter.testSetCompleted( wrap, detailsForThis, testResults );
137         }
138         consoleOutputReceiver.testSetCompleted( wrap );
139         if ( fileReporter != null )
140         {
141             fileReporter.testSetCompleted( wrap, detailsForThis, testResults );
142         }
143         if ( simpleXMLReporter != null )
144         {
145             simpleXMLReporter.testSetCompleted( wrap, detailsForThis );
146         }
147         if ( statisticsReporter != null )
148         {
149             statisticsReporter.testSetCompleted();
150         }
151         if ( consoleReporter != null )
152         {
153             consoleReporter.reset();
154         }
155 
156         globalStatistics.add( detailsForThis );
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         globalStatistics.addErrorSource( reportEntry.getStackTraceWriter() );
191         clearCapture();
192     }
193 
194     public void testFailed( ReportEntry reportEntry )
195     {
196         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.failure );
197         detailsForThis.testFailure( wrapped );
198         if ( statisticsReporter != null )
199         {
200             statisticsReporter.testFailed( reportEntry );
201         }
202         globalStatistics.addFailureSource( reportEntry.getStackTraceWriter() );
203         clearCapture();
204     }
205 
206     // ----------------------------------------------------------------------
207     // Counters
208     // ----------------------------------------------------------------------
209 
210     public void testSkipped( ReportEntry reportEntry )
211     {
212         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.skipped );
213         detailsForThis.testSkipped( wrapped );
214         if ( statisticsReporter != null )
215         {
216             statisticsReporter.testSkipped( reportEntry );
217         }
218         clearCapture();
219     }
220 
221     public void testAssumptionFailure( ReportEntry report )
222     {
223         testSkipped( report );
224     }
225 
226     private WrappedReportEntry wrap( ReportEntry other, ReportEntryType reportEntryType )
227     {
228         final int estimatedElapsed;
229         if ( reportEntryType != ReportEntryType.skipped )
230         {
231             if ( other.getElapsed() != null )
232             {
233                 estimatedElapsed = other.getElapsed();
234             }
235             else
236             {
237                 estimatedElapsed = detailsForThis.getElapsedSinceLastStart();
238             }
239         }
240         else
241         {
242             estimatedElapsed = 0;
243         }
244 
245         return new WrappedReportEntry( other, reportEntryType, estimatedElapsed, testStdOut, testStdErr );
246     }
247 
248     private WrappedReportEntry wrapTestSet( ReportEntry other )
249     {
250         return new WrappedReportEntry( other, null, other.getElapsed() != null
251             ? other.getElapsed()
252             : detailsForThis.getElapsedSinceTestSetStart(), testStdOut, testStdErr );
253     }
254 
255     public void close()
256     {
257         if ( consoleOutputReceiver != null )
258         {
259             consoleOutputReceiver.close();
260         }
261     }
262 }