Coverage Report - org.apache.maven.surefire.common.junit48.FilterFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
FilterFactory
0%
0/21
0%
0/8
2.571
FilterFactory$CombinedCategoryFilter
0%
0/28
0%
0/22
2.571
FilterFactory$MethodFilter
0%
0/9
0%
0/8
2.571
 
 1  
 package org.apache.maven.surefire.common.junit48;
 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.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.Properties;
 25  
 import org.codehaus.plexus.util.SelectorUtils;
 26  
 
 27  
 import org.junit.experimental.categories.Categories;
 28  
 import org.junit.runner.Description;
 29  
 import org.junit.runner.manipulation.Filter;
 30  
 
 31  
 /**
 32  
  * @author Todd Lipcon
 33  
  */
 34  
 public class FilterFactory
 35  
 {
 36  
     private final ClassLoader testClassLoader;
 37  
 
 38  
     public FilterFactory( ClassLoader testClassLoader )
 39  0
     {
 40  0
         this.testClassLoader = testClassLoader;
 41  0
     }
 42  
 
 43  
     public Filter createGroupFilter( Properties providerProperties )
 44  
     {
 45  0
         String groups = providerProperties.getProperty( "groups" );
 46  0
         String excludedGroups = providerProperties.getProperty( "excludedgroups" );
 47  0
         List<Filter> included = commaSeparatedListToFilters( groups );
 48  0
         List<Filter> excluded = commaSeparatedListToFilters( excludedGroups );
 49  0
         return new CombinedCategoryFilter( included, excluded );
 50  
     }
 51  
 
 52  
     private List<Filter> commaSeparatedListToFilters( String str )
 53  
     {
 54  0
         List<Filter> included = new ArrayList<Filter>();
 55  0
         if ( str != null )
 56  
         {
 57  0
             for ( String group : str.split( "," ) )
 58  
             {
 59  0
                 group = group.trim();
 60  0
                 if ( group == null || group.length() == 0)
 61  
                 {
 62  0
                     continue;
 63  
                 }
 64  0
                 Class<?> categoryType = classloadCategory( group );
 65  0
                 included.add( Categories.CategoryFilter.include( categoryType ) );
 66  
             }
 67  
         }
 68  0
         return included;
 69  
     }
 70  
 
 71  
     public Filter createMethodFilter( String requestedTestMethod )
 72  
     {
 73  0
         return new MethodFilter( requestedTestMethod );
 74  
     }
 75  
 
 76  
     private static class MethodFilter
 77  
         extends Filter
 78  
     {
 79  
         private final String requestedTestMethod;
 80  
 
 81  
         public MethodFilter( String requestedTestMethod )
 82  0
         {
 83  0
             this.requestedTestMethod = requestedTestMethod;
 84  0
         }
 85  
 
 86  
         @Override
 87  
         public boolean shouldRun( Description description )
 88  
         {
 89  0
             for ( Description o : description.getChildren() )
 90  
             {
 91  0
                 if (isDescriptionMatch( o )){
 92  0
                     return true;
 93  
                 }
 94  
                 
 95  
             }
 96  0
             return isDescriptionMatch( description );
 97  
         }
 98  
 
 99  
         private boolean isDescriptionMatch( Description description )
 100  
         {
 101  0
             return description.getMethodName() != null && SelectorUtils.match( requestedTestMethod,
 102  
                                                                                description.getMethodName() );
 103  
         }
 104  
 
 105  
 
 106  
         @Override
 107  
         public String describe()
 108  
         {
 109  0
             return "By method"  + requestedTestMethod;
 110  
         }
 111  
     }
 112  
 
 113  
 
 114  
     private static class CombinedCategoryFilter
 115  
         extends Filter
 116  
     {
 117  
         private final List<Filter> includedFilters;
 118  
 
 119  
         private final List<Filter> excludedFilters;
 120  
 
 121  
         public CombinedCategoryFilter( List<Filter> includedFilters, List<Filter> excludedFilters )
 122  0
         {
 123  0
             this.includedFilters = includedFilters;
 124  0
             this.excludedFilters = excludedFilters;
 125  0
         }
 126  
 
 127  
         @Override
 128  
         public boolean shouldRun( Description description )
 129  
         {
 130  0
             return ( includedFilters.isEmpty() || inOneOfFilters( includedFilters, description ) ) && (
 131  
                 excludedFilters.isEmpty() || !inOneOfFilters( excludedFilters, description ) );
 132  
         }
 133  
 
 134  
         private boolean inOneOfFilters( List<Filter> filters, Description description )
 135  
         {
 136  0
             for ( Filter f : filters )
 137  
             {
 138  0
                 if ( f.shouldRun( description ) )
 139  
                 {
 140  0
                     return true;
 141  
                 }
 142  
             }
 143  0
             return false;
 144  
         }
 145  
 
 146  
         @Override
 147  
         public String describe()
 148  
         {
 149  0
             StringBuilder sb = new StringBuilder();
 150  0
             if ( !includedFilters.isEmpty() )
 151  
             {
 152  0
                 sb.append( "(" );
 153  0
                 sb.append( joinFilters( includedFilters, " OR " ) );
 154  0
                 sb.append( ")" );
 155  0
                 if ( !excludedFilters.isEmpty() )
 156  
                 {
 157  0
                     sb.append( " AND " );
 158  
                 }
 159  
             }
 160  0
             if ( !excludedFilters.isEmpty() )
 161  
             {
 162  0
                 sb.append( "NOT (" );
 163  0
                 sb.append( joinFilters( includedFilters, " OR " ) );
 164  0
                 sb.append( ")" );
 165  
             }
 166  
 
 167  0
             return sb.toString();
 168  
         }
 169  
 
 170  
         private String joinFilters( List<Filter> filters, String sep )
 171  
         {
 172  0
             int i = 0;
 173  0
             StringBuilder sb = new StringBuilder();
 174  0
             for ( Filter f : filters )
 175  
             {
 176  0
                 if ( i++ > 0 )
 177  
                 {
 178  0
                     sb.append( sep );
 179  
                 }
 180  0
                 sb.append( f.describe() );
 181  
             }
 182  0
             return sb.toString();
 183  
         }
 184  
     }
 185  
 
 186  
     private Class<?> classloadCategory( String category )
 187  
     {
 188  
         try
 189  
         {
 190  0
             return testClassLoader.loadClass( category );
 191  
         }
 192  0
         catch ( ClassNotFoundException e )
 193  
         {
 194  0
             throw new RuntimeException( "Unable to load category: " + category, e );
 195  
         }
 196  
     }
 197  
 
 198  
 }