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     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         clearCapture();
190     }
191 
192     public void testFailed( ReportEntry reportEntry )
193     {
194         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.FAILURE );
195         detailsForThis.testFailure( wrapped );
196         if ( statisticsReporter != null )
197         {
198             statisticsReporter.testFailed( reportEntry );
199         }
200         clearCapture();
201     }
202 
203     // ----------------------------------------------------------------------
204     // Counters
205     // ----------------------------------------------------------------------
206 
207     public void testSkipped( ReportEntry reportEntry )
208     {
209         WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.SKIPPED );
210 
211         detailsForThis.testSkipped( wrapped );
212         if ( statisticsReporter != null )
213         {
214             statisticsReporter.testSkipped( reportEntry );
215         }
216         clearCapture();
217     }
218 
219     public void testExecutionSkippedByUser()
220     {
221     }
222 
223     public void testAssumptionFailure( ReportEntry report )
224     {
225         testSkipped( report );
226     }
227 
228     private WrappedReportEntry wrap( ReportEntry other, ReportEntryType reportEntryType )
229     {
230         final int estimatedElapsed;
231         if ( reportEntryType != ReportEntryType.SKIPPED )
232         {
233             if ( other.getElapsed() != null )
234             {
235                 estimatedElapsed = other.getElapsed();
236             }
237             else
238             {
239                 estimatedElapsed = detailsForThis.getElapsedSinceLastStart();
240             }
241         }
242         else
243         {
244             estimatedElapsed = 0;
245         }
246 
247         return new WrappedReportEntry( other, reportEntryType, estimatedElapsed, testStdOut, testStdErr );
248     }
249 
250     private WrappedReportEntry wrapTestSet( ReportEntry other )
251     {
252         return new WrappedReportEntry( other, null, other.getElapsed() != null
253             ? other.getElapsed()
254             : detailsForThis.getElapsedSinceTestSetStart(), testStdOut, testStdErr );
255     }
256 
257     public void close()
258     {
259         if ( consoleOutputReceiver != null )
260         {
261             consoleOutputReceiver.close();
262         }
263     }
264 
265     public void  addTestMethodStats()
266     {
267         for ( WrappedReportEntry reportEntry : detailsForThis.getReportEntries() )
268         {
269             TestMethodStats methodStats =
270                 new TestMethodStats( reportEntry.getClassMethodName(), reportEntry.getReportEntryType(),
271                                      reportEntry.getStackTraceWriter() );
272             testMethodStats.add( methodStats );
273         }
274     }
275 
276     public List<TestMethodStats> getTestMethodStats()
277     {
278         return testMethodStats;
279     }
280 }