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