Search in sources :

Example 11 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition 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 12 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition in project ArchUnit by TNG.

the class GivenMembersTest method beAnnotatedWith.

static ArchCondition<JavaMember> beAnnotatedWith(final Class<? extends Annotation> annotationType) {
    return new ArchCondition<JavaMember>("be annotated with @%s", annotationType.getSimpleName()) {

        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean satisfied = member.isAnnotatedWith(annotationType);
            String message = String.format("Member '%s' %s @%s", formatMember(member), satisfied ? "is annotated with" : "is not annotated with", annotationType.getSimpleName());
            events.add(new SimpleConditionEvent(member, satisfied, message));
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMember(com.tngtech.archunit.core.domain.JavaMember) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 13 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition 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 14 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition in project ArchUnit by TNG.

the class PublicAPIRules method notBePublic.

private static ArchCondition<JavaMember> notBePublic() {
    return new ArchCondition<JavaMember>("not be public") {

        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean satisfied = !member.getModifiers().contains(PUBLIC);
            events.add(new SimpleConditionEvent(member, satisfied, String.format("member %s.%s is %spublic in %s", member.getOwner().getName(), member.getName(), satisfied ? "not " : "", member.getSourceCodeLocation())));
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMember(com.tngtech.archunit.core.domain.JavaMember) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 15 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition in project ArchUnit by TNG.

the class PublicAPIRules method bePublicAPIForInheritance.

private static ArchCondition<JavaClass> bePublicAPIForInheritance() {
    return new ArchCondition<JavaClass>("be public API for inheritance") {

        @Override
        public void check(JavaClass item, ConditionEvents events) {
            boolean satisfied = item.isAnnotatedWith(publicApiForInheritance()) || markedAsPublicAPIForInheritance().test(item);
            events.add(new SimpleConditionEvent(item, satisfied, String.format("class %s is %smeant for inheritance", item.getName(), satisfied ? "" : "not ")));
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Aggregations

ArchCondition (com.tngtech.archunit.lang.ArchCondition)30 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)30 JavaClass (com.tngtech.archunit.core.domain.JavaClass)23 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)21 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)20 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)8 List (java.util.List)8 JavaMember (com.tngtech.archunit.core.domain.JavaMember)7 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 Collections (java.util.Collections)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 Test (org.junit.Test)5 JavaField (com.tngtech.archunit.core.domain.JavaField)4 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)2 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)2