Search in sources :

Example 6 with JavaEnumConstant

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

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_constructor_annotations.

@Test
public void automatically_resolves_constructor_annotations() {
    JavaClass clazz = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ANNOTATION_TYPES_PROPERTY_NAME).importClass(ClassWithAnnotatedMethods.class);
    JavaAnnotation<?> annotation = clazz.getConstructor().getAnnotationOfType(MethodAnnotationWithEnumAndArrayValue.class.getName());
    assertThat(annotation.getRawType()).isFullyImported(true);
    assertThat((JavaEnumConstant) annotation.get("value").get()).isEquivalentTo(OTHER_VALUE);
    MethodAnnotationWithEnumAndArrayValue reflected = annotation.as(MethodAnnotationWithEnumAndArrayValue.class);
    assertThat(reflected.value()).isEqualTo(OTHER_VALUE);
}
Also used : MethodAnnotationWithEnumAndArrayValue(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 8 with JavaEnumConstant

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

the class JavaAnnotationAssertion method hasEnumProperty.

public JavaAnnotationAssertion hasEnumProperty(String propertyName, Enum<?> expectedEnumConstant) {
    JavaEnumConstant actualEnumConstant = getPropertyOfType(propertyName, JavaEnumConstant.class);
    assertThat(actualEnumConstant).as(annotationPropertyDescription(actualEnumConstant.getDeclaringClass().getSimpleName(), propertyName)).isEquivalentTo(expectedEnumConstant);
    return this;
}
Also used : JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant)

Example 9 with JavaEnumConstant

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

the class ClassFileImporterTest method imports_simple_enum.

@Test
public void imports_simple_enum() {
    JavaClass javaClass = new ClassFileImporter().importUrl(getClass().getResource("testexamples/simpleimport")).get(EnumToImport.class);
    assertThat(javaClass).matches(EnumToImport.class).hasRawSuperclassMatching(Enum.class).hasNoInterfaces().hasAllInterfacesMatchingInAnyOrder(Enum.class.getInterfaces()).isInterface(false).isEnum(true).isAnnotation(false).isRecord(false);
    JavaEnumConstant constant = javaClass.getEnumConstant(EnumToImport.FIRST.name());
    assertThatType(constant.getDeclaringClass()).as("declaring class").isEqualTo(javaClass);
    assertThat(constant.name()).isEqualTo(EnumToImport.FIRST.name());
    assertThat(javaClass.getEnumConstants()).extractingResultOf("name").as("enum constant names").containsOnly(EnumToImport.FIRST.name(), EnumToImport.SECOND.name());
}
Also used : SomeEnum(com.tngtech.archunit.core.importer.testexamples.SomeEnum) JavaClass(com.tngtech.archunit.core.domain.JavaClass) EnumToImport(com.tngtech.archunit.core.importer.testexamples.simpleimport.EnumToImport) 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