use of org.springframework.core.annotation.AnnotationAttributes in project spring-framework by spring-projects.
the class AnnotationMetadataTests method doTestAnnotationInfo.
private void doTestAnnotationInfo(AnnotationMetadata metadata) {
assertThat(metadata.getClassName()).isEqualTo(AnnotatedComponent.class.getName());
assertThat(metadata.isInterface()).isFalse();
assertThat(metadata.isAnnotation()).isFalse();
assertThat(metadata.isAbstract()).isFalse();
assertThat(metadata.isConcrete()).isTrue();
assertThat(metadata.hasSuperClass()).isTrue();
assertThat(metadata.getSuperClassName()).isEqualTo(Object.class.getName());
assertThat(metadata.getInterfaceNames().length).isEqualTo(1);
assertThat(metadata.getInterfaceNames()[0]).isEqualTo(Serializable.class.getName());
assertThat(metadata.isAnnotated(Component.class.getName())).isTrue();
assertThat(metadata.isAnnotated(NamedComposedAnnotation.class.getName())).isTrue();
assertThat(metadata.hasAnnotation(Component.class.getName())).isTrue();
assertThat(metadata.hasAnnotation(Scope.class.getName())).isTrue();
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName())).isTrue();
assertThat(metadata.hasAnnotation(NamedComposedAnnotation.class.getName())).isTrue();
assertThat(metadata.getAnnotationTypes()).containsExactlyInAnyOrder(Component.class.getName(), Scope.class.getName(), SpecialAttr.class.getName(), DirectAnnotation.class.getName(), MetaMetaAnnotation.class.getName(), EnumSubclasses.class.getName(), NamedComposedAnnotation.class.getName());
AnnotationAttributes compAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(Component.class.getName());
assertThat(compAttrs).hasSize(1);
assertThat(compAttrs.getString("value")).isEqualTo("myName");
AnnotationAttributes scopeAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(Scope.class.getName());
assertThat(scopeAttrs).hasSize(1);
assertThat(scopeAttrs.getString("value")).isEqualTo("myScope");
Set<MethodMetadata> methods = metadata.getAnnotatedMethods(DirectAnnotation.class.getName());
MethodMetadata method = methods.iterator().next();
assertThat(method.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value")).isEqualTo("direct");
assertThat(method.getAnnotationAttributes(DirectAnnotation.class.getName()).get("myValue")).isEqualTo("direct");
List<Object> allMeta = method.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
assertThat(new HashSet<>(allMeta)).isEqualTo(new HashSet<Object>(Arrays.asList("direct", "meta")));
allMeta = method.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("additional");
assertThat(new HashSet<>(allMeta)).isEqualTo(new HashSet<Object>(Arrays.asList("direct")));
assertThat(metadata.isAnnotated(IsAnnotatedAnnotation.class.getName())).isTrue();
{
// perform tests with classValuesAsString = false (the default)
AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(SpecialAttr.class.getName());
assertThat(specialAttrs).hasSize(6);
assertThat(String.class.isAssignableFrom(specialAttrs.getClass("clazz"))).isTrue();
assertThat(specialAttrs.getEnum("state").equals(Thread.State.NEW)).isTrue();
AnnotationAttributes nestedAnno = specialAttrs.getAnnotation("nestedAnno");
assertThat("na").isEqualTo(nestedAnno.getString("value"));
assertThat(nestedAnno.getEnum("anEnum").equals(SomeEnum.LABEL1)).isTrue();
assertThat((Class<?>[]) nestedAnno.get("classArray")).isEqualTo(new Class<?>[] { String.class });
AnnotationAttributes[] nestedAnnoArray = specialAttrs.getAnnotationArray("nestedAnnoArray");
assertThat(nestedAnnoArray.length).isEqualTo(2);
assertThat(nestedAnnoArray[0].getString("value")).isEqualTo("default");
assertThat(nestedAnnoArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT)).isTrue();
assertThat((Class<?>[]) nestedAnnoArray[0].get("classArray")).isEqualTo(new Class<?>[] { Void.class });
assertThat(nestedAnnoArray[1].getString("value")).isEqualTo("na1");
assertThat(nestedAnnoArray[1].getEnum("anEnum").equals(SomeEnum.LABEL2)).isTrue();
assertThat((Class<?>[]) nestedAnnoArray[1].get("classArray")).isEqualTo(new Class<?>[] { Number.class });
assertThat(nestedAnnoArray[1].getClassArray("classArray")).isEqualTo(new Class<?>[] { Number.class });
AnnotationAttributes optional = specialAttrs.getAnnotation("optional");
assertThat(optional.getString("value")).isEqualTo("optional");
assertThat(optional.getEnum("anEnum").equals(SomeEnum.DEFAULT)).isTrue();
assertThat((Class<?>[]) optional.get("classArray")).isEqualTo(new Class<?>[] { Void.class });
assertThat(optional.getClassArray("classArray")).isEqualTo(new Class<?>[] { Void.class });
AnnotationAttributes[] optionalArray = specialAttrs.getAnnotationArray("optionalArray");
assertThat(optionalArray.length).isEqualTo(1);
assertThat(optionalArray[0].getString("value")).isEqualTo("optional");
assertThat(optionalArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT)).isTrue();
assertThat((Class<?>[]) optionalArray[0].get("classArray")).isEqualTo(new Class<?>[] { Void.class });
assertThat(optionalArray[0].getClassArray("classArray")).isEqualTo(new Class<?>[] { Void.class });
assertThat(metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value")).isEqualTo("direct");
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
assertThat(new HashSet<>(allMeta)).isEqualTo(new HashSet<Object>(Arrays.asList("direct", "meta")));
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("additional");
assertThat(new HashSet<>(allMeta)).isEqualTo(new HashSet<Object>(Arrays.asList("direct", "")));
assertThat(metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("additional")).isEqualTo("");
assertThat(((String[]) metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("additionalArray")).length).isEqualTo(0);
}
{
// perform tests with classValuesAsString = true
AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(SpecialAttr.class.getName(), true);
assertThat(specialAttrs).hasSize(6);
assertThat(specialAttrs.get("clazz")).isEqualTo(String.class.getName());
assertThat(specialAttrs.getString("clazz")).isEqualTo(String.class.getName());
AnnotationAttributes nestedAnno = specialAttrs.getAnnotation("nestedAnno");
assertThat(nestedAnno.getStringArray("classArray")).isEqualTo(new String[] { String.class.getName() });
assertThat(nestedAnno.getStringArray("classArray")).isEqualTo(new String[] { String.class.getName() });
AnnotationAttributes[] nestedAnnoArray = specialAttrs.getAnnotationArray("nestedAnnoArray");
assertThat((String[]) nestedAnnoArray[0].get("classArray")).isEqualTo(new String[] { Void.class.getName() });
assertThat(nestedAnnoArray[0].getStringArray("classArray")).isEqualTo(new String[] { Void.class.getName() });
assertThat((String[]) nestedAnnoArray[1].get("classArray")).isEqualTo(new String[] { Number.class.getName() });
assertThat(nestedAnnoArray[1].getStringArray("classArray")).isEqualTo(new String[] { Number.class.getName() });
AnnotationAttributes optional = specialAttrs.getAnnotation("optional");
assertThat((String[]) optional.get("classArray")).isEqualTo(new String[] { Void.class.getName() });
assertThat(optional.getStringArray("classArray")).isEqualTo(new String[] { Void.class.getName() });
AnnotationAttributes[] optionalArray = specialAttrs.getAnnotationArray("optionalArray");
assertThat((String[]) optionalArray[0].get("classArray")).isEqualTo(new String[] { Void.class.getName() });
assertThat(optionalArray[0].getStringArray("classArray")).isEqualTo(new String[] { Void.class.getName() });
assertThat(metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value")).isEqualTo("direct");
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
assertThat(new HashSet<>(allMeta)).isEqualTo(new HashSet<Object>(Arrays.asList("direct", "meta")));
}
}
use of org.springframework.core.annotation.AnnotationAttributes in project spring-framework by spring-projects.
the class AnnotationMetadataTests method assertMultipleAnnotationsWithIdenticalAttributeNames.
private void assertMultipleAnnotationsWithIdenticalAttributeNames(AnnotationMetadata metadata) {
AnnotationAttributes attributes1 = (AnnotationAttributes) metadata.getAnnotationAttributes(NamedAnnotation1.class.getName(), false);
String name1 = attributes1.getString("name");
assertThat(name1).as("name of NamedAnnotation1").isEqualTo("name 1");
AnnotationAttributes attributes2 = (AnnotationAttributes) metadata.getAnnotationAttributes(NamedAnnotation2.class.getName(), false);
String name2 = attributes2.getString("name");
assertThat(name2).as("name of NamedAnnotation2").isEqualTo("name 2");
AnnotationAttributes attributes3 = (AnnotationAttributes) metadata.getAnnotationAttributes(NamedAnnotation3.class.getName(), false);
String name3 = attributes3.getString("name");
assertThat(name3).as("name of NamedAnnotation3").isEqualTo("name 3");
}
use of org.springframework.core.annotation.AnnotationAttributes in project spring-boot by spring-projects.
the class DiscoveredOperationMethodTests method getProducesMediaTypesShouldReturnMediaTypes.
@Test
void getProducesMediaTypesShouldReturnMediaTypes() {
Method method = ReflectionUtils.findMethod(getClass(), "example");
AnnotationAttributes annotationAttributes = new AnnotationAttributes();
String[] produces = new String[] { "application/json" };
annotationAttributes.put("produces", produces);
annotationAttributes.put("producesFrom", Producible.class);
DiscoveredOperationMethod discovered = new DiscoveredOperationMethod(method, OperationType.READ, annotationAttributes);
assertThat(discovered.getProducesMediaTypes()).containsExactly("application/json");
}
use of org.springframework.core.annotation.AnnotationAttributes in project spring-boot by spring-projects.
the class DiscoveredOperationMethodTests method getProducesMediaTypesWhenProducesFromShouldReturnMediaTypes.
@Test
void getProducesMediaTypesWhenProducesFromShouldReturnMediaTypes() {
Method method = ReflectionUtils.findMethod(getClass(), "example");
AnnotationAttributes annotationAttributes = new AnnotationAttributes();
annotationAttributes.put("produces", new String[0]);
annotationAttributes.put("producesFrom", ExampleProducible.class);
DiscoveredOperationMethod discovered = new DiscoveredOperationMethod(method, OperationType.READ, annotationAttributes);
assertThat(discovered.getProducesMediaTypes()).containsExactly("one/*", "two/*", "three/*");
}
use of org.springframework.core.annotation.AnnotationAttributes in project spring-boot by spring-projects.
the class DiscoveredJmxOperationTests method getOperation.
private DiscoveredJmxOperation getOperation(String methodName) {
Method method = findMethod(methodName);
AnnotationAttributes annotationAttributes = new AnnotationAttributes();
annotationAttributes.put("produces", "application/xml");
DiscoveredOperationMethod operationMethod = new DiscoveredOperationMethod(method, OperationType.READ, annotationAttributes);
return new DiscoveredJmxOperation(EndpointId.of("test"), operationMethod, mock(OperationInvoker.class));
}
Aggregations