Search in sources :

Example 1 with TryCatchBlock

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

the class ClassFileImporterAccessesTest method imports_complex_nested_try_catch_blocks.

@Test
public void imports_complex_nested_try_catch_blocks() {
    JavaClass javaClass = new ClassFileImporter().importClass(ClassWithComplexTryCatchBlocks.class);
    JavaConstructor constructor = javaClass.getConstructor();
    Set<TryCatchBlock> tryCatchBlocks = constructor.getTryCatchBlocks();
    assertThat(tryCatchBlocks).hasSize(3).areExactly(1, tryCatchBlock().declaredIn(constructor).catching(Exception.class).atLocation(ClassWithComplexTryCatchBlocks.class, 14)).areExactly(1, tryCatchBlock().declaredIn(constructor).catching(IllegalArgumentException.class, UnsupportedOperationException.class).atLocation(ClassWithComplexTryCatchBlocks.class, 17)).areExactly(1, tryCatchBlock().declaredIn(constructor).catching(Throwable.class).atLocation(ClassWithComplexTryCatchBlocks.class, 26));
}
Also used : ClassWithComplexTryCatchBlocks(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithComplexTryCatchBlocks) JavaClass(com.tngtech.archunit.core.domain.JavaClass) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 2 with TryCatchBlock

use of com.tngtech.archunit.core.domain.TryCatchBlock 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 3 with TryCatchBlock

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

the class ClassFileImporterAccessesTest method imports_try_catch_block_without_caught_throwables.

@Test
public void imports_try_catch_block_without_caught_throwables() {
    JavaClass javaClass = new ClassFileImporter().importClass(ClassWithTryCatchBlockWithoutThrowables.class);
    JavaMethod method = javaClass.getMethod("method");
    Set<TryCatchBlock> tryCatchBlocks = method.getTryCatchBlocks();
    assertThat(tryCatchBlocks).hasSize(1).areExactly(1, tryCatchBlock().declaredIn(method).catchingNoThrowables().atLocation(ClassWithTryCatchBlockWithoutThrowables.class, 7));
}
Also used : ClassWithTryCatchBlockWithoutThrowables(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithTryCatchBlockWithoutThrowables) 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) Test(org.junit.Test)

Example 4 with TryCatchBlock

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

the class ClassFileImporterAccessesTest method imports_try_catch_block_with_multiple_caught_throwables.

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

        void method() {
            try {
                new Object();
            } catch (IllegalStateException | IllegalArgumentException ignored) {
            } catch (UnsupportedOperationException ignored) {
                System.out.println("unsupported");
            } finally {
                System.out.println("finally");
            }
        }
    }
    JavaMethod method = new ClassFileImporter().importClass(SomeClass.class).getMethod("method");
    TryCatchBlock tryCatchBlock = getOnlyElement(method.getTryCatchBlocks());
    assertThatTypes(tryCatchBlock.getCaughtThrowables()).matchInAnyOrder(IllegalStateException.class, IllegalArgumentException.class, UnsupportedOperationException.class);
}
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 5 with TryCatchBlock

use of com.tngtech.archunit.core.domain.TryCatchBlock 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)

Aggregations

TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)7 Assertions.assertThatTryCatchBlock (com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock)7 Test (org.junit.Test)7 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)5 JavaClass (com.tngtech.archunit.core.domain.JavaClass)4 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 ClassWithComplexTryCatchBlocks (com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithComplexTryCatchBlocks)1 ClassWithTryCatchBlockWithoutThrowables (com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithTryCatchBlockWithoutThrowables)1 ClassWithTryWithResources (com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithTryWithResources)1