Search in sources :

Example 6 with JavaAnnotation

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());
}
Also used : Serializable(java.io.Serializable) ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 7 with JavaAnnotation

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));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassWithOneAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithOneAnnotation) SimpleAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 8 with JavaAnnotation

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();
}
Also used : ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 9 with JavaAnnotation

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"));
}
Also used : JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithAnnotatedFields(com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 10 with JavaAnnotation

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();
}
Also used : JavaField(com.tngtech.archunit.core.domain.JavaField) ClassWithAnnotatedFields(com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Aggregations

JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)15 Test (org.junit.Test)13 JavaClass (com.tngtech.archunit.core.domain.JavaClass)6 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)6 JavaEnumConstant (com.tngtech.archunit.core.domain.JavaEnumConstant)4 ClassWithAnnotatedMethods (com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods)4 JavaField (com.tngtech.archunit.core.domain.JavaField)3 ClassWithAnnotatedFields (com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)2 JavaParameter (com.tngtech.archunit.core.domain.JavaParameter)2 TypeAnnotationWithEnumAndArrayValue (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.TypeAnnotationWithEnumAndArrayValue)2 ClassWithMethodWithAnnotatedParameters (com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters)2 Annotation (java.lang.annotation.Annotation)2 Set (java.util.Set)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 ClassWithComplexAnnotations (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithComplexAnnotations)1 ClassWithOneAnnotation (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithOneAnnotation)1 SimpleAnnotation (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation)1