Coverage Report - org.apache.maven.surefire.junitcore.TestMethod
 
Classes in this File Line Coverage Branch Coverage Complexity
TestMethod
95%
45/47
91%
11/12
1,467
 
 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.report.CategorizedReportEntry;
 23  
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 24  
 import org.apache.maven.surefire.report.ConsoleOutputReceiverForCurrentThread;
 25  
 import org.apache.maven.surefire.report.ReportEntry;
 26  
 import org.apache.maven.surefire.report.RunListener;
 27  
 
 28  
 /**
 29  
  * Represents the test-state of a single test method that is run.
 30  
  * <p/>
 31  
  * Notes about thread safety: This instance is serially confined to 1-3 threads (construction, test-run, reporting),
 32  
  * without any actual parallel access
 33  
  */
 34  
 class TestMethod
 35  
     implements ConsoleOutputReceiver
 36  
 {
 37  
     private final ReportEntry description;
 38  
 
 39  
     private final TestSet testSet;
 40  
 
 41  
     private final long startTime;
 42  
 
 43  
     private long endTime;
 44  
 
 45  
     private volatile ReportEntry testFailure;
 46  
 
 47  
     private volatile ReportEntry testError;
 48  
 
 49  
     private volatile ReportEntry ignored;
 50  
 
 51  1
     private static final InheritableThreadLocal<TestMethod> TEST_METHOD = new InheritableThreadLocal<TestMethod>();
 52  
 
 53  
     private volatile LogicalStream output;
 54  
 
 55  
     public TestMethod( ReportEntry description, TestSet testSet )
 56  87
     {
 57  87
         this.description = description;
 58  87
         this.testSet = testSet;
 59  87
         startTime = System.currentTimeMillis();
 60  87
     }
 61  
 
 62  
     public void testFinished()
 63  
     {
 64  70
         setEndTime();
 65  70
     }
 66  
 
 67  
     public void testIgnored( ReportEntry description )
 68  
     {
 69  7
         ignored = description;
 70  7
         setEndTime();
 71  7
     }
 72  
 
 73  
     public void testFailure( ReportEntry failure )
 74  
     {
 75  8
         this.testFailure = failure;
 76  8
         setEndTime();
 77  8
     }
 78  
 
 79  
     public void testError( ReportEntry failure )
 80  
     {
 81  2
         this.testError = failure;
 82  2
         setEndTime();
 83  2
     }
 84  
 
 85  
     private void setEndTime()
 86  
     {
 87  87
         this.endTime = System.currentTimeMillis();
 88  87
     }
 89  
 
 90  
     public int getElapsed()
 91  
     {
 92  182
         return endTime > 0 ? (int) ( endTime - startTime ) : 0;
 93  
     }
 94  
 
 95  
 
 96  
     public void replay( RunListener reporter )
 97  
     {
 98  
 
 99  86
         if ( ignored != null )
 100  
         {
 101  7
             reporter.testSkipped( createReportEntry( ignored ) );
 102  7
             return;
 103  
         }
 104  
 
 105  79
         ReportEntry descriptionReport = createReportEntry( description );
 106  79
         reporter.testStarting( descriptionReport );
 107  79
         if ( output != null )
 108  
         {
 109  1
             output.writeDetails( ( (ConsoleOutputReceiver) reporter ) );
 110  
         }
 111  
 
 112  79
         if ( testFailure != null )
 113  
         {
 114  7
             reporter.testFailed( createReportEntry( testFailure ) );
 115  
         }
 116  72
         else if ( testError != null )
 117  
         {
 118  2
             reporter.testError( createReportEntry( testError ) );
 119  
         }
 120  
         else
 121  
         {
 122  70
             reporter.testSucceeded( descriptionReport );
 123  
         }
 124  79
     }
 125  
 
 126  
     private ReportEntry createReportEntry( ReportEntry reportEntry )
 127  
     {
 128  95
         return new CategorizedReportEntry( reportEntry.getSourceName(), reportEntry.getName(), reportEntry.getGroup(),
 129  
                                            reportEntry.getStackTraceWriter(), getElapsed(), reportEntry.getMessage() );
 130  
     }
 131  
 
 132  
     public void attachToThread()
 133  
     {
 134  86
         TEST_METHOD.set( this );
 135  86
         ConsoleOutputReceiverForCurrentThread.set( this );
 136  
 
 137  86
     }
 138  
 
 139  
     public void detachFromCurrentThread()
 140  
     {
 141  86
         TEST_METHOD.remove();
 142  86
         ConsoleOutputReceiverForCurrentThread.remove();
 143  86
     }
 144  
 
 145  
     public static TestMethod getThreadTestMethod()
 146  
     {
 147  1129
         return TEST_METHOD.get();
 148  
     }
 149  
 
 150  
     public LogicalStream getLogicalStream()
 151  
     {
 152  1002
         if ( output == null )
 153  
         {
 154  1
             output = new LogicalStream();
 155  
         }
 156  1002
         return output;
 157  
     }
 158  
 
 159  
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
 160  
     {
 161  0
         getLogicalStream().write( stdout, buf, off, len );
 162  0
     }
 163  
 
 164  
     public TestSet getTestSet()
 165  
     {
 166  70
         return testSet;
 167  
     }
 168  
 }