1 | |
package org.apache.maven.surefire.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.ResourceBundle; |
23 | |
import java.util.regex.Matcher; |
24 | |
import java.util.regex.Pattern; |
25 | |
|
26 | |
import org.apache.maven.surefire.Surefire; |
27 | |
import org.apache.maven.surefire.report.ReportEntry; |
28 | |
import org.apache.maven.surefire.report.ReporterManager; |
29 | |
import org.junit.runner.Description; |
30 | |
import org.junit.runner.Result; |
31 | |
import org.junit.runner.notification.Failure; |
32 | |
import org.junit.runner.notification.RunListener; |
33 | |
|
34 | |
public class JUnit4TestSetReporter |
35 | |
extends RunListener |
36 | |
{ |
37 | |
|
38 | 0 | private static ResourceBundle bundle = ResourceBundle.getBundle( Surefire.SUREFIRE_BUNDLE_NAME ); |
39 | |
|
40 | |
|
41 | |
private JUnit4TestSet testSet; |
42 | |
|
43 | |
private ReporterManager reportMgr; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private boolean failureFlag; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
JUnit4TestSetReporter( JUnit4TestSet testSet, ReporterManager reportManager ) |
58 | 0 | { |
59 | 0 | this.testSet = testSet; |
60 | 0 | this.reportMgr = reportManager; |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public void testRunStarted( Description description ) |
69 | |
throws Exception |
70 | |
{ |
71 | 0 | String rawString = bundle.getString( "testSetStarting" ); |
72 | 0 | ReportEntry report = new ReportEntry( testSet.getName(), testSet.getName(), rawString ); |
73 | |
|
74 | 0 | this.reportMgr.testSetStarting( report ); |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void testRunFinished( Result result ) |
83 | |
throws Exception |
84 | |
{ |
85 | 0 | String rawString = bundle.getString( "testSetCompletedNormally" ); |
86 | 0 | ReportEntry report = new ReportEntry( testSet.getName(), testSet.getName(), rawString ); |
87 | |
|
88 | 0 | this.reportMgr.testSetCompleted( report ); |
89 | 0 | this.reportMgr.reset(); |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public void testIgnored( Description description ) |
98 | |
throws Exception |
99 | |
{ |
100 | 0 | String rawString = bundle.getString( "testSkipped" ); |
101 | 0 | ReportEntry report = new ReportEntry( extractClassName( description ), description.getDisplayName(), rawString ); |
102 | |
|
103 | 0 | this.reportMgr.testSkipped( report ); |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void testStarted( Description description ) |
112 | |
throws Exception |
113 | |
{ |
114 | 0 | String rawString = bundle.getString( "testStarting" ); |
115 | 0 | ReportEntry report = new ReportEntry( extractClassName( description ), description.getDisplayName(), rawString ); |
116 | |
|
117 | 0 | this.reportMgr.testStarting( report ); |
118 | |
|
119 | 0 | this.failureFlag = false; |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public void testFailure( Failure failure ) |
128 | |
throws Exception |
129 | |
{ |
130 | 0 | String rawString = bundle.getString( "executeException" ); |
131 | 0 | ReportEntry report = |
132 | |
new ReportEntry( extractClassName( failure.getDescription() ), failure.getTestHeader(), rawString, new JUnit4StackTraceWriter( failure ) ); |
133 | |
|
134 | 0 | if ( failure.getException() instanceof AssertionError ) |
135 | |
{ |
136 | 0 | this.reportMgr.testFailed( report ); |
137 | |
} |
138 | |
else |
139 | |
{ |
140 | 0 | this.reportMgr.testError( report ); |
141 | |
} |
142 | |
|
143 | 0 | failureFlag = true; |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public void testFinished( Description description ) |
152 | |
throws Exception |
153 | |
{ |
154 | 0 | if ( failureFlag == false ) |
155 | |
{ |
156 | 0 | String rawString = bundle.getString( "testSuccessful" ); |
157 | 0 | ReportEntry report = new ReportEntry( extractClassName( description ), description.getDisplayName(), rawString ); |
158 | |
|
159 | 0 | this.reportMgr.testSucceeded( report ); |
160 | |
} |
161 | 0 | } |
162 | |
|
163 | |
private String extractClassName( Description description ) |
164 | |
{ |
165 | 0 | String displayName = description.getDisplayName(); |
166 | 0 | final Pattern PARENS = Pattern.compile( |
167 | |
"^" + |
168 | |
"[^\\(\\)]+" + |
169 | |
"\\((" + |
170 | |
"[^\\\\(\\\\)]+" + |
171 | |
")\\)" + |
172 | |
"$" ); |
173 | 0 | Matcher m = PARENS.matcher( displayName ); |
174 | 0 | if (!m.find()) return displayName; |
175 | 0 | return m.group( 1 ); |
176 | |
} |
177 | |
} |