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