Search in sources :

Example 1 with JavaEnumConstant

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_class_annotations.

@Test
public void automatically_resolves_class_annotations() {
    JavaClass clazz = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ANNOTATION_TYPES_PROPERTY_NAME).importClass(ClassWithUnimportedAnnotation.class);
    JavaAnnotation<?> annotation = clazz.getAnnotationOfType(SomeAnnotation.class.getName());
    assertThat(annotation.getRawType()).isFullyImported(true);
    assertThat(annotation.get("mandatory")).contains("mandatory");
    assertThat(annotation.get("optional")).contains("optional");
    assertThat((JavaEnumConstant) annotation.get("mandatoryEnum").get()).isEquivalentTo(SOME_VALUE);
    assertThat((JavaEnumConstant) annotation.get("optionalEnum").get()).isEquivalentTo(OTHER_VALUE);
    SomeAnnotation reflected = clazz.getAnnotationOfType(SomeAnnotation.class);
    assertThat(reflected.mandatory()).isEqualTo("mandatory");
    assertThat(reflected.optional()).isEqualTo("optional");
    assertThat(reflected.mandatoryEnum()).isEqualTo(SOME_VALUE);
    assertThat(reflected.optionalEnum()).isEqualTo(OTHER_VALUE);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) SomeAnnotation(com.tngtech.archunit.core.importer.testexamples.SomeAnnotation) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 2 with JavaEnumConstant

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_parameter_annotations.

@Test
public void automatically_resolves_parameter_annotations() {
    JavaClass clazz = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ANNOTATION_TYPES_PROPERTY_NAME).importClass(ClassWithMethodWithAnnotatedParameters.class);
    JavaAnnotation<?> annotation = clazz.getMethod(methodWithOneAnnotatedParameterWithTwoAnnotations, String.class).getParameters().get(0).getAnnotationOfType(SomeParameterAnnotation.class.getName());
    assertThat(annotation.getRawType()).isFullyImported(true);
    assertThat((JavaEnumConstant) annotation.get("value").get()).isEquivalentTo(OTHER_VALUE);
    assertThat((JavaEnumConstant) annotation.get("valueWithDefault").get()).isEquivalentTo(SOME_VALUE);
    SomeParameterAnnotation reflected = annotation.as(SomeParameterAnnotation.class);
    assertThat(reflected.value()).isEqualTo(OTHER_VALUE);
    assertThat(reflected.valueWithDefault()).isEqualTo(SOME_VALUE);
}
Also used : SomeParameterAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters.SomeParameterAnnotation) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 3 with JavaEnumConstant

use of com.tngtech.archunit.core.domain.JavaEnumConstant 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 4 with JavaEnumConstant

use of com.tngtech.archunit.core.domain.JavaEnumConstant 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 5 with JavaEnumConstant

use of com.tngtech.archunit.core.domain.JavaEnumConstant 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)

Aggregations

JavaEnumConstant (com.tngtech.archunit.core.domain.JavaEnumConstant)9 JavaClass (com.tngtech.archunit.core.domain.JavaClass)8 Test (org.junit.Test)8 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)4 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 JavaField (com.tngtech.archunit.core.domain.JavaField)1 JavaParameter (com.tngtech.archunit.core.domain.JavaParameter)1 SomeAnnotation (com.tngtech.archunit.core.importer.testexamples.SomeAnnotation)1 SomeEnum (com.tngtech.archunit.core.importer.testexamples.SomeEnum)1 ClassWithComplexAnnotations (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithComplexAnnotations)1 TypeAnnotationWithEnumAndArrayValue (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.TypeAnnotationWithEnumAndArrayValue)1 ClassWithMethodWithAnnotatedParameters (com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters)1 SomeParameterAnnotation (com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters.SomeParameterAnnotation)1 ClassWithAnnotatedFields (com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields)1 ClassWithAnnotatedMethods (com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods)1 MethodAnnotationWithEnumAndArrayValue (com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue)1 EnumToImport (com.tngtech.archunit.core.importer.testexamples.simpleimport.EnumToImport)1 Set (java.util.Set)1