Coverage Report - org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
ConsoleOutputFileReporter
78%
18/23
50%
3/6
2
 
 1  
 package org.apache.maven.plugin.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  
 import org.apache.maven.surefire.report.ReportEntry;
 28  
 import org.apache.maven.surefire.report.ReporterException;
 29  
 import org.apache.maven.surefire.util.NestedRuntimeException;
 30  
 
 31  
 /**
 32  
  * Surefire output consumer proxy that writes test output to a {@link java.io.File} for each test suite.
 33  
  * <p/>
 34  
  * This class is not threadsafe, but can be serially handed off from thread to thread.
 35  
  *
 36  
  * @author Kristian Rosenvold
 37  
  * @author Carlos Sanchez
 38  
  */
 39  
 public class ConsoleOutputFileReporter
 40  
     implements TestcycleConsoleOutputReceiver
 41  
 {
 42  
     private final File reportsDirectory;
 43  
 
 44  
     private final String reportNameSuffix;
 45  
 
 46  2
     private PrintWriter printWriter = null;
 47  
 
 48  
     private String reportEntryName;
 49  
 
 50  
     public ConsoleOutputFileReporter( File reportsDirectory, String reportNameSuffix )
 51  2
     {
 52  2
         this.reportsDirectory = reportsDirectory;
 53  2
         this.reportNameSuffix = reportNameSuffix;
 54  2
     }
 55  
 
 56  
     public void testSetStarting( ReportEntry reportEntry )
 57  
     {
 58  2
         close();
 59  2
         this.reportEntryName = reportEntry.getName();
 60  2
     }
 61  
 
 62  
     public void testSetCompleted( ReportEntry report )
 63  
         throws ReporterException
 64  
     {
 65  2
     }
 66  
 
 67  
     public void close()
 68  
     {
 69  2
         if ( printWriter != null )
 70  
         {
 71  0
             printWriter.close();
 72  0
             printWriter = null;
 73  
         }
 74  2
     }
 75  
 
 76  
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
 77  
     {
 78  
         try
 79  
         {
 80  2
             if ( printWriter == null )
 81  
             {
 82  2
                 if ( !reportsDirectory.exists() )
 83  
                 {
 84  
                     //noinspection ResultOfMethodCallIgnored
 85  0
                     reportsDirectory.mkdirs();
 86  
                 }
 87  2
                 File file =
 88  
                     FileReporter.getReportFile( reportsDirectory, reportEntryName, reportNameSuffix, "-output.txt" );
 89  2
                 printWriter = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) );
 90  
             }
 91  2
             printWriter.write( new String( buf, off, len ) );
 92  
         }
 93  0
         catch ( IOException e )
 94  
         {
 95  0
             throw new NestedRuntimeException( e );
 96  2
         }
 97  2
     }
 98  
 }