Coverage Report - org.apache.maven.surefire.junitcore.ConcurrentRunListener
 
Classes in this File Line Coverage Branch Coverage Complexity
ConcurrentRunListener
92%
64/69
75%
15/20
1,706
ConcurrentRunListener$1
100%
2/2
N/A
1,706
 
 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  26
 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  26
     {
 58  26
         this.reportImmediately = reportImmediately;
 59  26
         this.reporterFactory = reporterFactory;
 60  26
         this.classMethodCounts = classMethodCounts;
 61  26
         this.consoleLogger = consoleLogger;
 62  
 
 63  26
         this.reporterManagerThreadLocal = new ThreadLocal<RunListener>()
 64  52
         {
 65  
             @Override
 66  
             protected RunListener initialValue()
 67  
             {
 68  26
                 return ConcurrentRunListener.this.reporterFactory.createReporter();
 69  
             }
 70  
         };
 71  26
     }
 72  
 
 73  
     public void testSetStarting( ReportEntry description )
 74  
     {
 75  26
     }
 76  
 
 77  
     public void testSetCompleted( ReportEntry result )
 78  
     {
 79  26
         final RunListener reporterManager = getRunListener();
 80  26
         for ( TestSet testSet : classMethodCounts.values() )
 81  
         {
 82  38
             testSet.replay( reporterManager );
 83  38
         }
 84  26
         reporterManagerThreadLocal.remove();
 85  26
     }
 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  3
         final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
 100  3
         if ( testMethod != null )
 101  
         {
 102  2
             testMethod.testError( failure );
 103  2
             testMethod.detachFromCurrentThread();
 104  
         }
 105  3
     }
 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  77
         TestSet testSet = getTestSet( description );
 129  77
         testSet.createThreadAttachedTestMethod( description );
 130  
 
 131  77
         checkIfTestSetCanBeReported( testSet );
 132  77
         testSet.attachToThread();
 133  77
     }
 134  
 
 135  
     public void testSucceeded( ReportEntry report )
 136  
     {
 137  70
         TestMethod testMethod = getTestMethod();
 138  70
         if ( null != testMethod )
 139  
         {
 140  70
             testMethod.testFinished();
 141  70
             testMethod.getTestSet().incrementFinishedTests( getRunListener(), reportImmediately );
 142  70
             testMethod.detachFromCurrentThread();
 143  
         }
 144  70
     }
 145  
 
 146  
     private TestMethod getOrCreateThreadAttachedTestMethod( ReportEntry description )
 147  
     {
 148  10
         TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
 149  10
         if ( threadTestMethod != null )
 150  
         {
 151  7
             return threadTestMethod;
 152  
         }
 153  3
         TestSet testSet = getTestSet( description );
 154  3
         if ( testSet == null )
 155  
         {
 156  1
             consoleLogger.info( description.getName() );
 157  1
             consoleLogger.info( description.getStackTraceWriter().writeTraceToString() );
 158  1
             return null;
 159  
         }
 160  
         else
 161  
         {
 162  2
             return testSet.createThreadAttachedTestMethod( description );
 163  
         }
 164  
     }
 165  
 
 166  
     protected abstract void checkIfTestSetCanBeReported( TestSet testSetForTest );
 167  
 
 168  
     TestMethod getTestMethod()
 169  
     {
 170  70
         return TestMethod.getThreadTestMethod();
 171  
     }
 172  
 
 173  
     TestSet getTestSet( ReportEntry description )
 174  
     {
 175  87
         return classMethodCounts.get( description.getSourceName() );
 176  
     }
 177  
 
 178  
     RunListener getRunListener()
 179  
     {
 180  127
         return reporterManagerThreadLocal.get();
 181  
     }
 182  
 
 183  
 
 184  
     public static ConcurrentRunListener createInstance( Map<String, TestSet> classMethodCounts,
 185  
                                                             ReporterFactory reporterManagerFactory,
 186  
                                                             boolean parallelClasses, boolean parallelBoth,
 187  
                                                             ConsoleLogger consoleLogger )
 188  
         throws TestSetFailedException
 189  
     {
 190  4
         if ( parallelClasses )
 191  
         {
 192  1
             return new ClassesParallelRunListener( classMethodCounts, reporterManagerFactory, consoleLogger );
 193  
         }
 194  3
         return new MethodsParallelRunListener( classMethodCounts, reporterManagerFactory, !parallelBoth,
 195  
                                                consoleLogger );
 196  
     }
 197  
 
 198  
 
 199  
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
 200  
     {
 201  1049
         TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
 202  1049
         if ( threadTestMethod != null )
 203  
         {
 204  1002
             final LogicalStream logicalStream = threadTestMethod.getLogicalStream();
 205  1002
             logicalStream.write( stdout, buf, off, len );
 206  1002
         }
 207  
         else
 208  
         {
 209  
             // Not able to assocaite output with any thread. Just dump to console
 210  47
             consoleLogger.info( new String( buf, off, len ) );
 211  
         }
 212  1049
     }
 213  
 
 214  
 }