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