Search in sources :

Example 96 with JavaClass

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_enclosing_classes.

@Test
public void automatically_resolves_enclosing_classes() throws ClassNotFoundException {
    @SuppressWarnings("unused")
    class Outermost {

        class LessOuter {

            class LeastOuter {

                void call() {
                    class LessInner {

                        class Innermost {
                        }
                    }
                }
            }
        }
    }
    Class<?> lessInnerClass = Class.forName(Outermost.LessOuter.LeastOuter.class.getName() + "$1LessInner");
    JavaClass innermost = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ENCLOSING_TYPES_PROPERTY_NAME, MAX_ITERATIONS_FOR_ENCLOSING_TYPES_DEFAULT_VALUE).importClass(Class.forName(lessInnerClass.getName() + "$Innermost"));
    JavaClass lessInner = innermost.getEnclosingClass().get();
    assertThat(lessInner).isFullyImported(true);
    assertThatType(lessInner).matches(lessInnerClass);
    JavaClass leastOuter = lessInner.getEnclosingClass().get();
    assertThat(leastOuter).isFullyImported(true);
    assertThatType(leastOuter).matches(Outermost.LessOuter.LeastOuter.class);
    JavaClass lessOuter = leastOuter.getEnclosingClass().get();
    assertThat(lessOuter).isFullyImported(true);
    assertThatType(lessOuter).matches(Outermost.LessOuter.class);
    JavaClass outermost = lessOuter.getEnclosingClass().get();
    assertThat(outermost).isFullyImported(true);
    assertThatType(outermost).matches(Outermost.class);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 97 with JavaClass

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_field_access_target_owners.

@Test
public void automatically_resolves_field_access_target_owners() {
    class Target {

        String field;
    }
    @SuppressWarnings({ "unused", "ConstantConditions" })
    class Origin {

        Object resolvesFieldAccessOwner() {
            Target target = null;
            return target.field;
        }
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ACCESSES_TO_TYPES_PROPERTY_NAME).importClass(Origin.class);
    JavaFieldAccess access = getOnlyElement(javaClass.getMethod("resolvesFieldAccessOwner").getFieldAccesses());
    assertThat(access.getTargetOwner()).isFullyImported(true);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 98 with JavaClass

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

the class ClassFileImporterCodeUnitReferencesTest method imports_method_and_constructor_references_to_self_as_accesses.

@Test
public void imports_method_and_constructor_references_to_self_as_accesses() {
    JavaClasses javaClasses = new ClassFileImporter().importClasses(Data_imports_method_and_constructor_references_as_accesses.Origin.class, Data_imports_method_and_constructor_references_as_accesses.ReferencedTarget.class);
    JavaClass targetClass = javaClasses.get(Data_imports_method_and_constructor_references_as_accesses.ReferencedTarget.class);
    assertThat(targetClass.getAccessesToSelf()).containsAll(targetClass.getCodeUnitAccessesToSelf());
    assertThat(targetClass.getCodeUnitAccessesToSelf()).containsAll(targetClass.getCodeUnitReferencesToSelf());
    assertThat(targetClass.getCodeUnitReferencesToSelf()).hasSize(8).containsAll(targetClass.getMethodReferencesToSelf()).containsAll(targetClass.getConstructorReferencesToSelf()).containsAll(targetClass.getMethod("call").getReferencesToSelf()).containsAll(targetClass.getConstructor().getReferencesToSelf());
    assertThat(targetClass.getConstructorReferencesToSelf()).hasSize(4).containsAll(targetClass.getConstructor().getReferencesToSelf());
    assertThat(targetClass.getMethodReferencesToSelf()).hasSize(4).containsAll(targetClass.getMethod("call").getReferencesToSelf());
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 99 with JavaClass

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

the class ClassFileImporterCodeUnitReferencesTest method test_imports_instance_method_references_bound_to_instance.

@Test
@UseDataProvider
public void test_imports_instance_method_references_bound_to_instance(Class<?> originClassInput, Class<?> targetClassInput) {
    JavaClasses javaClasses = new ClassFileImporter().importClasses(originClassInput, targetClassInput);
    JavaClass originClass = javaClasses.get(originClassInput);
    JavaClass targetClass = javaClasses.get(targetClassInput);
    assertThatAccess(getOnlyElement(originClass.getMethod("referencesInstanceMethodBoundToInstance").getMethodReferencesFromSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
    assertThatAccess(getOnlyElement(targetClass.getMethod("instanceMethodToReference").getReferencesToSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 100 with JavaClass

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

the class ClassFileImporterCodeUnitReferencesTest method test_imports_instance_method_references_not_bound_to_instance.

@Test
@UseDataProvider
public void test_imports_instance_method_references_not_bound_to_instance(Class<?> originClassInput, Class<?> targetClassInput) {
    JavaClasses javaClasses = new ClassFileImporter().importClasses(originClassInput, targetClassInput);
    JavaClass originClass = javaClasses.get(originClassInput);
    JavaClass targetClass = javaClasses.get(targetClassInput);
    assertThatAccess(getOnlyElement(originClass.getMethod("referencesInstanceMethodNotBoundToInstance").getMethodReferencesFromSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodNotBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
    assertThatAccess(getOnlyElement(targetClass.getMethod("instanceMethodToReference").getReferencesToSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodNotBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

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