View Javadoc

1   package org.apache.maven.surefire.booter.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   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
35   * @version $Id: FileOutputConsumerProxy.java 594580 2007-11-13 16:59:18Z brett $
36   * @since 2.1
37   */
38  public class FileOutputConsumerProxy
39      extends OutputConsumerProxy
40  {
41  
42      private static final String USER_DIR = System.getProperty( "user.dir" );
43  
44      private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
45  
46      private File reportsDirectory;
47  
48      private PrintWriter printWriter;
49      
50      private StringBuffer outputBuffer = new StringBuffer();
51      
52      /**
53       * Create a consumer that will write to a {@link File} for each test.
54       * Files will be saved in working directory.
55       */
56      public FileOutputConsumerProxy( OutputConsumer outputConsumer )
57      {
58          this( outputConsumer, new File( USER_DIR ) );
59      }
60  
61      /**
62       * Create a consumer that will write to a {@link File} for each test
63       *
64       * @param reportsDirectory directory where files will be saved
65       */
66      public FileOutputConsumerProxy( OutputConsumer outputConsumer, File reportsDirectory )
67      {
68          super( outputConsumer );
69          this.setReportsDirectory( reportsDirectory );
70      }
71  
72      /**
73       * Set the directory where reports will be saved
74       *
75       * @param reportsDirectory the directory
76       */
77      public void setReportsDirectory( File reportsDirectory )
78      {
79          this.reportsDirectory = reportsDirectory;
80      }
81  
82      /**
83       * Get the directory where reports will be saved
84       */
85      public File getReportsDirectory()
86      {
87          return reportsDirectory;
88      }
89  
90      /**
91       * Set the {@link PrintWriter} used for the current test suite
92       *
93       * @param writer
94       */
95      public void setPrintWriter( PrintWriter writer )
96      {
97          this.printWriter = writer;
98      }
99  
100     /**
101      * Get the {@link PrintWriter} used for the current test suite
102      */
103     public PrintWriter getPrintWriter()
104     {
105         return printWriter;
106     }
107 
108     public void testSetStarting( ReportEntry reportEntry )
109     {
110         if ( getPrintWriter() != null )
111         {
112             throw new IllegalStateException( "testSetStarting called twice" );
113         }
114         File file = new File( getReportsDirectory(), reportEntry.getName() + "-output.txt" );
115         try
116         {
117             setPrintWriter( new PrintWriter( new BufferedWriter( new FileWriter( file ) ) ) );
118         }
119         catch ( IOException e )
120         {
121             throw new NestedRuntimeException( e );
122         }
123         super.testSetStarting( reportEntry );
124     }
125 
126     public void testSetCompleted()
127     {
128         if ( getPrintWriter() == null )
129         {
130             throw new IllegalStateException( "testSetCompleted called before testSetStarting" );
131         }
132         if ( outputBuffer.length() > 0 )
133         {
134             getPrintWriter().write( outputBuffer.toString() );
135             getPrintWriter().write( LINE_SEPARATOR );
136             outputBuffer.setLength( 0 );
137         }
138         getPrintWriter().close();
139         setPrintWriter( null );
140         super.testSetCompleted();
141     }
142 
143     /**
144      * Write the output to the current test file
145      */
146     public void consumeOutputLine( String line )
147     {
148         if ( getPrintWriter() == null )
149         {
150             outputBuffer.append( line );
151             outputBuffer.append( LINE_SEPARATOR );
152             return;
153         }
154         
155         if ( outputBuffer.length() > 0 )
156         {
157             getPrintWriter().write( outputBuffer.toString() );
158             getPrintWriter().write( LINE_SEPARATOR );
159             outputBuffer.setLength( 0 );
160         }
161         getPrintWriter().write( line );
162         getPrintWriter().write( LINE_SEPARATOR );
163     }
164 
165 }