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 java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
import java.util.Properties; |
25 | |
import org.codehaus.plexus.util.SelectorUtils; |
26 | |
|
27 | |
import org.junit.experimental.categories.Categories; |
28 | |
import org.junit.runner.Description; |
29 | |
import org.junit.runner.manipulation.Filter; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class FilterFactory |
35 | |
{ |
36 | |
private final ClassLoader testClassLoader; |
37 | |
|
38 | |
public FilterFactory( ClassLoader testClassLoader ) |
39 | 0 | { |
40 | 0 | this.testClassLoader = testClassLoader; |
41 | 0 | } |
42 | |
|
43 | |
public Filter createGroupFilter( Properties providerProperties ) |
44 | |
{ |
45 | 0 | String groups = providerProperties.getProperty( "groups" ); |
46 | 0 | String excludedGroups = providerProperties.getProperty( "excludedgroups" ); |
47 | 0 | List<Filter> included = commaSeparatedListToFilters( groups ); |
48 | 0 | List<Filter> excluded = commaSeparatedListToFilters( excludedGroups ); |
49 | 0 | return new CombinedCategoryFilter( included, excluded ); |
50 | |
} |
51 | |
|
52 | |
private List<Filter> commaSeparatedListToFilters( String str ) |
53 | |
{ |
54 | 0 | List<Filter> included = new ArrayList<Filter>(); |
55 | 0 | if ( str != null ) |
56 | |
{ |
57 | 0 | for ( String group : str.split( "," ) ) |
58 | |
{ |
59 | 0 | group = group.trim(); |
60 | 0 | if ( group == null || group.length() == 0) |
61 | |
{ |
62 | 0 | continue; |
63 | |
} |
64 | 0 | Class<?> categoryType = classloadCategory( group ); |
65 | 0 | included.add( Categories.CategoryFilter.include( categoryType ) ); |
66 | |
} |
67 | |
} |
68 | 0 | return included; |
69 | |
} |
70 | |
|
71 | |
public Filter createMethodFilter( String requestedTestMethod ) |
72 | |
{ |
73 | 0 | return new MethodFilter( requestedTestMethod ); |
74 | |
} |
75 | |
|
76 | |
private static class MethodFilter |
77 | |
extends Filter |
78 | |
{ |
79 | |
private final String requestedTestMethod; |
80 | |
|
81 | |
public MethodFilter( String requestedTestMethod ) |
82 | 0 | { |
83 | 0 | this.requestedTestMethod = requestedTestMethod; |
84 | 0 | } |
85 | |
|
86 | |
@Override |
87 | |
public boolean shouldRun( Description description ) |
88 | |
{ |
89 | 0 | for ( Description o : description.getChildren() ) |
90 | |
{ |
91 | 0 | if (isDescriptionMatch( o )){ |
92 | 0 | return true; |
93 | |
} |
94 | |
|
95 | |
} |
96 | 0 | return isDescriptionMatch( description ); |
97 | |
} |
98 | |
|
99 | |
private boolean isDescriptionMatch( Description description ) |
100 | |
{ |
101 | 0 | return description.getMethodName() != null && SelectorUtils.match( requestedTestMethod, |
102 | |
description.getMethodName() ); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
public String describe() |
108 | |
{ |
109 | 0 | return "By method" + requestedTestMethod; |
110 | |
} |
111 | |
} |
112 | |
|
113 | |
|
114 | |
private static class CombinedCategoryFilter |
115 | |
extends Filter |
116 | |
{ |
117 | |
private final List<Filter> includedFilters; |
118 | |
|
119 | |
private final List<Filter> excludedFilters; |
120 | |
|
121 | |
public CombinedCategoryFilter( List<Filter> includedFilters, List<Filter> excludedFilters ) |
122 | 0 | { |
123 | 0 | this.includedFilters = includedFilters; |
124 | 0 | this.excludedFilters = excludedFilters; |
125 | 0 | } |
126 | |
|
127 | |
@Override |
128 | |
public boolean shouldRun( Description description ) |
129 | |
{ |
130 | 0 | return ( includedFilters.isEmpty() || inOneOfFilters( includedFilters, description ) ) && ( |
131 | |
excludedFilters.isEmpty() || !inOneOfFilters( excludedFilters, description ) ); |
132 | |
} |
133 | |
|
134 | |
private boolean inOneOfFilters( List<Filter> filters, Description description ) |
135 | |
{ |
136 | 0 | for ( Filter f : filters ) |
137 | |
{ |
138 | 0 | if ( f.shouldRun( description ) ) |
139 | |
{ |
140 | 0 | return true; |
141 | |
} |
142 | |
} |
143 | 0 | return false; |
144 | |
} |
145 | |
|
146 | |
@Override |
147 | |
public String describe() |
148 | |
{ |
149 | 0 | StringBuilder sb = new StringBuilder(); |
150 | 0 | if ( !includedFilters.isEmpty() ) |
151 | |
{ |
152 | 0 | sb.append( "(" ); |
153 | 0 | sb.append( joinFilters( includedFilters, " OR " ) ); |
154 | 0 | sb.append( ")" ); |
155 | 0 | if ( !excludedFilters.isEmpty() ) |
156 | |
{ |
157 | 0 | sb.append( " AND " ); |
158 | |
} |
159 | |
} |
160 | 0 | if ( !excludedFilters.isEmpty() ) |
161 | |
{ |
162 | 0 | sb.append( "NOT (" ); |
163 | 0 | sb.append( joinFilters( includedFilters, " OR " ) ); |
164 | 0 | sb.append( ")" ); |
165 | |
} |
166 | |
|
167 | 0 | return sb.toString(); |
168 | |
} |
169 | |
|
170 | |
private String joinFilters( List<Filter> filters, String sep ) |
171 | |
{ |
172 | 0 | int i = 0; |
173 | 0 | StringBuilder sb = new StringBuilder(); |
174 | 0 | for ( Filter f : filters ) |
175 | |
{ |
176 | 0 | if ( i++ > 0 ) |
177 | |
{ |
178 | 0 | sb.append( sep ); |
179 | |
} |
180 | 0 | sb.append( f.describe() ); |
181 | |
} |
182 | 0 | return sb.toString(); |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
private Class<?> classloadCategory( String category ) |
187 | |
{ |
188 | |
try |
189 | |
{ |
190 | 0 | return testClassLoader.loadClass( category ); |
191 | |
} |
192 | 0 | catch ( ClassNotFoundException e ) |
193 | |
{ |
194 | 0 | throw new RuntimeException( "Unable to load category: " + category, e ); |
195 | |
} |
196 | |
} |
197 | |
|
198 | |
} |