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 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 | |
|
38 | |
|
39 | |
public class TestSet |
40 | |
{ |
41 | |
private final Description testSetDescription; |
42 | |
|
43 | 0 | private final AtomicInteger numberOfCompletedChildren = new AtomicInteger( 0 ); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | private final AtomicInteger numberOfTests = new AtomicInteger( 0 ); |
49 | |
|
50 | 0 | private final List<TestMethod> testMethods = Collections.synchronizedList( new ArrayList<TestMethod>() ); |
51 | |
|
52 | 0 | private static final InheritableThreadLocal<TestSet> testSet = new InheritableThreadLocal<TestSet>(); |
53 | |
|
54 | 0 | private final AtomicBoolean allScheduled = new AtomicBoolean(); |
55 | |
|
56 | 0 | private final AtomicBoolean played = new AtomicBoolean(); |
57 | |
|
58 | |
|
59 | |
public TestSet( Description testSetDescription ) |
60 | 0 | { |
61 | 0 | this.testSetDescription = testSetDescription; |
62 | 0 | } |
63 | |
|
64 | |
public void replay( RunListener target ) |
65 | |
{ |
66 | 0 | if ( !played.compareAndSet( false, true ) ) |
67 | |
{ |
68 | 0 | return; |
69 | |
} |
70 | |
|
71 | |
try |
72 | |
{ |
73 | 0 | int elapsed = 0; |
74 | 0 | for ( TestMethod testMethod : testMethods ) |
75 | |
{ |
76 | 0 | elapsed += testMethod.getElapsed(); |
77 | |
} |
78 | 0 | ReportEntry report = createReportEntry( null ); |
79 | |
|
80 | 0 | target.testSetStarting( report ); |
81 | |
|
82 | 0 | for ( TestMethod testMethod : testMethods ) |
83 | |
{ |
84 | 0 | testMethod.replay( (Reporter) target ); |
85 | |
} |
86 | 0 | report = createReportEntry( elapsed ); |
87 | |
|
88 | 0 | target.testSetCompleted( report ); |
89 | |
} |
90 | 0 | catch ( Exception e ) |
91 | |
{ |
92 | 0 | throw new NestedRuntimeException( e ); |
93 | 0 | } |
94 | 0 | } |
95 | |
|
96 | |
public TestMethod createTestMethod( ReportEntry description ) |
97 | |
{ |
98 | 0 | TestMethod testMethod = new TestMethod( description ); |
99 | 0 | addTestMethod( testMethod ); |
100 | 0 | return testMethod; |
101 | |
} |
102 | |
|
103 | |
private ReportEntry createReportEntry( Integer elapsed ) |
104 | |
{ |
105 | 0 | boolean isJunit3 = testSetDescription.getTestClass() == null; |
106 | 0 | String classNameToUse = |
107 | |
isJunit3 ? testSetDescription.getChildren().get( 0 ).getClassName() : testSetDescription.getClassName(); |
108 | 0 | return new SimpleReportEntry( classNameToUse, classNameToUse, elapsed ); |
109 | |
} |
110 | |
|
111 | |
public void incrementTestMethodCount() |
112 | |
{ |
113 | 0 | numberOfTests.incrementAndGet(); |
114 | 0 | } |
115 | |
|
116 | |
public void addTestMethod( TestMethod testMethod ) |
117 | |
{ |
118 | 0 | testMethods.add( testMethod ); |
119 | 0 | } |
120 | |
|
121 | |
public void incrementFinishedTests( RunListener reporterManager, boolean reportImmediately ) |
122 | |
{ |
123 | 0 | numberOfCompletedChildren.incrementAndGet(); |
124 | 0 | if ( allScheduled.get() && isAllTestsDone() && reportImmediately ) |
125 | |
{ |
126 | 0 | replay( reporterManager ); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
public void setAllScheduled( RunListener reporterManager ) |
131 | |
{ |
132 | 0 | allScheduled.set( true ); |
133 | 0 | if ( isAllTestsDone() ) |
134 | |
{ |
135 | 0 | replay( reporterManager ); |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
private boolean isAllTestsDone() |
140 | |
{ |
141 | 0 | return testMethods.size() == numberOfCompletedChildren.get(); |
142 | |
} |
143 | |
|
144 | |
public void attachToThread() |
145 | |
{ |
146 | 0 | testSet.set( this ); |
147 | 0 | } |
148 | |
|
149 | |
public static TestSet getThreadTestSet() |
150 | |
{ |
151 | 0 | return testSet.get(); |
152 | |
} |
153 | |
} |