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 | 85 | { |
57 | 85 | this.description = description; |
58 | 85 | this.testSet = testSet; |
59 | 85 | startTime = System.currentTimeMillis(); |
60 | 85 | } |
61 | |
|
62 | |
public void testFinished() |
63 | |
{ |
64 | 68 | setEndTime(); |
65 | 68 | } |
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 | 85 | this.endTime = System.currentTimeMillis(); |
88 | 85 | } |
89 | |
|
90 | |
public int getElapsed() |
91 | |
{ |
92 | 178 | return (int) ( endTime - startTime ); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
public void replay( RunListener reporter ) |
97 | |
{ |
98 | |
|
99 | 84 | if ( ignored != null ) |
100 | |
{ |
101 | 7 | reporter.testSkipped( createReportEntry( ignored ) ); |
102 | 7 | return; |
103 | |
} |
104 | |
|
105 | 77 | ReportEntry descriptionReport = createReportEntry( description ); |
106 | 77 | reporter.testStarting( descriptionReport ); |
107 | 77 | if ( output != null ) |
108 | |
{ |
109 | 0 | output.writeDetails( ( (ConsoleOutputReceiver) reporter ) ); |
110 | |
} |
111 | |
|
112 | 77 | if ( testFailure != null ) |
113 | |
{ |
114 | 7 | reporter.testFailed( createReportEntry( testFailure ) ); |
115 | |
} |
116 | 70 | else if ( testError != null ) |
117 | |
{ |
118 | 2 | reporter.testError( createReportEntry( testError ) ); |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 68 | reporter.testSucceeded( descriptionReport ); |
123 | |
} |
124 | 77 | } |
125 | |
|
126 | |
private ReportEntry createReportEntry( ReportEntry reportEntry ) |
127 | |
{ |
128 | 93 | return new CategorizedReportEntry( reportEntry.getSourceName(), reportEntry.getName(), reportEntry.getGroup(), |
129 | |
reportEntry.getStackTraceWriter(), getElapsed(), reportEntry.getMessage() ); |
130 | |
} |
131 | |
|
132 | |
public void attachToThread() |
133 | |
{ |
134 | 84 | TEST_METHOD.set( this ); |
135 | 84 | ConsoleOutputReceiverForCurrentThread.set( this ); |
136 | |
|
137 | 84 | } |
138 | |
|
139 | |
public void detachFromCurrentThread() |
140 | |
{ |
141 | 84 | TEST_METHOD.remove(); |
142 | 84 | ConsoleOutputReceiverForCurrentThread.remove(); |
143 | 84 | } |
144 | |
|
145 | |
public static TestMethod getThreadTestMethod() |
146 | |
{ |
147 | 94 | return TEST_METHOD.get(); |
148 | |
} |
149 | |
|
150 | |
public LogicalStream getLogicalStream() |
151 | |
{ |
152 | 0 | if ( output == null ) |
153 | |
{ |
154 | 0 | output = new LogicalStream(); |
155 | |
} |
156 | 0 | 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 | 68 | return testSet; |
167 | |
} |
168 | |
} |