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