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