Coverage Report - org.apache.maven.surefire.testng.utils.GroupMatcherMethodSelector
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupMatcherMethodSelector
0%
0/31
0%
0/20
4,5
 
 1  
 package org.apache.maven.surefire.testng.utils;
 2  
 /*
 3  
  * Licensed to the Apache Software Foundation (ASF) under one
 4  
  * or more contributor license agreements.  See the NOTICE file
 5  
  * distributed with this work for additional information
 6  
  * regarding copyright ownership.  The ASF licenses this file
 7  
  * to you under the Apache License, Version 2.0 (the
 8  
  * "License"); you may not use this file except in compliance
 9  
  * with the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *     http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing,
 14  
  * software distributed under the License is distributed on an
 15  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16  
  * KIND, either express or implied.  See the License for the
 17  
  * specific language governing permissions and limitations
 18  
  * under the License.
 19  
  */
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import org.apache.maven.surefire.group.match.AndGroupMatcher;
 25  
 import org.apache.maven.surefire.group.match.GroupMatcher;
 26  
 import org.apache.maven.surefire.group.match.InverseGroupMatcher;
 27  
 import org.apache.maven.surefire.group.parse.GroupMatcherParser;
 28  
 import org.apache.maven.surefire.group.parse.ParseException;
 29  
 
 30  
 import org.testng.IMethodSelector;
 31  
 import org.testng.IMethodSelectorContext;
 32  
 import org.testng.ITestNGMethod;
 33  
 
 34  0
 public class GroupMatcherMethodSelector
 35  
     implements IMethodSelector
 36  
 {
 37  
 
 38  
     private static final long serialVersionUID = 1L;
 39  
 
 40  
     private static GroupMatcher matcher;
 41  
 
 42  0
     private Map<ITestNGMethod, Boolean> answers = new HashMap<ITestNGMethod, Boolean>();
 43  
 
 44  
     public boolean includeMethod( IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod )
 45  
     {
 46  
         // System.out.println( "Checking: " + method + " vs. matcher: " + matcher );
 47  0
         Boolean result = (Boolean) answers.get( method );
 48  0
         if ( result != null )
 49  
         {
 50  
             // System.out.println( "Enabled? " + result );
 51  0
             return result;
 52  
         }
 53  
 
 54  0
         if ( matcher == null )
 55  
         {
 56  
             // System.out.println( "No matcher, enable by default" );
 57  0
             return true;
 58  
         }
 59  
 
 60  0
         String[] groups = method.getGroups();
 61  0
         result = Boolean.valueOf( matcher.enabled( groups ) );
 62  
 
 63  0
         answers.put( method, result );
 64  
 
 65  
         // System.out.println( "Enabled? " + result );
 66  0
         return result;
 67  
     }
 68  
 
 69  
     public void setTestMethods( List<ITestNGMethod> testMethods )
 70  
     {
 71  0
     }
 72  
 
 73  
     public static void setGroups( String groups, String excludedGroups )
 74  
     {
 75  
         // System.out.println( "Processing group includes: '" + groups + "'\nExcludes: '" + excludedGroups + "'" );
 76  
 
 77  
         try
 78  
         {
 79  0
             AndGroupMatcher matcher = new AndGroupMatcher();
 80  0
             GroupMatcher in = null;
 81  0
             if ( groups != null && groups.trim().length() > 0 )
 82  
             {
 83  0
                 in = new GroupMatcherParser( groups ).parse();
 84  
             }
 85  
 
 86  0
             if ( in != null )
 87  
             {
 88  0
                 matcher.addMatcher( in );
 89  
             }
 90  
 
 91  0
             GroupMatcher ex = null;
 92  0
             if ( excludedGroups != null && excludedGroups.trim().length() > 0 )
 93  
             {
 94  0
                 ex = new GroupMatcherParser( excludedGroups ).parse();
 95  
             }
 96  
 
 97  0
             if ( ex != null )
 98  
             {
 99  0
                 matcher.addMatcher( new InverseGroupMatcher( ex ) );
 100  
             }
 101  
 
 102  0
             if ( in != null || ex != null )
 103  
             {
 104  
                 // System.out.println( "Group matcher: " + matcher );
 105  0
                 GroupMatcherMethodSelector.matcher = matcher;
 106  
             }
 107  
         }
 108  0
         catch ( ParseException e )
 109  
         {
 110  0
             throw new IllegalArgumentException(
 111  
                 "Cannot parse group includes/excludes expression(s):\nIncludes: " + groups + "\nExcludes: "
 112  
                     + excludedGroups, e );
 113  0
         }
 114  0
     }
 115  
 
 116  
     public static void setGroupMatcher( GroupMatcher matcher )
 117  
     {
 118  0
         GroupMatcherMethodSelector.matcher = matcher;
 119  0
     }
 120  
 
 121  
 }