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 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  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      {
58          this.reportImmediately = reportImmediately;
59          this.reporterFactory = reporterFactory;
60          this.classMethodCounts = classMethodCounts;
61          this.consoleLogger = consoleLogger;
62  
63          this.reporterManagerThreadLocal = new ThreadLocal<RunListener>()
64          {
65              @Override
66              protected RunListener initialValue()
67              {
68                  return ConcurrentRunListener.this.reporterFactory.createReporter();
69              }
70          };
71      }
72  
73      public void testSetStarting( ReportEntry description )
74      {
75      }
76  
77      public void testSetCompleted( ReportEntry result )
78      {
79          final RunListener reporterManager = getRunListener();
80          for ( TestSet testSet : classMethodCounts.values() )
81          {
82              testSet.replay( reporterManager );
83          }
84          reporterManagerThreadLocal.remove();
85      }
86  
87      public void testFailed( ReportEntry failure )
88      {
89          final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
90          if ( testMethod != null )
91          {
92              testMethod.testFailure( failure );
93              testMethod.detachFromCurrentThread();
94          }
95      }
96  
97      public void testError( ReportEntry failure )
98      {
99          final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
100         if ( testMethod != null )
101         {
102             testMethod.testError( failure );
103             testMethod.detachFromCurrentThread();
104         }
105     }
106 
107     public void testSkipped( ReportEntry description )
108     {
109         TestSet testSet = getTestSet( description );
110         TestMethod testMethod = testSet.createThreadAttachedTestMethod( description );
111         testMethod.testIgnored( description );
112         testSet.incrementFinishedTests( getRunListener(), reportImmediately );
113         testMethod.detachFromCurrentThread();
114     }
115 
116     public void testAssumptionFailure( ReportEntry failure )
117     {
118         final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
119         if ( testMethod != null )
120         {
121             testMethod.testIgnored( failure );
122             testMethod.detachFromCurrentThread();
123         }
124     }
125 
126     public void testStarting( ReportEntry description )
127     {
128         TestSet testSet = getTestSet( description );
129         testSet.createThreadAttachedTestMethod( description );
130 
131         checkIfTestSetCanBeReported( testSet );
132         testSet.attachToThread();
133     }
134 
135     public void testSucceeded( ReportEntry report )
136     {
137         TestMethod testMethod = getTestMethod();
138         if ( null != testMethod )
139         {
140             testMethod.testFinished();
141             testMethod.getTestSet().incrementFinishedTests( getRunListener(), reportImmediately );
142             testMethod.detachFromCurrentThread();
143         }
144     }
145 
146     private TestMethod getOrCreateThreadAttachedTestMethod( ReportEntry description )
147     {
148         TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
149         if ( threadTestMethod != null )
150         {
151             return threadTestMethod;
152         }
153         TestSet testSet = getTestSet( description );
154         if ( testSet == null )
155         {
156             consoleLogger.info( description.getName() );
157             consoleLogger.info( description.getStackTraceWriter().writeTraceToString() );
158             return null;
159         }
160         else
161         {
162             return testSet.createThreadAttachedTestMethod( description );
163         }
164     }
165 
166     protected abstract void checkIfTestSetCanBeReported( TestSet testSetForTest );
167 
168     TestMethod getTestMethod()
169     {
170         return TestMethod.getThreadTestMethod();
171     }
172 
173     TestSet getTestSet( ReportEntry description )
174     {
175         return classMethodCounts.get( description.getSourceName() );
176     }
177 
178     RunListener getRunListener()
179     {
180         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         if ( parallelClasses )
191         {
192             return new ClassesParallelRunListener( classMethodCounts, reporterManagerFactory, consoleLogger );
193         }
194         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         TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
202         if ( threadTestMethod != null )
203         {
204             final LogicalStream logicalStream = threadTestMethod.getLogicalStream();
205             logicalStream.write( stdout, buf, off, len );
206         }
207         else
208         {
209             // Not able to assocaite output with any thread. Just dump to console
210             consoleLogger.info( new String( buf, off, len ) );
211         }
212     }
213 
214 }