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<ITestNGMethod, Boolean>();
48
49 public boolean includeMethod( IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod )
50 {
51 Boolean result = answers.get( method );
52 if ( result != null )
53 {
54 return result;
55 }
56
57 if ( matcher == null )
58 {
59 return true;
60 }
61
62 String[] groups = method.getGroups();
63 result = matcher.enabled( groups );
64 answers.put( method, result );
65 return result;
66 }
67
68 public void setTestMethods( List<ITestNGMethod> testMethods )
69 {
70 }
71
72 public static void setGroups( String groups, String excludedGroups )
73 {
74
75
76 try
77 {
78 AndGroupMatcher matcher = new AndGroupMatcher();
79 GroupMatcher in = null;
80 if ( groups != null && groups.trim().length() > 0 )
81 {
82 in = new GroupMatcherParser( groups ).parse();
83 }
84
85 if ( in != null )
86 {
87 matcher.addMatcher( in );
88 }
89
90 GroupMatcher ex = null;
91 if ( excludedGroups != null && excludedGroups.trim().length() > 0 )
92 {
93 ex = new GroupMatcherParser( excludedGroups ).parse();
94 }
95
96 if ( ex != null )
97 {
98 matcher.addMatcher( new InverseGroupMatcher( ex ) );
99 }
100
101 if ( in != null || ex != null )
102 {
103
104 GroupMatcherMethodSelector.matcher = matcher;
105 }
106 }
107 catch ( ParseException e )
108 {
109 throw new IllegalArgumentException(
110 "Cannot parse group includes/excludes expression(s):\nIncludes: " + groups + "\nExcludes: "
111 + excludedGroups, e );
112 }
113 }
114
115 public static void setGroupMatcher( GroupMatcher matcher )
116 {
117 GroupMatcherMethodSelector.matcher = matcher;
118 }
119
120 }