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