use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method fields_know_their_accesses.
@Test
public void fields_know_their_accesses() {
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<JavaFieldAccess> accesses = classHoldingDependencies.getField("someInt").getAccessesToSelf();
Set<JavaFieldAccess> expected = ImmutableSet.<JavaFieldAccess>builder().addAll(getByName(classHoldingDependencies.getFieldAccessesFromSelf(), "someInt")).addAll(getByName(firstClassWithDependency.getFieldAccessesFromSelf(), "someInt")).addAll(getByName(secondClassWithDependency.getFieldAccessesFromSelf(), "someInt")).build();
assertThat(accesses).as("Field Accesses to someInt").isEqualTo(expected);
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_try_catch_block_with_resources.
@Test
public void imports_try_catch_block_with_resources() {
JavaClass javaClass = new ClassFileImporter().importClass(ClassWithTryWithResources.class);
JavaMethod method = javaClass.getMethod("method");
Set<TryCatchBlock> tryCatchBlocks = method.getTryCatchBlocks();
assertThat(tryCatchBlocks).hasSize(// each declared closeable in try-with-resources adds another try-catch-block
3).areExactly(1, tryCatchBlock().declaredIn(method).catching(IOException.class).atLocation(ClassWithTryWithResources.class, 11));
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_method_calls_on_overridden_external_class.
@Test
public void imports_method_calls_on_overridden_external_class() {
JavaClass classThatCallsExternalMethod = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(ExternalOverriddenMethodCall.class);
JavaMethodCall call = getOnlyElement(classThatCallsExternalMethod.getMethodCallsFromSelf());
assertThatCall(call).isFrom(classThatCallsExternalMethod.getCodeUnitWithParameterTypes("call")).inLineNumber(9);
MethodCallTarget target = call.getTarget();
assertThat(target.getFullName()).isEqualTo(ChildClass.class.getName() + ".overrideMe()");
assertThat(resolve(target).getFullName()).isEqualTo(ChildClass.class.getName() + ".overrideMe()");
assertThat(reflect(target)).isEqualTo(method(ChildClass.class, "overrideMe"));
}
use of com.tngtech.archunit.core.domain.JavaClass 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);
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_method_calls_on_other.
@Test
public void imports_method_calls_on_other() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport"));
JavaClass classThatCallsOtherMethod = classes.get(CallsOtherMethod.class);
JavaClass other = classes.get(CallsOwnMethod.class);
JavaMethodCall call = getOnlyElement(classThatCallsOtherMethod.getMethodCallsFromSelf());
assertThatCall(call).isFrom(classThatCallsOtherMethod.getCodeUnitWithParameterTypes("getFromOther")).isTo(other.getMethod("getString")).inLineNumber(7);
}
Aggregations