Skip to content

Commit d905414

Browse files
author
Ricky
committed
changed arguments to validate methods to use JUnit classes as opposed to java.lang.reflect
1 parent fe5d86e commit d905414

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

src/main/java/org/junit/experimental/categories/CategoryValidator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static java.util.Arrays.asList;
66

77
import java.lang.annotation.Annotation;
8-
import java.lang.reflect.Method;
98
import java.util.ArrayList;
109
import java.util.HashSet;
1110
import java.util.List;
@@ -15,6 +14,7 @@
1514
import org.junit.AfterClass;
1615
import org.junit.Before;
1716
import org.junit.BeforeClass;
17+
import org.junit.runners.model.FrameworkMethod;
1818
import org.junit.validator.AnnotationValidator;
1919

2020
/**
@@ -40,10 +40,10 @@ public final class CategoryValidator extends AnnotationValidator {
4040
* @since 4.12
4141
*/
4242
@Override
43-
public List<Exception> validateAnnotatedMethod(Method method) {
43+
public List<Exception> validateAnnotatedMethod(FrameworkMethod method) {
4444
List<Exception> errors = new ArrayList<Exception>();
45-
Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
46-
for (Annotation annotation : declaredAnnotations) {
45+
Annotation[] annotations = method.getAnnotations();
46+
for (Annotation annotation : annotations) {
4747
for (Class clazz : INCOMPATIBLE_ANNOTATIONS) {
4848
if (annotation.annotationType().isAssignableFrom(clazz)) {
4949
addErrorMessage(errors, clazz);

src/main/java/org/junit/runners/ParentRunner.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void invokeValidatorsOnClass(List<Throwable> errors) {
138138
if (validateWithAnnotation != null) {
139139
AnnotationValidator annotationValidator =
140140
fAnnotationValidatorFactory.createAnnotationValidator(validateWithAnnotation);
141-
errors.addAll(annotationValidator.validateAnnotatedClass(getTestClass().getJavaClass()));
141+
errors.addAll(annotationValidator.validateAnnotatedClass(getTestClass()));
142142
}
143143
}
144144
}
@@ -151,7 +151,7 @@ private void invokeValidatorsOnMethods(List<Throwable> errors) {
151151
for (FrameworkMethod frameworkMethod : annotationMap.get(annotationType)) {
152152
AnnotationValidator annotationValidator =
153153
fAnnotationValidatorFactory.createAnnotationValidator(validateWithAnnotation);
154-
errors.addAll(annotationValidator.validateAnnotatedMethod(frameworkMethod.getMethod()));
154+
errors.addAll(annotationValidator.validateAnnotatedMethod(frameworkMethod));
155155
}
156156
}
157157
}
@@ -165,7 +165,7 @@ private void invokeValidatorsOnFields(List<Throwable> errors) {
165165
for (FrameworkField frameworkField : annotationMap.get(annotationType)) {
166166
AnnotationValidator annotationValidator =
167167
fAnnotationValidatorFactory.createAnnotationValidator(validateWithAnnotation);
168-
errors.addAll(annotationValidator.validateAnnotatedField(frameworkField.getField()));
168+
errors.addAll(annotationValidator.validateAnnotatedField(frameworkField));
169169
}
170170
}
171171
}

src/main/java/org/junit/validator/AnnotationValidator.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.junit.validator;
22

3+
import org.junit.runners.model.FrameworkField;
4+
import org.junit.runners.model.FrameworkMethod;
5+
import org.junit.runners.model.TestClass;
6+
37
import static java.util.Collections.emptyList;
48

5-
import java.lang.reflect.Field;
6-
import java.lang.reflect.Method;
79
import java.util.List;
810

911
/**
@@ -22,12 +24,12 @@ public abstract class AnnotationValidator {
2224
/**
2325
* Validates annotation on the given class.
2426
*
25-
* @param type that is being validated
27+
* @param testClass that is being validated
2628
* @return A list of exceptions. Default behavior is to return an empty list.
2729
*
2830
* @since 4.12
2931
*/
30-
public List<Exception> validateAnnotatedClass(Class<?> type) {
32+
public List<Exception> validateAnnotatedClass(TestClass testClass) {
3133
return NO_VALIDATION_ERRORS;
3234
}
3335

@@ -39,7 +41,7 @@ public List<Exception> validateAnnotatedClass(Class<?> type) {
3941
*
4042
* @since 4.12
4143
*/
42-
public List<Exception> validateAnnotatedField(Field field) {
44+
public List<Exception> validateAnnotatedField(FrameworkField field) {
4345
return NO_VALIDATION_ERRORS;
4446

4547
}
@@ -52,7 +54,7 @@ public List<Exception> validateAnnotatedField(Field field) {
5254
*
5355
* @since 4.12
5456
*/
55-
public List<Exception> validateAnnotatedMethod(Method method) {
57+
public List<Exception> validateAnnotatedMethod(FrameworkMethod method) {
5658
return NO_VALIDATION_ERRORS;
5759
}
5860
}

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

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.junit.tests.experimental.categories;
22

3-
43
import static org.hamcrest.CoreMatchers.is;
54
import static org.junit.Assert.assertThat;
65

7-
import java.lang.reflect.Method;
86
import java.util.List;
97

108
import org.junit.After;
@@ -14,6 +12,8 @@
1412
import org.junit.Test;
1513
import org.junit.experimental.categories.Category;
1614
import org.junit.experimental.categories.CategoryValidator;
15+
import org.junit.runners.model.FrameworkMethod;
16+
import org.junit.runners.model.TestClass;
1717

1818
public class CategoryValidatorTest {
1919

@@ -47,30 +47,30 @@ public static void methodWithCategory() {
4747
}
4848

4949
@Test
50-
public void errorIsAddedWhenCategoryIsUsedWithBeforeClass() throws NoSuchMethodException {
51-
Method method = CategoryTest.class.getMethod("methodWithCategoryAndBeforeClass");
52-
testAndAssertErrrorMessage(method, "@BeforeClass can not be combined with @Category");
50+
public void errorIsAddedWhenCategoryIsUsedWithBeforeClass() {
51+
FrameworkMethod method = new TestClass(CategoryTest.class).getAnnotatedMethods(BeforeClass.class).get(0);
52+
testAndAssertErrorMessage(method, "@BeforeClass can not be combined with @Category");
5353
}
5454

5555
@Test
56-
public void errorIsAddedWhenCategoryIsUsedWithAfterClass() throws NoSuchMethodException {
57-
Method method = CategoryTest.class.getMethod("methodWithCategoryAndAfterClass");
58-
testAndAssertErrrorMessage(method, "@AfterClass can not be combined with @Category");
56+
public void errorIsAddedWhenCategoryIsUsedWithAfterClass() {
57+
FrameworkMethod method = new TestClass(CategoryTest.class).getAnnotatedMethods(AfterClass.class).get(0);
58+
testAndAssertErrorMessage(method, "@AfterClass can not be combined with @Category");
5959
}
6060

6161
@Test
62-
public void errorIsAddedWhenCategoryIsUsedWithBefore() throws NoSuchMethodException {
63-
Method method = CategoryTest.class.getMethod("methodWithCategoryAndBefore");
64-
testAndAssertErrrorMessage(method, "@Before can not be combined with @Category");
62+
public void errorIsAddedWhenCategoryIsUsedWithBefore() {
63+
FrameworkMethod method = new TestClass(CategoryTest.class).getAnnotatedMethods(Before.class).get(0);
64+
testAndAssertErrorMessage(method, "@Before can not be combined with @Category");
6565
}
6666

6767
@Test
68-
public void errorIsAddedWhenCategoryIsUsedWithAfter() throws NoSuchMethodException {
69-
Method method = CategoryTest.class.getMethod("methodWithCategoryAndAfter");
70-
testAndAssertErrrorMessage(method, "@After can not be combined with @Category");
68+
public void errorIsAddedWhenCategoryIsUsedWithAfter() {
69+
FrameworkMethod method = new TestClass(CategoryTest.class).getAnnotatedMethods(After.class).get(0);
70+
testAndAssertErrorMessage(method, "@After can not be combined with @Category");
7171
}
7272

73-
private void testAndAssertErrrorMessage(Method method, String expectedErrorMessage) throws NoSuchMethodException {
73+
private void testAndAssertErrorMessage(FrameworkMethod method, String expectedErrorMessage) {
7474
List<Exception> errors = new CategoryValidator().validateAnnotatedMethod(method);
7575

7676
assertThat(errors.size(), is(1));
@@ -80,8 +80,8 @@ private void testAndAssertErrrorMessage(Method method, String expectedErrorMessa
8080

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

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

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.hamcrest.Matcher;
44
import org.hamcrest.TypeSafeMatcher;
55
import org.junit.Test;
6+
import org.junit.runners.model.FrameworkField;
7+
import org.junit.runners.model.FrameworkMethod;
8+
import org.junit.runners.model.TestClass;
69
import org.junit.validator.AnnotationValidator;
710
import org.junit.validator.ValidateWith;
811
import org.junit.runner.Description;
@@ -22,8 +25,6 @@
2225
import java.lang.annotation.Inherited;
2326
import java.lang.annotation.Retention;
2427
import java.lang.annotation.RetentionPolicy;
25-
import java.lang.reflect.Field;
26-
import java.lang.reflect.Method;
2728
import java.util.ArrayList;
2829
import java.util.List;
2930

@@ -154,17 +155,17 @@ public static class ExampleAnnotationValidator extends AnnotationValidator {
154155
private static final String ANNOTATED_CLASS_CALLED = "annotated class called";
155156

156157
@Override
157-
public List<Exception> validateAnnotatedClass(Class<?> type) {
158+
public List<Exception> validateAnnotatedClass(TestClass testClass) {
158159
return asList(new Exception(ANNOTATED_CLASS_CALLED));
159160
}
160161

161162
@Override
162-
public List<Exception> validateAnnotatedField(Field field) {
163+
public List<Exception> validateAnnotatedField(FrameworkField field) {
163164
return asList(new Exception(ANNOTATED_FIELD_CALLED));
164165
}
165166

166167
@Override
167-
public List<Exception> validateAnnotatedMethod(Method method) {
168+
public List<Exception> validateAnnotatedMethod(FrameworkMethod method) {
168169
return asList(new Exception(ANNOTATED_METHOD_CALLED));
169170
}
170171
}

0 commit comments

Comments
 (0)