Coverage Report - org.apache.maven.surefire.report.AbstractTextReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTextReporter
0%
0/71
0%
0/20
1.438
 
 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  
     protected static final String BRIEF = "brief";
 36  
 
 37  
     protected static final String PLAIN = "plain";
 38  
 
 39  
     protected 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  
     protected AbstractTextReporter( ReporterConfiguration reporterConfiguration, String format )
 50  
     {
 51  0
         super( reporterConfiguration );
 52  
 
 53  0
         this.format = format;
 54  0
     }
 55  
 
 56  
 
 57  
     protected AbstractTextReporter( PrintWriter writer, String format, ReporterConfiguration reporterConfiguration )
 58  
     {
 59  0
         super( reporterConfiguration );
 60  
 
 61  0
         this.writer = writer;
 62  
 
 63  0
         this.format = format;
 64  0
     }
 65  
 
 66  
     public void setWriter( PrintWriter writer )
 67  
     {
 68  0
         this.writer = writer;
 69  0
     }
 70  
 
 71  
     public void writeMessage( String message )
 72  
     {
 73  0
         if ( writer != null )
 74  
         {
 75  0
             writer.println( message );
 76  
 
 77  0
             writer.flush();
 78  
         }
 79  0
     }
 80  
 
 81  
     public void writeDetailMessage( String message )
 82  
     {
 83  0
         writeMessage( message );
 84  0
     }
 85  
 
 86  
     public void testSucceeded( ReportEntry report )
 87  
     {
 88  0
         super.testSucceeded( report );
 89  
 
 90  0
         if ( PLAIN.equals( format ) )
 91  
         {
 92  0
             testResults.add( getElapsedTimeSummary( report ) );
 93  
         }
 94  0
     }
 95  
 
 96  
     public void testSkipped( ReportEntry report )
 97  
     {
 98  0
         super.testSkipped( report );
 99  
 
 100  0
         if ( PLAIN.equals( format ) )
 101  
         {
 102  0
             testResults.add( report.getName() + " skipped" );
 103  
         }
 104  0
     }
 105  
 
 106  
     public void testError( ReportEntry report, String stdOut, String stdErr )
 107  
     {
 108  0
         super.testError( report, stdOut, stdErr );
 109  
 
 110  0
         testResults.add( getOutput( report, "ERROR" ) );
 111  0
     }
 112  
 
 113  
     public void testFailed( ReportEntry report, String stdOut, String stdErr )
 114  
     {
 115  0
         super.testFailed( report, stdOut, stdErr );
 116  
 
 117  0
         testResults.add( getOutput( report, "FAILURE" ) );
 118  0
     }
 119  
 
 120  
     public void testSetStarting( ReportEntry report )
 121  
         throws ReporterException
 122  
     {
 123  0
         super.testSetStarting( report );
 124  
 
 125  0
         testResults = new ArrayList();
 126  0
     }
 127  
 
 128  
     public void testSetCompleted( ReportEntry report )
 129  
         throws ReporterException
 130  
     {
 131  0
         super.testSetCompleted( report );
 132  
 
 133  0
         writeMessage( getTestSetSummary( report ) );
 134  
 
 135  0
         if ( format.equals( BRIEF ) || format.equals( PLAIN ) )
 136  
         {
 137  0
             for ( Iterator i = testResults.iterator(); i.hasNext(); )
 138  
             {
 139  0
                 writeMessage( (String) i.next() );
 140  
             }
 141  
         }
 142  0
     }
 143  
 
 144  
     protected String getTestSetSummary( ReportEntry report )
 145  
     {
 146  0
         StringBuffer buf = new StringBuffer();
 147  
 
 148  0
         buf.append( TEST_SET_COMPLETED_PREFIX );
 149  0
         buf.append( completedCount );
 150  0
         buf.append( ", Failures: " );
 151  0
         buf.append( failures );
 152  0
         buf.append( ", Errors: " );
 153  0
         buf.append( errors );
 154  0
         buf.append( ", Skipped: " );
 155  0
         buf.append( skipped );
 156  0
         buf.append( ", Time elapsed: " );
 157  0
         int elapsed = report.getElapsed() != null
 158  
             ? report.getElapsed().intValue()
 159  
             : (int) ( System.currentTimeMillis() - testSetStartTime );
 160  0
         buf.append( elapsedTimeAsString( elapsed ) );
 161  0
         buf.append( " sec" );
 162  
 
 163  0
         if ( failures > 0 || errors > 0 )
 164  
         {
 165  0
             buf.append( " <<< FAILURE!" );
 166  
         }
 167  
 
 168  0
         return buf.toString();
 169  
     }
 170  
 
 171  
     protected String getElapsedTimeSummary( ReportEntry report )
 172  
     {
 173  0
         StringBuffer reportContent = new StringBuffer();
 174  0
         long runTime = getActualRunTime( report );
 175  
 
 176  0
         reportContent.append( report.getName() );
 177  0
         reportContent.append( "  Time elapsed: " );
 178  0
         reportContent.append( elapsedTimeAsString( runTime ) );
 179  0
         reportContent.append( " sec" );
 180  
 
 181  0
         return reportContent.toString();
 182  
     }
 183  
 
 184  
     protected String getOutput( ReportEntry report, String msg )
 185  
     {
 186  0
         StringBuffer buf = new StringBuffer();
 187  
 
 188  0
         buf.append( getElapsedTimeSummary( report ) );
 189  
 
 190  0
         buf.append( "  <<< " ).append( msg ).append( "!" ).append( NL );
 191  
 
 192  0
         buf.append( getStackTrace( report ) );
 193  
 
 194  0
         return buf.toString();
 195  
     }
 196  
 
 197  
     /**
 198  
      * Check if the String passed as argument is a "test set completed" message.
 199  
      *
 200  
      * @param message the message to check
 201  
      * @return true if it is a "test set completed" message
 202  
      */
 203  
     public static boolean isTestSetCompletedMessage( String message )
 204  
     {
 205  0
         return message.startsWith( TEST_SET_COMPLETED_PREFIX );
 206  
     }
 207  
 
 208  
     public void reset()
 209  
     {
 210  0
         super.reset();
 211  0
         if ( writer != null )
 212  
         {
 213  0
             writer.flush();
 214  
         }
 215  0
     }
 216  
 }