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