Skip to content

Commit a4fa6a7

Browse files
committed
Test status quo for @inherited annotation support in AnnotationMetadata
1 parent 3b9d1a0 commit a4fa6a7

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @author Chris Beams
4949
* @author Phillip Webb
5050
* @author Sam Brannen
51+
* @see InheritedAnnotationsAnnotationMetadataTests
5152
*/
5253
class AnnotationMetadataTests {
5354

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.type;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Inherited;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
import java.util.Map;
25+
import java.util.Set;
26+
27+
import org.junit.jupiter.api.Test;
28+
29+
import org.springframework.core.type.classreading.MetadataReader;
30+
import org.springframework.core.type.classreading.MetadataReaderFactory;
31+
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
32+
import org.springframework.util.MultiValueMap;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Unit tests demonstrating that the reflection-based {@link StandardAnnotationMetadata}
38+
* supports {@link Inherited @Inherited} annotations; whereas, the ASM-based
39+
* {@code SimpleAnnotationMetadata} does not.
40+
*
41+
* @author Sam Brannen
42+
* @since 5.2.3
43+
* @see AnnotationMetadataTests
44+
*/
45+
class InheritedAnnotationsAnnotationMetadataTests {
46+
47+
private final AnnotationMetadata standardMetadata = AnnotationMetadata.introspect(AnnotatedSubclass.class);
48+
49+
private final AnnotationMetadata asmMetadata;
50+
51+
52+
InheritedAnnotationsAnnotationMetadataTests() throws Exception {
53+
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
54+
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedSubclass.class.getName());
55+
this.asmMetadata = metadataReader.getAnnotationMetadata();
56+
}
57+
58+
@Test
59+
void getAnnotationTypes() {
60+
assertThat(standardMetadata.getAnnotationTypes()).containsExactlyInAnyOrder(
61+
NamedAnnotation3.class.getName(),
62+
InheritedComposedAnnotation.class.getName());
63+
64+
assertThat(asmMetadata.getAnnotationTypes()).containsExactly(
65+
NamedAnnotation3.class.getName());
66+
}
67+
68+
@Test
69+
void hasAnnotation() {
70+
assertThat(standardMetadata.hasAnnotation(InheritedComposedAnnotation.class.getName())).isTrue();
71+
assertThat(standardMetadata.hasAnnotation(NamedAnnotation3.class.getName())).isTrue();
72+
73+
// true because @NamedAnnotation3 is also directly present
74+
assertThat(asmMetadata.hasAnnotation(NamedAnnotation3.class.getName())).isTrue();
75+
76+
assertThat(asmMetadata.hasAnnotation(InheritedComposedAnnotation.class.getName())).isFalse();
77+
}
78+
79+
@Test
80+
void getMetaAnnotationTypes() {
81+
Set<String> metaAnnotationTypes;
82+
83+
metaAnnotationTypes = standardMetadata.getMetaAnnotationTypes(InheritedComposedAnnotation.class.getName());
84+
assertThat(metaAnnotationTypes).containsExactlyInAnyOrder(
85+
MetaAnnotation.class.getName(),
86+
NamedAnnotation1.class.getName(),
87+
NamedAnnotation2.class.getName(),
88+
NamedAnnotation3.class.getName());
89+
90+
metaAnnotationTypes = asmMetadata.getMetaAnnotationTypes(InheritedComposedAnnotation.class.getName());
91+
assertThat(metaAnnotationTypes).isEmpty();
92+
}
93+
94+
@Test
95+
void hasMetaAnnotation() {
96+
assertThat(standardMetadata.hasMetaAnnotation(NamedAnnotation1.class.getName())).isTrue();
97+
assertThat(standardMetadata.hasMetaAnnotation(NamedAnnotation2.class.getName())).isTrue();
98+
assertThat(standardMetadata.hasMetaAnnotation(NamedAnnotation3.class.getName())).isTrue();
99+
assertThat(standardMetadata.hasMetaAnnotation(MetaAnnotation.class.getName())).isTrue();
100+
101+
assertThat(asmMetadata.hasMetaAnnotation(NamedAnnotation1.class.getName())).isFalse();
102+
assertThat(asmMetadata.hasMetaAnnotation(NamedAnnotation2.class.getName())).isFalse();
103+
assertThat(asmMetadata.hasMetaAnnotation(NamedAnnotation3.class.getName())).isFalse();
104+
assertThat(asmMetadata.hasMetaAnnotation(MetaAnnotation.class.getName())).isFalse();
105+
}
106+
107+
@Test
108+
void isAnnotated() {
109+
assertThat(standardMetadata.isAnnotated(InheritedComposedAnnotation.class.getName())).isTrue();
110+
assertThat(standardMetadata.isAnnotated(NamedAnnotation1.class.getName())).isTrue();
111+
assertThat(standardMetadata.isAnnotated(NamedAnnotation2.class.getName())).isTrue();
112+
assertThat(standardMetadata.isAnnotated(NamedAnnotation3.class.getName())).isTrue();
113+
assertThat(standardMetadata.isAnnotated(MetaAnnotation.class.getName())).isTrue();
114+
115+
// true because @NamedAnnotation3 is also directly present
116+
assertThat(asmMetadata.isAnnotated(NamedAnnotation3.class.getName())).isTrue();
117+
118+
assertThat(asmMetadata.isAnnotated(InheritedComposedAnnotation.class.getName())).isFalse();
119+
assertThat(asmMetadata.isAnnotated(NamedAnnotation1.class.getName())).isFalse();
120+
assertThat(asmMetadata.isAnnotated(NamedAnnotation2.class.getName())).isFalse();
121+
assertThat(asmMetadata.isAnnotated(MetaAnnotation.class.getName())).isFalse();
122+
}
123+
124+
@Test
125+
void getAnnotationAttributes() {
126+
Map<String, Object> annotationAttributes;
127+
128+
annotationAttributes = standardMetadata.getAnnotationAttributes(NamedAnnotation1.class.getName());
129+
assertThat(annotationAttributes.get("name")).isEqualTo("name 1");
130+
131+
annotationAttributes = asmMetadata.getAnnotationAttributes(NamedAnnotation1.class.getName());
132+
assertThat(annotationAttributes).isNull();
133+
}
134+
135+
@Test
136+
void getAllAnnotationAttributes() {
137+
MultiValueMap<String, Object> annotationAttributes;
138+
139+
annotationAttributes = standardMetadata.getAllAnnotationAttributes(NamedAnnotation3.class.getName());
140+
assertThat(annotationAttributes).containsKey("name");
141+
assertThat(annotationAttributes.get("name")).containsExactlyInAnyOrder("name 3", "local");
142+
143+
annotationAttributes = asmMetadata.getAllAnnotationAttributes(NamedAnnotation1.class.getName());
144+
assertThat(annotationAttributes).isNull();
145+
}
146+
147+
148+
@Retention(RetentionPolicy.RUNTIME)
149+
@Target(ElementType.ANNOTATION_TYPE)
150+
@interface MetaAnnotation {
151+
}
152+
153+
@MetaAnnotation
154+
@Retention(RetentionPolicy.RUNTIME)
155+
@interface NamedAnnotation1 {
156+
157+
String name() default "";
158+
}
159+
160+
@Retention(RetentionPolicy.RUNTIME)
161+
@interface NamedAnnotation2 {
162+
163+
String name() default "";
164+
}
165+
166+
@Retention(RetentionPolicy.RUNTIME)
167+
@interface NamedAnnotation3 {
168+
169+
String name() default "";
170+
}
171+
172+
@Retention(RetentionPolicy.RUNTIME)
173+
@NamedAnnotation1(name = "name 1")
174+
@NamedAnnotation2(name = "name 2")
175+
@NamedAnnotation3(name = "name 3")
176+
@Inherited
177+
@interface InheritedComposedAnnotation {
178+
}
179+
180+
@InheritedComposedAnnotation
181+
private static class AnnotatedClass {
182+
}
183+
184+
@NamedAnnotation3(name = "local")
185+
private static class AnnotatedSubclass extends AnnotatedClass {
186+
}
187+
188+
}

0 commit comments

Comments
 (0)