Search in sources :

Example 16 with JavaMethod

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);
}
Also used : ClassWithThrowingMethod(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithThrowingMethod) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Test(org.junit.Test)

Example 17 with JavaMethod

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");
}
Also used : ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMember(com.tngtech.archunit.core.domain.JavaMember) ClassWithVariousMembers(com.tngtech.archunit.lang.syntax.elements.GivenCodeUnitsTest.ClassWithVariousMembers) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Example 18 with JavaMethod

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);
}
Also used : BufferedReader(java.io.BufferedReader) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Socket(java.net.Socket) IOException(java.io.IOException) Test(org.junit.Test)

Example 19 with JavaMethod

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));
}
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 20 with JavaMethod

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

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