@@ -66,12 +66,6 @@ private final class StatelessAnnotated {
66
66
private static final class SessionScopedAnnotated implements Serializable {
67
67
}
68
68
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
-
75
69
@ Test
76
70
void noAnnotations () {
77
71
var wrapper = new AnnotatedTypeWrapper <>(annotatedType );
@@ -89,28 +83,23 @@ void noAdditionalAnnotations() {
89
83
@ SuppressWarnings ("MagicNumber" )
90
84
void twoAdditionalAnnotations () {
91
85
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 );
95
89
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 ));
106
95
}
107
96
108
97
@ Test
109
98
void removeAnnotations () {
110
99
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 );
114
103
assertEquals (3 , wrapper .getAnnotations ().size ());
115
104
assertFalse (wrapper .isAnnotationPresent (RequiresGuest .class ));
116
105
assertTrue (wrapper .isAnnotationPresent (SessionScoped .class ));
@@ -137,18 +126,18 @@ void overriddenAnnotation() {
137
126
initializeStubs ();
138
127
when (annotatedType .getJavaClass ()).thenReturn (Void .class );
139
128
assertEquals (3 , annotatedType .getAnnotations ().size ());
129
+ Annotation shiroSecureAnnoations = getAnnotation (ShiroSecureAnnotated .class , ShiroSecureAnnotation .class );
130
+ Annotation statelessAnnoations = getAnnotation (StatelessAnnotated .class , Stateless .class );
140
131
var wrapper = new AnnotatedTypeWrapper <>(annotatedType , false ,
141
- Set .of (ShiroSecureAnnotated .class .getDeclaredAnnotations ()[0 ],
142
- StatelessAnnotated .class .getDeclaredAnnotations ()[0 ]),
132
+ Set .of (shiroSecureAnnoations , statelessAnnoations ),
143
133
Set .of ());
144
134
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 ));
149
137
assertEquals (Void .class , wrapper .getJavaClass ());
150
138
}
151
139
140
+
152
141
@ Test
153
142
void decreaseAnnotationsToZero () {
154
143
initializeStubs ();
@@ -161,4 +150,11 @@ private void initializeStubs() {
161
150
when (annotatedType .getAnnotations ()).thenReturn (Stream .of (Annotated .class .getDeclaredAnnotations ())
162
151
.collect (Collectors .toSet ()));
163
152
}
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
+ }
164
160
}
0 commit comments