diff options
author | Paul Duffin <paulduffin@google.com> | 2016-12-16 15:11:13 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2016-12-19 14:41:11 +0000 |
commit | f3aca4db137104480c0501f29d459f64b1f3f618 (patch) | |
tree | 7c1eb8e6d4cad179f72f11bf1334d2fcd51536af /src/main/java/org/junit/internal | |
parent | 8f7a4bf1039370c7810bcfb287f6e0a6224f8063 (diff) | |
download | junit-f3aca4db137104480c0501f29d459f64b1f3f618.tar.gz |
Minor changes to JUnit 4.10 to make it more like 4.12
Some other projects break when built against 4.12. This allows
those projects to be updated separately to the JUnit 4.12
upgrade.
Assert.java and Description.java are copied directly from
upstream 4.12; that brings in new methods from 4.12. The changes
are so extensive because the upstream code was reformatted
between 4.10 anbd 4.12.
Between 4.10 and 4.12 RuleFieldValidator.java was renamed to
RuleMemberValidator.java. To allow existing code that uses
RuleFieldValidator to be modified to use RuleMemberValidator
instead RuleFieldValidator.java has been copied to
RuleMemberValidator.java.
The change to BlockJUnit4ClassRunner reflects an upstream change
made between 4.10 and 4.12.
Bug: 33613916
Test: make checkbuild
Change-Id: Id8ee6d08268152ec1a22a309c4582f8f6663ae88
Diffstat (limited to 'src/main/java/org/junit/internal')
-rw-r--r-- | src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java b/src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java new file mode 100644 index 0000000..23f921b --- /dev/null +++ b/src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java @@ -0,0 +1,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)); + } +} |