View Javadoc

1   package org.apache.maven.plugin.surefire.booterclient.output;
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 org.apache.maven.surefire.report.ReportEntry;
23  import org.apache.maven.surefire.util.NestedRuntimeException;
24  
25  import java.io.BufferedWriter;
26  import java.io.File;
27  import java.io.FileWriter;
28  import java.io.IOException;
29  import java.io.PrintWriter;
30  
31  /**
32   * Surefire output consumer proxy that writes test output to a {@link File} for each test suite.
33   *
34   * This class is not threadsafe, but can be encapsulated with a SynchronizedOutputConsumer. It may still be
35   * accessed from different threads (serially).
36   *
37   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
38   * @version $Id: FileOutputConsumerProxy.java 1049843 2010-12-16 09:32:19Z krosenvold $
39   * @since 2.1
40   */
41  public class FileOutputConsumerProxy
42      extends OutputConsumerProxy
43  {
44  
45      private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
46  
47      private final File reportsDirectory;
48  
49      private final StringBuffer outputBuffer = new StringBuffer();
50  
51      private volatile PrintWriter printWriter;
52  
53      /**
54       * Create a consumer that will write to a {@link File} for each test
55       *
56       * @param outputConsumer   the output consumer
57       * @param reportsDirectory directory where files will be saved
58       */
59      public FileOutputConsumerProxy( OutputConsumer outputConsumer, File reportsDirectory )
60      {
61          super( outputConsumer );
62          this.reportsDirectory = reportsDirectory;
63      }
64  
65      public void testSetStarting( ReportEntry reportEntry )
66      {
67          if ( printWriter != null )
68          {
69              throw new IllegalStateException( "testSetStarting called twice" );
70          }
71  
72          if ( !reportsDirectory.exists() )
73          {
74              //noinspection ResultOfMethodCallIgnored
75              reportsDirectory.mkdirs();
76          }
77  
78          File file = new File( reportsDirectory, reportEntry.getName() + "-output.txt" );
79          try
80          {
81              this.printWriter = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) );
82          }
83          catch ( IOException e )
84          {
85              throw new NestedRuntimeException( e );
86          }
87          super.testSetStarting( reportEntry );
88      }
89  
90      public void testSetCompleted()
91      {
92          if ( printWriter == null )
93          {
94              throw new IllegalStateException( "testSetCompleted called before testSetStarting" );
95          }
96          if ( outputBuffer.length() > 0 )
97          {
98              printWriter.write( outputBuffer.toString() );
99              printWriter.write( LINE_SEPARATOR );
100             outputBuffer.setLength( 0 );
101         }
102         printWriter.close();
103         this.printWriter = null;
104         super.testSetCompleted();
105     }
106 
107     /**
108      * Write the output to the current test file
109      * <p/>
110      */
111     public void consumeOutputLine( String line )
112     {
113         if ( printWriter == null )
114         {
115             outputBuffer.append( line );
116             outputBuffer.append( LINE_SEPARATOR );
117             return;
118         }
119 
120         if ( outputBuffer.length() > 0 )
121         {
122             printWriter.write( outputBuffer.toString() );
123             printWriter.write( LINE_SEPARATOR );
124             outputBuffer.setLength( 0 );
125         }
126         printWriter.write( line );
127         printWriter.write( LINE_SEPARATOR );
128     }
129 
130 }