use of com.tngtech.archunit.core.domain.JavaCodeUnit in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_non_unique_targets_for_diamond_scenarios.
@Test
public void imports_non_unique_targets_for_diamond_scenarios() {
JavaClasses diamondScenario = new ClassFileImporter().importUrl(getClass().getResource("testexamples/diamond"));
JavaClass classCallingDiamond = diamondScenario.get(ClassCallingDiamond.class);
JavaClass diamondPeakInterface = diamondScenario.get(InterfaceD.class);
JavaClass diamondPeakClass = diamondScenario.get(ClassImplementingD.class);
Set<JavaMethodCall> calls = classCallingDiamond.getMethodCallsFromSelf();
assertThat(calls).hasSize(2);
JavaCodeUnit callInterface = classCallingDiamond.getCodeUnitWithParameterTypes(ClassCallingDiamond.callInterface);
JavaMethodCall callToInterface = getOnlyByCaller(calls, callInterface);
assertThatCall(callToInterface).isFrom(callInterface).inLineNumber(ClassCallingDiamond.callInterfaceLineNumber);
// NOTE: There is no java.lang.reflect.Method InterfaceD.implementMe(), because the method is inherited
assertThat(callToInterface.getTarget().getName()).isEqualTo(InterfaceD.implementMe);
assertThat(callToInterface.getTarget().getOwner()).isEqualTo(diamondPeakInterface);
assertThat(callToInterface.getTarget().getRawParameterTypes()).isEmpty();
assertThat(resolve(callToInterface.getTarget())).isEquivalentTo(InterfaceB.class, InterfaceB.implementMe);
JavaCodeUnit callImplementation = classCallingDiamond.getCodeUnitWithParameterTypes(ClassCallingDiamond.callImplementation);
assertThatCall(getOnlyByCaller(calls, callImplementation)).isFrom(callImplementation).isTo(diamondPeakClass.getMethod(InterfaceD.implementMe)).inLineNumber(ClassCallingDiamond.callImplementationLineNumber);
}
use of com.tngtech.archunit.core.domain.JavaCodeUnit in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_constructor_calls_on_external_class.
@Test
public void imports_constructor_calls_on_external_class() throws Exception {
JavaClass classThatCallsOwnConstructor = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsOwnConstructor.class);
JavaCodeUnit constructorCallingObjectInit = classThatCallsOwnConstructor.getConstructor(String.class);
JavaConstructorCall objectInitCall = getOnlyElement(constructorCallingObjectInit.getConstructorCallsFromSelf());
assertThatCall(objectInitCall).isFrom(constructorCallingObjectInit).inLineNumber(4);
ConstructorCallTarget target = objectInitCall.getTarget();
assertThat(target.getFullName()).isEqualTo(Object.class.getName() + ".<init>()");
assertThat(reflect(target)).isEqualTo(Object.class.getConstructor());
}
use of com.tngtech.archunit.core.domain.JavaCodeUnit in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_constructor_calls_on_other.
@Test
public void imports_constructor_calls_on_other() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport"));
JavaClass classThatCallsOtherConstructor = classes.get(CallsOtherConstructor.class);
JavaClass otherClass = classes.get(CallsOwnConstructor.class);
JavaCodeUnit caller = classThatCallsOtherConstructor.getCodeUnitWithParameterTypes("createOther");
Set<JavaConstructorCall> calls = classThatCallsOtherConstructor.getConstructorCallsFromSelf();
assertThatCall(getOnlyByCaller(calls, caller)).isFrom(caller).isTo(otherClass.getConstructor(String.class)).inLineNumber(5);
}
Aggregations