Search in sources :

Example 21 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition in project flink by splunk.

the class Conditions method haveLeafReturnTypes.

/**
 * Tests leaf return types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafReturnTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf return types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            for (JavaClass leafType : getLeafTypes(method.getReturnType())) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Returned leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 22 with ArchCondition

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

Example 23 with ArchCondition

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

the class GivenCodeUnitsTest method types_match_for_methods.

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

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

        @Override
        public boolean test(JavaMethod input) {
            return true;
        }
    }).should(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 : DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMember(com.tngtech.archunit.core.domain.JavaMember) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Example 24 with ArchCondition

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

the class SessionBeanRulesTest method haveAUniqueImplementation.

private static ArchCondition<JavaClass> haveAUniqueImplementation() {
    return new ArchCondition<JavaClass>("have a unique implementation") {

        @Override
        public void check(JavaClass businessInterface, ConditionEvents events) {
            events.add(new SimpleConditionEvent(businessInterface, businessInterface.getAllSubclasses().size() <= 1, describe(businessInterface)));
        }

        private String describe(JavaClass businessInterface) {
            return String.format("%s is implemented by %s", businessInterface.getSimpleName(), joinNamesOf(businessInterface.getAllSubclasses()));
        }

        private String joinNamesOf(Set<JavaClass> implementations) {
            if (implementations.isEmpty()) {
                return "";
            }
            Deque<JavaClass> toJoin = new LinkedList<>(implementations);
            StringBuilder sb = new StringBuilder(toJoin.pollFirst().getSimpleName());
            for (JavaClass javaClass : toJoin) {
                sb.append(", ").append(javaClass.getSimpleName());
            }
            return sb.toString();
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) Set(java.util.Set) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) LinkedList(java.util.LinkedList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 25 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition in project git-machete-intellij-plugin by VirtusLab.

the class UiThreadUnsafeMethodInvocationsTestSuite method only_ui_thread_unsafe_methods_should_call_other_ui_thread_unsafe_methods.

@Test
public void only_ui_thread_unsafe_methods_should_call_other_ui_thread_unsafe_methods() {
    methods().that().areNotAnnotatedWith(UIThreadUnsafe.class).should(new ArchCondition<JavaMethod>("never call any ${UIThreadUnsafeName} methods") {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            // which would in turn heavily reduce readability, hence potentially leading to bugs in the long run.
            if (method.getName().startsWith("access$") || method.getName().startsWith("lambda$")) {
                return;
            }
            method.getCallsFromSelf().forEach(access -> {
                AccessTarget accessTarget = access.getTarget();
                if (accessTarget.isAnnotatedWith(UIThreadUnsafe.class)) {
                    String message = "a non-${UIThreadUnsafeName} method ${method.getFullName()} " + "calls a ${UIThreadUnsafeName} method ${accessTarget.getFullName()}";
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            });
        }
    }).check(importedClasses);
}
Also used : ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

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