Coverage Report - org.apache.maven.surefire.testset.PojoTestSet
 
Classes in this File Line Coverage Branch Coverage Complexity
PojoTestSet
0%
0/89
0%
0/44
4.111
 
 1  
 package org.apache.maven.surefire.testset;
 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.Surefire;
 23  
 import org.apache.maven.surefire.report.PojoStackTraceWriter;
 24  
 import org.apache.maven.surefire.report.ReportEntry;
 25  
 import org.apache.maven.surefire.report.ReporterManager;
 26  
 
 27  
 import java.lang.reflect.InvocationTargetException;
 28  
 import java.lang.reflect.Method;
 29  
 import java.lang.reflect.Modifier;
 30  
 import java.text.MessageFormat;
 31  
 import java.util.ArrayList;
 32  
 import java.util.List;
 33  
 import java.util.ResourceBundle;
 34  
 
 35  
 public class PojoTestSet
 36  
     extends AbstractTestSet
 37  
 {
 38  0
     private ResourceBundle bundle = ResourceBundle.getBundle( Surefire.SUREFIRE_BUNDLE_NAME );
 39  
 
 40  
     private static final String TEST_METHOD_PREFIX = "test";
 41  
 
 42  0
     private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
 43  
 
 44  
     private Object testObject;
 45  
 
 46  
     protected List testMethods;
 47  
 
 48  
     public PojoTestSet( Class testClass )
 49  
         throws TestSetFailedException
 50  
     {
 51  0
         super( testClass );
 52  
 
 53  
         try
 54  
         {
 55  0
             testObject = testClass.newInstance();
 56  
         }
 57  0
         catch ( InstantiationException e )
 58  
         {
 59  0
             throw new TestSetFailedException( "Unable to instantiate POJO '" + testClass + "'", e );
 60  
         }
 61  0
         catch ( IllegalAccessException e )
 62  
         {
 63  0
             throw new TestSetFailedException( "Unable to instantiate POJO '" + testClass + "'", e );
 64  0
         }
 65  0
     }
 66  
 
 67  
     public void execute( ReporterManager reportManager, ClassLoader loader )
 68  
         throws TestSetFailedException
 69  
     {
 70  0
         if ( reportManager == null )
 71  
         {
 72  0
             throw new NullPointerException( "reportManager is null" );
 73  
         }
 74  
 
 75  0
         executeTestMethods( reportManager );
 76  0
     }
 77  
 
 78  
     protected void executeTestMethods( ReporterManager reportManager )
 79  
     {
 80  0
         if ( reportManager == null )
 81  
         {
 82  0
             throw new NullPointerException( "reportManager is null" );
 83  
         }
 84  
 
 85  0
         if ( testMethods == null )
 86  
         {
 87  0
             discoverTestMethods();
 88  
         }
 89  
 
 90  0
         boolean abort = false;
 91  
 
 92  0
         for ( int i = 0; i < testMethods.size() && !abort; ++i )
 93  
         {
 94  0
             abort = executeTestMethod( (Method) testMethods.get( i ), EMPTY_OBJECT_ARRAY, reportManager );
 95  
         }
 96  0
     }
 97  
 
 98  
     /**
 99  
      * @noinspection CatchGenericClass,OverlyBroadCatchBlock,MethodWithMultipleReturnPoints
 100  
      */
 101  
     protected boolean executeTestMethod( Method method, Object[] args, ReporterManager reportManager )
 102  
     {
 103  0
         if ( method == null || args == null || reportManager == null )
 104  
         {
 105  0
             throw new NullPointerException();
 106  
         }
 107  
 
 108  0
         String userFriendlyMethodName = method.getName() + '(';
 109  
 
 110  0
         if ( args.length != 0 )
 111  
         {
 112  0
             userFriendlyMethodName += "Reporter";
 113  
         }
 114  
 
 115  0
         userFriendlyMethodName += ')';
 116  
 
 117  0
         ReportEntry report = new ReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ),
 118  
                                               getName() );
 119  
 
 120  0
         reportManager.testStarting( report );
 121  
 
 122  
         try
 123  
         {
 124  0
             setUpFixture();
 125  
         }
 126  0
         catch ( Exception e )
 127  
         {
 128  
             // Treat any exception from setUpFixture as a failure of the test.
 129  0
             String rawString = bundle.getString( "setupFixtureFailed" );
 130  
 
 131  0
             MessageFormat msgFmt = new MessageFormat( rawString );
 132  
 
 133  0
             Object[] stringArgs = {method.getName()};
 134  
 
 135  0
             String stringToPrint = msgFmt.format( stringArgs );
 136  
 
 137  0
             report = new ReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ),
 138  
                                       stringToPrint, new PojoStackTraceWriter( testObject.getClass().getName(),
 139  
                                                                                method.getName(), e ) );
 140  
 
 141  0
             reportManager.testFailed( report );
 142  
 
 143  
             // A return value of true indicates to this class's executeTestMethods
 144  
             // method that it should abort and not attempt to execute
 145  
             // any other test methods. The other caller of this method,
 146  
             // TestRerunner.rerun, ignores this return value, because it is
 147  
             // only running one test.
 148  0
             return true;
 149  0
         }
 150  
 
 151  
         // Make sure that tearDownFixture
 152  
         try
 153  
         {
 154  0
             method.invoke( testObject, args );
 155  
 
 156  0
             report = new ReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ),
 157  
                                       getName() );
 158  
 
 159  0
             reportManager.testSucceeded( report );
 160  
         }
 161  0
         catch ( InvocationTargetException ite )
 162  
         {
 163  0
             Throwable t = ite.getTargetException();
 164  
 
 165  0
             String msg = t.getMessage();
 166  
 
 167  0
             if ( msg == null )
 168  
             {
 169  0
                 msg = t.toString();
 170  
             }
 171  
 
 172  0
             report = new ReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ), msg,
 173  
                 new PojoStackTraceWriter( testObject.getClass().getName(), method.getName(), t ) );
 174  
 
 175  0
             reportManager.testFailed( report );
 176  
             // Don't return  here, because tearDownFixture should be called even
 177  
             // if the test method throws an exception.
 178  
         }
 179  0
         catch ( Throwable t )
 180  
         {
 181  0
             String msg = t.getMessage();
 182  
 
 183  0
             if ( msg == null )
 184  
             {
 185  0
                 msg = t.toString();
 186  
             }
 187  
 
 188  0
             report = new ReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ), msg,
 189  
                 new PojoStackTraceWriter( testObject.getClass().getName(), method.getName(), t ) );
 190  
 
 191  0
             reportManager.testFailed( report );
 192  
             // Don't return  here, because tearDownFixture should be called even
 193  
             // if the test method throws an exception.
 194  0
         }
 195  
 
 196  
         try
 197  
         {
 198  0
             tearDownFixture();
 199  
         }
 200  0
         catch ( Throwable t )
 201  
         {
 202  
             // Treat any exception from tearDownFixture as a failure of the test.
 203  0
             String rawString = bundle.getString( "cleanupFixtureFailed" );
 204  
 
 205  0
             MessageFormat msgFmt = new MessageFormat( rawString );
 206  
 
 207  0
             Object[] stringArgs = {method.getName()};
 208  
 
 209  0
             String stringToPrint = msgFmt.format( stringArgs );
 210  
 
 211  0
             report = new ReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ),
 212  
                                       stringToPrint, new PojoStackTraceWriter( testObject.getClass().getName(),
 213  
                                                                                method.getName(), t ) );
 214  
 
 215  0
             reportManager.testFailed( report );
 216  
 
 217  
             // A return value of true indicates to this class's executeTestMethods
 218  
             // method that it should abort and not attempt to execute
 219  
             // any other test methods. The other caller of this method,
 220  
             // TestRerunner.rerun, ignores this return value, because it is
 221  
             // only running one test.
 222  0
             return true;
 223  0
         }
 224  
 
 225  
         // A return value of false indicates to this class's executeTestMethods
 226  
         // method that it should keep plowing ahead and invoke more test methods.
 227  
         // The other caller of this method,
 228  
         // TestRerunner.rerun, ignores this return value, because it is
 229  
         // only running one test.
 230  0
         return false;
 231  
     }
 232  
 
 233  
     public String getTestName( String testMethodName )
 234  
     {
 235  0
         if ( testMethodName == null )
 236  
         {
 237  0
             throw new NullPointerException( "testMethodName is null" );
 238  
         }
 239  
 
 240  0
         return getTestClass().getName() + "." + testMethodName;
 241  
     }
 242  
 
 243  
     public void setUpFixture()
 244  
     {
 245  0
     }
 246  
 
 247  
     public void tearDownFixture()
 248  
     {
 249  0
     }
 250  
 
 251  
     private void discoverTestMethods()
 252  
     {
 253  0
         if ( testMethods == null )
 254  
         {
 255  0
             testMethods = new ArrayList();
 256  
 
 257  0
             Method[] methods = getTestClass().getMethods();
 258  
 
 259  0
             for ( int i = 0; i < methods.length; ++i )
 260  
             {
 261  0
                 Method m = methods[i];
 262  
 
 263  0
                 if ( isValidTestMethod( m ) )
 264  
                 {
 265  0
                     String simpleName = m.getName();
 266  
 
 267  
                     // name must have 5 or more chars
 268  0
                     if ( simpleName.length() > 4 )
 269  
                     {
 270  0
                         String firstFour = simpleName.substring( 0, 4 );
 271  
 
 272  
                         // name must start with "test"
 273  0
                         if ( firstFour.equals( TEST_METHOD_PREFIX ) )
 274  
                         {
 275  0
                             testMethods.add( m );
 276  
                         }
 277  
                     }
 278  
                 }
 279  
             }
 280  
         }
 281  0
     }
 282  
 
 283  
     private static boolean isValidTestMethod( Method m )
 284  
     {
 285  0
         boolean isInstanceMethod = !Modifier.isStatic( m.getModifiers() );
 286  
 
 287  0
         boolean returnsVoid = m.getReturnType().equals( void.class );
 288  
 
 289  0
         boolean hasNoParams = m.getParameterTypes().length == 0;
 290  
 
 291  0
         return isInstanceMethod && returnsVoid && hasNoParams;
 292  
     }
 293  
 }