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 java.util.ResourceBundle;
23  import org.apache.maven.surefire.report.CategorizedReportEntry;
24  import org.apache.maven.surefire.report.PojoStackTraceWriter;
25  import org.apache.maven.surefire.report.ReportEntry;
26  import org.apache.maven.surefire.report.RunListener;
27  import org.apache.maven.surefire.report.SimpleReportEntry;
28  
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  import org.testng.TestNG;
35  
36  /**
37   * Listens for and provides and adaptor layer so that
38   * TestNG tests can report their status to the current
39   * {@link org.apache.maven.surefire.report.RunListener}.
40   *
41   * @author jkuhnert
42   * @noinspection ThrowableResultOfMethodCallIgnored
43   */
44  public class TestNGReporter
45      implements ITestListener, ISuiteListener
46  {
47      public static final String SUREFIRE_BUNDLE_NAME = "org.apache.maven.surefire.surefire";
48  
49      private final ResourceBundle bundle = ResourceBundle.getBundle( SUREFIRE_BUNDLE_NAME );
50  
51      /**
52       * core Surefire reporting
53       */
54      private final RunListener reporter;
55  
56      /**
57       * Constructs a new instance that will listen to
58       * test updates from a {@link TestNG} class instance.
59       * <p/>
60       * <p/>It is assumed that the requisite {@link TestNG#addListener(ITestListener)}
61       * method call has already associated with this instance <i>before</i> the test
62       * suite is run.
63       *
64       * @param reportManager Instance to report suite status to
65       */
66      public TestNGReporter( RunListener reportManager )
67      {
68          this.reporter = reportManager;
69  
70          if ( reportManager == null )
71          {
72              throw new IllegalArgumentException( "ReportManager passed in was null." );
73          }
74  
75      }
76  
77      public void onTestStart( ITestResult result )
78      {
79          String rawString = bundle.getString( "testStarting" );
80          String group = groupString( result.getMethod().getGroups(), result.getTestClass().getName() );
81          ReportEntry report =
82              new CategorizedReportEntry( getSource( result ), getUserFriendlyTestName( result ), group );
83          reporter.testStarting( report );
84      }
85  
86      private String getSource( ITestResult result )
87      {
88          return result.getTestClass().getName();
89      }
90  
91      public void onTestSuccess( ITestResult result )
92      {
93          ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) );
94          reporter.testSucceeded( report );
95      }
96  
97      public void onTestFailure( ITestResult result )
98      {
99          ReportEntry report = SimpleReportEntry.withException( getSource( result ), getUserFriendlyTestName( result ),
100                                                               new PojoStackTraceWriter(
101                                                                   result.getTestClass().getRealClass().getName(),
102                                                                   result.getMethod().getMethodName(),
103                                                                   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 = SimpleReportEntry.withException( getSource( result ), getUserFriendlyTestName( result ),
124                                                               new PojoStackTraceWriter(
125                                                                   result.getTestClass().getRealClass().getName(),
126                                                                   result.getMethod().getMethodName(),
127                                                                   result.getThrowable() ) );
128 
129         reporter.testError( report );
130     }
131 
132     public void onStart( ITestContext context )
133     {
134 
135     }
136 
137     public void onFinish( ITestContext context )
138     {
139 
140     }
141 
142 
143     public void onStart( ISuite suite )
144     {
145 
146     }
147 
148     public void onFinish( ISuite suite )
149     {
150 
151     }
152 
153     /**
154      * Creates a string out of the list of testng groups in the
155      * form of <pre>"group1,group2,group3"</pre>.
156      *
157      * @param groups       The groups being run
158      * @param defaultValue The default to use if no groups
159      * @return a string describing the groups
160      */
161     private static String groupString( String[] groups, String defaultValue )
162     {
163         String retVal;
164         if ( groups != null && groups.length > 0 )
165         {
166             StringBuilder str = new StringBuilder();
167             for ( int i = 0; i < groups.length; i++ )
168             {
169                 str.append( groups[i] );
170                 if ( i + 1 < groups.length )
171                 {
172                     str.append( "," );
173                 }
174             }
175             retVal = str.toString();
176         }
177         else
178         {
179             retVal = defaultValue;
180         }
181         return retVal;
182     }
183 
184     public void onConfigurationFailure( ITestResult result )
185     {
186         onTestFailure( result );
187     }
188 
189     public void onConfigurationSkip( ITestResult result )
190     {
191         onTestSkipped( result );
192     }
193 
194     public void onConfigurationSuccess( ITestResult result )
195     {
196         // DGF Don't record configuration successes as separate tests
197         //onTestSuccess( result );
198     }
199 
200 }