View Javadoc

1   package org.apache.maven.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  
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 encapsulated with a SynchronizedOutputConsumer. It may still be
34   * accessed from different threads (serially).
35   *
36   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
37   * @version $Id: ConsoleOutputFileReporter.java 1143207 2011-07-05 21:37:57Z pgier $
38   * @since 2.1
39   */
40  public class ConsoleOutputFileReporter
41      implements Reporter
42  {
43      private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
44  
45      private final File reportsDirectory;
46  
47      private PrintWriter printWriter = null;
48  
49      private String reportEntryName;
50  
51      private ReportEntry report;
52  
53      private final String reportNameSuffix;
54  
55      public ConsoleOutputFileReporter( File reportsDirectory )
56      {
57          this( reportsDirectory, null );
58      }
59  
60      public ConsoleOutputFileReporter( File reportsDirectory, String reportNameSuffix )
61      {
62          this.reportsDirectory = reportsDirectory;
63          this.reportNameSuffix = reportNameSuffix;
64      }
65  
66      public void testSetStarting( ReportEntry reportEntry )
67      {
68          this.reportEntryName = reportEntry.getName();
69      }
70  
71      public void testSetCompleted( ReportEntry report )
72          throws ReporterException
73      {
74          if ( printWriter != null )
75          {
76              printWriter.close();
77              printWriter = null;
78          }
79      }
80  
81      public void writeMessage( byte[] b, int off, int len )
82      {
83          try
84          {
85              if ( printWriter == null )
86              {
87                  if ( !reportsDirectory.exists() )
88                  {
89                      //noinspection ResultOfMethodCallIgnored
90                      reportsDirectory.mkdirs();
91                  }
92                  File file = AbstractFileReporter.getReportFile( reportsDirectory, reportEntryName, reportNameSuffix, "-output.txt" );
93                  printWriter = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) );
94              }
95              printWriter.write( new String( b, off, len ) );
96              printWriter.write( LINE_SEPARATOR );
97  
98          }
99          catch ( IOException e )
100         {
101             throw new NestedRuntimeException( e );
102         }
103     }
104 
105 
106     public void testStarting( ReportEntry report )
107     {
108     }
109 
110     public void testSucceeded( ReportEntry report )
111     {
112     }
113 
114     public void testSkipped( ReportEntry report )
115     {
116     }
117 
118     public void testError( ReportEntry report, String stdOut, String stdErr )
119     {
120     }
121 
122     public void testFailed( ReportEntry report, String stdOut, String stdErr )
123     {
124     }
125 
126     public void writeMessage( String message )
127     {
128     }
129 
130 
131     public void reset()
132     {
133     }
134 }