Skip to content

Commit dc390a8

Browse files
committed
Refactored all tests to retrieve annotations by utility function instead of array indexing
1 parent 5954466 commit dc390a8

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

support/cdi/src/test/java/org/apache/shiro/cdi/AnnotatedTypeWrapperTest.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ private final class StatelessAnnotated {
6666
private static final class SessionScopedAnnotated implements Serializable {
6767
}
6868

69-
private Set<Annotation> getAnnotation(Class<?> annotatedClass, Class<?> annotation) {
70-
return Arrays.stream(annotatedClass.getDeclaredAnnotations())
71-
.filter(a -> a.annotationType().equals(annotation))
72-
.collect(Collectors.toSet());
73-
}
74-
7569
@Test
7670
void noAnnotations() {
7771
var wrapper = new AnnotatedTypeWrapper<>(annotatedType);
@@ -89,28 +83,23 @@ void noAdditionalAnnotations() {
8983
@SuppressWarnings("MagicNumber")
9084
void twoAdditionalAnnotations() {
9185
initializeStubs();
92-
var wrapper = new AnnotatedTypeWrapper<>(annotatedType,
93-
ShiroSecureAnnotated.class.getDeclaredAnnotations()[0],
94-
StatelessAnnotated.class.getDeclaredAnnotations()[0]);
86+
Annotation shiroSecureAnnotation = getAnnotation(ShiroSecureAnnotated.class, ShiroSecureAnnotation.class);
87+
Annotation statelessAnnotation = getAnnotation(StatelessAnnotated.class, Stateless.class);
88+
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, shiroSecureAnnotation, statelessAnnotation);
9589
assertEquals(5, wrapper.getAnnotations().size());
96-
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotated.class
97-
.getDeclaredAnnotations()[0].annotationType()));
98-
assertTrue(wrapper.isAnnotationPresent(StatelessAnnotated.class
99-
.getDeclaredAnnotations()[0].annotationType()));
100-
assertTrue(wrapper.isAnnotationPresent(Annotated.class
101-
.getDeclaredAnnotations()[0].annotationType()));
102-
assertTrue(wrapper.isAnnotationPresent(Annotated.class
103-
.getDeclaredAnnotations()[1].annotationType()));
104-
assertTrue(wrapper.isAnnotationPresent(Annotated.class
105-
.getDeclaredAnnotations()[2].annotationType()));
90+
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotation.class));
91+
assertTrue(wrapper.isAnnotationPresent(Stateless.class));
92+
assertTrue(wrapper.isAnnotationPresent(RequiresAuthentication.class));
93+
assertTrue(wrapper.isAnnotationPresent(RequiresGuest.class));
94+
assertTrue(wrapper.isAnnotationPresent(RequiresPermissions.class));
10695
}
10796

10897
@Test
10998
void removeAnnotations() {
11099
initializeStubs();
111-
Set<Annotation> sessionScopeAnnoations = getAnnotation(SessionScopedAnnotated.class, SessionScoped.class);
112-
Set<Annotation> requiresGuestAnnoations = getAnnotation(Annotated.class, RequiresGuest.class);
113-
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, true, sessionScopeAnnoations, requiresGuestAnnoations);
100+
Set<Annotation> sessionScopeAnnoationsSet = Set.of(getAnnotation(SessionScopedAnnotated.class, SessionScoped.class));
101+
Set<Annotation> requiresGuestAnnoationsSet = Set.of(getAnnotation(Annotated.class, RequiresGuest.class));
102+
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, true, sessionScopeAnnoationsSet, requiresGuestAnnoationsSet);
114103
assertEquals(3, wrapper.getAnnotations().size());
115104
assertFalse(wrapper.isAnnotationPresent(RequiresGuest.class));
116105
assertTrue(wrapper.isAnnotationPresent(SessionScoped.class));
@@ -137,18 +126,18 @@ void overriddenAnnotation() {
137126
initializeStubs();
138127
when(annotatedType.getJavaClass()).thenReturn(Void.class);
139128
assertEquals(3, annotatedType.getAnnotations().size());
129+
Annotation shiroSecureAnnoations = getAnnotation(ShiroSecureAnnotated.class, ShiroSecureAnnotation.class);
130+
Annotation statelessAnnoations = getAnnotation(StatelessAnnotated.class, Stateless.class);
140131
var wrapper = new AnnotatedTypeWrapper<>(annotatedType, false,
141-
Set.of(ShiroSecureAnnotated.class.getDeclaredAnnotations()[0],
142-
StatelessAnnotated.class.getDeclaredAnnotations()[0]),
132+
Set.of(shiroSecureAnnoations, statelessAnnoations),
143133
Set.of());
144134
assertEquals(2, wrapper.getAnnotations().size());
145-
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotated.class
146-
.getDeclaredAnnotations()[0].annotationType()));
147-
assertTrue(wrapper.isAnnotationPresent(StatelessAnnotated.class
148-
.getDeclaredAnnotations()[0].annotationType()));
135+
assertTrue(wrapper.isAnnotationPresent(ShiroSecureAnnotation.class));
136+
assertTrue(wrapper.isAnnotationPresent(Stateless.class));
149137
assertEquals(Void.class, wrapper.getJavaClass());
150138
}
151139

140+
152141
@Test
153142
void decreaseAnnotationsToZero() {
154143
initializeStubs();
@@ -161,4 +150,11 @@ private void initializeStubs() {
161150
when(annotatedType.getAnnotations()).thenReturn(Stream.of(Annotated.class.getDeclaredAnnotations())
162151
.collect(Collectors.toSet()));
163152
}
153+
154+
private Annotation getAnnotation(Class<?> annotatedClass, Class<?> annotation) {
155+
return Arrays.stream(annotatedClass.getDeclaredAnnotations())
156+
.filter(a -> a.annotationType().equals(annotation))
157+
.findFirst()
158+
.orElse(null);
159+
}
164160
}

0 commit comments

Comments
 (0)