2
2
3
3
import java .lang .annotation .Retention ;
4
4
import java .lang .annotation .RetentionPolicy ;
5
+ import java .util .Arrays ;
5
6
import java .util .Collections ;
6
7
import java .util .HashSet ;
8
+ import java .util .LinkedHashSet ;
7
9
import java .util .Set ;
8
10
9
11
import org .junit .runner .Description ;
@@ -117,10 +119,7 @@ public static class CategoryFilter extends Filter {
117
119
private final boolean excludedAny ;
118
120
119
121
public static CategoryFilter include (boolean matchAny , Class <?>... categories ) {
120
- if (hasNull (categories )) {
121
- throw new NullPointerException ("has null category" );
122
- }
123
- return categoryFilter (matchAny , createSet (categories ), true , null );
122
+ return new CategoryFilter (matchAny , categories , true , null );
124
123
}
125
124
126
125
public static CategoryFilter include (Class <?> category ) {
@@ -132,10 +131,7 @@ public static CategoryFilter include(Class<?>... categories) {
132
131
}
133
132
134
133
public static CategoryFilter exclude (boolean matchAny , Class <?>... categories ) {
135
- if (hasNull (categories )) {
136
- throw new NullPointerException ("has null category" );
137
- }
138
- return categoryFilter (true , null , matchAny , createSet (categories ));
134
+ return new CategoryFilter (true , null , matchAny , categories );
139
135
}
140
136
141
137
public static CategoryFilter exclude (Class <?> category ) {
@@ -151,14 +147,30 @@ public static CategoryFilter categoryFilter(boolean matchAnyInclusions, Set<Clas
151
147
return new CategoryFilter (matchAnyInclusions , inclusions , matchAnyExclusions , exclusions );
152
148
}
153
149
150
+ @ Deprecated
151
+ public CategoryFilter (Class <?> includedCategory , Class <?> excludedCategory ) {
152
+ includedAny = true ;
153
+ excludedAny = true ;
154
+ included = nullableClassToSet (includedCategory );
155
+ excluded = nullableClassToSet (excludedCategory );
156
+ }
157
+
154
158
protected CategoryFilter (boolean matchAnyIncludes , Set <Class <?>> includes ,
155
- boolean matchAnyExcludes , Set <Class <?>> excludes ) {
159
+ boolean matchAnyExcludes , Set <Class <?>> excludes ) {
156
160
includedAny = matchAnyIncludes ;
157
161
excludedAny = matchAnyExcludes ;
158
162
included = copyAndRefine (includes );
159
163
excluded = copyAndRefine (excludes );
160
164
}
161
165
166
+ private CategoryFilter (boolean matchAnyIncludes , Class <?>[] inclusions ,
167
+ boolean matchAnyExcludes , Class <?>[] exclusions ) {
168
+ includedAny = matchAnyIncludes ;
169
+ excludedAny = matchAnyExcludes ;
170
+ included = createSet (inclusions );
171
+ excluded = createSet (exclusions );
172
+ }
173
+
162
174
/**
163
175
* @see #toString()
164
176
*/
@@ -284,23 +296,13 @@ private static Class<?>[] directCategories(Description description) {
284
296
}
285
297
286
298
private static Set <Class <?>> copyAndRefine (Set <Class <?>> classes ) {
287
- Set <Class <?>> c = new HashSet <Class <?>>();
299
+ Set <Class <?>> c = new LinkedHashSet <Class <?>>();
288
300
if (classes != null ) {
289
301
c .addAll (classes );
290
302
}
291
303
c .remove (null );
292
304
return c ;
293
305
}
294
-
295
- private static boolean hasNull (Class <?>... classes ) {
296
- if (classes == null ) return false ;
297
- for (Class <?> clazz : classes ) {
298
- if (clazz == null ) {
299
- return true ;
300
- }
301
- }
302
- return false ;
303
- }
304
306
}
305
307
306
308
public Categories (Class <?> klass , RunnerBuilder builder ) throws InitializationError {
@@ -346,11 +348,28 @@ private static boolean hasAssignableTo(Set<Class<?>> assigns, Class<?> to) {
346
348
return false ;
347
349
}
348
350
349
- private static Set <Class <?>> createSet (Class <?>... t ) {
350
- final Set <Class <?>> set = new HashSet <Class <?>>();
351
- if (t != null ) {
352
- Collections .addAll (set , t );
351
+ private static Set <Class <?>> createSet (Class <?>[] classes ) {
352
+ // Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.12
353
+ // for include(boolean, Class<?>...) and exclude(boolean, Class<?>...)
354
+ if (classes == null || classes .length == 0 ) {
355
+ return Collections .emptySet ();
356
+ }
357
+ for (Class <?> category : classes ) {
358
+ if (category == null ) {
359
+ throw new NullPointerException ("has null category" );
360
+ }
353
361
}
354
- return set ;
362
+
363
+ return classes .length == 1
364
+ ? Collections .<Class <?>>singleton (classes [0 ])
365
+ : new LinkedHashSet <Class <?>>(Arrays .asList (classes ));
366
+ }
367
+
368
+ private static Set <Class <?>> nullableClassToSet (Class <?> nullableClass ) {
369
+ // Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.11
370
+ // for CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory)
371
+ return nullableClass == null
372
+ ? Collections .<Class <?>>emptySet ()
373
+ : Collections .<Class <?>>singleton (nullableClass );
355
374
}
356
375
}
0 commit comments