use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_constructors_with_complex_annotations_correctly.
@Test
public void imports_constructors_with_complex_annotations_correctly() throws Exception {
JavaConstructor constructor = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getConstructor();
JavaAnnotation<JavaConstructor> annotation = constructor.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue.class.getName());
assertThat((Object[]) annotation.get("classes").get()).extracting("name").containsExactly(Object.class.getName(), Serializable.class.getName());
assertThat(annotation.getOwner()).isEqualTo(constructor);
JavaAnnotation<?> subAnnotation = (JavaAnnotation<?>) annotation.get("subAnnotation").get();
assertThat(subAnnotation.get("value").get()).isEqualTo("changed");
assertThat(subAnnotation.getOwner()).isEqualTo(annotation);
assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(constructor);
JavaAnnotation<?>[] subAnnotationArray = (JavaAnnotation<?>[]) annotation.get("subAnnotationArray").get();
assertThat(subAnnotationArray[0].get("value").get()).isEqualTo("another");
assertThat(subAnnotationArray[0].getOwner()).isEqualTo(annotation);
assertThat(subAnnotationArray[0].getAnnotatedElement()).isEqualTo(constructor);
assertThat(constructor).isEquivalentTo(ClassWithAnnotatedMethods.class.getConstructor());
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method classes_know_which_annotations_have_their_type.
@Test
public void classes_know_which_annotations_have_their_type() {
JavaClasses classes = new ClassFileImporter().importClasses(ClassWithOneAnnotation.class, SimpleAnnotation.class);
Set<JavaAnnotation<?>> annotations = classes.get(SimpleAnnotation.class).getAnnotationsWithTypeOfSelf();
assertThat(getOnlyElement(annotations).getOwner()).isEqualTo(classes.get(ClassWithOneAnnotation.class));
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method methods_handle_optional_annotation_correctly.
@Test
public void methods_handle_optional_annotation_correctly() {
JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getMethod(stringAnnotatedMethod);
assertThat(method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class)).isPresent();
assertThat(method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue.class)).isEmpty();
Optional<JavaAnnotation<JavaMethod>> optionalAnnotation = method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class.getName());
assertThat(optionalAnnotation.get().getOwner()).as("owner of optional annotation").isEqualTo(method);
assertThat(method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue.class.getName())).isEmpty();
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_fields_with_complex_annotations_correctly.
@Test
public void imports_fields_with_complex_annotations_correctly() throws Exception {
JavaField field = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedFields.class).get(ClassWithAnnotatedFields.class).getField("enumAndArrayAnnotatedField");
JavaAnnotation<JavaField> annotation = field.getAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithEnumClassAndArrayValue.class.getName());
assertThat((JavaEnumConstant) annotation.get("value").get()).isEquivalentTo(OTHER_VALUE);
assertThat((JavaEnumConstant) annotation.get("valueWithDefault").get()).isEquivalentTo(SOME_VALUE);
assertThat(((JavaEnumConstant[]) annotation.get("enumArray").get())).matches(SOME_VALUE, OTHER_VALUE);
assertThat(((JavaEnumConstant[]) annotation.get("enumArrayWithDefault").get())).matches(OTHER_VALUE);
JavaAnnotation<?> subAnnotation = (JavaAnnotation<?>) annotation.get("subAnnotation").get();
assertThat(subAnnotation.get("value").get()).isEqualTo("changed");
assertThat(subAnnotation.getOwner()).isEqualTo(annotation);
assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(field);
assertThat(((JavaAnnotation<?>) annotation.get("subAnnotationWithDefault").get()).get("value").get()).isEqualTo("default");
JavaAnnotation<?>[] subAnnotationArray = (JavaAnnotation<?>[]) annotation.get("subAnnotationArray").get();
assertThat(subAnnotationArray[0].get("value").get()).isEqualTo("another");
assertThat(subAnnotationArray[0].getOwner()).isEqualTo(annotation);
assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(field);
assertThat(((JavaAnnotation<?>[]) annotation.get("subAnnotationArrayWithDefault").get())[0].get("value").get()).isEqualTo("first");
assertThatType((JavaClass) annotation.get("clazz").get()).matches(Map.class);
assertThatType((JavaClass) annotation.get("clazzWithDefault").get()).matches(String.class);
assertThat((JavaClass[]) annotation.get("classes").get()).matchExactly(Object.class, Serializable.class);
assertThat((JavaClass[]) annotation.get("classesWithDefault").get()).matchExactly(Serializable.class, List.class);
assertThat(field).isEquivalentTo(field.getOwner().reflect().getDeclaredField("enumAndArrayAnnotatedField"));
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method fields_handle_optional_annotation_correctly.
@Test
public void fields_handle_optional_annotation_correctly() {
JavaField field = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedFields.class).get(ClassWithAnnotatedFields.class).getField("stringAnnotatedField");
assertThat(field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class)).isPresent();
assertThat(field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithEnumClassAndArrayValue.class)).isEmpty();
Optional<JavaAnnotation<JavaField>> optionalAnnotation = field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class.getName());
assertThat(optionalAnnotation.get().getOwner()).as("owner of optional annotation").isEqualTo(field);
assertThat(field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithEnumClassAndArrayValue.class.getName())).as("optional annotation").isEmpty();
}
Aggregations