From dc390a8f4fb49525bce22b8df1b274664df62c47 Mon Sep 17 00:00:00 2001 From: wang3820 Date: Thu, 2 Nov 2023 18:42:05 -0500 Subject: [PATCH] Refactored all tests to retrieve annotations by utility function instead of array indexing --- .../shiro/cdi/AnnotatedTypeWrapperTest.java | 52 +++++++++---------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/support/cdi/src/test/java/org/apache/shiro/cdi/AnnotatedTypeWrapperTest.java b/support/cdi/src/test/java/org/apache/shiro/cdi/AnnotatedTypeWrapperTest.java index 1dd56fce34..011f110eeb 100644 --- a/support/cdi/src/test/java/org/apache/shiro/cdi/AnnotatedTypeWrapperTest.java +++ b/support/cdi/src/test/java/org/apache/shiro/cdi/AnnotatedTypeWrapperTest.java @@ -66,12 +66,6 @@ private final class StatelessAnnotated { private static final class SessionScopedAnnotated implements Serializable { } - private Set 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); @@ -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 sessionScopeAnnoations = getAnnotation(SessionScopedAnnotated.class, SessionScoped.class); - Set requiresGuestAnnoations = getAnnotation(Annotated.class, RequiresGuest.class); - var wrapper = new AnnotatedTypeWrapper<>(annotatedType, true, sessionScopeAnnoations, requiresGuestAnnoations); + Set sessionScopeAnnoationsSet = Set.of(getAnnotation(SessionScopedAnnotated.class, SessionScoped.class)); + Set 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)); @@ -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(); @@ -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); + } }