Coverage Report - org.apache.maven.surefire.report.TestConsoleOutputRunListener
 
Classes in this File Line Coverage Branch Coverage Complexity
TestConsoleOutputRunListener
0%
0/29
0%
0/2
1.138
TestConsoleOutputRunListener$OneThreadPerClassConsoleOutputRunListener
0%
0/13
0%
0/2
1.138
TestConsoleOutputRunListener$UnknownThreadPerClassConsoleOutputRunListener
0%
0/25
0%
0/6
1.138
 
 1  
 package org.apache.maven.surefire.report;
 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.Collections;
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * Attaches the currently executing test method to the thread, allowing
 28  
  * test output to be directed to the proper test set.
 29  
  *
 30  
  * @author Kristian Rosenvold
 31  
  */
 32  
 public abstract class TestConsoleOutputRunListener
 33  
     implements RunListener, ConsoleOutputReceiver
 34  
 {
 35  
     private final ReporterFactory reporterFactory;
 36  
 
 37  
     protected TestConsoleOutputRunListener( ReporterFactory reporterFactory )
 38  0
     {
 39  0
         this.reporterFactory = reporterFactory;
 40  0
     }
 41  
 
 42  
     public static TestConsoleOutputRunListener createInstance( ReporterFactory reporterFactory,
 43  
                                                                boolean oneThreadPerClass )
 44  
     {
 45  0
         return oneThreadPerClass ? (TestConsoleOutputRunListener) new OneThreadPerClassConsoleOutputRunListener(
 46  
             reporterFactory ) : new UnknownThreadPerClassConsoleOutputRunListener( reporterFactory );
 47  
     }
 48  
 
 49  
     protected abstract RunListener getTestSetRunListener( ReportEntry reportEntry );
 50  
 
 51  
     protected abstract void clearTestSetRunListener( ReportEntry reportEntry );
 52  
 
 53  
     protected abstract RunListener getTestMethodRunListener( ReportEntry report );
 54  
 
 55  
     protected abstract void clearTestMethodRunListener( ReportEntry reportEntry );
 56  
 
 57  
     protected abstract ConsoleOutputReceiver getConsoleOutputReceiver();
 58  
 
 59  
     protected ReporterFactory getReporterFactory()
 60  
     {
 61  0
         return reporterFactory;
 62  
     }
 63  
 
 64  
 
 65  
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
 66  
     {
 67  0
         getConsoleOutputReceiver().writeTestOutput( buf, off, len, stdout );
 68  0
     }
 69  
 
 70  
     public void testSetStarting( ReportEntry report )
 71  
         throws ReporterException
 72  
     {
 73  0
         getTestSetRunListener( report ).testSetStarting( report );
 74  0
     }
 75  
 
 76  
     public void testSetCompleted( ReportEntry report )
 77  
         throws ReporterException
 78  
     {
 79  0
         getTestSetRunListener( report ).testSetCompleted( report );
 80  0
         clearTestSetRunListener( report );
 81  0
     }
 82  
 
 83  
     public void testStarting( ReportEntry report )
 84  
     {
 85  0
         getTestMethodRunListener( report ).testStarting( report );
 86  0
     }
 87  
 
 88  
     public void testSucceeded( ReportEntry report )
 89  
     {
 90  0
         getTestMethodRunListener( report ).testSucceeded( report );
 91  0
         clearTestMethodRunListener( report );
 92  0
     }
 93  
 
 94  
     public void testAssumptionFailure( ReportEntry report )
 95  
     {
 96  0
         getTestMethodRunListener( report ).testAssumptionFailure( report );
 97  0
         clearTestMethodRunListener( report );
 98  0
     }
 99  
 
 100  
     public void testError( ReportEntry report )
 101  
     {
 102  0
         getTestMethodRunListener( report ).testError( report );
 103  0
         clearTestMethodRunListener( report );
 104  0
     }
 105  
 
 106  
     public void testFailed( ReportEntry report )
 107  
     {
 108  0
         getTestMethodRunListener( report ).testFailed( report );
 109  0
         clearTestMethodRunListener( report );
 110  0
     }
 111  
 
 112  
     public void testSkipped( ReportEntry report )
 113  
     {
 114  0
         getTestMethodRunListener( report ).testSkipped( report );
 115  0
         clearTestMethodRunListener( report );
 116  0
     }
 117  
 
 118  
     public static class OneThreadPerClassConsoleOutputRunListener
 119  
         extends TestConsoleOutputRunListener
 120  
     {
 121  0
         private final ThreadLocal currentTestMethodListener = new InheritableThreadLocal();
 122  
 
 123  
         public OneThreadPerClassConsoleOutputRunListener( ReporterFactory reporterFactory )
 124  
         {
 125  0
             super( reporterFactory );
 126  0
         }
 127  
 
 128  
         protected RunListener getTestSetRunListener( ReportEntry reportEntry )
 129  
         {
 130  0
             return getTestMethodRunListener( reportEntry );
 131  
         }
 132  
 
 133  
         protected void clearTestSetRunListener( ReportEntry reportEntry )
 134  
         {
 135  0
             currentTestMethodListener.remove();
 136  0
         }
 137  
 
 138  
         protected void clearTestMethodRunListener( ReportEntry reportEntry )
 139  
         {
 140  
             // Dont clear, we do this in testset.
 141  0
         }
 142  
 
 143  
         protected RunListener getTestMethodRunListener( ReportEntry report )
 144  
         {
 145  0
             RunListener runListener = (RunListener) currentTestMethodListener.get();
 146  0
             if ( runListener == null )
 147  
             {
 148  0
                 runListener = getReporterFactory().createReporter();
 149  0
                 currentTestMethodListener.set( runListener );
 150  
             }
 151  0
             return runListener;
 152  
         }
 153  
 
 154  
         protected ConsoleOutputReceiver getConsoleOutputReceiver()
 155  
         {
 156  0
             return (ConsoleOutputReceiver) currentTestMethodListener.get();
 157  
         }
 158  
 
 159  
 
 160  
     }
 161  
 
 162  
     public static class UnknownThreadPerClassConsoleOutputRunListener
 163  
         extends TestConsoleOutputRunListener
 164  
     {
 165  0
         private final ThreadLocal currentTestMethodListener = new InheritableThreadLocal();
 166  
 
 167  0
         private final ThreadLocal currentTestSetListener = new InheritableThreadLocal();
 168  
 
 169  0
         private final Map testSetToRunListener = Collections.synchronizedMap( new HashMap() );
 170  
 
 171  
         public UnknownThreadPerClassConsoleOutputRunListener( ReporterFactory reporterFactory )
 172  
         {
 173  0
             super( reporterFactory );
 174  0
         }
 175  
 
 176  
 
 177  
         protected RunListener getTestSetRunListener( ReportEntry reportEntry )
 178  
         {
 179  0
             RunListener result = (RunListener) testSetToRunListener.get( reportEntry.getSourceName() );
 180  0
             if ( result == null )
 181  
             {
 182  0
                 result = getReporterFactory().createReporter();
 183  0
                 testSetToRunListener.put( reportEntry.getSourceName(), result );
 184  
             }
 185  0
             currentTestSetListener.set( result );
 186  0
             return result;
 187  
         }
 188  
 
 189  
         protected void clearTestSetRunListener( ReportEntry reportEntry )
 190  
         {
 191  0
             currentTestSetListener.remove();
 192  0
         }
 193  
 
 194  
         protected RunListener getTestMethodRunListener( ReportEntry report )
 195  
         {
 196  
             RunListener runListener;
 197  0
             runListener = (RunListener) testSetToRunListener.get( report.getSourceName() );
 198  0
             if ( runListener == null )
 199  
             {
 200  0
                 runListener = getReporterFactory().createReporter();
 201  0
                 testSetToRunListener.put( report.getSourceName(), runListener );
 202  
             }
 203  0
             currentTestMethodListener.set( runListener );
 204  0
             return runListener;
 205  
         }
 206  
 
 207  
         protected void clearTestMethodRunListener( ReportEntry reportEntry )
 208  
         {
 209  0
             currentTestMethodListener.remove();
 210  0
         }
 211  
 
 212  
         protected ConsoleOutputReceiver getConsoleOutputReceiver()
 213  
         {
 214  0
             ConsoleOutputReceiver consoleOutputReceiver = (ConsoleOutputReceiver) currentTestMethodListener.get();
 215  0
             if ( consoleOutputReceiver == null )
 216  
             {
 217  0
                 consoleOutputReceiver = (ConsoleOutputReceiver) currentTestSetListener.get();
 218  
             }
 219  0
             return consoleOutputReceiver;
 220  
         }
 221  
 
 222  
 
 223  
     }
 224  
 
 225  
 }