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 private final AtomicInteger numberOfCompletedChildren = new AtomicInteger( 0 );
44
45
46
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 }