Skip to content

Commit

Permalink
Refactored all tests to retrieve annotations by utility function inst…
Browse files Browse the repository at this point in the history
…ead of array indexing
  • Loading branch information
wang3820 committed Nov 2, 2023
1 parent 5954466 commit dc390a8
Showing 1 changed file with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ private final class StatelessAnnotated {
private static final class SessionScopedAnnotated implements Serializable {
}

private Set<Annotation> getAnnotation(Class<?> annotatedClass, Class<?> annotation) {
return Arrays.stream(annotatedClass.getDeclaredAnnotations())
.filter(a -> a.annotationType().equals(annotation))
.collect(Collectors.toSet());
}

@Test
void noAnnotations() {
var wrapper = new AnnotatedTypeWrapper<>(annotatedType);
Expand All @@ -89,28 +83,23 @@ void noAdditionalAnnotations() {
@SuppressWarnings("MagicNumber")
void twoAdditionalAnnotations() {
initializeStubs();
var wrapper = new AnnotatedTypeWrapper<>(annotatedType,
ShiroSecureAnnotated.class.getDeclaredAnnotations()[0],
StatelessAnnotated.class.getDeclaredAnnotations()[0]);
Annotation shiroSecureAnnotation = getAnnotation(ShiroSecureAnnotated.class, ShiroSecureAnnotation.class);
Annotation statelessAnnotation = getAnnotation(StatelessAnnotated.class, Stateless.class);
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, shiroSecureAnnotation, statelessAnnotation);
assertEquals(5, wrapper.getAnnotations().size());
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotated.class
.getDeclaredAnnotations()[0].annotationType()));
assertTrue(wrapper.isAnnotationPresent(StatelessAnnotated.class
.getDeclaredAnnotations()[0].annotationType()));
assertTrue(wrapper.isAnnotationPresent(Annotated.class
.getDeclaredAnnotations()[0].annotationType()));
assertTrue(wrapper.isAnnotationPresent(Annotated.class
.getDeclaredAnnotations()[1].annotationType()));
assertTrue(wrapper.isAnnotationPresent(Annotated.class
.getDeclaredAnnotations()[2].annotationType()));
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotation.class));
assertTrue(wrapper.isAnnotationPresent(Stateless.class));
assertTrue(wrapper.isAnnotationPresent(RequiresAuthentication.class));
assertTrue(wrapper.isAnnotationPresent(RequiresGuest.class));
assertTrue(wrapper.isAnnotationPresent(RequiresPermissions.class));
}

@Test
void removeAnnotations() {
initializeStubs();
Set<Annotation> sessionScopeAnnoations = getAnnotation(SessionScopedAnnotated.class, SessionScoped.class);
Set<Annotation> requiresGuestAnnoations = getAnnotation(Annotated.class, RequiresGuest.class);
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, true, sessionScopeAnnoations, requiresGuestAnnoations);
Set<Annotation> sessionScopeAnnoationsSet = Set.of(getAnnotation(SessionScopedAnnotated.class, SessionScoped.class));
Set<Annotation> requiresGuestAnnoationsSet = Set.of(getAnnotation(Annotated.class, RequiresGuest.class));
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, true, sessionScopeAnnoationsSet, requiresGuestAnnoationsSet);
assertEquals(3, wrapper.getAnnotations().size());
assertFalse(wrapper.isAnnotationPresent(RequiresGuest.class));
assertTrue(wrapper.isAnnotationPresent(SessionScoped.class));
Expand All @@ -137,18 +126,18 @@ void overriddenAnnotation() {
initializeStubs();
when(annotatedType.getJavaClass()).thenReturn(Void.class);
assertEquals(3, annotatedType.getAnnotations().size());
Annotation shiroSecureAnnoations = getAnnotation(ShiroSecureAnnotated.class, ShiroSecureAnnotation.class);
Annotation statelessAnnoations = getAnnotation(StatelessAnnotated.class, Stateless.class);
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, false,
Set.of(ShiroSecureAnnotated.class.getDeclaredAnnotations()[0],
StatelessAnnotated.class.getDeclaredAnnotations()[0]),
Set.of(shiroSecureAnnoations, statelessAnnoations),
Set.of());
assertEquals(2, wrapper.getAnnotations().size());
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotated.class
.getDeclaredAnnotations()[0].annotationType()));
assertTrue(wrapper.isAnnotationPresent(StatelessAnnotated.class
.getDeclaredAnnotations()[0].annotationType()));
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotation.class));
assertTrue(wrapper.isAnnotationPresent(Stateless.class));
assertEquals(Void.class, wrapper.getJavaClass());
}


@Test
void decreaseAnnotationsToZero() {
initializeStubs();
Expand All @@ -161,4 +150,11 @@ private void initializeStubs() {
when(annotatedType.getAnnotations()).thenReturn(Stream.of(Annotated.class.getDeclaredAnnotations())
.collect(Collectors.toSet()));
}

private Annotation getAnnotation(Class<?> annotatedClass, Class<?> annotation) {
return Arrays.stream(annotatedClass.getDeclaredAnnotations())
.filter(a -> a.annotationType().equals(annotation))
.findFirst()
.orElse(null);
}
}

0 comments on commit dc390a8

Please sign in to comment.