Search in sources :

Example 1 with JavaConstructor

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

the class GivenCodeUnitsTest method types_match_for_constructors.

@Test
public void types_match_for_constructors() {
    EvaluationResult result = constructors().that(new DescribedPredicate<JavaMember>("are there") {

        @Override
        public boolean test(JavaMember input) {
            return true;
        }
    }).and(new DescribedPredicate<JavaConstructor>("are there") {

        @Override
        public boolean test(JavaConstructor input) {
            return true;
        }
    }).should(new ArchCondition<JavaConstructor>("not exist") {

        @Override
        public void check(JavaConstructor constructor, ConditionEvents events) {
            events.add(SimpleConditionEvent.violated(constructor, "expected violation"));
        }
    }).evaluate(importClasses(ClassWithVariousMembers.class));
    assertThat(Joiner.on(" ").join(result.getFailureReport().getDetails())).contains("expected violation");
}
Also used : DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMember(com.tngtech.archunit.core.domain.JavaMember) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Example 2 with JavaConstructor

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

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

the class ClassFileImporterAccessesTest method resolve.

// only temporary to make sure resolveMember() and resolve() are in sync. Inline again when we throw out resolve()
private JavaConstructor resolve(ConstructorCallTarget target) {
    JavaConstructor constructor = target.resolveMember().get();
    assertThat(target.resolve()).containsExactly(constructor);
    return constructor;
}
Also used : JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor)

Example 4 with JavaConstructor

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

the class ClassFileImporterAnnotationsTest method imports_constructors_with_complex_annotations_correctly.

@Test
public void imports_constructors_with_complex_annotations_correctly() throws Exception {
    JavaConstructor constructor = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getConstructor();
    JavaAnnotation<JavaConstructor> annotation = constructor.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue.class.getName());
    assertThat((Object[]) annotation.get("classes").get()).extracting("name").containsExactly(Object.class.getName(), Serializable.class.getName());
    assertThat(annotation.getOwner()).isEqualTo(constructor);
    JavaAnnotation<?> subAnnotation = (JavaAnnotation<?>) annotation.get("subAnnotation").get();
    assertThat(subAnnotation.get("value").get()).isEqualTo("changed");
    assertThat(subAnnotation.getOwner()).isEqualTo(annotation);
    assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(constructor);
    JavaAnnotation<?>[] subAnnotationArray = (JavaAnnotation<?>[]) annotation.get("subAnnotationArray").get();
    assertThat(subAnnotationArray[0].get("value").get()).isEqualTo("another");
    assertThat(subAnnotationArray[0].getOwner()).isEqualTo(annotation);
    assertThat(subAnnotationArray[0].getAnnotatedElement()).isEqualTo(constructor);
    assertThat(constructor).isEquivalentTo(ClassWithAnnotatedMethods.class.getConstructor());
}
Also used : Serializable(java.io.Serializable) ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 5 with JavaConstructor

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

the class CodeUnitsShouldTest method types_match_for_constructors.

@Test
public void types_match_for_constructors() {
    EvaluationResult result = constructors().that().arePrivate().should(new ArchCondition<JavaMember>("exist") {

        @Override
        public void check(JavaMember item, ConditionEvents events) {
        }
    }).andShould(new ArchCondition<JavaConstructor>("not exist") {

        @Override
        public void check(JavaConstructor constructor, ConditionEvents events) {
            events.add(SimpleConditionEvent.violated(constructor, "expected violation"));
        }
    }).evaluate(importClasses(ClassWithVariousMembers.class));
    assertThat(Joiner.on(" ").join(result.getFailureReport().getDetails())).contains("expected violation");
}
Also used : ArchCondition(com.tngtech.archunit.lang.ArchCondition) 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) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Aggregations

JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)13 Test (org.junit.Test)11 JavaClass (com.tngtech.archunit.core.domain.JavaClass)7 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 JavaMember (com.tngtech.archunit.core.domain.JavaMember)2 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)2 ArchCondition (com.tngtech.archunit.lang.ArchCondition)2 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)2 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)1 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)1 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)1 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)1 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)1 JavaParameter (com.tngtech.archunit.core.domain.JavaParameter)1 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)1 SimpleAnnotation (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation)1 ClassWithAnnotatedMethods (com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods)1 ClassWithComplexTryCatchBlocks (com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithComplexTryCatchBlocks)1