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 org.apache.maven.surefire.report.RunListener;
23 import org.apache.maven.surefire.report.ReportEntry;
24 import org.apache.maven.surefire.report.SimpleReportEntry;
25 import org.junit.runner.Description;
26 import org.junit.runner.notification.Failure;
27
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
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 boolean failureFlag;
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 = false;
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 = true;
104 }
105
106 public void testAssumptionFailure( Failure failure )
107 {
108 this.reporter.testAssumptionFailure( createReportEntry( failure.getDescription() ) );
109 failureFlag = true;
110 }
111
112
113
114
115
116
117
118 public void testFinished( Description description )
119 throws Exception
120 {
121 if ( !failureFlag )
122 {
123 reporter.testSucceeded( createReportEntry( description ) );
124 }
125 }
126
127 private SimpleReportEntry createReportEntry( Description description )
128 {
129 return new SimpleReportEntry( extractClassName( description ), description.getDisplayName() );
130 }
131
132
133 protected String extractClassName( Description description )
134 {
135 String displayName = description.getDisplayName();
136 Matcher m = PARENS.matcher( displayName );
137 if ( !m.find() )
138 {
139 return displayName;
140 }
141 return m.group( 1 );
142 }
143 }