Search in sources :

Example 6 with JavaCodeUnit

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);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 7 with JavaCodeUnit

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());
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Example 8 with JavaCodeUnit

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);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) 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)

Aggregations

JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)8 JavaClass (com.tngtech.archunit.core.domain.JavaClass)7 Test (org.junit.Test)7 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)3 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)1 AccessTarget (com.tngtech.archunit.core.domain.AccessTarget)1 ConstructorCallTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget)1 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)1 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)1 CodeUnit (com.tngtech.archunit.core.importer.RawAccessRecord.CodeUnit)1