1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.surefire.testng.utils;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.maven.surefire.group.match.AndGroupMatcher;
26  import org.apache.maven.surefire.group.match.GroupMatcher;
27  import org.apache.maven.surefire.group.match.InverseGroupMatcher;
28  import org.apache.maven.surefire.group.parse.GroupMatcherParser;
29  import org.apache.maven.surefire.group.parse.ParseException;
30  import org.testng.IMethodSelector;
31  import org.testng.IMethodSelectorContext;
32  import org.testng.ITestNGMethod;
33  
34  
35  
36  
37  
38  public class GroupMatcherMethodSelector implements IMethodSelector {
39  
40      private static final long serialVersionUID = 1L;
41  
42      private static GroupMatcher matcher;
43  
44      private Map<ITestNGMethod, Boolean> answers = new HashMap<>();
45  
46      @Override
47      public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
48          Boolean result = answers.get(method);
49          if (result != null) {
50              return result;
51          }
52  
53          if (matcher == null) {
54              return true;
55          }
56  
57          String[] groups = method.getGroups();
58          result = matcher.enabled(groups);
59          answers.put(method, result);
60          return result;
61      }
62  
63      @Override
64      public void setTestMethods(List<ITestNGMethod> testMethods) {}
65  
66      public static void setGroups(String groups, String excludedGroups) {
67          
68  
69          try {
70              AndGroupMatcher matcher = new AndGroupMatcher();
71              GroupMatcher in = null;
72              if (groups != null && !groups.trim().isEmpty()) {
73                  in = new GroupMatcherParser(groups).parse();
74              }
75  
76              if (in != null) {
77                  matcher.addMatcher(in);
78              }
79  
80              GroupMatcher ex = null;
81              if (excludedGroups != null && !excludedGroups.trim().isEmpty()) {
82                  ex = new GroupMatcherParser(excludedGroups).parse();
83              }
84  
85              if (ex != null) {
86                  matcher.addMatcher(new InverseGroupMatcher(ex));
87              }
88  
89              if (in != null || ex != null) {
90                  
91                  GroupMatcherMethodSelector.matcher = matcher;
92              }
93          } catch (ParseException e) {
94              throw new IllegalArgumentException(
95                      "Cannot parse group includes/excludes expression(s):\nIncludes: " + groups + "\nExcludes: "
96                              + excludedGroups,
97                      e);
98          }
99      }
100 
101     public static void setGroupMatcher(GroupMatcher matcher) {
102         GroupMatcherMethodSelector.matcher = matcher;
103     }
104 }