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