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 public class GroupMatcherMethodSelector implements IMethodSelector {
38
39 private static final long serialVersionUID = 1L;
40
41 private static GroupMatcher matcher;
42
43 private Map<ITestNGMethod, Boolean> answers = new HashMap<>();
44
45 @Override
46 public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
47 Boolean result = answers.get(method);
48 if (result != null) {
49 return result;
50 }
51
52 if (matcher == null) {
53 return true;
54 }
55
56 String[] groups = method.getGroups();
57 result = matcher.enabled(groups);
58 answers.put(method, result);
59 return result;
60 }
61
62 @Override
63 public void setTestMethods(List<ITestNGMethod> testMethods) {}
64
65 public static void setGroups(String groups, String excludedGroups) {
66
67
68 try {
69 AndGroupMatcher matcher = new AndGroupMatcher();
70 GroupMatcher in = null;
71 if (groups != null && !groups.trim().isEmpty()) {
72 in = new GroupMatcherParser(groups).parse();
73 }
74
75 if (in != null) {
76 matcher.addMatcher(in);
77 }
78
79 GroupMatcher ex = null;
80 if (excludedGroups != null && !excludedGroups.trim().isEmpty()) {
81 ex = new GroupMatcherParser(excludedGroups).parse();
82 }
83
84 if (ex != null) {
85 matcher.addMatcher(new InverseGroupMatcher(ex));
86 }
87
88 if (in != null || ex != null) {
89
90 GroupMatcherMethodSelector.matcher = matcher;
91 }
92 } catch (ParseException e) {
93 throw new IllegalArgumentException(
94 "Cannot parse group includes/excludes expression(s):\nIncludes: " + groups + "\nExcludes: "
95 + excludedGroups,
96 e);
97 }
98 }
99
100 public static void setGroupMatcher(GroupMatcher matcher) {
101 GroupMatcherMethodSelector.matcher = matcher;
102 }
103 }