Coverage Report - org.apache.maven.plugin.surefire.booterclient.output.FileOutputConsumerProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
FileOutputConsumerProxy
0 %
0/37
0 %
0/12
3,75
 
 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  0
     private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
 46  
 
 47  
     private final File reportsDirectory;
 48  
 
 49  0
     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  0
         super( outputConsumer );
 62  0
         this.reportsDirectory = reportsDirectory;
 63  0
     }
 64  
 
 65  
     public void testSetStarting( ReportEntry reportEntry )
 66  
     {
 67  0
         if ( printWriter != null )
 68  
         {
 69  0
             throw new IllegalStateException( "testSetStarting called twice" );
 70  
         }
 71  
 
 72  0
         if ( !reportsDirectory.exists() )
 73  
         {
 74  
             //noinspection ResultOfMethodCallIgnored
 75  0
             reportsDirectory.mkdirs();
 76  
         }
 77  
 
 78  0
         File file = new File( reportsDirectory, reportEntry.getName() + "-output.txt" );
 79  
         try
 80  
         {
 81  0
             this.printWriter = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) );
 82  
         }
 83  0
         catch ( IOException e )
 84  
         {
 85  0
             throw new NestedRuntimeException( e );
 86  0
         }
 87  0
         super.testSetStarting( reportEntry );
 88  0
     }
 89  
 
 90  
     public void testSetCompleted()
 91  
     {
 92  0
         if ( printWriter == null )
 93  
         {
 94  0
             throw new IllegalStateException( "testSetCompleted called before testSetStarting" );
 95  
         }
 96  0
         if ( outputBuffer.length() > 0 )
 97  
         {
 98  0
             printWriter.write( outputBuffer.toString() );
 99  0
             printWriter.write( LINE_SEPARATOR );
 100  0
             outputBuffer.setLength( 0 );
 101  
         }
 102  0
         printWriter.close();
 103  0
         this.printWriter = null;
 104  0
         super.testSetCompleted();
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Write the output to the current test file
 109  
      * <p/>
 110  
      */
 111  
     public void consumeOutputLine( String line )
 112  
     {
 113  0
         if ( printWriter == null )
 114  
         {
 115  0
             outputBuffer.append( line );
 116  0
             outputBuffer.append( LINE_SEPARATOR );
 117  0
             return;
 118  
         }
 119  
 
 120  0
         if ( outputBuffer.length() > 0 )
 121  
         {
 122  0
             printWriter.write( outputBuffer.toString() );
 123  0
             printWriter.write( LINE_SEPARATOR );
 124  0
             outputBuffer.setLength( 0 );
 125  
         }
 126  0
         printWriter.write( line );
 127  0
         printWriter.write( LINE_SEPARATOR );
 128  0
     }
 129  
 
 130  
 }