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