1 package org.apache.maven.surefire.common.junit4;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import org.apache.maven.surefire.report.ReportEntry;
25 import org.apache.maven.surefire.report.RunListener;
26 import org.apache.maven.surefire.report.SimpleReportEntry;
27
28 import org.junit.runner.Description;
29 import org.junit.runner.notification.Failure;
30
31 public class JUnit4RunListener
32 extends org.junit.runner.notification.RunListener
33 {
34 private static final Pattern PARENS = Pattern.compile( "^" + "[^\\(\\)]+"
35 + "\\(("
36 + "[^\\\\(\\\\)]+"
37 + ")\\)" + "$" );
38
39 protected final RunListener reporter;
40
41
42
43
44
45 private final ThreadLocal<Boolean> failureFlag = new InheritableThreadLocal<Boolean>();
46
47
48
49
50
51
52 public JUnit4RunListener( RunListener reporter )
53 {
54 this.reporter = reporter;
55 }
56
57
58
59
60
61
62
63
64 public void testIgnored( Description description )
65 throws Exception
66 {
67 reporter.testSkipped( createReportEntry( description ) );
68 }
69
70
71
72
73
74
75 public void testStarted( Description description )
76 throws Exception
77 {
78 reporter.testStarting( createReportEntry( description ) );
79 failureFlag.remove();
80 }
81
82
83
84
85
86
87 @SuppressWarnings( { "ThrowableResultOfMethodCallIgnored" } )
88 public void testFailure( Failure failure )
89 throws Exception
90 {
91 ReportEntry report =
92 new SimpleReportEntry( extractClassName( failure.getDescription() ), failure.getTestHeader(),
93 new JUnit4StackTraceWriter( failure ) );
94
95 if ( failure.getException() instanceof AssertionError )
96 {
97 this.reporter.testFailed( report );
98 }
99 else
100 {
101 this.reporter.testError( report );
102 }
103 failureFlag.set( Boolean.TRUE );
104 }
105
106 @SuppressWarnings( { "UnusedDeclaration" } )
107 public void testAssumptionFailure( Failure failure )
108 {
109 this.reporter.testAssumptionFailure( createReportEntry( failure.getDescription() ) );
110 failureFlag.set( Boolean.TRUE );
111 }
112
113
114
115
116
117
118
119 public void testFinished( Description description )
120 throws Exception
121 {
122 Boolean failure = failureFlag.get();
123 if ( failure == null )
124 {
125 reporter.testSucceeded( createReportEntry( description ) );
126 }
127 }
128
129 private SimpleReportEntry createReportEntry( Description description )
130 {
131 return new SimpleReportEntry( extractClassName( description ), description.getDisplayName() );
132 }
133
134
135 String extractClassName( Description description )
136 {
137 String displayName = description.getDisplayName();
138 Matcher m = PARENS.matcher( displayName );
139 if ( !m.find() )
140 {
141 return displayName;
142 }
143 return m.group( 1 );
144 }
145 }