Coverage Report - org.apache.maven.surefire.testng.TestNGReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
TestNGReporter
0%
0/44
0%
0/10
1,375
 
 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  0
     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  0
     {
 68  0
         this.reporter = reportManager;
 69  
 
 70  0
         if ( reportManager == null )
 71  
         {
 72  0
             throw new IllegalArgumentException( "ReportManager passed in was null." );
 73  
         }
 74  
 
 75  0
     }
 76  
 
 77  
     public void onTestStart( ITestResult result )
 78  
     {
 79  0
         String rawString = bundle.getString( "testStarting" );
 80  0
         String group = groupString( result.getMethod().getGroups(), result.getTestClass().getName() );
 81  0
         ReportEntry report =
 82  
             new CategorizedReportEntry( getSource( result ), getUserFriendlyTestName( result ), group );
 83  0
         reporter.testStarting( report );
 84  0
     }
 85  
 
 86  
     private String getSource( ITestResult result )
 87  
     {
 88  0
         return result.getTestClass().getName();
 89  
     }
 90  
 
 91  
     public void onTestSuccess( ITestResult result )
 92  
     {
 93  0
         ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) );
 94  0
         reporter.testSucceeded( report );
 95  0
     }
 96  
 
 97  
     public void onTestFailure( ITestResult result )
 98  
     {
 99  0
         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  0
         reporter.testFailed( report );
 106  0
     }
 107  
 
 108  
     private static String getUserFriendlyTestName( ITestResult result )
 109  
     {
 110  
         // This is consistent with the JUnit output
 111  0
         return result.getName() + "(" + result.getTestClass().getName() + ")";
 112  
     }
 113  
 
 114  
     public void onTestSkipped( ITestResult result )
 115  
     {
 116  0
         ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) );
 117  
 
 118  0
         reporter.testSkipped( report );
 119  0
     }
 120  
 
 121  
     public void onTestFailedButWithinSuccessPercentage( ITestResult result )
 122  
     {
 123  0
         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  0
         reporter.testError( report );
 130  0
     }
 131  
 
 132  
     public void onStart( ITestContext context )
 133  
     {
 134  
 
 135  0
     }
 136  
 
 137  
     public void onFinish( ITestContext context )
 138  
     {
 139  
 
 140  0
     }
 141  
 
 142  
 
 143  
     public void onStart( ISuite suite )
 144  
     {
 145  
 
 146  0
     }
 147  
 
 148  
     public void onFinish( ISuite suite )
 149  
     {
 150  
 
 151  0
     }
 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  0
         if ( groups != null && groups.length > 0 )
 165  
         {
 166  0
             StringBuilder str = new StringBuilder();
 167  0
             for ( int i = 0; i < groups.length; i++ )
 168  
             {
 169  0
                 str.append( groups[i] );
 170  0
                 if ( i + 1 < groups.length )
 171  
                 {
 172  0
                     str.append( "," );
 173  
                 }
 174  
             }
 175  0
             retVal = str.toString();
 176  0
         }
 177  
         else
 178  
         {
 179  0
             retVal = defaultValue;
 180  
         }
 181  0
         return retVal;
 182  
     }
 183  
 
 184  
     public void onConfigurationFailure( ITestResult result )
 185  
     {
 186  0
         onTestFailure( result );
 187  0
     }
 188  
 
 189  
     public void onConfigurationSkip( ITestResult result )
 190  
     {
 191  0
         onTestSkipped( result );
 192  0
     }
 193  
 
 194  
     public void onConfigurationSuccess( ITestResult result )
 195  
     {
 196  
         // DGF Don't record configuration successes as separate tests
 197  
         //onTestSuccess( result );
 198  0
     }
 199  
 
 200  
 }