Coverage Report - org.apache.maven.surefire.booter.output.FileOutputConsumerProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
FileOutputConsumerProxy
66%
29/44
50%
5/10
2.111
 
 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  4
     private static final String USER_DIR = System.getProperty( "user.dir" );
 43  
 
 44  4
     private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
 45  
 
 46  
     private File reportsDirectory;
 47  
 
 48  
     private PrintWriter printWriter;
 49  
     
 50  24
     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  0
         this( outputConsumer, new File( USER_DIR ) );
 59  0
     }
 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  24
         super( outputConsumer );
 69  24
         this.setReportsDirectory( reportsDirectory );
 70  24
     }
 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  24
         this.reportsDirectory = reportsDirectory;
 80  24
     }
 81  
 
 82  
     /**
 83  
      * Get the directory where reports will be saved
 84  
      */
 85  
     public File getReportsDirectory()
 86  
     {
 87  24
         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  44
         this.printWriter = writer;
 98  44
     }
 99  
 
 100  
     /**
 101  
      * Get the {@link PrintWriter} used for the current test suite
 102  
      */
 103  
     public PrintWriter getPrintWriter()
 104  
     {
 105  76
         return printWriter;
 106  
     }
 107  
 
 108  
     public void testSetStarting( ReportEntry reportEntry )
 109  
     {
 110  24
         if ( getPrintWriter() != null )
 111  
         {
 112  0
             throw new IllegalStateException( "testSetStarting called twice" );
 113  
         }
 114  24
         File file = new File( getReportsDirectory(), reportEntry.getName() + "-output.txt" );
 115  
         try
 116  
         {
 117  24
             setPrintWriter( new PrintWriter( new BufferedWriter( new FileWriter( file ) ) ) );
 118  
         }
 119  0
         catch ( IOException e )
 120  
         {
 121  0
             throw new NestedRuntimeException( e );
 122  24
         }
 123  24
         super.testSetStarting( reportEntry );
 124  24
     }
 125  
 
 126  
     public void testSetCompleted()
 127  
     {
 128  20
         if ( getPrintWriter() == null )
 129  
         {
 130  0
             throw new IllegalStateException( "testSetCompleted called before testSetStarting" );
 131  
         }
 132  20
         if ( outputBuffer.length() > 0 )
 133  
         {
 134  0
             getPrintWriter().write( outputBuffer.toString() );
 135  0
             getPrintWriter().write( LINE_SEPARATOR );
 136  0
             outputBuffer.setLength( 0 );
 137  
         }
 138  20
         getPrintWriter().close();
 139  20
         setPrintWriter( null );
 140  20
         super.testSetCompleted();
 141  20
     }
 142  
 
 143  
     /**
 144  
      * Write the output to the current test file
 145  
      */
 146  
     public void consumeOutputLine( String line )
 147  
     {
 148  4
         if ( getPrintWriter() == null )
 149  
         {
 150  0
             outputBuffer.append( line );
 151  0
             outputBuffer.append( LINE_SEPARATOR );
 152  0
             return;
 153  
         }
 154  
         
 155  4
         if ( outputBuffer.length() > 0 )
 156  
         {
 157  0
             getPrintWriter().write( outputBuffer.toString() );
 158  0
             getPrintWriter().write( LINE_SEPARATOR );
 159  0
             outputBuffer.setLength( 0 );
 160  
         }
 161  4
         getPrintWriter().write( line );
 162  4
         getPrintWriter().write( LINE_SEPARATOR );
 163  4
     }
 164  
 
 165  
 }