Search in sources :

Example 66 with JavaClass

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

Example 67 with JavaClass

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));
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) ClassWithTryWithResources(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithTryWithResources) Test(org.junit.Test)

Example 68 with JavaClass

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"));
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ChildClass(com.tngtech.archunit.core.importer.testexamples.complexexternal.ChildClass) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 69 with JavaClass

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

Example 70 with JavaClass

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