Skip to content

Commit 6ec5c8d

Browse files
committed
Remove duplicated code that handles null categories.
1 parent 20d2298 commit 6ec5c8d

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

Diff for: src/main/java/org/junit/experimental/categories/Categories.java

+24-21
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ public static class CategoryFilter extends Filter {
118118
private final boolean excludedAny;
119119

120120
public static CategoryFilter include(boolean matchAny, Class<?>... categories) {
121-
if (hasNull(categories)) {
122-
throw new NullPointerException("has null category");
123-
}
124121
return new CategoryFilter(matchAny, categories, true, null);
125122
}
126123

@@ -133,9 +130,6 @@ public static CategoryFilter include(Class<?>... categories) {
133130
}
134131

135132
public static CategoryFilter exclude(boolean matchAny, Class<?>... categories) {
136-
if (hasNull(categories)) {
137-
throw new NullPointerException("has null category");
138-
}
139133
return new CategoryFilter(true, null, matchAny, categories);
140134
}
141135

@@ -154,7 +148,10 @@ public static CategoryFilter categoryFilter(boolean matchAnyInclusions, Set<Clas
154148

155149
@Deprecated
156150
public CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory) {
157-
this(true, createSet(includedCategory), true, createSet(excludedCategory));
151+
includedAny = true;
152+
excludedAny = true;
153+
included = nullableClassToSet(includedCategory);
154+
excluded = nullableClassToSet(excludedCategory);
158155
}
159156

160157
protected CategoryFilter(boolean matchAnyIncludes, Set<Class<?>> includes,
@@ -305,16 +302,6 @@ private static Set<Class<?>> copyAndRefine(Set<Class<?>> classes) {
305302
c.remove(null);
306303
return c;
307304
}
308-
309-
private static boolean hasNull(Class<?>... classes) {
310-
if (classes == null) return false;
311-
for (Class<?> clazz : classes) {
312-
if (clazz == null) {
313-
return true;
314-
}
315-
}
316-
return false;
317-
}
318305
}
319306

320307
public Categories(Class<?> klass, RunnerBuilder builder) throws InitializationError {
@@ -360,12 +347,28 @@ private static boolean hasAssignableTo(Set<Class<?>> assigns, Class<?> to) {
360347
return false;
361348
}
362349

363-
private static Set<Class<?>> createSet(Class<?>... t) {
350+
private static Set<Class<?>> createSet(Class<?>[] t) {
351+
// Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.12
352+
// for include(boolean, Class<?>...) and exclude(boolean, Class<?>...)
364353
if (t == null || t.length == 0) {
365354
return Collections.emptySet();
366-
} else if (t.length == 1) {
367-
return Collections.<Class<?>>singleton(t[0]);
368355
}
369-
return new HashSet<Class<?>>(Arrays.asList(t));
356+
for (Class<?> category : t) {
357+
if (category == null) {
358+
throw new NullPointerException("has null category");
359+
}
360+
}
361+
362+
return t.length == 1
363+
? Collections.<Class<?>>singleton(t[0])
364+
: new HashSet<Class<?>>(Arrays.asList(t));
365+
}
366+
367+
private static Set<Class<?>> nullableClassToSet(Class<?> nullableClass) {
368+
// Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.11
369+
// for CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory)
370+
return nullableClass == null
371+
? Collections.<Class<?>>emptySet()
372+
: Collections.<Class<?>>singleton(nullableClass);
370373
}
371374
}

0 commit comments

Comments
 (0)