Search in sources :

Example 81 with JavaClass

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

the class ClassFileImporterAnnotationsTest method imports_simple_annotation.

@Test
public void imports_simple_annotation() {
    JavaClass javaClass = new ClassFileImporter().importPackagesOf(AnnotationToImport.class).get(AnnotationToImport.class);
    assertThat(javaClass).matches(AnnotationToImport.class).hasNoSuperclass().hasInterfacesMatchingInAnyOrder(Annotation.class).isInterface(true).isEnum(false).isAnnotation(true).isRecord(false);
    assertThat(getAnnotationDefaultValue(javaClass, "someStringMethod", String.class)).isEqualTo("DEFAULT");
    assertThatType(getAnnotationDefaultValue(javaClass, "someTypeMethod", JavaClass.class)).matches(List.class);
    assertThat(getAnnotationDefaultValue(javaClass, "someEnumMethod", JavaEnumConstant.class)).isEquivalentTo(EnumToImport.SECOND);
    assertThatType(getAnnotationDefaultValue(javaClass, "someAnnotationMethod", JavaAnnotation.class).getRawType()).matches(AnnotationParameter.class);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) AnnotationToImport(com.tngtech.archunit.core.importer.testexamples.simpleimport.AnnotationToImport) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 82 with JavaClass

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

the class ClassFileImporterAnnotationsTest method imports_class_with_annotation_with_empty_array.

@Test
public void imports_class_with_annotation_with_empty_array() {
    JavaClass clazz = new ClassFileImporter().importPackagesOf(ClassWithAnnotationWithEmptyArrays.class).get(ClassWithAnnotationWithEmptyArrays.class);
    JavaAnnotation<?> annotation = clazz.getAnnotationOfType(ClassAnnotationWithArrays.class.getName());
    assertThat(Array.getLength(annotation.get("primitives").get())).isZero();
    assertThat(Array.getLength(annotation.get("objects").get())).isZero();
    assertThat(Array.getLength(annotation.get("enums").get())).isZero();
    assertThat(Array.getLength(annotation.get("classes").get())).isZero();
    assertThat(Array.getLength(annotation.get("annotations").get())).isZero();
    ClassAnnotationWithArrays reflected = clazz.getAnnotationOfType(ClassAnnotationWithArrays.class);
    assertThat(reflected.primitives()).isEmpty();
    assertThat(reflected.objects()).isEmpty();
    assertThat(reflected.enums()).isEmpty();
    assertThat(reflected.classes()).isEmpty();
    assertThat(reflected.annotations()).isEmpty();
}
Also used : ClassAnnotationWithArrays(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassAnnotationWithArrays) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithAnnotationWithEmptyArrays(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithAnnotationWithEmptyArrays) Test(org.junit.Test)

Example 83 with JavaClass

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

the class ClassFileImporterAnnotationsTest method imports_method_with_annotation_with_empty_array.

@Test
public void imports_method_with_annotation_with_empty_array() {
    JavaClass clazz = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class);
    JavaAnnotation<?> annotation = clazz.getMethod(methodAnnotatedWithEmptyArrays).getAnnotationOfType(MethodAnnotationWithArrays.class.getName());
    assertThat(Array.getLength(annotation.get("primitives").get())).isZero();
    assertThat(Array.getLength(annotation.get("objects").get())).isZero();
    assertThat(Array.getLength(annotation.get("enums").get())).isZero();
    assertThat(Array.getLength(annotation.get("classes").get())).isZero();
    assertThat(Array.getLength(annotation.get("annotations").get())).isZero();
    MethodAnnotationWithArrays reflected = annotation.as(MethodAnnotationWithArrays.class);
    assertThat(reflected.primitives()).isEmpty();
    assertThat(reflected.objects()).isEmpty();
    assertThat(reflected.enums()).isEmpty();
    assertThat(reflected.classes()).isEmpty();
    assertThat(reflected.annotations()).isEmpty();
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) MethodAnnotationWithArrays(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.MethodAnnotationWithArrays) Test(org.junit.Test)

Example 84 with JavaClass

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_field_types.

@Test
public void automatically_resolves_field_types() {
    @SuppressWarnings("unused")
    class FieldTypeWithoutAnyFurtherReference {

        String field;
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_MEMBER_TYPES_PROPERTY_NAME).importClass(FieldTypeWithoutAnyFurtherReference.class);
    assertThat(javaClass.getField("field").getRawType()).as("field type").isFullyImported(true);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 85 with JavaClass

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_method_return_types.

@Test
public void automatically_resolves_method_return_types() {
    @SuppressWarnings("unused")
    class MemberTypesWithoutAnyFurtherReference {

        File returnType() {
            return null;
        }
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_MEMBER_TYPES_PROPERTY_NAME).importClass(MemberTypesWithoutAnyFurtherReference.class);
    assertThat(javaClass.getMethod("returnType").getRawReturnType()).as("method return type").isFullyImported(true);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9