Search in sources :

Example 11 with JavaAnnotation

use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_class_with_complex_annotations_correctly.

@Test
public void imports_class_with_complex_annotations_correctly() {
    JavaClass clazz = new ClassFileImporter().importPackagesOf(ClassWithComplexAnnotations.class).get(ClassWithComplexAnnotations.class);
    assertThat(clazz.getAnnotations()).as("annotations of " + clazz.getSimpleName()).hasSize(2);
    JavaAnnotation<JavaClass> annotation = clazz.getAnnotationOfType(TypeAnnotationWithEnumAndArrayValue.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("sub");
    assertThat(subAnnotation.getOwner()).isEqualTo(annotation);
    assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(clazz);
    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("otherFirst");
    assertThat(subAnnotationArray[0].getOwner()).isEqualTo(annotation);
    assertThat(subAnnotationArray[0].getAnnotatedElement()).isEqualTo(clazz);
    assertThat(((JavaAnnotation<?>[]) annotation.get("subAnnotationArrayWithDefault").get())[0].get("value").get()).isEqualTo("first");
    assertThatType((JavaClass) annotation.get("clazz").get()).matches(Serializable.class);
    assertThatType((JavaClass) annotation.get("clazzWithDefault").get()).matches(String.class);
    assertThat((JavaClass[]) annotation.get("classes").get()).matchExactly(Serializable.class, String.class);
    assertThat((JavaClass[]) annotation.get("classesWithDefault").get()).matchExactly(Serializable.class, List.class);
    assertThatType(clazz).matches(ClassWithComplexAnnotations.class);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) TypeAnnotationWithEnumAndArrayValue(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.TypeAnnotationWithEnumAndArrayValue) ClassWithComplexAnnotations(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithComplexAnnotations) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 12 with JavaAnnotation

use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_methods_with_complex_annotations_correctly.

@Test
public void imports_methods_with_complex_annotations_correctly() throws Exception {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getMethod(enumAndArrayAnnotatedMethod);
    JavaAnnotation<?> annotation = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue.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(method);
    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(subAnnotationArray[0].getAnnotatedElement()).isEqualTo(method);
    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(method).isEquivalentTo(ClassWithAnnotatedMethods.class.getMethod(enumAndArrayAnnotatedMethod));
}
Also used : ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 13 with JavaAnnotation

use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_method_with_one_parameter_with_one_annotation.

@Test
public void imports_method_with_one_parameter_with_one_annotation() {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithMethodWithAnnotatedParameters.class).get(ClassWithMethodWithAnnotatedParameters.class).getMethod(ClassWithMethodWithAnnotatedParameters.methodWithOneAnnotatedParameterWithOneAnnotation, String.class);
    List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = method.getParameterAnnotations();
    JavaParameter parameter = getOnlyElement(method.getParameters());
    JavaAnnotation<JavaParameter> annotation = getOnlyElement(getOnlyElement(parameterAnnotations));
    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(parameter);
    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("one");
    assertThat(subAnnotationArray[0].getOwner()).isEqualTo(annotation);
    assertThat(subAnnotationArray[0].getAnnotatedElement()).isEqualTo(parameter);
    assertThat(subAnnotationArray[1].get("value").get()).isEqualTo("two");
    assertThat(subAnnotationArray[1].getOwner()).isEqualTo(annotation);
    assertThat(subAnnotationArray[1].getAnnotatedElement()).isEqualTo(parameter);
    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);
    assertThatAnnotation(getOnlyElement(parameter.getAnnotations())).matches(method.reflect().getParameterAnnotations()[0][0]);
    assertThatAnnotation(annotation).matches(method.reflect().getParameterAnnotations()[0][0]);
}
Also used : ClassWithMethodWithAnnotatedParameters(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 14 with JavaAnnotation

use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_fields_with_two_annotations_correctly.

@Test
public void imports_fields_with_two_annotations_correctly() throws Exception {
    JavaField field = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedFields.class).get(ClassWithAnnotatedFields.class).getField("stringAndIntAnnotatedField");
    Set<JavaAnnotation<JavaField>> annotations = field.getAnnotations();
    assertThat(annotations).hasSize(2);
    assertThat(annotations).extractingResultOf("getOwner").containsOnly(field);
    assertThat(annotations).extractingResultOf("getAnnotatedElement").containsOnly(field);
    JavaAnnotation<JavaField> annotationWithString = field.getAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class.getName());
    assertThat(annotationWithString.get("value").get()).isEqualTo("otherThing");
    JavaAnnotation<JavaField> annotationWithInt = field.getAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithIntValue.class.getName());
    assertThat(annotationWithInt.get("intValue").get()).as("Annotation value with default").isEqualTo(0);
    assertThat(annotationWithInt.get("otherValue").get()).isEqualTo("overridden");
    assertThat(field).isEquivalentTo(field.getOwner().reflect().getDeclaredField("stringAndIntAnnotatedField"));
}
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)

Example 15 with JavaAnnotation

use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.

the class ImportTestUtils method get.

private static Object get(Annotation annotation, Class<?> owner, String methodName, ImportContext importContext) {
    try {
        Method method = annotation.annotationType().getMethod(methodName);
        method.setAccessible(true);
        Object result = method.invoke(annotation);
        if (result instanceof Class) {
            return importContext.resolveClass(((Class<?>) result).getName());
        }
        if (result instanceof Class[]) {
            List<JavaClass> classes = javaClassesFrom((Class<?>[]) result, importContext);
            return classes.toArray(new JavaClass[0]);
        }
        if (result instanceof Enum<?>) {
            return ImportTestUtils.enumConstant((Enum<?>) result);
        }
        if (result instanceof Enum[]) {
            return enumConstants((Enum<?>[]) result);
        }
        if (result instanceof Annotation) {
            return javaAnnotationFrom((Annotation) result, owner);
        }
        if (result instanceof Annotation[]) {
            return javaAnnotationsFrom((Annotation[]) result, owner).toArray(new JavaAnnotation<?>[0]);
        }
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Method(java.lang.reflect.Method) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Annotation(java.lang.annotation.Annotation) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation)

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