Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ private AttributeMethods(@Nullable Class<? extends Annotation> annotationType, M
foundNestedAnnotation = true;
}
ReflectionUtils.makeAccessible(method);
this.canThrowTypeNotPresentException[i] = (type == Class.class || type == Class[].class || type.isEnum());
this.canThrowTypeNotPresentException[i] = (type == Class.class || type == Class[].class ||
type.isEnum() || (type.isArray() && type.componentType().isEnum()));
}
this.hasDefaultValueMethod = foundDefaultValueMethod;
this.hasNestedAnnotation = foundNestedAnnotation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ void canThrowTypeNotPresentExceptionWhenHasClassArrayAttributeReturnsTrue() {
assertThat(methods.canThrowTypeNotPresentException(0)).isTrue();
}

@Test
void canThrowTypeNotPresentExceptionWhenHasEnumArrayAttributeReturnsTrue() {
AttributeMethods methods = AttributeMethods.forAnnotationType(EnumArrayValue.class);
assertThat(methods.canThrowTypeNotPresentException(0)).isTrue();
}

@Test
void canThrowTypeNotPresentExceptionWhenHasNonEnumArrayAttributeReturnsFalse() {
AttributeMethods methods = AttributeMethods.forAnnotationType(StringArrayValue.class);
assertThat(methods.canThrowTypeNotPresentException(0)).isFalse();
}

@Test
void canThrowTypeNotPresentExceptionWhenNotClassOrClassArrayAttributeReturnsFalse() {
AttributeMethods methods = AttributeMethods.forAnnotationType(ValueOnly.class);
Expand Down Expand Up @@ -141,6 +153,38 @@ void validateWhenDoesNotHaveTypeNotPresentExceptionThrowsNothing() {
attributes.validate(annotation);
}

@Test
void isValidWhenHasEnumConstantNotPresentExceptionReturnsFalse() {
EnumArrayValue annotation = mockAnnotation(EnumArrayValue.class);
given(annotation.value()).willThrow(new EnumConstantNotPresentException(ExampleEnum.class, "MISSING"));
AttributeMethods attributes = AttributeMethods.forAnnotationType(annotation.annotationType());
assertThat(attributes.canLoad(annotation, getClass())).isFalse();
}

@Test
void isValidWhenDoesNotHaveEnumConstantNotPresentExceptionReturnsTrue() {
EnumArrayValue annotation = mockAnnotation(EnumArrayValue.class);
given(annotation.value()).willReturn(new ExampleEnum[] {ExampleEnum.ONE});
AttributeMethods attributes = AttributeMethods.forAnnotationType(annotation.annotationType());
assertThat(attributes.canLoad(annotation, getClass())).isTrue();
}

@Test
void validateWhenHasEnumConstantNotPresentExceptionThrowsException() {
EnumArrayValue annotation = mockAnnotation(EnumArrayValue.class);
given(annotation.value()).willThrow(new EnumConstantNotPresentException(ExampleEnum.class, "MISSING"));
AttributeMethods attributes = AttributeMethods.forAnnotationType(annotation.annotationType());
assertThatIllegalStateException().isThrownBy(() -> attributes.validate(annotation));
}

@Test
void validateWhenDoesNotHaveEnumConstantNotPresentExceptionThrowsNothing() {
EnumArrayValue annotation = mockAnnotation(EnumArrayValue.class);
given(annotation.value()).willReturn(new ExampleEnum[] {ExampleEnum.ONE});
AttributeMethods attributes = AttributeMethods.forAnnotationType(annotation.annotationType());
attributes.validate(annotation);
}

private List<Method> getAll(AttributeMethods attributes) {
List<Method> result = new ArrayList<>(attributes.size());
for (int i = 0; i < attributes.size(); i++) {
Expand Down Expand Up @@ -208,4 +252,24 @@ private <A extends Annotation> A mockAnnotation(Class<A> annotationType) {

}

@Retention(RetentionPolicy.RUNTIME)
@interface EnumArrayValue {

ExampleEnum[] value();

}

@Retention(RetentionPolicy.RUNTIME)
@interface StringArrayValue {

String[] value();

}

enum ExampleEnum {

ONE

}

}