Skip to content

Commit

Permalink
Merge pull request #1149 from wang3820/fix-flaky
Browse files Browse the repository at this point in the history
[Test] Refactored test to use utility function to get annotation instead of array indexing
  • Loading branch information
lprimak authored Nov 3, 2023
2 parents f886f2b + e09c403 commit 9888e87
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.apache.shiro.cdi;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -29,7 +31,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -82,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();
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, true,
Set.of(SessionScopedAnnotated.class.getDeclaredAnnotations()[0]),
Set.of(Annotated.class.getDeclaredAnnotations()[1]));
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 @@ -130,15 +126,14 @@ 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());
}

Expand All @@ -154,4 +149,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 9888e87

Please sign in to comment.