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