Skip to content

Commit

Permalink
Make @ValidateWith only applicable to annotation types (#1612)
Browse files Browse the repository at this point in the history
Add `@Target(ANNOTATION_TYPE)` to `@ValidateWith` since it's
only designed to be applied to another annotation.

Fixes #1611.
  • Loading branch information
panchenko authored and marcphilipp committed Oct 18, 2019
1 parent f727ecf commit 7065f37
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/junit/validator/ValidateWith.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.junit.validator;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Allows for an {@link AnnotationValidator} to be attached to an annotation.
Expand All @@ -13,6 +15,7 @@
* @since 4.12
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@Inherited
public @interface ValidateWith {
Class<? extends AnnotationValidator> value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ public class AnnotationValidatorFactoryTest {

@Test
public void createAnnotationValidator() {
ValidateWith validateWith = SampleTestWithValidator.class.getAnnotation(ValidateWith.class);
ValidateWith validateWith = SampleAnnotationWithValidator.class.getAnnotation(ValidateWith.class);
AnnotationValidator annotationValidator = new AnnotationValidatorFactory().createAnnotationValidator(validateWith);
assertThat(annotationValidator, is(instanceOf(Validator.class)));
}

@ValidateWith(value = Validator.class)
public static class SampleTestWithValidator {
public @interface SampleAnnotationWithValidator {
}

public static class Validator extends AnnotationValidator {
}

@Test
public void exceptionWhenAnnotationValidatorCantBeCreated() {
ValidateWith validateWith = SampleTestWithValidatorThatThrowsException.class.getAnnotation(ValidateWith.class);
ValidateWith validateWith = SampleAnnotationWithValidatorThatThrowsException.class.getAnnotation(ValidateWith.class);
exception.expect(RuntimeException.class);
exception.expectMessage("Exception received when creating AnnotationValidator class " +
"org.junit.validator.AnnotationValidatorFactoryTest$ValidatorThatThrowsException");
new AnnotationValidatorFactory().createAnnotationValidator(validateWith);
}

@ValidateWith(value = ValidatorThatThrowsException.class)
public static class SampleTestWithValidatorThatThrowsException {
public @interface SampleAnnotationWithValidatorThatThrowsException {
}

public static class ValidatorThatThrowsException extends AnnotationValidator {
Expand Down

0 comments on commit 7065f37

Please sign in to comment.