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 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
47 Boolean result = (Boolean) answers.get( method );
48 if ( result != null )
49 {
50
51 return result;
52 }
53
54 if ( matcher == null )
55 {
56
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
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
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
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 }