Coverage Report - org.apache.maven.surefire.junitcore.NonConcurrentRunListener
 
Classes in this File Line Coverage Branch Coverage Complexity
NonConcurrentRunListener
0%
0/34
0%
0/6
1,25
 
 1  
 package org.apache.maven.surefire.junitcore;
 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.common.junit4.JUnit4RunListener;
 23  
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 24  
 import org.apache.maven.surefire.report.RunListener;
 25  
 import org.apache.maven.surefire.report.SimpleReportEntry;
 26  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 27  
 
 28  
 import org.junit.runner.Description;
 29  
 import org.junit.runner.Result;
 30  
 import org.junit.runner.notification.Failure;
 31  
 
 32  
 /**
 33  
  * A class to be used when there is no JUnit parallelism (methods or/and class). This allow to workaround JUnit
 34  
  * limitation a la Junit4 provider. Specifically, we can redirect properly the output even if we don't have class
 35  
  * demarcation in JUnit. It works when if there is a JVM instance per test run, i.e. with forkMode=always or perthread.
 36  
  */
 37  
 public class NonConcurrentRunListener
 38  
     extends JUnit4RunListener
 39  
     implements ConsoleOutputReceiver
 40  
 {
 41  
 
 42  
     private java.lang.Class<?> currentTestClass;
 43  
 
 44  
     private Description lastFinishedDescription;
 45  
 
 46  
     public NonConcurrentRunListener( RunListener reporter )
 47  
         throws TestSetFailedException
 48  
     {
 49  0
         super( reporter );
 50  0
     }
 51  
 
 52  
     public synchronized void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
 53  
     {
 54  
         // We can write immediately: no parallelism and a single class.
 55  0
         ( (ConsoleOutputReceiver) reporter ).writeTestOutput( buf, off, len, stdout );
 56  0
     }
 57  
 
 58  
     protected SimpleReportEntry createReportEntry( Description description )
 59  
     {
 60  0
         return new SimpleReportEntry( description.getClassName(), description.getDisplayName()/*,
 61  
                                        (int) ( System.currentTimeMillis() - startTime ) */);
 62  
     }
 63  
 
 64  
     protected SimpleReportEntry createReportEntryForTestSet( Description description )
 65  
     {
 66  0
         return new SimpleReportEntry( description.getClassName(), description.getClassName() /*,
 67  
                                        (int) ( System.currentTimeMillis() - startTime ) */);
 68  
     }
 69  
 
 70  
     @Override
 71  
     public void testStarted( Description description )
 72  
         throws Exception
 73  
     {
 74  0
         finishLastTestSetIfNeccessary( description );
 75  0
         super.testStarted( description );
 76  0
     }
 77  
 
 78  
     private void finishLastTestSetIfNeccessary( Description description )
 79  
     {
 80  0
         if ( !description.getTestClass().equals( currentTestClass ) )
 81  
         {
 82  0
             currentTestClass = description.getTestClass();
 83  0
             if ( lastFinishedDescription != null )
 84  
             {
 85  0
                 reporter.testSetCompleted( createReportEntryForTestSet( lastFinishedDescription ) );
 86  0
                 lastFinishedDescription = null;
 87  
             }
 88  0
             reporter.testSetStarting( createReportEntryForTestSet( description ) );
 89  
         }
 90  0
     }
 91  
 
 92  
     @Override
 93  
     public void testFinished( Description description )
 94  
         throws Exception
 95  
     {
 96  0
         super.testFinished( description );
 97  0
         this.lastFinishedDescription = description;
 98  0
     }
 99  
 
 100  
     @Override
 101  
     public void testIgnored( Description description )
 102  
         throws Exception
 103  
     {
 104  0
         finishLastTestSetIfNeccessary( description );
 105  
 
 106  0
         super.testIgnored( description );
 107  0
         this.lastFinishedDescription = description;
 108  0
     }
 109  
 
 110  
     @Override
 111  
     public void testFailure( Failure failure )
 112  
         throws Exception
 113  
     {
 114  0
         super.testFailure( failure );
 115  0
         this.lastFinishedDescription = failure.getDescription();
 116  0
     }
 117  
 
 118  
     @Override
 119  
     public void testAssumptionFailure( Failure failure )
 120  
     {
 121  0
         super.testAssumptionFailure( failure );
 122  0
         this.lastFinishedDescription = failure.getDescription();
 123  0
     }
 124  
 
 125  
     @Override
 126  
     public void testRunStarted( Description description )
 127  
         throws Exception
 128  
     {
 129  0
     }
 130  
 
 131  
     @Override
 132  
     public void testRunFinished( Result result )
 133  
         throws Exception
 134  
     {
 135  0
         if ( lastFinishedDescription != null )
 136  
         {
 137  0
             reporter.testSetCompleted( createReportEntryForTestSet( lastFinishedDescription ) );
 138  0
             lastFinishedDescription = null;
 139  
         }
 140  0
     }
 141  
 }