Search in sources :

Example 1 with JavaAccess

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

the class ClassFileImporterAccessesTest method classes_know_accesses_to_themselves.

@Test
public void classes_know_accesses_to_themselves() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/dependents"));
    JavaClass classHoldingDependencies = classes.get(ClassHoldingDependencies.class);
    JavaClass firstClassWithDependency = classes.get(FirstClassWithDependency.class);
    JavaClass secondClassWithDependency = classes.get(SecondClassWithDependency.class);
    Set<JavaAccess<?>> accesses = classHoldingDependencies.getAccessesToSelf();
    Set<JavaAccess<?>> expected = ImmutableSet.<JavaAccess<?>>builder().addAll(getByTargetOwner(classHoldingDependencies.getAccessesFromSelf(), classHoldingDependencies)).addAll(getByTargetOwner(firstClassWithDependency.getAccessesFromSelf(), classHoldingDependencies)).addAll(getByTargetOwner(secondClassWithDependency.getAccessesFromSelf(), classHoldingDependencies)).build();
    assertThat(accesses).as("Accesses to ClassWithDependents").isEqualTo(expected);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) Test(org.junit.Test)

Example 2 with JavaAccess

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

the class ClassFileImporterAccessesTest method all_accesses_know_which_exceptions_are_handled.

@Test
public void all_accesses_know_which_exceptions_are_handled() {
    @SuppressWarnings({ "unused", "WriteOnlyObject" })
    class Origin {

        void method() {
            new ArrayList<>().add(1);
            try {
                Data_all_accesses_know_which_exceptions_are_handled.Target target = new Data_all_accesses_know_which_exceptions_are_handled.Target();
                target.target(target.field);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            new Object();
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Origin.class, Data_all_accesses_know_which_exceptions_are_handled.Target.class);
    JavaMethod method = classes.get(Origin.class).getMethod("method");
    Set<JavaAccess<?>> accesses = method.getAccessesFromSelf();
    Function<JavaAccess<?>, Boolean> targetsTarget = GET_TARGET.then(Get.owner()).then(equivalentTo(Data_all_accesses_know_which_exceptions_are_handled.Target.class)::test);
    Map<Boolean, List<JavaAccess<?>>> accessesByTargetsTarget = accesses.stream().collect(groupingBy(targetsTarget));
    assertThat(accessesByTargetsTarget.get(true)).as("accesses that target target").isNotEmpty();
    for (JavaAccess<?> access : accessesByTargetsTarget.get(true)) {
        assertThatTryCatchBlock(getOnlyElement(access.getContainingTryBlocks())).isDeclaredIn(method).catches(IllegalStateException.class);
    }
    assertThat(accessesByTargetsTarget.get(false)).as("accesses that do not target target").isNotEmpty();
    for (JavaAccess<?> access : accessesByTargetsTarget.get(false)) {
        assertThat(access.getContainingTryBlocks()).as("containing try-catch-blocks").isEmpty();
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) ClassCallingSpecialTarget(com.tngtech.archunit.core.importer.testexamples.specialtargets.ClassCallingSpecialTarget) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with JavaAccess

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

the class ClassFileImporterLambdaAccessesTest method imports_complex_combination_of_lambda_accesses_to_nested_class.

@Test
public void imports_complex_combination_of_lambda_accesses_to_nested_class() {
    @SuppressWarnings("unused")
    class Caller {

        private Target target;

        void call() {
            Supplier<Supplier<Supplier<Target>>> quiteNestedConstructorCallSupplier = () -> () -> Target::new;
            Function<String, Supplier<Supplier<Supplier<Target>>>> quiteNestedMethodCallSupplier = s -> () -> () -> () -> target.inner.method(s);
            Supplier<Supplier<Supplier<String>>> quiteNestedFieldSupplier = () -> () -> () -> target.inner.field;
        }

        class Target {

            final Inner inner = new Inner();

            class Inner {

                String field;

                Target method(String s) {
                    return Target.this;
                }
            }
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Caller.class, Caller.Target.class);
    Set<JavaAccess<?>> accesses = classes.get(Caller.class).getAccessesFromSelf();
    assertThatAccesses(accesses).contain(access().fromOrigin(Caller.class, "call").toTarget(Caller.Target.class, CONSTRUCTOR_NAME)).contain(access().fromOrigin(Caller.class, "call").toTarget(Caller.Target.Inner.class, "method")).contain(access().fromOrigin(Caller.class, "call").toTarget(Caller.Target.Inner.class, "field"));
}
Also used : AccessesAssertion.access(com.tngtech.archunit.testutil.assertion.AccessesAssertion.access) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) RunWith(org.junit.runner.RunWith) DataProviders.$(com.tngtech.java.junit.dataprovider.DataProviders.$) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) JavaClassDescriptorImporterTestUtils.isLambdaMethodName(com.tngtech.archunit.core.importer.JavaClassDescriptorImporterTestUtils.isLambdaMethodName) Assertions.assertThatCall(com.tngtech.archunit.testutil.Assertions.assertThatCall) Assertions.assertThatAccesses(com.tngtech.archunit.testutil.Assertions.assertThatAccesses) CONSTRUCTOR_NAME(com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Assertions.assertThat(com.tngtech.archunit.testutil.Assertions.assertThat) Utils.namesOf(com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf) JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) Collectors.toSet(java.util.stream.Collectors.toSet) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) DataProviders.$$(com.tngtech.java.junit.dataprovider.DataProviders.$$) Predicate(java.util.function.Predicate) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) Formatters.formatNamesOf(com.tngtech.archunit.core.domain.Formatters.formatNamesOf) Consumer(java.util.function.Consumer) JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) Assertions.assertThatAccess(com.tngtech.archunit.testutil.Assertions.assertThatAccess) Condition(org.assertj.core.api.Condition) JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaConstructorReference(com.tngtech.archunit.core.domain.JavaConstructorReference) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) Supplier(java.util.function.Supplier) JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) Test(org.junit.Test)

Example 4 with JavaAccess

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

the class JavaAccessConditionTest method assertThatOnlyAccessToSomeClassFor.

private ConditionEventsAssertion assertThatOnlyAccessToSomeClassFor(JavaClass clazz, JavaAccessCondition<JavaAccess<?>> condition) {
    Set<JavaAccess<?>> accesses = filterByTarget(clazz.getAccessesFromSelf(), SomeClass.class);
    ConditionEvents events = new ConditionEvents();
    for (JavaAccess<?> access : accesses) {
        condition.check(access, events);
    }
    return assertThat(events);
}
Also used : JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Aggregations

JavaAccess (com.tngtech.archunit.core.domain.JavaAccess)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 Test (org.junit.Test)3 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)2 JavaClass (com.tngtech.archunit.core.domain.JavaClass)2 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)2 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 AccessTarget (com.tngtech.archunit.core.domain.AccessTarget)1 ConstructorCallTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget)1 FieldAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget)1 Formatters.formatNamesOf (com.tngtech.archunit.core.domain.Formatters.formatNamesOf)1 CONSTRUCTOR_NAME (com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME)1 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)1 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)1 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)1 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)1 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)1 Utils.namesOf (com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf)1 JavaClassDescriptorImporterTestUtils.isLambdaMethodName (com.tngtech.archunit.core.importer.JavaClassDescriptorImporterTestUtils.isLambdaMethodName)1 ClassCallingSpecialTarget (com.tngtech.archunit.core.importer.testexamples.specialtargets.ClassCallingSpecialTarget)1