Search in sources :

Example 26 with ConditionEvents

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

the class ContainsOnlyConditionTest method satisfied_works_and_description_contains_mismatches.

@Test
public void satisfied_works_and_description_contains_mismatches() {
    ConditionEvents events = new ConditionEvents();
    containOnlyElementsThat(IS_SERIALIZABLE).check(ONE_SERIALIZABLE_AND_ONE_NON_SERIALIZABLE_OBJECT, events);
    assertThat(events).containViolations(isSerializableMessageFor(Object.class));
    events = new ConditionEvents();
    containOnlyElementsThat(IS_SERIALIZABLE).check(TWO_SERIALIZABLE_OBJECTS, events);
    assertThat(events).containNoViolation();
}
Also used : ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Example 27 with ConditionEvents

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

use of com.tngtech.archunit.lang.ConditionEvents 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)

Example 29 with ConditionEvents

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

the class PublicAPIRules method bePubliclyAccessible.

private static ArchCondition<JavaMember> bePubliclyAccessible() {
    return new ArchCondition<JavaMember>("be publicly accessible") {

        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean declaringClassIsPublic = getAllEnclosingClasses(member).allMatch(c -> c.getModifiers().contains(PUBLIC));
            boolean satisfied = member.getModifiers().contains(PUBLIC) && declaringClassIsPublic;
            events.add(new SimpleConditionEvent(member, satisfied, String.format("member %s.%s is %sdeclared in public location in %s", member.getOwner().getName(), member.getName(), satisfied ? "" : "not ", member.getSourceCodeLocation())));
        }

        private Stream<JavaClass> getAllEnclosingClasses(JavaMember member) {
            List<JavaClass> enclosingClasses = newArrayList(member.getOwner());
            while (getLast(enclosingClasses).getEnclosingClass().isPresent()) {
                enclosingClasses.add(getLast(enclosingClasses).getEnclosingClass().get());
            }
            return enclosingClasses.stream();
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMember(com.tngtech.archunit.core.domain.JavaMember) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 30 with ConditionEvents

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

the class PublicAPIRules method beInterfaces.

private static ArchCondition<? super JavaClass> beInterfaces() {
    return new ArchCondition<JavaClass>("be interfaces") {

        @Override
        public void check(JavaClass item, ConditionEvents events) {
            boolean satisfied = item.isInterface();
            events.add(new SimpleConditionEvent(item, satisfied, String.format("class %s is %sinterface", item.getName(), satisfied ? "" : "no ")));
        }
    };
}
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

ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)53 ArchCondition (com.tngtech.archunit.lang.ArchCondition)31 JavaClass (com.tngtech.archunit.core.domain.JavaClass)25 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)22 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)22 Test (org.junit.Test)15 List (java.util.List)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)8 JavaMember (com.tngtech.archunit.core.domain.JavaMember)7 Collectors (java.util.stream.Collectors)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 Stream (java.util.stream.Stream)6 JavaField (com.tngtech.archunit.core.domain.JavaField)5 ConditionEvent (com.tngtech.archunit.lang.ConditionEvent)4 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3