aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java
blob: 23f921b118666e0dc997d1b7471488640d5a723d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.junit.internal.runners.rules;

import java.lang.annotation.Annotation;
import java.util.List;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.TestClass;

/**
 * A RuleFieldValidator validates the rule fields of a
 * {@link TestClass}. All reasons for rejecting the
 * {@code TestClass} are written to a list of errors.
 *
 * There are two slightly different validators. The {@link #CLASS_RULE_VALIDATOR}
 * validates fields with a {@link ClassRule} annotation and the
 * {@link #RULE_VALIDATOR} validates fields with a {@link Rule} annotation.
 */
public enum RuleMemberValidator {
	/**
	 * Validates fields with a {@link ClassRule} annotation.
	 */
	CLASS_RULE_VALIDATOR(ClassRule.class, true),
	/**
	 * Validates fields with a {@link Rule} annotation.
	 */
	RULE_VALIDATOR(Rule.class, false);

	private final Class<? extends Annotation> fAnnotation;

	private final boolean fOnlyStaticFields;

	private RuleMemberValidator(Class<? extends Annotation> annotation,
			boolean onlyStaticFields) {
		this.fAnnotation= annotation;
		this.fOnlyStaticFields= onlyStaticFields;
	}

	/**
	 * Validate the {@link TestClass} and adds reasons
	 * for rejecting the class to a list of errors.
	 * @param target the {@code TestClass} to validate.
	 * @param errors the list of errors.
	 */
	public void validate(TestClass target, List<Throwable> errors) {
		List<FrameworkField> fields= target.getAnnotatedFields(fAnnotation);
		for (FrameworkField each : fields)
			validateField(each, errors);
	}

	private void validateField(FrameworkField field, List<Throwable> errors) {
		optionallyValidateStatic(field, errors);
		validatePublic(field, errors);
		validateTestRuleOrMethodRule(field, errors);
	}

	private void optionallyValidateStatic(FrameworkField field,
			List<Throwable> errors) {
		if (fOnlyStaticFields && !field.isStatic())
			addError(errors, field, "must be static.");
	}

	private void validatePublic(FrameworkField field, List<Throwable> errors) {
		if (!field.isPublic())
			addError(errors, field, "must be public.");
	}

	private void validateTestRuleOrMethodRule(FrameworkField field,
			List<Throwable> errors) {
		if (!isMethodRule(field) && !isTestRule(field))
			addError(errors, field, "must implement MethodRule or TestRule.");
	}

	private boolean isTestRule(FrameworkField target) {
		return TestRule.class.isAssignableFrom(target.getType());
	}

	@SuppressWarnings("deprecation")
	private boolean isMethodRule(FrameworkField target) {
		return org.junit.rules.MethodRule.class.isAssignableFrom(target
				.getType());
	}

	private void addError(List<Throwable> errors, FrameworkField field,
			String suffix) {
		String message= "The @" + fAnnotation.getSimpleName() + " '"
				+ field.getName() + "' " + suffix;
		errors.add(new Exception(message));
	}
}