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.CategorizedReportEntry;
23 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
24 import org.apache.maven.surefire.report.ConsoleOutputReceiverForCurrentThread;
25 import org.apache.maven.surefire.report.ReportEntry;
26 import org.apache.maven.surefire.report.RunListener;
27
28 import java.util.concurrent.atomic.AtomicReference;
29
30
31
32
33
34
35
36 class TestMethod
37 implements ConsoleOutputReceiver
38 {
39 private static final InheritableThreadLocal<TestMethod> TEST_METHOD = new InheritableThreadLocal<>();
40
41 private final AtomicReference<LogicalStream> output = new AtomicReference<>();
42
43 private final ReportEntry description;
44
45 private final TestSet testSet;
46
47 private final long startTime;
48
49 private volatile long endTime;
50
51 private volatile ReportEntry testFailure;
52
53 private volatile ReportEntry testError;
54
55 private volatile ReportEntry testIgnored;
56
57 private volatile ReportEntry testAssumption;
58
59 TestMethod( ReportEntry description, TestSet testSet )
60 {
61 this.description = description;
62 this.testSet = testSet;
63 startTime = System.currentTimeMillis();
64 }
65
66 void testFinished()
67 {
68 setEndTime();
69 }
70
71 void testIgnored( ReportEntry description )
72 {
73 testIgnored = description;
74 setEndTime();
75 }
76
77 void testFailure( ReportEntry failure )
78 {
79 this.testFailure = failure;
80 setEndTime();
81 }
82
83 void testError( ReportEntry failure )
84 {
85 this.testError = failure;
86 setEndTime();
87 }
88
89 void testAssumption( ReportEntry failure )
90 {
91 this.testAssumption = failure;
92 setEndTime();
93 }
94
95 private void setEndTime()
96 {
97 this.endTime = System.currentTimeMillis();
98 }
99
100 int getElapsed()
101 {
102 return endTime > 0 ? (int) ( endTime - startTime ) : 0;
103 }
104
105 long getStartTime()
106 {
107 return startTime;
108 }
109
110 long getEndTime()
111 {
112 return endTime;
113 }
114
115 void replay( RunListener reporter )
116 {
117 if ( testIgnored != null )
118 {
119 reporter.testSkipped( createReportEntry( testIgnored ) );
120 }
121 else
122 {
123 ReportEntry descriptionReport = createReportEntry( description );
124 reporter.testStarting( descriptionReport );
125 LogicalStream ls = output.get();
126 if ( ls != null )
127 {
128 ls.writeDetails( (ConsoleOutputReceiver) reporter );
129 }
130
131 if ( testFailure != null )
132 {
133 reporter.testFailed( createReportEntry( testFailure ) );
134 }
135 else if ( testError != null )
136 {
137 reporter.testError( createReportEntry( testError ) );
138 }
139 else if ( testAssumption != null )
140 {
141 reporter.testAssumptionFailure( createReportEntry( testAssumption ) );
142 }
143 else
144 {
145 reporter.testSucceeded( descriptionReport );
146 }
147 }
148 }
149
150 private ReportEntry createReportEntry( ReportEntry reportEntry )
151 {
152 return new CategorizedReportEntry( reportEntry.getSourceName(), reportEntry.getName(), reportEntry.getGroup(),
153 reportEntry.getStackTraceWriter(), getElapsed(), reportEntry.getMessage() );
154 }
155
156 void attachToThread()
157 {
158 TEST_METHOD.set( this );
159 ConsoleOutputReceiverForCurrentThread.set( this );
160 }
161
162 void detachFromCurrentThread()
163 {
164 TEST_METHOD.remove();
165 ConsoleOutputReceiverForCurrentThread.remove();
166 }
167
168 static TestMethod getThreadTestMethod()
169 {
170 return TEST_METHOD.get();
171 }
172
173 LogicalStream getLogicalStream()
174 {
175 LogicalStream ls = output.get();
176 if ( ls == null )
177 {
178 ls = new LogicalStream();
179 if ( !output.compareAndSet( null, ls ) )
180 {
181 ls = output.get();
182 }
183 }
184 return ls;
185 }
186
187 @Override
188 public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
189 {
190 getLogicalStream().write( stdout, buf, off, len );
191 }
192
193 public TestSet getTestSet()
194 {
195 return testSet;
196 }
197 }