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.BufferedOutputStream;
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.OutputStream;
27  
28  import org.apache.maven.surefire.report.ReportEntry;
29  
30  import static org.apache.maven.plugin.surefire.report.FileReporter.getReportFile;
31  
32  /**
33   * Surefire output consumer proxy that writes test output to a {@link java.io.File} for each test suite.
34   * <br>
35   * This class is not threadsafe, but can be serially handed off from thread to thread.
36   *
37   * @author Kristian Rosenvold
38   * @author Carlos Sanchez
39   */
40  public class ConsoleOutputFileReporter
41      implements TestcycleConsoleOutputReceiver
42  {
43      private final File reportsDirectory;
44  
45      private final String reportNameSuffix;
46  
47      private String reportEntryName;
48  
49      private OutputStream fileOutputStream;
50  
51      public ConsoleOutputFileReporter( File reportsDirectory, String reportNameSuffix )
52      {
53          this.reportsDirectory = reportsDirectory;
54          this.reportNameSuffix = reportNameSuffix;
55      }
56  
57      @Override
58      public void testSetStarting( ReportEntry reportEntry )
59      {
60          close();
61          reportEntryName = reportEntry.getName();
62      }
63  
64      @Override
65      public void testSetCompleted( ReportEntry report )
66      {
67      }
68  
69      @Override
70      @SuppressWarnings( "checkstyle:emptyblock" )
71      public void close()
72      {
73          if ( fileOutputStream != null )
74          {
75              //noinspection EmptyCatchBlock
76              try
77              {
78                  fileOutputStream.flush();
79              }
80              catch ( IOException e )
81              {
82              }
83              finally
84              {
85                  try
86                  {
87                      fileOutputStream.close();
88                  }
89                  catch ( IOException ignored )
90                  {
91                  }
92              }
93              fileOutputStream = null;
94          }
95      }
96  
97      @Override
98      public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
99      {
100         try
101         {
102             if ( fileOutputStream == null )
103             {
104                 if ( !reportsDirectory.exists() )
105                 {
106                     //noinspection ResultOfMethodCallIgnored
107                     reportsDirectory.mkdirs();
108                 }
109                 File file = getReportFile( reportsDirectory, reportEntryName, reportNameSuffix, "-output.txt" );
110                 fileOutputStream = new BufferedOutputStream( new FileOutputStream( file ), 16 * 1024 );
111             }
112             fileOutputStream.write( buf, off, len );
113         }
114         catch ( IOException e )
115         {
116             throw new RuntimeException( e );
117         }
118     }
119 }