1 | |
package org.apache.maven.surefire.testng; |
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 org.apache.maven.surefire.report.CategorizedReportEntry; |
24 | |
import org.apache.maven.surefire.report.PojoStackTraceWriter; |
25 | |
import org.apache.maven.surefire.report.ReportEntry; |
26 | |
import org.apache.maven.surefire.report.RunListener; |
27 | |
import org.apache.maven.surefire.report.SimpleReportEntry; |
28 | |
|
29 | |
import org.testng.ISuite; |
30 | |
import org.testng.ISuiteListener; |
31 | |
import org.testng.ITestContext; |
32 | |
import org.testng.ITestListener; |
33 | |
import org.testng.ITestResult; |
34 | |
import org.testng.TestNG; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class TestNGReporter |
45 | |
implements ITestListener, ISuiteListener |
46 | |
{ |
47 | |
public static final String SUREFIRE_BUNDLE_NAME = "org.apache.maven.surefire.surefire"; |
48 | |
|
49 | 0 | private final ResourceBundle bundle = ResourceBundle.getBundle( SUREFIRE_BUNDLE_NAME ); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private final RunListener reporter; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public TestNGReporter( RunListener reportManager ) |
67 | 0 | { |
68 | 0 | this.reporter = reportManager; |
69 | |
|
70 | 0 | if ( reportManager == null ) |
71 | |
{ |
72 | 0 | throw new IllegalArgumentException( "ReportManager passed in was null." ); |
73 | |
} |
74 | |
|
75 | 0 | } |
76 | |
|
77 | |
public void onTestStart( ITestResult result ) |
78 | |
{ |
79 | 0 | String rawString = bundle.getString( "testStarting" ); |
80 | 0 | String group = groupString( result.getMethod().getGroups(), result.getTestClass().getName() ); |
81 | 0 | ReportEntry report = |
82 | |
new CategorizedReportEntry( getSource( result ), getUserFriendlyTestName( result ), group ); |
83 | 0 | reporter.testStarting( report ); |
84 | 0 | } |
85 | |
|
86 | |
private String getSource( ITestResult result ) |
87 | |
{ |
88 | 0 | return result.getTestClass().getName(); |
89 | |
} |
90 | |
|
91 | |
public void onTestSuccess( ITestResult result ) |
92 | |
{ |
93 | 0 | ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) ); |
94 | 0 | reporter.testSucceeded( report ); |
95 | 0 | } |
96 | |
|
97 | |
public void onTestFailure( ITestResult result ) |
98 | |
{ |
99 | 0 | ReportEntry report = SimpleReportEntry.withException( getSource( result ), getUserFriendlyTestName( result ), |
100 | |
new PojoStackTraceWriter( |
101 | |
result.getTestClass().getRealClass().getName(), |
102 | |
result.getMethod().getMethodName(), |
103 | |
result.getThrowable() ) ); |
104 | |
|
105 | 0 | reporter.testFailed( report ); |
106 | 0 | } |
107 | |
|
108 | |
private static String getUserFriendlyTestName( ITestResult result ) |
109 | |
{ |
110 | |
|
111 | 0 | return result.getName() + "(" + result.getTestClass().getName() + ")"; |
112 | |
} |
113 | |
|
114 | |
public void onTestSkipped( ITestResult result ) |
115 | |
{ |
116 | 0 | ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) ); |
117 | |
|
118 | 0 | reporter.testSkipped( report ); |
119 | 0 | } |
120 | |
|
121 | |
public void onTestFailedButWithinSuccessPercentage( ITestResult result ) |
122 | |
{ |
123 | 0 | ReportEntry report = SimpleReportEntry.withException( getSource( result ), getUserFriendlyTestName( result ), |
124 | |
new PojoStackTraceWriter( |
125 | |
result.getTestClass().getRealClass().getName(), |
126 | |
result.getMethod().getMethodName(), |
127 | |
result.getThrowable() ) ); |
128 | |
|
129 | 0 | reporter.testError( report ); |
130 | 0 | } |
131 | |
|
132 | |
public void onStart( ITestContext context ) |
133 | |
{ |
134 | |
|
135 | 0 | } |
136 | |
|
137 | |
public void onFinish( ITestContext context ) |
138 | |
{ |
139 | |
|
140 | 0 | } |
141 | |
|
142 | |
|
143 | |
public void onStart( ISuite suite ) |
144 | |
{ |
145 | |
|
146 | 0 | } |
147 | |
|
148 | |
public void onFinish( ISuite suite ) |
149 | |
{ |
150 | |
|
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
private static String groupString( String[] groups, String defaultValue ) |
162 | |
{ |
163 | |
String retVal; |
164 | 0 | if ( groups != null && groups.length > 0 ) |
165 | |
{ |
166 | 0 | StringBuffer str = new StringBuffer(); |
167 | 0 | for ( int i = 0; i < groups.length; i++ ) |
168 | |
{ |
169 | 0 | str.append( groups[i] ); |
170 | 0 | if ( i + 1 < groups.length ) |
171 | |
{ |
172 | 0 | str.append( "," ); |
173 | |
} |
174 | |
} |
175 | 0 | retVal = str.toString(); |
176 | 0 | } |
177 | |
else |
178 | |
{ |
179 | 0 | retVal = defaultValue; |
180 | |
} |
181 | 0 | return retVal; |
182 | |
} |
183 | |
|
184 | |
public void onConfigurationFailure( ITestResult result ) |
185 | |
{ |
186 | 0 | onTestFailure( result ); |
187 | 0 | } |
188 | |
|
189 | |
public void onConfigurationSkip( ITestResult result ) |
190 | |
{ |
191 | 0 | onTestSkipped( result ); |
192 | 0 | } |
193 | |
|
194 | |
public void onConfigurationSuccess( ITestResult result ) |
195 | |
{ |
196 | |
|
197 | |
|
198 | 0 | } |
199 | |
|
200 | |
} |