Coverage Report - org.apache.maven.surefire.common.junit4.JUnit4RunListener
 
Classes in this File Line Coverage Branch Coverage Complexity
JUnit4RunListener
0%
0/27
0%
0/6
0
 
 1  
 package org.apache.maven.surefire.common.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 org.apache.maven.surefire.report.RunListener;
 23  
 import org.apache.maven.surefire.report.ReportEntry;
 24  
 import org.apache.maven.surefire.report.SimpleReportEntry;
 25  
 import org.junit.runner.Description;
 26  
 import org.junit.runner.notification.Failure;
 27  
 
 28  
 import java.util.regex.Matcher;
 29  
 import java.util.regex.Pattern;
 30  
 
 31  
 public class JUnit4RunListener
 32  
     extends org.junit.runner.notification.RunListener
 33  
 {
 34  0
     private static final Pattern PARENS = Pattern.compile( "^" + "[^\\(\\)]+" //non-parens
 35  
                                                                + "\\((" // then an open-paren (start matching a group)
 36  
                                                                + "[^\\\\(\\\\)]+" //non-parens
 37  
                                                                + ")\\)" + "$" ); // then a close-paren (end group match)
 38  
 
 39  
     protected final RunListener reporter;
 40  
 
 41  
     /**
 42  
      * This flag is set after a failure has occurred so that a <code>testSucceeded</code> event is not fired.
 43  
      * This is necessary because JUnit4 always fires a <code>testRunFinished</code> event-- even if there was a failure.
 44  
      */
 45  
     private boolean failureFlag;
 46  
 
 47  
     /**
 48  
      * Constructor.
 49  
      *
 50  
      * @param reporter the reporter to log testing events to
 51  
      */
 52  
     public JUnit4RunListener( RunListener reporter )
 53  0
     {
 54  0
         this.reporter = reporter;
 55  0
     }
 56  
 
 57  
     // Testrun methods are not invoked when using the runner
 58  
 
 59  
     /**
 60  
      * Called when a specific test has been skipped (for whatever reason).
 61  
      *
 62  
      * @see org.junit.runner.notification.RunListener#testIgnored(org.junit.runner.Description)
 63  
      */
 64  
     public void testIgnored( Description description )
 65  
         throws Exception
 66  
     {
 67  0
         reporter.testSkipped( createReportEntry( description ) );
 68  0
     }
 69  
 
 70  
     /**
 71  
      * Called when a specific test has started.
 72  
      *
 73  
      * @see org.junit.runner.notification.RunListener#testStarted(org.junit.runner.Description)
 74  
      */
 75  
     public void testStarted( Description description )
 76  
         throws Exception
 77  
     {
 78  0
         reporter.testStarting( createReportEntry( description ) );
 79  0
         failureFlag = false;
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Called when a specific test has failed.
 84  
      *
 85  
      * @see org.junit.runner.notification.RunListener#testFailure(org.junit.runner.notification.Failure)
 86  
      */
 87  
     @SuppressWarnings( { "ThrowableResultOfMethodCallIgnored" } )
 88  
     public void testFailure( Failure failure )
 89  
         throws Exception
 90  
     {
 91  0
         ReportEntry report =
 92  
             new SimpleReportEntry( extractClassName( failure.getDescription() ), failure.getTestHeader(),
 93  
                                    new JUnit4StackTraceWriter( failure ) );
 94  
 
 95  0
         if ( failure.getException() instanceof AssertionError )
 96  
         {
 97  0
             this.reporter.testFailed( report );
 98  
         }
 99  
         else
 100  
         {
 101  0
             this.reporter.testError( report );
 102  
         }
 103  0
         failureFlag = true;
 104  0
     }
 105  
 
 106  
     public void testAssumptionFailure( Failure failure )
 107  
     {
 108  0
         this.reporter.testAssumptionFailure( createReportEntry( failure.getDescription() ) );
 109  0
         failureFlag = true;
 110  0
     }
 111  
 
 112  
 
 113  
     /**
 114  
      * Called after a specific test has finished.
 115  
      *
 116  
      * @see org.junit.runner.notification.RunListener#testFinished(org.junit.runner.Description)
 117  
      */
 118  
     public void testFinished( Description description )
 119  
         throws Exception
 120  
     {
 121  0
         if ( !failureFlag )
 122  
         {
 123  0
             reporter.testSucceeded( createReportEntry( description ) );
 124  
         }
 125  0
     }
 126  
 
 127  
     private SimpleReportEntry createReportEntry( Description description )
 128  
     {
 129  0
         return new SimpleReportEntry( extractClassName( description ), description.getDisplayName() );
 130  
     }
 131  
 
 132  
 
 133  
     protected String extractClassName( Description description )
 134  
     {
 135  0
         String displayName = description.getDisplayName();
 136  0
         Matcher m = PARENS.matcher( displayName );
 137  0
         if ( !m.find() )
 138  
         {
 139  0
             return displayName;
 140  
         }
 141  0
         return m.group( 1 );
 142  
     }
 143  
 }