Skip to content

Commit fe5d86e

Browse files
author
Ricky
committed
- statically imported Collections where appropriate
- refactored the construction of the incompatiable annotations of CategoryValidator to be initialized immediately
1 parent f5e7e1f commit fe5d86e

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.junit.experimental.categories;
22

3+
import static java.util.Collections.unmodifiableList;
4+
import static java.util.Collections.unmodifiableSet;
5+
import static java.util.Arrays.asList;
6+
37
import java.lang.annotation.Annotation;
48
import java.lang.reflect.Method;
59
import java.util.ArrayList;
6-
import java.util.Collections;
710
import java.util.HashSet;
811
import java.util.List;
912
import java.util.Set;
@@ -23,44 +26,36 @@
2326
*/
2427
public final class CategoryValidator extends AnnotationValidator {
2528

26-
private static Set<Class<? extends Annotation>> fIncompatibleAnnotations = buildIncompatibleAnnotationsSet();
29+
private static final Set<Class<? extends Annotation>> INCOMPATIBLE_ANNOTATIONS = unmodifiableSet(new HashSet<Class<? extends Annotation>>(
30+
asList(BeforeClass.class, AfterClass.class, Before.class, After.class)));
2731

2832
/**
2933
* Adds to {@code errors} a throwable for each problem detected. Looks for
3034
* {@code BeforeClass}, {@code AfterClass}, {@code Before} and {@code After}
3135
* annotations.
3236
*
3337
* @param method the method that is being validated
34-
* @return A list of errors detected
38+
* @return A list of exceptions detected
3539
*
3640
* @since 4.12
3741
*/
3842
@Override
39-
public List<Throwable> validateAnnotatedMethod(Method method) {
40-
List<Throwable> errors = new ArrayList<Throwable>();
43+
public List<Exception> validateAnnotatedMethod(Method method) {
44+
List<Exception> errors = new ArrayList<Exception>();
4145
Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
4246
for (Annotation annotation : declaredAnnotations) {
43-
for (Class clazz : fIncompatibleAnnotations) {
47+
for (Class clazz : INCOMPATIBLE_ANNOTATIONS) {
4448
if (annotation.annotationType().isAssignableFrom(clazz)) {
4549
addErrorMessage(errors, clazz);
4650
}
4751
}
4852
}
49-
return Collections.unmodifiableList(errors);
50-
}
51-
52-
private static Set<Class<? extends Annotation>> buildIncompatibleAnnotationsSet() {
53-
Set<Class<? extends Annotation>> incompatibleAnnotations = new HashSet<Class<? extends Annotation>>();
54-
incompatibleAnnotations.add(BeforeClass.class);
55-
incompatibleAnnotations.add(AfterClass.class);
56-
incompatibleAnnotations.add(Before.class);
57-
incompatibleAnnotations.add(After.class);
58-
return Collections.unmodifiableSet(incompatibleAnnotations);
53+
return unmodifiableList(errors);
5954
}
6055

61-
private void addErrorMessage(List<Throwable> errors, Class clazz) {
56+
private void addErrorMessage(List<Exception> errors, Class clazz) {
6257
String message = String.format("@%s can not be combined with @Category",
6358
clazz.getSimpleName());
64-
errors.add(new Throwable(message));
59+
errors.add(new Exception(message));
6560
}
6661
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.junit.validator;
22

3+
import static java.util.Collections.emptyList;
4+
35
import java.lang.reflect.Field;
46
import java.lang.reflect.Method;
5-
import java.util.Collections;
67
import java.util.List;
78

89
/**
@@ -15,40 +16,43 @@
1516
* @since 4.12
1617
*/
1718
public abstract class AnnotationValidator {
19+
20+
private static final List<Exception> NO_VALIDATION_ERRORS = emptyList();
21+
1822
/**
1923
* Validates annotation on the given class.
2024
*
2125
* @param type that is being validated
22-
* @return A list of throwables. Default behavior is to return an empty list.
26+
* @return A list of exceptions. Default behavior is to return an empty list.
2327
*
2428
* @since 4.12
2529
*/
26-
public List<Throwable> validateAnnotatedClass(Class<?> type) {
27-
return Collections.emptyList();
30+
public List<Exception> validateAnnotatedClass(Class<?> type) {
31+
return NO_VALIDATION_ERRORS;
2832
}
2933

3034
/**
3135
* Validates annotation on the given field.
3236
*
3337
* @param field that is being validated
34-
* @return A list of throwables. Default behavior is to return an empty list.
38+
* @return A list of exceptions. Default behavior is to return an empty list.
3539
*
3640
* @since 4.12
3741
*/
38-
public List<Throwable> validateAnnotatedField(Field field) {
39-
return Collections.emptyList();
42+
public List<Exception> validateAnnotatedField(Field field) {
43+
return NO_VALIDATION_ERRORS;
4044

4145
}
4246

4347
/**
4448
* Validates annotation on the given method.
4549
*
4650
* @param method that is being validated
47-
* @return A list of throwables. Default behavior is to return an empty list.
51+
* @return A list of exceptions. Default behavior is to return an empty list.
4852
*
4953
* @since 4.12
5054
*/
51-
public List<Throwable> validateAnnotatedMethod(Method method) {
52-
return Collections.emptyList();
55+
public List<Exception> validateAnnotatedMethod(Method method) {
56+
return NO_VALIDATION_ERRORS;
5357
}
5458
}

src/test/java/org/junit/tests/experimental/categories/CategoryValidatorTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.junit.tests.experimental.categories;
22

33

4+
import static org.hamcrest.CoreMatchers.is;
5+
import static org.junit.Assert.assertThat;
6+
47
import java.lang.reflect.Method;
58
import java.util.List;
69

@@ -12,9 +15,6 @@
1215
import org.junit.experimental.categories.Category;
1316
import org.junit.experimental.categories.CategoryValidator;
1417

15-
import static org.hamcrest.CoreMatchers.is;
16-
import static org.junit.Assert.assertThat;
17-
1818
public class CategoryValidatorTest {
1919

2020
public static class SampleCategory {
@@ -71,17 +71,17 @@ public void errorIsAddedWhenCategoryIsUsedWithAfter() throws NoSuchMethodExcepti
7171
}
7272

7373
private void testAndAssertErrrorMessage(Method method, String expectedErrorMessage) throws NoSuchMethodException {
74-
List<Throwable> errors = new CategoryValidator().validateAnnotatedMethod(method);
74+
List<Exception> errors = new CategoryValidator().validateAnnotatedMethod(method);
7575

7676
assertThat(errors.size(), is(1));
77-
Throwable throwable = errors.get(0);
78-
assertThat(throwable.getMessage(), is(expectedErrorMessage));
77+
Exception exception = errors.get(0);
78+
assertThat(exception.getMessage(), is(expectedErrorMessage));
7979
}
8080

8181
@Test
8282
public void errorIsNotAddedWhenCategoryIsNotCombinedWithIllegalCombination() throws NoSuchMethodException {
8383
Method beforeClass = CategoryTest.class.getMethod("methodWithCategory");
84-
List<Throwable> errors = new CategoryValidator().validateAnnotatedMethod(beforeClass);
84+
List<Exception> errors = new CategoryValidator().validateAnnotatedMethod(beforeClass);
8585

8686
assertThat(errors.size(), is(0));
8787
}

src/test/java/org/junit/tests/running/classes/ParentRunnerTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import java.lang.reflect.Field;
2626
import java.lang.reflect.Method;
2727
import java.util.ArrayList;
28-
import java.util.Arrays;
2928
import java.util.List;
3029

30+
import static java.util.Arrays.asList;
3131
import static org.hamcrest.core.IsCollectionContaining.hasItem;
3232
import static org.junit.Assert.assertEquals;
3333
import static org.junit.Assert.assertThat;
@@ -154,18 +154,18 @@ public static class ExampleAnnotationValidator extends AnnotationValidator {
154154
private static final String ANNOTATED_CLASS_CALLED = "annotated class called";
155155

156156
@Override
157-
public List<Throwable> validateAnnotatedClass(Class<?> type) {
158-
return Arrays.asList(new Throwable(ANNOTATED_CLASS_CALLED));
157+
public List<Exception> validateAnnotatedClass(Class<?> type) {
158+
return asList(new Exception(ANNOTATED_CLASS_CALLED));
159159
}
160160

161161
@Override
162-
public List<Throwable> validateAnnotatedField(Field field) {
163-
return Arrays.asList(new Throwable(ANNOTATED_FIELD_CALLED));
162+
public List<Exception> validateAnnotatedField(Field field) {
163+
return asList(new Exception(ANNOTATED_FIELD_CALLED));
164164
}
165165

166166
@Override
167-
public List<Throwable> validateAnnotatedMethod(Method method) {
168-
return Arrays.asList(new Throwable(ANNOTATED_METHOD_CALLED));
167+
public List<Exception> validateAnnotatedMethod(Method method) {
168+
return asList(new Exception(ANNOTATED_METHOD_CALLED));
169169
}
170170
}
171171

0 commit comments

Comments
 (0)