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