Coverage Report - org.apache.maven.surefire.junitcore.ConcurrentRunListener
 
Classes in this File Line Coverage Branch Coverage Complexity
ConcurrentRunListener
82%
55/67
55%
10/18
1,647
ConcurrentRunListener$1
100%
2/2
N/A
1,647
 
 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 java.util.Map;
 23  
 import org.apache.maven.surefire.report.ConsoleLogger;
 24  
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 25  
 import org.apache.maven.surefire.report.ReportEntry;
 26  
 import org.apache.maven.surefire.report.ReporterFactory;
 27  
 import org.apache.maven.surefire.report.RunListener;
 28  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 29  
 
 30  
 /**
 31  
  * Handles responses from concurrent junit
 32  
  * <p/>
 33  
  * Stuff to remember about JUnit threading:
 34  
  * parallel=classes; beforeClass/afterClass, constructor and all tests method run on same thread
 35  
  * parallel=methods; beforeClass/afterClass run on main thread, constructor + each test method run on same thread
 36  
  * parallel=both; same as parallel=methods
 37  
  *
 38  
  * @see org.apache.maven.surefire.junitcore.JUnitCoreRunListener for details about regular junit run listening
 39  
  * @author Kristian Rosenvold
 40  
  */
 41  24
 public abstract class ConcurrentRunListener
 42  
     implements RunListener, ConsoleOutputReceiver
 43  
 {
 44  
     private final Map<String, TestSet> classMethodCounts;
 45  
 
 46  
     private final ThreadLocal<RunListener> reporterManagerThreadLocal;
 47  
 
 48  
     private final boolean reportImmediately;
 49  
 
 50  
     private final ReporterFactory reporterFactory;
 51  
 
 52  
     private final ConsoleLogger consoleLogger;
 53  
 
 54  
     ConcurrentRunListener( ReporterFactory reporterFactory, ConsoleLogger consoleLogger, boolean reportImmediately,
 55  
                            Map<String, TestSet> classMethodCounts )
 56  
         throws TestSetFailedException
 57  24
     {
 58  24
         this.reportImmediately = reportImmediately;
 59  24
         this.reporterFactory = reporterFactory;
 60  24
         this.classMethodCounts = classMethodCounts;
 61  24
         this.consoleLogger = consoleLogger;
 62  
 
 63  24
         this.reporterManagerThreadLocal = new ThreadLocal<RunListener>()
 64  48
         {
 65  
             @Override
 66  
             protected RunListener initialValue()
 67  
             {
 68  24
                 return ConcurrentRunListener.this.reporterFactory.createReporter();
 69  
             }
 70  
         };
 71  24
     }
 72  
 
 73  
     public void testSetStarting( ReportEntry description )
 74  
     {
 75  24
     }
 76  
 
 77  
     public void testSetCompleted( ReportEntry result )
 78  
     {
 79  24
         final RunListener reporterManager = getRunListener();
 80  24
         for ( TestSet testSet : classMethodCounts.values() )
 81  
         {
 82  36
             testSet.replay( reporterManager );
 83  
         }
 84  24
         reporterManagerThreadLocal.remove();
 85  24
     }
 86  
 
 87  
     public void testFailed( ReportEntry failure )
 88  
     {
 89  7
         final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
 90  7
         if ( testMethod != null )
 91  
         {
 92  7
             testMethod.testFailure( failure );
 93  7
             testMethod.detachFromCurrentThread();
 94  
         }
 95  7
     }
 96  
 
 97  
     public void testError( ReportEntry failure )
 98  
     {
 99  2
         final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
 100  2
         if ( testMethod != null )
 101  
         {
 102  2
             testMethod.testError( failure );
 103  2
             testMethod.detachFromCurrentThread();
 104  
         }
 105  2
     }
 106  
 
 107  
     public void testSkipped( ReportEntry description )
 108  
     {
 109  7
         TestSet testSet = getTestSet( description );
 110  7
         TestMethod testMethod = testSet.createThreadAttachedTestMethod( description );
 111  7
         testMethod.testIgnored( description );
 112  7
         testSet.incrementFinishedTests( getRunListener(), reportImmediately );
 113  7
         testMethod.detachFromCurrentThread();
 114  7
     }
 115  
 
 116  
     public void testAssumptionFailure( ReportEntry failure )
 117  
     {
 118  0
         final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
 119  0
         if ( testMethod != null )
 120  
         {
 121  0
             testMethod.testIgnored( failure );
 122  0
             testMethod.detachFromCurrentThread();
 123  
         }
 124  0
     }
 125  
 
 126  
     public void testStarting( ReportEntry description )
 127  
     {
 128  75
         TestSet testSet = getTestSet( description );
 129  75
         testSet.createThreadAttachedTestMethod( description );
 130  
 
 131  75
         checkIfTestSetCanBeReported( testSet );
 132  75
         testSet.attachToThread();
 133  75
     }
 134  
 
 135  
     public void testSucceeded( ReportEntry report )
 136  
     {
 137  68
         TestMethod testMethod = getTestMethod();
 138  68
         testMethod.testFinished();
 139  68
         testMethod.getTestSet().incrementFinishedTests( getRunListener(), reportImmediately );
 140  68
         testMethod.detachFromCurrentThread();
 141  68
     }
 142  
 
 143  
     private TestMethod getOrCreateThreadAttachedTestMethod( ReportEntry description )
 144  
     {
 145  9
         TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
 146  9
         if ( threadTestMethod != null )
 147  
         {
 148  7
             return threadTestMethod;
 149  
         }
 150  2
         TestSet testSet = getTestSet( description );
 151  2
         if ( testSet == null )
 152  
         {
 153  0
             consoleLogger.info( description.getName() );
 154  0
             consoleLogger.info( description.getStackTraceWriter().writeTraceToString() );
 155  0
             return null;
 156  
         }
 157  
         else
 158  
         {
 159  2
             return testSet.createThreadAttachedTestMethod( description );
 160  
         }
 161  
     }
 162  
 
 163  
     protected abstract void checkIfTestSetCanBeReported( TestSet testSetForTest );
 164  
 
 165  
     TestMethod getTestMethod()
 166  
     {
 167  68
         return TestMethod.getThreadTestMethod();
 168  
     }
 169  
 
 170  
     TestSet getTestSet( ReportEntry description )
 171  
     {
 172  84
         return classMethodCounts.get( description.getSourceName() );
 173  
     }
 174  
 
 175  
     RunListener getRunListener()
 176  
     {
 177  122
         return reporterManagerThreadLocal.get();
 178  
     }
 179  
 
 180  
 
 181  
     public static ConcurrentRunListener createInstance( Map<String, TestSet> classMethodCounts,
 182  
                                                             ReporterFactory reporterManagerFactory,
 183  
                                                             boolean parallelClasses, boolean parallelBoth,
 184  
                                                             ConsoleLogger consoleLogger )
 185  
         throws TestSetFailedException
 186  
     {
 187  2
         if ( parallelClasses )
 188  
         {
 189  0
             return new ClassesParallelRunListener( classMethodCounts, reporterManagerFactory, consoleLogger );
 190  
         }
 191  2
         return new MethodsParallelRunListener( classMethodCounts, reporterManagerFactory, !parallelBoth,
 192  
                                                consoleLogger );
 193  
     }
 194  
 
 195  
 
 196  
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
 197  
     {
 198  17
         TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
 199  17
         if ( threadTestMethod != null )
 200  
         {
 201  0
             final LogicalStream logicalStream = threadTestMethod.getLogicalStream();
 202  0
             logicalStream.write( stdout, buf, off, len );
 203  0
         }
 204  
         else
 205  
         {
 206  
             // Not able to assocaite output with any thread. Just dump to console
 207  17
             consoleLogger.info( new String( buf, off, len ) );
 208  
         }
 209  17
     }
 210  
 
 211  
 }