Search in sources :

Example 66 with JavaClasses

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

the class ClassCacheTest method if_whole_classpath_is_set_true_then_the_whole_classpath_is_imported.

@Test
public void if_whole_classpath_is_set_true_then_the_whole_classpath_is_imported() {
    TestAnalysisRequest defaultOptions = new TestAnalysisRequest().withWholeClasspath(true);
    Class<?>[] expectedImportResult = new Class[] { getClass() };
    doReturn(new ClassFileImporter().importClasses(expectedImportResult)).when(cacheClassFileImporter).importClasses(any(ImportOptions.class), ArgumentMatchers.<Location>anyCollection());
    JavaClasses classes = cache.getClassesToAnalyzeFor(TestClass.class, defaultOptions);
    assertThatTypes(classes).matchExactly(expectedImportResult);
    verify(cacheClassFileImporter).importClasses(any(ImportOptions.class), locationCaptor.capture());
    assertThat(locationCaptor.getValue()).has(locationContaining("archunit")).has(locationContaining("asm")).has(locationContaining("google")).has(locationContaining("mockito"));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ImportOptions(com.tngtech.archunit.core.importer.ImportOptions) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) CacheClassFileImporter(com.tngtech.archunit.junit.internal.ClassCache.CacheClassFileImporter) Test(org.junit.Test)

Example 67 with JavaClasses

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

the class ClassFileImporterLambdaAccessesTest method imports_multiple_method_calls_from_single_lambda.

@Test
public void imports_multiple_method_calls_from_single_lambda() {
    class Target {

        void target() {
        }
    }
    @SuppressWarnings("unused")
    class Caller {

        private Target target;

        Runnable call() {
            return () -> {
                target.target();
                target.target();
            };
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    Set<JavaMethodCall> calls = classes.get(Caller.class).getMethodCallsFromSelf();
    assertThat(calls).hasSize(2);
    calls.forEach(call -> assertThatCall(call).isFrom("call").isTo(Target.class, "target"));
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 68 with JavaClasses

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

the class ClassFileImporterLambdaAccessesTest method imports_multiple_method_calls_from_multiple_lambda_in_one_method.

@Test
public void imports_multiple_method_calls_from_multiple_lambda_in_one_method() {
    @SuppressWarnings("unused")
    class Target {

        Target target(String s) {
            return this;
        }

        Target target(int i) {
            return this;
        }
    }
    @SuppressWarnings("unused")
    class Caller {

        private Target target;

        Function<String, Target> call(int i) {
            Function<Target, Target> function = t -> t.target(i);
            return s -> target.target(s);
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    Set<JavaMethodCall> calls = classes.get(Caller.class).getMethodCallsFromSelf();
    assertThat(filterOriginByName(calls, "call")).extracting(JavaMethodCall::getTarget).areExactly(1, targetCodeUnit("target", String.class)).areExactly(1, targetCodeUnit("target", int.class)).hasSize(2);
}
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) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 69 with JavaClasses

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

the class ClassFileImporterLambdaAccessesTest method imports_constructor_call_from_lambda_without_parameter.

@Test
public void imports_constructor_call_from_lambda_without_parameter() {
    class Target {
    }
    @SuppressWarnings({ "unused", "Convert2MethodRef" })
    class Caller {

        Runnable call() {
            return () -> new Target();
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    JavaConstructorCall call = getOnlyElement(filterOriginByName(classes.get(Caller.class).getConstructorCallsFromSelf(), "call"));
    assertThatCall(call).isFrom("call").isTo(Target.class, CONSTRUCTOR_NAME, getClass());
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Example 70 with JavaClasses

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

the class ClassFileImporterLambdaAccessesTest method imports_method_call_from_lambda_with_parameter.

@Test
public void imports_method_call_from_lambda_with_parameter() {
    @SuppressWarnings("unused")
    class Target {

        void target(String param) {
        }
    }
    @SuppressWarnings("unused")
    class Caller {

        private Target target;

        Consumer<String> call() {
            return s -> target.target(s);
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    JavaMethodCall call = getOnlyElement(classes.get(Caller.class).getMethodCallsFromSelf());
    assertThatCall(call).isFrom("call").isTo(Target.class, "target", String.class);
}
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) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Aggregations

JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)248 Test (org.junit.Test)184 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)98 JavaClass (com.tngtech.archunit.core.domain.JavaClass)72 Test (org.junit.jupiter.api.Test)54 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 ArchRule (com.tngtech.archunit.lang.ArchRule)19 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)15 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)14 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)13 Workspace (com.structurizr.Workspace)12 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)12 Set (java.util.Set)12 Application (org.archifacts.core.model.Application)12 Component (com.structurizr.model.Component)10 Container (com.structurizr.model.Container)10 ArtifactContainer (org.archifacts.core.model.ArtifactContainer)10