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 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
class TestMethod |
35 | |
implements ConsoleOutputReceiver |
36 | |
{ |
37 | |
private final ReportEntry description; |
38 | |
|
39 | |
private final TestSet testSet; |
40 | |
|
41 | |
private final long startTime; |
42 | |
|
43 | |
private long endTime; |
44 | |
|
45 | |
private volatile ReportEntry testFailure; |
46 | |
|
47 | |
private volatile ReportEntry testError; |
48 | |
|
49 | |
private volatile ReportEntry ignored; |
50 | |
|
51 | 1 | private static final InheritableThreadLocal<TestMethod> TEST_METHOD = new InheritableThreadLocal<TestMethod>(); |
52 | |
|
53 | |
private volatile LogicalStream output; |
54 | |
|
55 | |
public TestMethod( ReportEntry description, TestSet testSet ) |
56 | 87 | { |
57 | 87 | this.description = description; |
58 | 87 | this.testSet = testSet; |
59 | 87 | startTime = System.currentTimeMillis(); |
60 | 87 | } |
61 | |
|
62 | |
public void testFinished() |
63 | |
{ |
64 | 70 | setEndTime(); |
65 | 70 | } |
66 | |
|
67 | |
public void testIgnored( ReportEntry description ) |
68 | |
{ |
69 | 7 | ignored = description; |
70 | 7 | setEndTime(); |
71 | 7 | } |
72 | |
|
73 | |
public void testFailure( ReportEntry failure ) |
74 | |
{ |
75 | 8 | this.testFailure = failure; |
76 | 8 | setEndTime(); |
77 | 8 | } |
78 | |
|
79 | |
public void testError( ReportEntry failure ) |
80 | |
{ |
81 | 2 | this.testError = failure; |
82 | 2 | setEndTime(); |
83 | 2 | } |
84 | |
|
85 | |
private void setEndTime() |
86 | |
{ |
87 | 87 | this.endTime = System.currentTimeMillis(); |
88 | 87 | } |
89 | |
|
90 | |
public int getElapsed() |
91 | |
{ |
92 | 182 | return endTime > 0 ? (int) ( endTime - startTime ) : 0; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
public void replay( RunListener reporter ) |
97 | |
{ |
98 | |
|
99 | 86 | if ( ignored != null ) |
100 | |
{ |
101 | 7 | reporter.testSkipped( createReportEntry( ignored ) ); |
102 | 7 | return; |
103 | |
} |
104 | |
|
105 | 79 | ReportEntry descriptionReport = createReportEntry( description ); |
106 | 79 | reporter.testStarting( descriptionReport ); |
107 | 79 | if ( output != null ) |
108 | |
{ |
109 | 1 | output.writeDetails( ( (ConsoleOutputReceiver) reporter ) ); |
110 | |
} |
111 | |
|
112 | 79 | if ( testFailure != null ) |
113 | |
{ |
114 | 7 | reporter.testFailed( createReportEntry( testFailure ) ); |
115 | |
} |
116 | 72 | else if ( testError != null ) |
117 | |
{ |
118 | 2 | reporter.testError( createReportEntry( testError ) ); |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 70 | reporter.testSucceeded( descriptionReport ); |
123 | |
} |
124 | 79 | } |
125 | |
|
126 | |
private ReportEntry createReportEntry( ReportEntry reportEntry ) |
127 | |
{ |
128 | 95 | return new CategorizedReportEntry( reportEntry.getSourceName(), reportEntry.getName(), reportEntry.getGroup(), |
129 | |
reportEntry.getStackTraceWriter(), getElapsed(), reportEntry.getMessage() ); |
130 | |
} |
131 | |
|
132 | |
public void attachToThread() |
133 | |
{ |
134 | 86 | TEST_METHOD.set( this ); |
135 | 86 | ConsoleOutputReceiverForCurrentThread.set( this ); |
136 | |
|
137 | 86 | } |
138 | |
|
139 | |
public void detachFromCurrentThread() |
140 | |
{ |
141 | 86 | TEST_METHOD.remove(); |
142 | 86 | ConsoleOutputReceiverForCurrentThread.remove(); |
143 | 86 | } |
144 | |
|
145 | |
public static TestMethod getThreadTestMethod() |
146 | |
{ |
147 | 1129 | return TEST_METHOD.get(); |
148 | |
} |
149 | |
|
150 | |
public LogicalStream getLogicalStream() |
151 | |
{ |
152 | 1002 | if ( output == null ) |
153 | |
{ |
154 | 1 | output = new LogicalStream(); |
155 | |
} |
156 | 1002 | return output; |
157 | |
} |
158 | |
|
159 | |
public void writeTestOutput( byte[] buf, int off, int len, boolean stdout ) |
160 | |
{ |
161 | 0 | getLogicalStream().write( stdout, buf, off, len ); |
162 | 0 | } |
163 | |
|
164 | |
public TestSet getTestSet() |
165 | |
{ |
166 | 70 | return testSet; |
167 | |
} |
168 | |
} |