Coverage Report - org.apache.maven.surefire.common.junit4.JUnit4TestChecker
 
Classes in this File Line Coverage Branch Coverage Complexity
JUnit4TestChecker
70%
17/24
45%
9/20
3,6
 
 1  
 package org.apache.maven.surefire.common.junit4;
 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.annotation.Annotation;
 23  
 import java.lang.reflect.Method;
 24  
 import org.apache.maven.surefire.NonAbstractClassFilter;
 25  
 import org.apache.maven.surefire.common.junit3.JUnit3TestChecker;
 26  
 import org.apache.maven.surefire.util.ReflectionUtils;
 27  
 import org.apache.maven.surefire.util.ScannerFilter;
 28  
 
 29  
 /**
 30  
  * @author Kristian Rosenvold
 31  
  */
 32  
 public class JUnit4TestChecker
 33  
     implements ScannerFilter
 34  
 {
 35  
     private final NonAbstractClassFilter nonAbstractClassFilter;
 36  
 
 37  
     private final Class runWith;
 38  
 
 39  
     private final JUnit3TestChecker jUnit3TestChecker;
 40  
 
 41  
 
 42  
     public JUnit4TestChecker( ClassLoader testClassLoader )
 43  2
     {
 44  2
         this.jUnit3TestChecker = new JUnit3TestChecker( testClassLoader );
 45  2
         this.runWith = getJUnitClass( testClassLoader, org.junit.runner.RunWith.class.getName() );
 46  2
         this.nonAbstractClassFilter = new NonAbstractClassFilter();
 47  2
     }
 48  
 
 49  
     public boolean accept( Class testClass )
 50  
     {
 51  1
         return jUnit3TestChecker.accept( testClass ) || isValidJUnit4Test( testClass );
 52  
     }
 53  
 
 54  
     @SuppressWarnings( { "unchecked" } )
 55  
     private boolean isValidJUnit4Test( Class testClass )
 56  
     {
 57  1
         if ( !nonAbstractClassFilter.accept( testClass ) )
 58  
         {
 59  0
             return false;
 60  
         }
 61  
 
 62  1
         if ( runWith != null )
 63  
         {
 64  0
             Annotation runWithAnnotation = testClass.getAnnotation( runWith );
 65  0
             if ( runWithAnnotation != null )
 66  
             {
 67  0
                 return true;
 68  
             }
 69  
         }
 70  
 
 71  1
         Class classToCheck = testClass;
 72  1
         while ( classToCheck != null )
 73  
         {
 74  1
             if ( checkforTestAnnotatedMethod( classToCheck ) )
 75  
             {
 76  1
                 return true;
 77  
             }
 78  0
             classToCheck = classToCheck.getSuperclass();
 79  
         }
 80  0
         return false;
 81  
     }
 82  
 
 83  
     private boolean checkforTestAnnotatedMethod( Class testClass )
 84  
     {
 85  1
         for ( Method lMethod : testClass.getDeclaredMethods() )
 86  
         {
 87  1
             for ( Annotation lAnnotation : lMethod.getAnnotations() )
 88  
             {
 89  1
                 if ( org.junit.Test.class.isAssignableFrom( lAnnotation.annotationType() ) )
 90  
                 {
 91  1
                     return true;
 92  
                 }
 93  
             }
 94  
         }
 95  0
         return false;
 96  
     }
 97  
 
 98  
     private Class getJUnitClass( ClassLoader classLoader, String className )
 99  
     {
 100  2
         return ReflectionUtils.tryLoadClass( classLoader, className );
 101  
     }
 102  
 
 103  
 }