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