1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.common.junit48;
20
21 import java.util.Map;
22
23 import org.apache.maven.surefire.api.booter.ProviderParameterNames;
24 import org.apache.maven.surefire.api.testset.TestListResolver;
25 import org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil;
26 import org.apache.maven.surefire.group.match.GroupMatcher;
27 import org.apache.maven.surefire.group.parse.GroupMatcherParser;
28 import org.apache.maven.surefire.group.parse.ParseException;
29 import org.junit.runner.Description;
30 import org.junit.runner.manipulation.Filter;
31
32 import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP;
33 import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_GROUPS_PROP;
34 import static org.apache.maven.surefire.shared.utils.StringUtils.isNotBlank;
35
36
37
38
39 public class FilterFactory {
40 private final ClassLoader testClassLoader;
41
42 public FilterFactory(ClassLoader testClassLoader) {
43 this.testClassLoader = testClassLoader;
44 }
45
46
47
48
49
50
51 public boolean canCreateGroupFilter(Map<String, String> providerProperties) {
52 String groups = providerProperties.get(TESTNG_GROUPS_PROP);
53 String excludedGroups = providerProperties.get(TESTNG_EXCLUDEDGROUPS_PROP);
54 return isNotBlank(groups) || isNotBlank(excludedGroups);
55 }
56
57
58
59
60
61
62 public Filter createGroupFilter(Map<String, String> providerProperties) {
63 String groups = providerProperties.get(TESTNG_GROUPS_PROP);
64
65 GroupMatcher included = null;
66 if (isNotBlank(groups)) {
67 try {
68 included = new GroupMatcherParser(groups).parse();
69 } catch (ParseException e) {
70 throw new IllegalArgumentException(
71 "Invalid group expression: '" + groups + "'. Reason: " + e.getMessage(), e);
72 }
73 }
74
75 String excludedGroups = providerProperties.get(TESTNG_EXCLUDEDGROUPS_PROP);
76
77 GroupMatcher excluded = null;
78 if (isNotBlank(excludedGroups)) {
79 try {
80 excluded = new GroupMatcherParser(excludedGroups).parse();
81 } catch (ParseException e) {
82 throw new IllegalArgumentException(
83 "Invalid group expression: '" + excludedGroups + "'. Reason: " + e.getMessage(), e);
84 }
85 }
86
87 if (included != null && testClassLoader != null) {
88 included.loadGroupClasses(testClassLoader);
89 }
90
91 if (excluded != null && testClassLoader != null) {
92 excluded.loadGroupClasses(testClassLoader);
93 }
94
95 return new GroupMatcherCategoryFilter(included, excluded);
96 }
97
98 public Filter createMethodFilter(String requestedTestMethod) {
99 return new MethodFilter(requestedTestMethod);
100 }
101
102 public Filter createMethodFilter(TestListResolver resolver) {
103 return new MethodFilter(resolver);
104 }
105
106 public Filter createMatchAnyDescriptionFilter(Iterable<Description> descriptions) {
107 return JUnit4ProviderUtil.createMatchAnyDescriptionFilter(descriptions);
108 }
109
110 public Filter and(Filter filter1, Filter filter2) {
111 return new AndFilter(filter1, filter2);
112 }
113 }