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.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   * Listens for and provides and adaptor layer so that
42   * TestNG tests can report their status to the current
43   * {@link RunListener}.
44   *
45   * @author jkuhnert
46   */
47  public class TestNGReporter
48      implements ITestListener, ISuiteListener
49  {
50      private final RunListener reporter;
51  
52      /**
53       * Constructs a new instance that will listen to
54       * test updates from a {@link org.testng.TestNG} class instance.
55       * <br>
56       * <br>It is assumed that the requisite {@link org.testng.TestNG#addListener(ITestListener)}
57       * method call has already associated with this instance <i>before</i> the test
58       * suite is run.
59       *
60       * @param reportManager Instance to report suite status to
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          //noinspection ThrowableResultOfMethodCallIgnored
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      * Creates a string out of the list of testng groups in the
141      * form of <pre>"group1,group2,group3"</pre>.
142      *
143      * @param groups       The groups being run
144      * @param defaultValue The default to use if no groups
145      * @return a string describing the groups
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         // DGF Don't record configuration successes as separate tests
183         //onTestSuccess( result );
184     }
185 
186     /**
187      * Acquire a better representation of the test name that includes parameters and the invocation count, if there are
188      * any parameters
189      *
190      * @param result the test result to extract from
191      * @return a descriptive name for the test
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 }