Search in sources :

Example 61 with JavaClass

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

the class ClassFileImporterAccessesTest method dependency_target_classes_are_derived_correctly.

@Test
public void dependency_target_classes_are_derived_correctly() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/integration"));
    JavaClass javaClass = classes.get(ClassXDependingOnClassesABCD.class);
    Set<JavaClass> expectedTargetClasses = ImmutableSet.of(classes.get(ClassA.class), classes.get(ClassBDependingOnClassA.class), classes.get(ClassCDependingOnClassB_SuperclassOfX.class), classes.get(ClassD.class), classes.get(InterfaceOfClassX.class));
    Set<JavaClass> targetClasses = withoutJavaLangTargets(javaClass.getDirectDependenciesFromSelf()).stream().map(Dependency::getTargetClass).collect(toSet());
    assertThat(targetClasses).isEqualTo(expectedTargetClasses);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassCDependingOnClassB_SuperclassOfX(com.tngtech.archunit.core.importer.testexamples.integration.ClassCDependingOnClassB_SuperclassOfX) JavaClass(com.tngtech.archunit.core.domain.JavaClass) InterfaceOfClassX(com.tngtech.archunit.core.importer.testexamples.integration.InterfaceOfClassX) ClassBDependingOnClassA(com.tngtech.archunit.core.importer.testexamples.integration.ClassBDependingOnClassA) ClassD(com.tngtech.archunit.core.importer.testexamples.integration.ClassD) ClassA(com.tngtech.archunit.core.importer.testexamples.integration.ClassA) ClassBDependingOnClassA(com.tngtech.archunit.core.importer.testexamples.integration.ClassBDependingOnClassA) Test(org.junit.Test)

Example 62 with JavaClass

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

the class ClassFileImporterAccessesTest method identifies_call_origin_if_signature_and_descriptor_deviate.

@Test
public void identifies_call_origin_if_signature_and_descriptor_deviate() {
    // Kotlin inline functions cause the creation of extra classes where the signature of the respective method shows
    // the real types while the descriptor shows the erased types. The erasure of the real types from the signature
    // does then not match the descriptor in some cases.
    // 
    // The file `MismatchBetweenDescriptorAndSignature.class` is a byproduct of the following source code:
    // --------------------
    // package com.example
    // 
    // object CrashArchUnit {
    // fun useInlineFunctionCrashingArchUnit(strings: List<String>) = strings.groupingBy { it.length }
    // }
    // --------------------
    // With the current Kotlin version this creates a synthetic class file `CrashArchUnit$useInlineFunctionCrashingArchUnit$$inlined$groupingBy$1.class`
    // which has been copied and renamed to `MismatchBetweenDescriptorAndSignature.class`.
    JavaClass javaClass = getOnlyElement(new ClassFileImporter().importLocations(singleton(Location.of(relativeResourceUri(getClass(), "testexamples/MismatchBetweenDescriptorAndSignature.class")))));
    // this method in the problematic compiled class has a mismatch between return type of descriptor and signature
    assertThat(javaClass.getMethod("keyOf", Object.class).getMethodCallsFromSelf()).isNotEmpty();
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 63 with JavaClass

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

the class ClassFileImporterAccessesTest method imports_complex_nested_try_catch_blocks.

@Test
public void imports_complex_nested_try_catch_blocks() {
    JavaClass javaClass = new ClassFileImporter().importClass(ClassWithComplexTryCatchBlocks.class);
    JavaConstructor constructor = javaClass.getConstructor();
    Set<TryCatchBlock> tryCatchBlocks = constructor.getTryCatchBlocks();
    assertThat(tryCatchBlocks).hasSize(3).areExactly(1, tryCatchBlock().declaredIn(constructor).catching(Exception.class).atLocation(ClassWithComplexTryCatchBlocks.class, 14)).areExactly(1, tryCatchBlock().declaredIn(constructor).catching(IllegalArgumentException.class, UnsupportedOperationException.class).atLocation(ClassWithComplexTryCatchBlocks.class, 17)).areExactly(1, tryCatchBlock().declaredIn(constructor).catching(Throwable.class).atLocation(ClassWithComplexTryCatchBlocks.class, 26));
}
Also used : ClassWithComplexTryCatchBlocks(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithComplexTryCatchBlocks) JavaClass(com.tngtech.archunit.core.domain.JavaClass) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 64 with JavaClass

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

the class ClassFileImporterAccessesTest method imports_constructor_calls_on_self.

@Test
public void imports_constructor_calls_on_self() {
    JavaClass classThatCallsOwnConstructor = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsOwnConstructor.class);
    JavaCodeUnit caller = classThatCallsOwnConstructor.getCodeUnitWithParameterTypes("copy");
    Set<JavaConstructorCall> calls = classThatCallsOwnConstructor.getConstructorCallsFromSelf();
    assertThatCall(getOnlyByCaller(calls, caller)).isFrom(caller).isTo(classThatCallsOwnConstructor.getConstructor(String.class)).inLineNumber(8);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Example 65 with JavaClass

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

the class ClassFileImporterAccessesTest method classes_know_method_calls_to_themselves.

@Test
public void classes_know_method_calls_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<JavaMethodCall> calls = classHoldingDependencies.getMethodCallsToSelf();
    Set<JavaMethodCall> expected = ImmutableSet.<JavaMethodCall>builder().addAll(classHoldingDependencies.getMethodCallsFromSelf()).addAll(getByTargetOwner(firstClassWithDependency.getMethodCallsFromSelf(), classHoldingDependencies)).addAll(getByTargetOwner(secondClassWithDependency.getMethodCallsFromSelf(), classHoldingDependencies)).build();
    assertThat(calls).as("Method calls to class").isEqualTo(expected);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) 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