Coverage Report - org.apache.maven.plugin.surefire.report.TestSetStats
 
Classes in this File Line Coverage Branch Coverage Complexity
TestSetStats
56%
42/75
25%
5/20
1,476
 
 1  
 package org.apache.maven.plugin.surefire.report;
 2  
 /*
 3  
  * Licensed to the Apache Software Foundation (ASF) under one
 4  
  * or more contributor license agreements.  See the NOTICE file
 5  
  * distributed with this work for additional information
 6  
  * regarding copyright ownership.  The ASF licenses this file
 7  
  * to you under the Apache License, Version 2.0 (the
 8  
  * "License"); you may not use this file except in compliance
 9  
  * with the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *     http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing,
 14  
  * software distributed under the License is distributed on an
 15  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16  
  * KIND, either express or implied.  See the License for the
 17  
  * specific language governing permissions and limitations
 18  
  * under the License.
 19  
  */
 20  
 
 21  
 import java.text.NumberFormat;
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.Locale;
 25  
 
 26  
 /**
 27  
  * Maintains per-thread test result state. Not thread safe.
 28  
  */
 29  
 public class TestSetStats
 30  
 {
 31  
     private final boolean trimStackTrace;
 32  
 
 33  
     private final boolean plainFormat;
 34  
 
 35  
     private long testSetStartAt;
 36  
 
 37  
     private long testStartAt;
 38  
 
 39  
     private int completedCount;
 40  
 
 41  
     private int errors;
 42  
 
 43  
     private int failures;
 44  
 
 45  
     private int skipped;
 46  
 
 47  
     private long lastStartAt;
 48  
 
 49  
     private long elapsedForTestSet;
 50  
 
 51  4
     private final List<WrappedReportEntry> reportEntries = new ArrayList<WrappedReportEntry>();
 52  
 
 53  
     public TestSetStats( boolean trimStackTrace, boolean plainFormat )
 54  4
     {
 55  4
         this.trimStackTrace = trimStackTrace;
 56  4
         this.plainFormat = plainFormat;
 57  4
     }
 58  
 
 59  
     public int getElapsedSinceTestSetStart()
 60  
     {
 61  0
         return (int) ( System.currentTimeMillis() - testSetStartAt );
 62  
     }
 63  
 
 64  
     public int getElapsedSinceLastStart()
 65  
     {
 66  0
         return (int) ( System.currentTimeMillis() - lastStartAt );
 67  
     }
 68  
 
 69  
     public void testSetStart()
 70  
     {
 71  0
         lastStartAt = testSetStartAt = System.currentTimeMillis();
 72  0
     }
 73  
 
 74  
     public void testStart()
 75  
     {
 76  0
         lastStartAt = testStartAt = System.currentTimeMillis();
 77  0
     }
 78  
 
 79  
     private long finishTest( WrappedReportEntry reportEntry )
 80  
     {
 81  3
         reportEntries.add( reportEntry );
 82  3
         incrementCompletedCount();
 83  3
         long testEndAt = System.currentTimeMillis();
 84  
         // SUREFIRE-398 skipped tests call endTest without calling testStarting
 85  
         // if startTime = 0, set it to endTime, so the diff will be 0
 86  3
         if ( testStartAt == 0 )
 87  
         {
 88  2
             testStartAt = testEndAt;
 89  
         }
 90  3
         long elapsedForThis = reportEntry.getElapsed() != null ? reportEntry.getElapsed() : testEndAt - testStartAt;
 91  3
         elapsedForTestSet += elapsedForThis;
 92  3
         return elapsedForThis;
 93  
     }
 94  
 
 95  
     public void testSucceeded( WrappedReportEntry reportEntry )
 96  
     {
 97  3
         finishTest( reportEntry );
 98  3
     }
 99  
 
 100  
 
 101  
     public void testError( WrappedReportEntry reportEntry )
 102  
     {
 103  0
         errors += 1;
 104  0
         finishTest( reportEntry );
 105  
 
 106  0
     }
 107  
 
 108  
     public void testFailure( WrappedReportEntry reportEntry )
 109  
     {
 110  0
         failures += 1;
 111  0
         finishTest( reportEntry );
 112  0
     }
 113  
 
 114  
     public void testSkipped( WrappedReportEntry reportEntry )
 115  
     {
 116  0
         skipped += 1;
 117  0
         finishTest( reportEntry );
 118  0
     }
 119  
 
 120  
     public void reset()
 121  
     {
 122  0
         completedCount = 0;
 123  0
         errors = 0;
 124  0
         failures = 0;
 125  0
         skipped = 0;
 126  0
         elapsedForTestSet = 0;
 127  0
         reportEntries.clear();
 128  0
     }
 129  
 
 130  
     public int getCompletedCount()
 131  
     {
 132  2
         return completedCount;
 133  
     }
 134  
 
 135  
     public int getErrors()
 136  
     {
 137  2
         return errors;
 138  
     }
 139  
 
 140  
     public int getFailures()
 141  
     {
 142  2
         return failures;
 143  
     }
 144  
 
 145  
     public int getSkipped()
 146  
     {
 147  2
         return skipped;
 148  
     }
 149  
 
 150  
     private static final String TEST_SET_COMPLETED_PREFIX = "Tests run: ";
 151  
 
 152  4
     private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
 153  
 
 154  
     private static final int MS_PER_SEC = 1000;
 155  
 
 156  
     String elapsedTimeAsString( long runTime )
 157  
     {
 158  2
         return numberFormat.format( (double) runTime / MS_PER_SEC );
 159  
     }
 160  
 
 161  
     public String getElapsedForTestSet()
 162  
     {
 163  2
         return elapsedTimeAsString( elapsedForTestSet );
 164  
     }
 165  
 
 166  
     private void incrementCompletedCount()
 167  
     {
 168  3
         completedCount += 1;
 169  3
     }
 170  
 
 171  
     public String getTestSetSummary( WrappedReportEntry reportEntry )
 172  
     {
 173  2
         StringBuilder buf = new StringBuilder();
 174  
 
 175  2
         buf.append( TEST_SET_COMPLETED_PREFIX );
 176  2
         buf.append( completedCount );
 177  2
         buf.append( ", Failures: " );
 178  2
         buf.append( failures );
 179  2
         buf.append( ", Errors: " );
 180  2
         buf.append( errors );
 181  2
         buf.append( ", Skipped: " );
 182  2
         buf.append( skipped );
 183  2
         buf.append( ", Time elapsed: " );
 184  2
         buf.append( reportEntry.elapsedTimeAsString() );
 185  2
         buf.append( " sec" );
 186  
 
 187  2
         if ( failures > 0 || errors > 0 )
 188  
         {
 189  0
             buf.append( " <<< FAILURE!" );
 190  
         }
 191  
         
 192  2
         buf.append( " - in " );
 193  2
         buf.append( reportEntry.getNameWithGroup() );
 194  
 
 195  2
         buf.append( "\n" );
 196  
 
 197  2
         return buf.toString();
 198  
     }
 199  
 
 200  
     public List<String> getTestResults()
 201  
     {
 202  0
         List<String> result = new ArrayList<String>();
 203  0
         for ( WrappedReportEntry testResult : reportEntries )
 204  
         {
 205  0
             if ( testResult.isErrorOrFailure() )
 206  
             {
 207  0
                 result.add( testResult.getOutput( trimStackTrace ) );
 208  
             }
 209  0
             else if ( plainFormat && testResult.isSkipped() )
 210  
             {
 211  0
                 result.add( testResult.getName() + " skipped" );
 212  
             }
 213  0
             else if ( plainFormat && testResult.isSucceeded() )
 214  
             {
 215  0
                 result.add( testResult.getElapsedTimeSummary() );
 216  
             }
 217  0
         }
 218  0
         return result;
 219  
     }
 220  
 
 221  
     public List<WrappedReportEntry> getReportEntries()
 222  
     {
 223  2
         return reportEntries;
 224  
     }
 225  
 }