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