Coverage Report - org.apache.maven.surefire.junit.JUnitTestSet
 
Classes in this File Line Coverage Branch Coverage Complexity
JUnitTestSet
59%
19/32
50%
2/4
4,25
 
 1  
 package org.apache.maven.surefire.junit;
 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.lang.reflect.InvocationTargetException;
 23  
 import java.lang.reflect.Method;
 24  
 import java.lang.reflect.Proxy;
 25  
 import org.apache.maven.surefire.common.junit3.JUnit3Reflector;
 26  
 import org.apache.maven.surefire.report.RunListener;
 27  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 28  
 
 29  
 public final class JUnitTestSet
 30  
     implements SurefireTestSet
 31  
 {
 32  
     private final Class testClass;
 33  
 
 34  
     private final JUnit3Reflector reflector;
 35  
 
 36  
     public JUnitTestSet( Class testClass, JUnit3Reflector reflector )
 37  
         throws TestSetFailedException
 38  1
     {
 39  1
         if ( testClass == null )
 40  
         {
 41  0
             throw new NullPointerException( "testClass is null" );
 42  
         }
 43  
 
 44  1
         this.testClass = testClass;
 45  1
         this.reflector = reflector;
 46  
 
 47  
         // ----------------------------------------------------------------------
 48  
         // Strategy for executing JUnit tests
 49  
         //
 50  
         // o look for the suite method and if that is present execute that method
 51  
         //   to get the test object.
 52  
         //
 53  
         // o look for test classes that are assignable from TestCase
 54  
         //
 55  
         // o look for test classes that only implement the Test interface
 56  
         // ----------------------------------------------------------------------
 57  
 
 58  
         // The interface implemented by the dynamic proxy (TestListener), happens to be
 59  
         // the same as the param types of TestResult.addTestListener
 60  1
     }
 61  
 
 62  
 
 63  
     public void execute( RunListener reporter, ClassLoader loader )
 64  
         throws TestSetFailedException
 65  
     {
 66  1
         Class testClass = getTestClass();
 67  
 
 68  
         try
 69  
         {
 70  1
             Object testObject = reflector.constructTestObject( testClass );
 71  
             final Method runMethod;
 72  
 
 73  1
             if ( this.reflector.getTestInterface().isAssignableFrom( testObject.getClass() ) )
 74  
             {
 75  1
                 runMethod = this.reflector.getTestInterfaceRunMethod();
 76  
             }
 77  
             else
 78  
             {
 79  0
                 runMethod = reflector.getRunMethod( this.testClass );
 80  
             }
 81  
 
 82  1
             Object instanceOfTestResult = reflector.getTestResultClass().newInstance();
 83  
 
 84  1
             TestListenerInvocationHandler invocationHandler = new TestListenerInvocationHandler( reporter );
 85  
 
 86  1
             Object testListener =
 87  
                 Proxy.newProxyInstance( loader, reflector.getInterfacesImplementedByDynamicProxy(), invocationHandler );
 88  
 
 89  1
             Object[] addTestListenerParams = { testListener };
 90  
 
 91  1
             reflector.getAddListenerMethod().invoke( instanceOfTestResult, addTestListenerParams );
 92  
 
 93  1
             Object[] runParams = { instanceOfTestResult };
 94  
 
 95  1
             runMethod.invoke( testObject, runParams );
 96  
         }
 97  0
         catch ( IllegalArgumentException e )
 98  
         {
 99  0
             throw new TestSetFailedException( testClass.getName(), e );
 100  
         }
 101  0
         catch ( InstantiationException e )
 102  
         {
 103  0
             throw new TestSetFailedException( testClass.getName(), e );
 104  
         }
 105  0
         catch ( IllegalAccessException e )
 106  
         {
 107  0
             throw new TestSetFailedException( testClass.getName(), e );
 108  
         }
 109  0
         catch ( InvocationTargetException e )
 110  
         {
 111  0
             throw new TestSetFailedException( testClass.getName(), e.getTargetException() );
 112  
         }
 113  0
         catch ( NoSuchMethodException e )
 114  
         {
 115  0
             throw new TestSetFailedException( "Class is not a JUnit TestCase", e );
 116  1
         }
 117  1
     }
 118  
 
 119  
     public String getName()
 120  
     {
 121  0
         return testClass.getName();
 122  
     }
 123  
 
 124  
     Class getTestClass()
 125  
     {
 126  1
         return testClass;
 127  
     }
 128  
 }