Search in sources :

Example 1 with JavaAnnotation

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

the class JavaAnnotationsAssertion method match.

public JavaAnnotationsAssertion match(Collection<Annotation> annotations) {
    assertThat(actual).hasSameSizeAs(annotations);
    Map<String, JavaAnnotation<?>> actualByClassName = annotationsByClassName(actual);
    Map<String, Annotation> reflectionByClassName = reflectionByClassName(annotations);
    assertThat(actualByClassName.keySet()).as("annotation type names").containsExactlyInAnyOrderElementsOf(reflectionByClassName.keySet());
    for (Map.Entry<String, JavaAnnotation<?>> entry : actualByClassName.entrySet()) {
        Annotation reflectionAnnotation = reflectionByClassName.get(entry.getKey());
        assertThatAnnotation(entry.getValue()).matches(reflectionAnnotation);
    }
    return myself;
}
Also used : JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Annotation(java.lang.annotation.Annotation) Assertions.assertThatAnnotation(com.tngtech.archunit.testutil.Assertions.assertThatAnnotation)

Example 2 with JavaAnnotation

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

the class ClassFileImporterAnnotationsTest method imports_methods_with_two_annotations_correctly.

@Test
public void imports_methods_with_two_annotations_correctly() throws Exception {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getMethod(stringAndIntAnnotatedMethod);
    Set<JavaAnnotation<JavaMethod>> annotations = method.getAnnotations();
    assertThat(annotations).hasSize(2);
    assertThat(annotations).extractingResultOf("getOwner").containsOnly(method);
    assertThat(annotations).extractingResultOf("getAnnotatedElement").containsOnly(method);
    JavaAnnotation<?> annotationWithString = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class.getName());
    assertThat(annotationWithString.get("value").get()).isEqualTo("otherThing");
    JavaAnnotation<?> annotationWithInt = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithIntValue.class.getName());
    assertThat(annotationWithInt.get("otherValue").get()).isEqualTo("overridden");
    assertThat(method).isEquivalentTo(ClassWithAnnotatedMethods.class.getMethod(stringAndIntAnnotatedMethod));
}
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 3 with JavaAnnotation

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

the class ClassFileImporterAnnotationsTest method imports_annotation_defaults.

@Test
public void imports_annotation_defaults() {
    JavaClass annotationType = new ClassFileImporter().importPackagesOf(TypeAnnotationWithEnumAndArrayValue.class).get(TypeAnnotationWithEnumAndArrayValue.class);
    assertThat((JavaEnumConstant) annotationType.getMethod("valueWithDefault").getDefaultValue().get()).as("default of valueWithDefault()").isEquivalentTo(SOME_VALUE);
    assertThat(((JavaEnumConstant[]) annotationType.getMethod("enumArrayWithDefault").getDefaultValue().get())).as("default of enumArrayWithDefault()").matches(OTHER_VALUE);
    assertThat(((JavaAnnotation<?>) annotationType.getMethod("subAnnotationWithDefault").getDefaultValue().get()).get("value").get()).as("default of subAnnotationWithDefault()").isEqualTo("default");
    assertThat(((JavaAnnotation<?>[]) annotationType.getMethod("subAnnotationArrayWithDefault").getDefaultValue().get())[0].get("value").get()).as("default of subAnnotationArrayWithDefault()").isEqualTo("first");
    assertThatType((JavaClass) annotationType.getMethod("clazzWithDefault").getDefaultValue().get()).as("default of clazzWithDefault()").matches(String.class);
    assertThat((JavaClass[]) annotationType.getMethod("classesWithDefault").getDefaultValue().get()).as("default of clazzWithDefault()").matchExactly(Serializable.class, List.class);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) TypeAnnotationWithEnumAndArrayValue(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.TypeAnnotationWithEnumAndArrayValue) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 4 with JavaAnnotation

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

the class ClassFileImporterAnnotationsTest method classes_know_which_annotation_members_have_their_type.

@Test
public void classes_know_which_annotation_members_have_their_type() {
    @SuppressWarnings("unused")
    @ParameterAnnotation(value = String.class)
    class Dependent {

        @ParameterAnnotation(value = String.class)
        String field;

        @ParameterAnnotation(value = String.class)
        Dependent() {
        }

        @ParameterAnnotation(value = String.class)
        void method() {
        }

        @ParameterAnnotation(value = List.class)
        void notToFind() {
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Dependent.class, ParameterAnnotation.class, String.class);
    Set<JavaAnnotation<?>> annotations = classes.get(String.class).getAnnotationsWithParameterTypeOfSelf();
    for (JavaAnnotation<?> annotation : annotations) {
        assertThatAnnotation(annotation).hasType(ParameterAnnotation.class);
    }
    Set<JavaAnnotation<?>> expected = ImmutableSet.<JavaAnnotation<?>>of(classes.get(Dependent.class).getAnnotationOfType(ParameterAnnotation.class.getName()), classes.get(Dependent.class).getField("field").getAnnotationOfType(ParameterAnnotation.class.getName()), classes.get(Dependent.class).getConstructor(getClass()).getAnnotationOfType(ParameterAnnotation.class.getName()), classes.get(Dependent.class).getMethod("method").getAnnotationOfType(ParameterAnnotation.class.getName()));
    assertThat(annotations).as("annotations with parameter type " + String.class.getSimpleName()).containsOnlyElementsOf(expected);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) List(java.util.List) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 5 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_two_annotations.

@Test
public void imports_method_with_one_parameter_with_two_annotations() {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithMethodWithAnnotatedParameters.class).get(ClassWithMethodWithAnnotatedParameters.class).getMethod(ClassWithMethodWithAnnotatedParameters.methodWithOneAnnotatedParameterWithTwoAnnotations, String.class);
    List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = method.getParameterAnnotations();
    Set<JavaAnnotation<JavaParameter>> annotations = getOnlyElement(parameterAnnotations);
    assertThat(annotations).isEqualTo(getOnlyElement(method.getParameters()).getAnnotations());
    assertThatAnnotations(annotations).match(ImmutableSet.copyOf(method.reflect().getParameterAnnotations()[0]));
}
Also used : ClassWithMethodWithAnnotatedParameters(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) 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