Search in sources :

Example 91 with JavaClass

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_field_annotations.

@Test
public void automatically_resolves_field_annotations() {
    JavaClass clazz = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ANNOTATION_TYPES_PROPERTY_NAME).importClass(ClassWithAnnotatedFields.class);
    JavaAnnotation<?> annotation = clazz.getField("fieldAnnotatedWithAnnotationFromParentPackage").getAnnotationOfType(SomeAnnotation.class.getName());
    assertThat(annotation.getRawType()).isFullyImported(true);
    assertThat(annotation.get("mandatory")).contains("mandatory");
    assertThat(annotation.get("optional")).contains("optional");
    SomeAnnotation reflected = annotation.as(SomeAnnotation.class);
    assertThat(reflected.mandatory()).isEqualTo("mandatory");
    assertThat(reflected.optional()).isEqualTo("optional");
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) SomeAnnotation(com.tngtech.archunit.core.importer.testexamples.SomeAnnotation) Test(org.junit.Test)

Example 92 with JavaClass

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

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_method_annotations.

@Test
public void automatically_resolves_method_annotations() {
    JavaClass clazz = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ANNOTATION_TYPES_PROPERTY_NAME).importClass(ClassWithAnnotatedMethods.class);
    JavaAnnotation<?> annotation = clazz.getMethod(methodAnnotatedWithAnnotationFromParentPackage).getAnnotationOfType(SomeAnnotation.class.getName());
    assertThat(annotation.getRawType()).isFullyImported(true);
    assertThat(annotation.get("mandatory")).contains("mandatory");
    assertThat(annotation.get("optional")).contains("optional");
    SomeAnnotation reflected = annotation.as(SomeAnnotation.class);
    assertThat(reflected.mandatory()).isEqualTo("mandatory");
    assertThat(reflected.optional()).isEqualTo("optional");
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) SomeAnnotation(com.tngtech.archunit.core.importer.testexamples.SomeAnnotation) Test(org.junit.Test)

Example 94 with JavaClass

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

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_constructor_call_target_owners.

@Test
public void automatically_resolves_constructor_call_target_owners() {
    class Target {
    }
    @SuppressWarnings("unused")
    class Origin {

        void resolvesConstructorCallTargetOwner() {
            new Target();
        }
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ACCESSES_TO_TYPES_PROPERTY_NAME).importClass(Origin.class);
    JavaConstructorCall call = getOnlyElement(javaClass.getMethod("resolvesConstructorCallTargetOwner").getConstructorCallsFromSelf());
    assertThat(call.getTargetOwner()).isFullyImported(true);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) 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