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]);
}
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);
}
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;
}
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());
}
Aggregations