View Javadoc
1   package org.apache.maven.surefire.testng;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.api.report.CategorizedReportEntry;
23  import org.apache.maven.surefire.api.report.OutputReportEntry;
24  import org.apache.maven.surefire.api.report.RunMode;
25  import org.apache.maven.surefire.api.report.StackTraceWriter;
26  import org.apache.maven.surefire.api.report.TestOutputReceiver;
27  import org.apache.maven.surefire.api.report.TestOutputReportEntry;
28  import org.apache.maven.surefire.api.report.TestReportListener;
29  import org.apache.maven.surefire.report.ClassMethodIndexer;
30  import org.apache.maven.surefire.report.PojoStackTraceWriter;
31  import org.apache.maven.surefire.api.report.ReportEntry;
32  import org.apache.maven.surefire.api.report.RunListener;
33  import org.apache.maven.surefire.api.report.SimpleReportEntry;
34  
35  import org.apache.maven.surefire.report.RunModeSetter;
36  import org.testng.IClass;
37  import org.testng.ISuite;
38  import org.testng.ISuiteListener;
39  import org.testng.ITestContext;
40  import org.testng.ITestListener;
41  import org.testng.ITestResult;
42  
43  import java.util.Arrays;
44  
45  import static org.apache.maven.surefire.api.report.SimpleReportEntry.ignored;
46  import static org.apache.maven.surefire.api.report.SimpleReportEntry.withException;
47  
48  /**
49   * Listens for and provides and adaptor layer so that
50   * TestNG tests can report their status to the current
51   * {@link RunListener}.
52   *
53   * @author jkuhnert
54   */
55  public class TestNGReporter
56      implements TestOutputReceiver<OutputReportEntry>, ITestListener, ISuiteListener, RunModeSetter
57  {
58      private final ClassMethodIndexer classMethodIndexer = new ClassMethodIndexer();
59      private final TestReportListener<TestOutputReportEntry> reporter;
60      private volatile RunMode runMode;
61  
62      /**
63       * Constructs a new instance that will listen to
64       * test updates from a {@link org.testng.TestNG} class instance.
65       * <br>
66       * <br>It is assumed that the requisite {@link org.testng.TestNG#addListener(ITestListener)}
67       * method call has already associated with this instance <i>before</i> the test
68       * suite is run.
69       *
70       * @param reportManager Instance to report suite status to
71       */
72      public TestNGReporter( TestReportListener<TestOutputReportEntry> reportManager )
73      {
74          this.reporter = reportManager;
75      }
76  
77      protected final RunListener getRunListener()
78      {
79          return reporter;
80      }
81  
82      @Override
83      public void onTestStart( ITestResult result )
84      {
85          String className = result.getTestClass().getName();
86          String methodName = testName( result );
87          long testRunId = classMethodIndexer.indexClassMethod( className, methodName );
88          String group = groupString( result.getMethod().getGroups(), className );
89          reporter.testStarting( new CategorizedReportEntry( runMode, testRunId, className, methodName, group ) );
90      }
91  
92      @Override
93      public void onTestSuccess( ITestResult result )
94      {
95          String className = result.getTestClass().getName();
96          String methodName = testName( result );
97          long testRunId = classMethodIndexer.indexClassMethod( className, methodName );
98          ReportEntry report = new SimpleReportEntry( runMode, testRunId, className, null, methodName, null );
99          reporter.testSucceeded( report );
100     }
101 
102     @Override
103     public void onTestFailure( ITestResult result )
104     {
105         IClass clazz = result.getTestClass();
106         String className = clazz.getName();
107         String methodName = testName( result );
108         long testRunId = classMethodIndexer.indexClassMethod( className, methodName );
109         StackTraceWriter stackTraceWriter = new PojoStackTraceWriter( clazz.getRealClass().getName(),
110             result.getMethod().getMethodName(), result.getThrowable() );
111         ReportEntry report = withException( runMode, testRunId, clazz.getName(), null, methodName,
112             null, stackTraceWriter );
113 
114         reporter.testFailed( report );
115     }
116 
117     @Override
118     public void onTestSkipped( ITestResult result )
119     {
120         String className = result.getTestClass().getName();
121         String methodName = testName( result );
122         long testRunId = classMethodIndexer.indexClassMethod( className, methodName );
123         //noinspection ThrowableResultOfMethodCallIgnored
124         Throwable t = result.getThrowable();
125         String reason = t == null ? null : t.getMessage();
126         ReportEntry report = ignored( runMode, testRunId, className, null, methodName, null, reason );
127         reporter.testSkipped( report );
128     }
129 
130     @Override
131     public void onTestFailedButWithinSuccessPercentage( ITestResult result )
132     {
133         IClass clazz = result.getTestClass();
134         String className = clazz.getName();
135         String methodName = testName( result );
136         long testRunId = classMethodIndexer.indexClassMethod( className, methodName );
137         StackTraceWriter stackTraceWriter = new PojoStackTraceWriter( clazz.getRealClass().getName(),
138             result.getMethod().getMethodName(), result.getThrowable() );
139         ReportEntry report = withException( runMode, testRunId, className, null, methodName, null, stackTraceWriter );
140         reporter.testSucceeded( report );
141     }
142 
143     @Override
144     public void onStart( ITestContext context )
145     {
146 
147     }
148 
149     @Override
150     public void onFinish( ITestContext context )
151     {
152 
153     }
154 
155 
156     @Override
157     public void onStart( ISuite suite )
158     {
159 
160     }
161 
162     @Override
163     public void onFinish( ISuite suite )
164     {
165 
166     }
167 
168     /**
169      * Creates a string out of the list of testng groups in the
170      * form of <pre>"group1,group2,group3"</pre>.
171      *
172      * @param groups       The groups being run
173      * @param defaultValue The default to use if no groups
174      * @return a string describing the groups
175      */
176     private static String groupString( String[] groups, String defaultValue )
177     {
178         String retVal;
179         if ( groups != null && groups.length > 0 )
180         {
181             StringBuilder str = new StringBuilder();
182             for ( int i = 0; i < groups.length; i++ )
183             {
184                 str.append( groups[i] );
185                 if ( i + 1 < groups.length )
186                 {
187                     str.append( "," );
188                 }
189             }
190             retVal = str.toString();
191         }
192         else
193         {
194             retVal = defaultValue;
195         }
196         return retVal;
197     }
198 
199     public void onConfigurationFailure( ITestResult result )
200     {
201         onTestFailure( result );
202     }
203 
204     public void onConfigurationSkip( ITestResult result )
205     {
206         onTestSkipped( result );
207     }
208 
209     public void onConfigurationSuccess( ITestResult result )
210     {
211         // DGF Don't record configuration successes as separate tests
212         //onTestSuccess( result );
213     }
214 
215     /**
216      * Acquire a better representation of the test name that includes parameters and the invocation count, if there are
217      * any parameters
218      *
219      * @param result the test result to extract from
220      * @return a descriptive name for the test
221      */
222     private static String testName( ITestResult result )
223     {
224         Object[] parameters = result.getParameters();
225         String name = result.getName();
226         return parameters == null || parameters.length == 0
227             ? name : name + Arrays.toString( parameters ) + "(" + result.getMethod().getCurrentInvocationCount() + ")";
228     }
229 
230     @Override
231     public void setRunMode( RunMode runMode )
232     {
233         this.runMode = runMode;
234     }
235 
236     @Override
237     public void writeTestOutput( OutputReportEntry reportEntry )
238     {
239         Long testRunId = classMethodIndexer.getLocalIndex();
240         reporter.writeTestOutput( new TestOutputReportEntry( reportEntry, runMode, testRunId ) );
241     }
242 }