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 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   * Listens for and provides and adaptor layer so that
39   * TestNG tests can report their status to the current
40   * {@link org.apache.maven.surefire.report.RunListener}.
41   *
42   * @author jkuhnert
43   * @noinspection ThrowableResultOfMethodCallIgnored
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       * core Surefire reporting
54       */
55      private final RunListener reporter;
56  
57      /**
58       * Constructs a new instance that will listen to
59       * test updates from a {@link TestNG} class instance.
60       * <p/>
61       * <p/>It is assumed that the requisite {@link TestNG#addListener(ITestListener)}
62       * method call has already associated with this instance <i>before</i> the test
63       * suite is run.
64       *
65       * @param reportManager Instance to report suite status to
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         // This is consistent with the JUnit output
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      * Creates a string out of the list of testng groups in the
154      * form of <pre>"group1,group2,group3"</pre>.
155      *
156      * @param groups       The groups being run
157      * @param defaultValue The default to use if no groups
158      * @return a string describing the groups
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         // DGF Don't record configuration successes as separate tests
196         //onTestSuccess( result );
197     }
198 
199 }