Search in sources :

Example 31 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.

the class ClassFileImporterAccessesTest method all_accesses_know_which_exceptions_are_handled.

@Test
public void all_accesses_know_which_exceptions_are_handled() {
    @SuppressWarnings({ "unused", "WriteOnlyObject" })
    class Origin {

        void method() {
            new ArrayList<>().add(1);
            try {
                Data_all_accesses_know_which_exceptions_are_handled.Target target = new Data_all_accesses_know_which_exceptions_are_handled.Target();
                target.target(target.field);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            new Object();
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Origin.class, Data_all_accesses_know_which_exceptions_are_handled.Target.class);
    JavaMethod method = classes.get(Origin.class).getMethod("method");
    Set<JavaAccess<?>> accesses = method.getAccessesFromSelf();
    Function<JavaAccess<?>, Boolean> targetsTarget = GET_TARGET.then(Get.owner()).then(equivalentTo(Data_all_accesses_know_which_exceptions_are_handled.Target.class)::test);
    Map<Boolean, List<JavaAccess<?>>> accessesByTargetsTarget = accesses.stream().collect(groupingBy(targetsTarget));
    assertThat(accessesByTargetsTarget.get(true)).as("accesses that target target").isNotEmpty();
    for (JavaAccess<?> access : accessesByTargetsTarget.get(true)) {
        assertThatTryCatchBlock(getOnlyElement(access.getContainingTryBlocks())).isDeclaredIn(method).catches(IllegalStateException.class);
    }
    assertThat(accessesByTargetsTarget.get(false)).as("accesses that do not target target").isNotEmpty();
    for (JavaAccess<?> access : accessesByTargetsTarget.get(false)) {
        assertThat(access.getContainingTryBlocks()).as("containing try-catch-blocks").isEmpty();
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) ClassCallingSpecialTarget(com.tngtech.archunit.core.importer.testexamples.specialtargets.ClassCallingSpecialTarget) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 32 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.

the class ClassFileImporterAccessesTest method method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks.

@Test
public void method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks() {
    JavaClasses classes = new ClassFileImporter().importClasses(ClassWithComplexTryCatchBlocks.class, ClassHoldingMethods.class);
    JavaClass classHoldingMethods = classes.get(ClassHoldingMethods.class);
    JavaClass classWithTryCatchBlocks = classes.get(ClassWithComplexTryCatchBlocks.class);
    JavaMethod setSomeInt = classHoldingMethods.getMethod("setSomeInt", int.class);
    JavaMethod setSomeString = classHoldingMethods.getMethod("setSomeString", String.class);
    JavaMethod doSomething = classHoldingMethods.getMethod("doSomething");
    JavaConstructor constructor = classWithTryCatchBlocks.getConstructor();
    assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(12)).isNotWrappedInTryCatch();
    assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(14)).isWrappedWithTryCatchFor(Exception.class);
    assertThatCall(methodCallTo(doSomething).from(constructor).inLineNumber(17)).isWrappedWithTryCatchFor(Exception.class).isWrappedWithTryCatchFor(IllegalArgumentException.class).isWrappedWithTryCatchFor(UnsupportedOperationException.class);
    assertThatCall(methodCallTo(setSomeString).from(constructor).inLineNumber(26)).isWrappedWithTryCatchFor(Throwable.class);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 33 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.

the class ClassFileImporterAccessesTest method imports_own_get_field_access.

@Test
public void imports_own_get_field_access() {
    JavaClass classWithOwnFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(OwnFieldAccess.class);
    JavaMethod getStringValue = classWithOwnFieldAccess.getMethod("getStringValue");
    JavaFieldAccess access = getOnlyElement(getStringValue.getFieldAccesses());
    assertThatAccess(access).isOfType(GET).isFrom(getStringValue).isTo("stringValue").inLineNumber(8);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Test(org.junit.Test)

Example 34 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.

the class ClassFileImporterAccessesTest method imports_simple_try_catch_block.

@Test
public void imports_simple_try_catch_block() {
    @SuppressWarnings("unused")
    class SomeClass {

        void method() {
            try {
                new Object();
            } catch (IllegalStateException ignored) {
            }
        }
    }
    JavaMethod method = new ClassFileImporter().importClass(SomeClass.class).getMethod("method");
    TryCatchBlock tryCatchBlock = getOnlyElement(method.getTryCatchBlocks());
    assertThatTypes(tryCatchBlock.getCaughtThrowables()).matchExactly(IllegalStateException.class);
    assertThat(tryCatchBlock.getAccessesContainedInTryBlock()).isEqualTo(method.getAccessesFromSelf());
    assertThat(tryCatchBlock.getOwner()).isEqualTo(method);
    assertThat(tryCatchBlock.getSourceCodeLocation()).isEqualTo(getOnlyElement(method.getAccessesFromSelf()).getSourceCodeLocation());
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) Test(org.junit.Test)

Example 35 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.

the class ClassFileImporterAccessesTest method imports_simple_try_catch_block_that_returns_directly_from_try.

@Test
public void imports_simple_try_catch_block_that_returns_directly_from_try() {
    // In this case the end-label has no associated line number, so it must be handled differently
    @SuppressWarnings("unused")
    class SomeClass {

        Object method() {
            try {
                return new Object();
            } catch (IllegalStateException ignored) {
                return null;
            }
        }
    }
    JavaMethod method = new ClassFileImporter().importClass(SomeClass.class).getMethod("method");
    TryCatchBlock tryCatchBlock = getOnlyElement(method.getTryCatchBlocks());
    assertThatTypes(tryCatchBlock.getCaughtThrowables()).matchExactly(IllegalStateException.class);
    assertThat(tryCatchBlock.getAccessesContainedInTryBlock()).isEqualTo(method.getAccessesFromSelf());
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) Test(org.junit.Test)

Aggregations

JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)56 JavaClass (com.tngtech.archunit.core.domain.JavaClass)36 Test (org.junit.Test)32 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)22 ArchCondition (com.tngtech.archunit.lang.ArchCondition)21 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)14 List (java.util.List)12 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)10 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)9 Collectors (java.util.stream.Collectors)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 Stream (java.util.stream.Stream)8 Collections (java.util.Collections)7 JavaField (com.tngtech.archunit.core.domain.JavaField)6 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)6 JavaType (com.tngtech.archunit.core.domain.JavaType)6 HasName (com.tngtech.archunit.core.domain.properties.HasName)6 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)5 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)5 Assertions.assertThatTryCatchBlock (com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock)5