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);
}
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();
}
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));
}
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);
}
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);
}
Aggregations