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.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.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 static org.apache.maven.surefire.report.SimpleReportEntry.ignored;
36  import static org.apache.maven.surefire.report.SimpleReportEntry.withException;
37  
38  /**
39   * Listens for and provides and adaptor layer so that
40   * TestNG tests can report their status to the current
41   * {@link org.apache.maven.surefire.report.RunListener}.
42   *
43   * @author jkuhnert
44   */
45  public class TestNGReporter
46      implements ITestListener, ISuiteListener
47  {
48      private final RunListener reporter;
49  
50      /**
51       * Constructs a new instance that will listen to
52       * test updates from a {@link org.testng.TestNG} class instance.
53       * <br>
54       * <br>It is assumed that the requisite {@link org.testng.TestNG#addListener(ITestListener)}
55       * method call has already associated with this instance <i>before</i> the test
56       * suite is run.
57       *
58       * @param reportManager Instance to report suite status to
59       */
60      public TestNGReporter( RunListener reportManager )
61      {
62          this.reporter = reportManager;
63      }
64  
65      @Override
66      public void onTestStart( ITestResult result )
67      {
68          String clazz = result.getTestClass().getName();
69          String group = groupString( result.getMethod().getGroups(), clazz );
70          reporter.testStarting( new CategorizedReportEntry( clazz, result.getName(), group ) );
71      }
72  
73      @Override
74      public void onTestSuccess( ITestResult result )
75      {
76          ReportEntry report = new SimpleReportEntry( result.getTestClass().getName(), null, result.getName(), null );
77          reporter.testSucceeded( report );
78      }
79  
80      @Override
81      public void onTestFailure( ITestResult result )
82      {
83          IClass clazz = result.getTestClass();
84          ReportEntry report = withException( clazz.getName(), null, result.getName(), null,
85                  new PojoStackTraceWriter( clazz.getRealClass().getName(),
86                          result.getMethod().getMethodName(),
87                          result.getThrowable() ) );
88  
89          reporter.testFailed( report );
90      }
91  
92      @Override
93      public void onTestSkipped( ITestResult result )
94      {
95          //noinspection ThrowableResultOfMethodCallIgnored
96          Throwable t = result.getThrowable();
97          String reason = t == null ? null : t.getMessage();
98          ReportEntry report = ignored( result.getTestClass().getName(), null, result.getName(), null, reason );
99          reporter.testSkipped( report );
100     }
101 
102     @Override
103     public void onTestFailedButWithinSuccessPercentage( ITestResult result )
104     {
105         IClass clazz = result.getTestClass();
106         ReportEntry report = withException( clazz.getName(), null, result.getName(), null,
107                 new PojoStackTraceWriter( clazz.getRealClass().getName(),
108                         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 }