View Javadoc

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  public class GroupMatcherMethodSelector
35      implements IMethodSelector
36  {
37  
38      private static final long serialVersionUID = 1L;
39  
40      private static GroupMatcher matcher;
41  
42      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          Boolean result = (Boolean) answers.get( method );
48          if ( result != null )
49          {
50              // System.out.println( "Enabled? " + result );
51              return result;
52          }
53  
54          if ( matcher == null )
55          {
56              // System.out.println( "No matcher, enable by default" );
57              return true;
58          }
59  
60          String[] groups = method.getGroups();
61          result = Boolean.valueOf( matcher.enabled( groups ) );
62  
63          answers.put( method, result );
64  
65          // System.out.println( "Enabled? " + result );
66          return result;
67      }
68  
69      public void setTestMethods( List<ITestNGMethod> testMethods )
70      {
71      }
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              AndGroupMatcher matcher = new AndGroupMatcher();
80              GroupMatcher in = null;
81              if ( groups != null && groups.trim().length() > 0 )
82              {
83                  in = new GroupMatcherParser( groups ).parse();
84              }
85  
86              if ( in != null )
87              {
88                  matcher.addMatcher( in );
89              }
90  
91              GroupMatcher ex = null;
92              if ( excludedGroups != null && excludedGroups.trim().length() > 0 )
93              {
94                  ex = new GroupMatcherParser( excludedGroups ).parse();
95              }
96  
97              if ( ex != null )
98              {
99                  matcher.addMatcher( new InverseGroupMatcher( ex ) );
100             }
101 
102             if ( in != null || ex != null )
103             {
104                 // System.out.println( "Group matcher: " + matcher );
105                 GroupMatcherMethodSelector.matcher = matcher;
106             }
107         }
108         catch ( ParseException e )
109         {
110             throw new IllegalArgumentException(
111                 "Cannot parse group includes/excludes expression(s):\nIncludes: " + groups + "\nExcludes: "
112                     + excludedGroups, e );
113         }
114     }
115 
116     public static void setGroupMatcher( GroupMatcher matcher )
117     {
118         GroupMatcherMethodSelector.matcher = matcher;
119     }
120 
121 }