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