1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37 public class TestSet
38 {
39 private final Description testSetDescription;
40
41 private final AtomicInteger numberOfCompletedChildren = new AtomicInteger( 0 );
42
43
44
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 }