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.RunListener;
23  import org.apache.maven.surefire.report.ReportEntry;
24  import org.apache.maven.surefire.report.Reporter;
25  import org.apache.maven.surefire.report.SimpleReportEntry;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  import java.util.concurrent.atomic.AtomicBoolean;
31  import java.util.concurrent.atomic.AtomicInteger;
32  
33  import org.apache.maven.surefire.util.NestedRuntimeException;
34  import org.junit.runner.Description;
35  
36  /**
37   * * Represents the test-state of a testset that is run.
38   */
39  public class TestSet
40  {
41      private final Description testSetDescription;
42  
43      private final AtomicInteger numberOfCompletedChildren = new AtomicInteger( 0 );
44  
45      // While the two parameters below may seem duplicated, it is not entirely the case,
46      // since numberOfTests has the correct value from the start, while testMethods grows as method execution starts.
47  
48      private final AtomicInteger numberOfTests = new AtomicInteger( 0 );
49  
50      private final List<TestMethod> testMethods = Collections.synchronizedList( new ArrayList<TestMethod>() );
51  
52      private static final InheritableThreadLocal<TestSet> testSet = new InheritableThreadLocal<TestSet>();
53  
54      private final AtomicBoolean allScheduled = new AtomicBoolean();
55  
56      private final AtomicBoolean played = new AtomicBoolean();
57  
58  
59      public TestSet( Description testSetDescription )
60      {
61          this.testSetDescription = testSetDescription;
62      }
63  
64      public void replay( RunListener target )
65      {
66          if ( !played.compareAndSet( false, true ) )
67          {
68              return;
69          }
70  
71          try
72          {
73              int elapsed = 0;
74              for ( TestMethod testMethod : testMethods )
75              {
76                  elapsed += testMethod.getElapsed();
77              }
78              ReportEntry report = createReportEntry( null );
79  
80              target.testSetStarting( report );
81  
82              for ( TestMethod testMethod : testMethods )
83              {
84                  testMethod.replay( (Reporter) target );
85              }
86              report = createReportEntry( elapsed );
87  
88              target.testSetCompleted( report );
89          }
90          catch ( Exception e )
91          {
92              throw new NestedRuntimeException( e );
93          }
94      }
95  
96      public TestMethod createTestMethod( ReportEntry description )
97      {
98          TestMethod testMethod = new TestMethod( description );
99          addTestMethod( testMethod );
100         return testMethod;
101     }
102 
103     private ReportEntry createReportEntry( Integer elapsed )
104     {
105         boolean isJunit3 = testSetDescription.getTestClass() == null;
106         String classNameToUse =
107             isJunit3 ? testSetDescription.getChildren().get( 0 ).getClassName() : testSetDescription.getClassName();
108         return new SimpleReportEntry( classNameToUse, classNameToUse, elapsed );
109     }
110 
111     public void incrementTestMethodCount()
112     {
113         numberOfTests.incrementAndGet();
114     }
115 
116     public void addTestMethod( TestMethod testMethod )
117     {
118         testMethods.add( testMethod );
119     }
120 
121     public void incrementFinishedTests( RunListener reporterManager, boolean reportImmediately )
122     {
123         numberOfCompletedChildren.incrementAndGet();
124         if ( allScheduled.get() && isAllTestsDone() && reportImmediately )
125         {
126             replay( reporterManager );
127         }
128     }
129 
130     public void setAllScheduled( RunListener reporterManager )
131     {
132         allScheduled.set( true );
133         if ( isAllTestsDone() )
134         {
135             replay( reporterManager );
136         }
137     }
138 
139     private boolean isAllTestsDone()
140     {
141         return testMethods.size() == numberOfCompletedChildren.get();
142     }
143 
144     public void attachToThread()
145     {
146         testSet.set( this );
147     }
148 
149     public static TestSet getThreadTestSet()
150     {
151         return testSet.get();
152     }
153 }