View Javadoc

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      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      {
57          this.description = description;
58          this.testSet = testSet;
59          startTime = System.currentTimeMillis();
60      }
61  
62      public void testFinished()
63      {
64          setEndTime();
65      }
66  
67      public void testIgnored( ReportEntry description )
68      {
69          ignored = description;
70          setEndTime();
71      }
72  
73      public void testFailure( ReportEntry failure )
74      {
75          this.testFailure = failure;
76          setEndTime();
77      }
78  
79      public void testError( ReportEntry failure )
80      {
81          this.testError = failure;
82          setEndTime();
83      }
84  
85      private void setEndTime()
86      {
87          this.endTime = System.currentTimeMillis();
88      }
89  
90      public int getElapsed()
91      {
92          return endTime > 0 ? (int) ( endTime - startTime ) : 0;
93      }
94  
95  
96      public void replay( RunListener reporter )
97      {
98  
99          if ( ignored != null )
100         {
101             reporter.testSkipped( createReportEntry( ignored ) );
102             return;
103         }
104 
105         ReportEntry descriptionReport = createReportEntry( description );
106         reporter.testStarting( descriptionReport );
107         if ( output != null )
108         {
109             output.writeDetails( ( (ConsoleOutputReceiver) reporter ) );
110         }
111 
112         if ( testFailure != null )
113         {
114             reporter.testFailed( createReportEntry( testFailure ) );
115         }
116         else if ( testError != null )
117         {
118             reporter.testError( createReportEntry( testError ) );
119         }
120         else
121         {
122             reporter.testSucceeded( descriptionReport );
123         }
124     }
125 
126     private ReportEntry createReportEntry( ReportEntry reportEntry )
127     {
128         return new CategorizedReportEntry( reportEntry.getSourceName(), reportEntry.getName(), reportEntry.getGroup(),
129                                            reportEntry.getStackTraceWriter(), getElapsed(), reportEntry.getMessage() );
130     }
131 
132     public void attachToThread()
133     {
134         TEST_METHOD.set( this );
135         ConsoleOutputReceiverForCurrentThread.set( this );
136 
137     }
138 
139     public void detachFromCurrentThread()
140     {
141         TEST_METHOD.remove();
142         ConsoleOutputReceiverForCurrentThread.remove();
143     }
144 
145     public static TestMethod getThreadTestMethod()
146     {
147         return TEST_METHOD.get();
148     }
149 
150     public LogicalStream getLogicalStream()
151     {
152         if ( output == null )
153         {
154             output = new LogicalStream();
155         }
156         return output;
157     }
158 
159     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
160     {
161         getLogicalStream().write( stdout, buf, off, len );
162     }
163 
164     public TestSet getTestSet()
165     {
166         return testSet;
167     }
168 }