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