Coverage Report - org.apache.maven.plugin.surefire.report.FileReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
FileReporter
83%
25/30
50%
6/12
3,25
 
 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.File;
 23  
 import java.io.FileWriter;
 24  
 import java.io.IOException;
 25  
 import java.io.PrintWriter;
 26  
 import java.util.List;
 27  
 import org.apache.maven.surefire.report.ReportEntry;
 28  
 import org.apache.maven.surefire.report.ReporterException;
 29  
 
 30  
 import static org.apache.maven.plugin.surefire.report.FileReporterUtils.stripIllegalFilenameChars;
 31  
 
 32  
 /**
 33  
  * Base class for file reporters.
 34  
  * 
 35  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 36  
  * @author Kristian Rosenvold
 37  
  */
 38  
 public class FileReporter
 39  
 {
 40  
     private final File reportsDirectory;
 41  
 
 42  
     private final boolean deleteOnStarting;
 43  
 
 44  
     private final String reportNameSuffix;
 45  
 
 46  
     public FileReporter( File reportsDirectory, String reportNameSuffix )
 47  2
     {
 48  2
         this.reportsDirectory = reportsDirectory;
 49  2
         this.deleteOnStarting = false;
 50  2
         this.reportNameSuffix = reportNameSuffix;
 51  2
     }
 52  
 
 53  
     private PrintWriter testSetStarting( ReportEntry report )
 54  
         throws ReporterException
 55  
     {
 56  2
         File reportFile = getReportFile( reportsDirectory, report.getName(), reportNameSuffix, ".txt" );
 57  
 
 58  2
         File reportDir = reportFile.getParentFile();
 59  
 
 60  
         // noinspection ResultOfMethodCallIgnored
 61  2
         reportDir.mkdirs();
 62  
 
 63  2
         if ( deleteOnStarting && reportFile.exists() )
 64  
         {
 65  
             // noinspection ResultOfMethodCallIgnored
 66  0
             reportFile.delete();
 67  
         }
 68  
 
 69  
         try
 70  
         {
 71  2
             PrintWriter writer = new PrintWriter( new FileWriter( reportFile ) );
 72  
 
 73  2
             writer.println( "-------------------------------------------------------------------------------" );
 74  
 
 75  2
             writer.println( "Test set: " + report.getName() );
 76  
 
 77  2
             writer.println( "-------------------------------------------------------------------------------" );
 78  
 
 79  2
             return writer;
 80  
         }
 81  0
         catch ( IOException e )
 82  
         {
 83  0
             throw new ReporterException( "Unable to create file for report: " + e.getMessage(), e );
 84  
         }
 85  
     }
 86  
 
 87  
     public static File getReportFile( File reportsDirectory, String reportEntryName, String reportNameSuffix,
 88  
                                       String fileExtension )
 89  
     {
 90  
         File reportFile;
 91  
 
 92  4
         if ( reportNameSuffix != null && reportNameSuffix.length() > 0 )
 93  
         {
 94  2
             reportFile =
 95  
                 new File( reportsDirectory, stripIllegalFilenameChars( reportEntryName + "-" + reportNameSuffix
 96  
                     + fileExtension ) );
 97  
         }
 98  
         else
 99  
         {
 100  2
             reportFile = new File( reportsDirectory, stripIllegalFilenameChars( reportEntryName + fileExtension ) );
 101  
         }
 102  4
         return reportFile;
 103  
     }
 104  
 
 105  
     public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults )
 106  
         throws ReporterException
 107  
     {
 108  2
         PrintWriter writer = testSetStarting( report );
 109  2
         writer.print( testSetStats.getTestSetSummary( report ) );
 110  
 
 111  2
         if ( testResults != null )
 112  
         {
 113  2
             for ( String testResult : testResults )
 114  
             {
 115  0
                 writer.println( testResult );
 116  0
             }
 117  
         }
 118  
 
 119  2
         writer.flush();
 120  
 
 121  2
         writer.close();
 122  2
     }
 123  
 }