use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterMembersTest method imports_methods_with_correct_throws_declarations.
@Test
public void imports_methods_with_correct_throws_declarations() {
JavaMethod method = new ClassFileImporter().importUrl(getClass().getResource("testexamples/methodimport")).get(ClassWithThrowingMethod.class).getMethod("throwExceptions");
assertThatThrowsClause(method.getThrowsClause()).as("Throws types of method 'throwsExceptions'").matches(FirstCheckedException.class, SecondCheckedException.class);
assertThatTypes(method.getExceptionTypes()).matchExactly(FirstCheckedException.class, SecondCheckedException.class);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class CodeUnitsShouldTest method types_match_for_methods.
@Test
public void types_match_for_methods() {
EvaluationResult result = methods().that().arePrivate().should(new ArchCondition<JavaMember>("exist") {
@Override
public void check(JavaMember item, ConditionEvents events) {
}
}).andShould(new ArchCondition<JavaMethod>("not exist") {
@Override
public void check(JavaMethod method, ConditionEvents events) {
events.add(SimpleConditionEvent.violated(method, "expected violation"));
}
}).evaluate(importClasses(ClassWithVariousMembers.class));
assertThat(Joiner.on(" ").join(result.getFailureReport().getDetails())).contains("expected violation");
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_try_catch_blocks_from_same_starting_label.
@Test
public void imports_try_catch_blocks_from_same_starting_label() {
// we test that the second recorded block is not cleared out by accident when the first block is closed
@SuppressWarnings({ "unused", "ConstantConditions", "TryFinallyCanBeTryWithResources", "UnnecessaryReturnStatement" })
class SomeClass {
private void method(final int first, final boolean second) {
try {
Socket client = new Socket("", 0);
BufferedReader reader = new BufferedReader(null);
try {
return;
} finally {
reader.close();
}
} catch (Exception ignored) {
}
}
}
JavaMethod method = new ClassFileImporter().importClass(SomeClass.class).getMethod("method", int.class, boolean.class);
assertThat(method.getTryCatchBlocks()).hasSize(3);
}
use of com.tngtech.archunit.core.domain.JavaMethod 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.JavaMethod 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));
}
Aggregations