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