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