Coverage Report - org.apache.maven.surefire.report.AbstractTextReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTextReporter
0%
0/68
0%
0/20
1.714
 
 1  
 package org.apache.maven.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.PrintWriter;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * Text based reporter.
 29  
  *
 30  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 31  
  */
 32  
 public abstract class AbstractTextReporter
 33  
     extends AbstractReporter
 34  
 {
 35  
     static final String BRIEF = "brief";
 36  
 
 37  
     static final String PLAIN = "plain";
 38  
 
 39  
     static final String SUMMARY = "summary";
 40  
 
 41  
     protected PrintWriter writer;
 42  
 
 43  
     private static final String TEST_SET_COMPLETED_PREFIX = "Tests run: ";
 44  
 
 45  
     private final String format;
 46  
 
 47  
     private List testResults;
 48  
 
 49  
 
 50  
     protected AbstractTextReporter( boolean trimStackTrace, String format )
 51  
     {
 52  0
         super( trimStackTrace );
 53  0
         this.format = format;
 54  0
     }
 55  
 
 56  
     protected AbstractTextReporter( PrintWriter writer, boolean trimStackTrace, String format )
 57  
     {
 58  0
         this( trimStackTrace, format);
 59  0
         this.writer = writer;
 60  0
     }
 61  
 
 62  
 
 63  
     public void setWriter( PrintWriter writer )
 64  
     {
 65  0
         this.writer = writer;
 66  0
     }
 67  
 
 68  
     public void writeMessage( String message )
 69  
     {
 70  0
         if ( writer != null )
 71  
         {
 72  0
             writer.print( message );
 73  
 
 74  0
             writer.flush();
 75  
         }
 76  0
     }
 77  
 
 78  
     public void testSucceeded( ReportEntry report )
 79  
     {
 80  0
         super.testSucceeded( report );
 81  
 
 82  0
         if ( PLAIN.equals( format ) )
 83  
         {
 84  0
             testResults.add( getElapsedTimeSummary( report ) );
 85  
         }
 86  0
     }
 87  
 
 88  
     public void testSkipped( ReportEntry report )
 89  
     {
 90  0
         super.testSkipped( report );
 91  
 
 92  0
         if ( PLAIN.equals( format ) )
 93  
         {
 94  0
             testResults.add( report.getName() + " skipped" );
 95  
         }
 96  0
     }
 97  
 
 98  
     public void testError( ReportEntry report, String stdOut, String stdErr )
 99  
     {
 100  0
         super.testError( report, stdOut, stdErr );
 101  
 
 102  0
         testResults.add( getOutput( report, "ERROR" ) );
 103  0
     }
 104  
 
 105  
     public void testFailed( ReportEntry report, String stdOut, String stdErr )
 106  
     {
 107  0
         super.testFailed( report, stdOut, stdErr );
 108  
 
 109  0
         testResults.add( getOutput( report, "FAILURE" ) );
 110  0
     }
 111  
 
 112  
     public void testSetStarting( ReportEntry report )
 113  
         throws ReporterException
 114  
     {
 115  0
         super.testSetStarting( report );
 116  
 
 117  0
         testResults = new ArrayList();
 118  0
     }
 119  
 
 120  
     public void testSetCompleted( ReportEntry report )
 121  
         throws ReporterException
 122  
     {
 123  0
         super.testSetCompleted( report );
 124  
 
 125  0
         writeMessage( getTestSetSummary( report ) );
 126  
 
 127  0
         if ( format.equals( BRIEF ) || format.equals( PLAIN ) )
 128  
         {
 129  0
             for ( Iterator i = testResults.iterator(); i.hasNext(); )
 130  
             {
 131  0
                 writeMessage( (String) i.next() );
 132  
             }
 133  
         }
 134  0
     }
 135  
 
 136  
     protected String getTestSetSummary( ReportEntry report )
 137  
     {
 138  0
         StringBuffer buf = new StringBuffer();
 139  
 
 140  0
         buf.append( TEST_SET_COMPLETED_PREFIX );
 141  0
         buf.append( completedCount );
 142  0
         buf.append( ", Failures: " );
 143  0
         buf.append( failures );
 144  0
         buf.append( ", Errors: " );
 145  0
         buf.append( errors );
 146  0
         buf.append( ", Skipped: " );
 147  0
         buf.append( skipped );
 148  0
         buf.append( ", Time elapsed: " );
 149  0
         int elapsed = report.getElapsed() != null
 150  
             ? report.getElapsed().intValue()
 151  
             : (int) ( System.currentTimeMillis() - testSetStartTime );
 152  0
         buf.append( elapsedTimeAsString( elapsed ) );
 153  0
         buf.append( " sec" );
 154  
 
 155  0
         if ( failures > 0 || errors > 0 )
 156  
         {
 157  0
             buf.append( " <<< FAILURE!" );
 158  
         }
 159  
 
 160  0
         buf.append( "\n" );
 161  
 
 162  0
         return buf.toString();
 163  
     }
 164  
 
 165  
     protected String getElapsedTimeSummary( ReportEntry report )
 166  
     {
 167  0
         StringBuffer reportContent = new StringBuffer();
 168  0
         long runTime = getActualRunTime( report );
 169  
 
 170  0
         reportContent.append( report.getName() );
 171  0
         reportContent.append( "  Time elapsed: " );
 172  0
         reportContent.append( elapsedTimeAsString( runTime ) );
 173  0
         reportContent.append( " sec" );
 174  
 
 175  0
         return reportContent.toString();
 176  
     }
 177  
 
 178  
     protected String getOutput( ReportEntry report, String msg )
 179  
     {
 180  0
         StringBuffer buf = new StringBuffer();
 181  
 
 182  0
         buf.append( getElapsedTimeSummary( report ) );
 183  
 
 184  0
         buf.append( "  <<< " ).append( msg ).append( "!" ).append( NL );
 185  
 
 186  0
         buf.append( getStackTrace( report ) );
 187  
 
 188  0
         return buf.toString();
 189  
     }
 190  
 
 191  
     public void reset()
 192  
     {
 193  0
         super.reset();
 194  0
         if ( writer != null )
 195  
         {
 196  0
             writer.flush();
 197  
         }
 198  0
     }
 199  
 }