Coverage Report - org.apache.maven.surefire.junit4.JUnit4TestSetReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
JUnit4TestSetReporter
0%
0/40
0%
0/6
1.5
 
 1  
 package org.apache.maven.surefire.junit4;
 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.util.ResourceBundle;
 23  
 import java.util.regex.Matcher;
 24  
 import java.util.regex.Pattern;
 25  
 
 26  
 import org.apache.maven.surefire.Surefire;
 27  
 import org.apache.maven.surefire.report.ReportEntry;
 28  
 import org.apache.maven.surefire.report.ReporterManager;
 29  
 import org.junit.runner.Description;
 30  
 import org.junit.runner.Result;
 31  
 import org.junit.runner.notification.Failure;
 32  
 import org.junit.runner.notification.RunListener;
 33  
 
 34  
 public class JUnit4TestSetReporter
 35  
     extends RunListener
 36  
 {
 37  
     // Constants
 38  0
     private static ResourceBundle bundle = ResourceBundle.getBundle( Surefire.SUREFIRE_BUNDLE_NAME );
 39  
 
 40  
     // Member Variables
 41  
     private JUnit4TestSet testSet;
 42  
 
 43  
     private ReporterManager reportMgr;
 44  
 
 45  
     /**
 46  
      * This flag is set after a failure has occurred so that a <code>testSucceeded</code> event is not fired.  This is necessary because JUnit4 always fires a <code>testRunFinished</code> event-- even if there was a failure.
 47  
      */
 48  
     private boolean failureFlag;
 49  
 
 50  
     /**
 51  
      * Constructor.
 52  
      *
 53  
      * @param testSet       the specific test set that this will report on as it is
 54  
      *                      executed
 55  
      * @param reportManager the report manager to log testing events to
 56  
      */
 57  
     JUnit4TestSetReporter( JUnit4TestSet testSet, ReporterManager reportManager )
 58  0
     {
 59  0
         this.testSet = testSet;
 60  0
         this.reportMgr = reportManager;
 61  0
     }
 62  
 
 63  
     /**
 64  
      * Called right before any tests from a specific class are run.
 65  
      *
 66  
      * @see org.junit.runner.notification.RunListener#testRunStarted(org.junit.runner.Description)
 67  
      */
 68  
     public void testRunStarted( Description description )
 69  
         throws Exception
 70  
     {
 71  0
         String rawString = bundle.getString( "testSetStarting" );
 72  0
         ReportEntry report = new ReportEntry( testSet.getName(), testSet.getName(), rawString );
 73  
 
 74  0
         this.reportMgr.testSetStarting( report );
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Called right after all tests from a specific class are run.
 79  
      *
 80  
      * @see org.junit.runner.notification.RunListener#testRunFinished(org.junit.runner.Result)
 81  
      */
 82  
     public void testRunFinished( Result result )
 83  
         throws Exception
 84  
     {
 85  0
         String rawString = bundle.getString( "testSetCompletedNormally" );
 86  0
         ReportEntry report = new ReportEntry( testSet.getName(), testSet.getName(), rawString );
 87  
 
 88  0
         this.reportMgr.testSetCompleted( report );
 89  0
         this.reportMgr.reset();
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Called when a specific test has been skipped (for whatever reason).
 94  
      *
 95  
      * @see org.junit.runner.notification.RunListener#testIgnored(org.junit.runner.Description)
 96  
      */
 97  
     public void testIgnored( Description description )
 98  
         throws Exception
 99  
     {
 100  0
         String rawString = bundle.getString( "testSkipped" );
 101  0
         ReportEntry report = new ReportEntry( extractClassName( description ), description.getDisplayName(), rawString );
 102  
 
 103  0
         this.reportMgr.testSkipped( report );
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Called when a specific test has started.
 108  
      *
 109  
      * @see org.junit.runner.notification.RunListener#testStarted(org.junit.runner.Description)
 110  
      */
 111  
     public void testStarted( Description description )
 112  
         throws Exception
 113  
     {
 114  0
         String rawString = bundle.getString( "testStarting" );
 115  0
         ReportEntry report = new ReportEntry( extractClassName( description ), description.getDisplayName(), rawString );
 116  
 
 117  0
         this.reportMgr.testStarting( report );
 118  
 
 119  0
         this.failureFlag = false;
 120  0
     }
 121  
 
 122  
     /**
 123  
      * Called when a specific test has failed.
 124  
      *
 125  
      * @see org.junit.runner.notification.RunListener#testFailure(org.junit.runner.notification.Failure)
 126  
      */
 127  
     public void testFailure( Failure failure )
 128  
         throws Exception
 129  
     {
 130  0
         String rawString = bundle.getString( "executeException" );
 131  0
         ReportEntry report =
 132  
             new ReportEntry( extractClassName( failure.getDescription() ), failure.getTestHeader(), rawString, new JUnit4StackTraceWriter( failure ) );
 133  
 
 134  0
         if ( failure.getException() instanceof AssertionError )
 135  
         {
 136  0
             this.reportMgr.testFailed( report );
 137  
         }
 138  
         else
 139  
         {
 140  0
             this.reportMgr.testError( report );
 141  
         }
 142  
 
 143  0
         failureFlag = true;
 144  0
     }
 145  
 
 146  
     /**
 147  
      * Called after a specific test has finished.
 148  
      *
 149  
      * @see org.junit.runner.notification.RunListener#testFinished(org.junit.runner.Description)
 150  
      */
 151  
     public void testFinished( Description description )
 152  
         throws Exception
 153  
     {
 154  0
         if ( failureFlag == false )
 155  
         {
 156  0
             String rawString = bundle.getString( "testSuccessful" );
 157  0
             ReportEntry report = new ReportEntry( extractClassName( description ), description.getDisplayName(), rawString );
 158  
 
 159  0
             this.reportMgr.testSucceeded( report );
 160  
         }
 161  0
     }
 162  
     
 163  
     private String extractClassName( Description description )
 164  
     {
 165  0
         String displayName = description.getDisplayName();
 166  0
         final Pattern PARENS = Pattern.compile(
 167  
                 "^" +
 168  
                 "[^\\(\\)]+" + //non-parens
 169  
                         "\\((" + // then an open-paren (start matching a group)
 170  
                         "[^\\\\(\\\\)]+" + //non-parens
 171  
                         ")\\)" +
 172  
                         "$" ); // then a close-paren (end group match)
 173  0
         Matcher m = PARENS.matcher( displayName );
 174  0
         if (!m.find()) return displayName;
 175  0
         return m.group( 1 );
 176  
     }
 177  
 }