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 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 {
68 this.reporter = reportManager;
69
70 if ( reportManager == null )
71 {
72 throw new IllegalArgumentException( "ReportManager passed in was null." );
73 }
74
75 }
76
77 public void onTestStart( ITestResult result )
78 {
79 String rawString = bundle.getString( "testStarting" );
80 String group = groupString( result.getMethod().getGroups(), result.getTestClass().getName() );
81 ReportEntry report =
82 new CategorizedReportEntry( getSource( result ), getUserFriendlyTestName( result ), group );
83 reporter.testStarting( report );
84 }
85
86 private String getSource( ITestResult result )
87 {
88 return result.getTestClass().getName();
89 }
90
91 public void onTestSuccess( ITestResult result )
92 {
93 ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) );
94 reporter.testSucceeded( report );
95 }
96
97 public void onTestFailure( ITestResult result )
98 {
99 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 reporter.testFailed( report );
106 }
107
108 private static String getUserFriendlyTestName( ITestResult result )
109 {
110
111 return result.getName() + "(" + result.getTestClass().getName() + ")";
112 }
113
114 public void onTestSkipped( ITestResult result )
115 {
116 ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) );
117
118 reporter.testSkipped( report );
119 }
120
121 public void onTestFailedButWithinSuccessPercentage( ITestResult result )
122 {
123 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 reporter.testError( report );
130 }
131
132 public void onStart( ITestContext context )
133 {
134
135 }
136
137 public void onFinish( ITestContext context )
138 {
139
140 }
141
142
143 public void onStart( ISuite suite )
144 {
145
146 }
147
148 public void onFinish( ISuite suite )
149 {
150
151 }
152
153
154
155
156
157
158
159
160
161 private static String groupString( String[] groups, String defaultValue )
162 {
163 String retVal;
164 if ( groups != null && groups.length > 0 )
165 {
166 StringBuilder str = new StringBuilder();
167 for ( int i = 0; i < groups.length; i++ )
168 {
169 str.append( groups[i] );
170 if ( i + 1 < groups.length )
171 {
172 str.append( "," );
173 }
174 }
175 retVal = str.toString();
176 }
177 else
178 {
179 retVal = defaultValue;
180 }
181 return retVal;
182 }
183
184 public void onConfigurationFailure( ITestResult result )
185 {
186 onTestFailure( result );
187 }
188
189 public void onConfigurationSkip( ITestResult result )
190 {
191 onTestSkipped( result );
192 }
193
194 public void onConfigurationSuccess( ITestResult result )
195 {
196
197
198 }
199
200 }