Search in sources :

Example 11 with SimpleConditionEvent

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

the class NeverConditionTest method addOneViolatedOneSatisfied.

private static void addOneViolatedOneSatisfied(Object item, ConditionEvents events) {
    events.add(new SimpleConditionEvent(item, false, ORIGINALLY_MISMATCH));
    events.add(new SimpleConditionEvent(item, true, ORIGINALLY_NO_MISMATCH));
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent)

Example 12 with SimpleConditionEvent

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

use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.

the class AbstractImmutableTests method haveAPublicGetter.

private ArchCondition<JavaField> haveAPublicGetter() {
    return new // $NON-NLS-1$
    ArchCondition<>(// $NON-NLS-1$
    "have a public getter") {

        @Override
        public void check(JavaField javaField, ConditionEvents events) {
            if (!javaField.getModifiers().contains(STATIC)) {
                JavaClass javaClass = javaField.getOwner();
                // $NON-NLS-1$
                String getterName = "get" + javaField.getName().substring(0, 1).toUpperCase() + javaField.getName().substring(1);
                if (javaField.getRawType().getFullName().equals("java.lang.Boolean") || javaField.getRawType().getFullName().equals("boolean")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    // $NON-NLS-1$
                    getterName = "is" + javaField.getName().substring(0, 1).toUpperCase() + javaField.getName().substring(1);
                }
                boolean isConditionSatisfied = false;
                try {
                    JavaMethod method = javaClass.getMethod(getterName);
                    isConditionSatisfied = method != null && method.getModifiers().contains(PUBLIC);
                } catch (IllegalArgumentException exception) {
                    // Getter not found
                    isConditionSatisfied = false;
                }
                // $NON-NLS-1$
                String message = "The field has a getter";
                if (!isConditionSatisfied) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    message = "The field " + javaField.getFullName() + " does not have a getter";
                }
                events.add(new SimpleConditionEvent(javaField, isConditionSatisfied, message));
            }
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaField(com.tngtech.archunit.core.domain.JavaField) 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 14 with SimpleConditionEvent

use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.

the class AbstractImmutableTests method notHaveSetters.

private ArchCondition<JavaClass> notHaveSetters() {
    return new // $NON-NLS-1$
    ArchCondition<>(// $NON-NLS-1$
    "not have setters") {

        @Override
        public void check(JavaClass javaClass, ConditionEvents events) {
            // @formatter:off
            long settersCount = javaClass.getMethods().stream().filter(// $NON-NLS-1$
            javaMethod -> javaMethod.getName().startsWith("set")).count();
            // @formatter:on
            boolean isConditionSatisfied = settersCount == 0;
            // $NON-NLS-1$
            String message = "The class does not have any setters";
            if (!isConditionSatisfied) {
                // $NON-NLS-1$//$NON-NLS-2$
                message = "The class " + javaClass.getName() + " does have setters";
            }
            events.add(new SimpleConditionEvent(javaClass, isConditionSatisfied, message));
        }
    };
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) Immutable(org.eclipse.sirius.components.annotations.Immutable) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) STATIC(com.tngtech.archunit.core.domain.JavaModifier.STATIC) ArchCondition(com.tngtech.archunit.lang.ArchCondition) FINAL(com.tngtech.archunit.core.domain.JavaModifier.FINAL) JavaField(com.tngtech.archunit.core.domain.JavaField) MessageFormat(java.text.MessageFormat) Test(org.junit.jupiter.api.Test) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) ArchRule(com.tngtech.archunit.lang.ArchRule) ArchRuleDefinition(com.tngtech.archunit.lang.syntax.ArchRuleDefinition) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) PUBLIC(com.tngtech.archunit.core.domain.JavaModifier.PUBLIC) 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

SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)14 ArchCondition (com.tngtech.archunit.lang.ArchCondition)13 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)13 JavaClass (com.tngtech.archunit.core.domain.JavaClass)11 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)5 JavaField (com.tngtech.archunit.core.domain.JavaField)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 JavaMember (com.tngtech.archunit.core.domain.JavaMember)3 FINAL (com.tngtech.archunit.core.domain.JavaModifier.FINAL)3 PUBLIC (com.tngtech.archunit.core.domain.JavaModifier.PUBLIC)3 STATIC (com.tngtech.archunit.core.domain.JavaModifier.STATIC)3 MessageFormat (java.text.MessageFormat)3 ArchRule (com.tngtech.archunit.lang.ArchRule)2 ArchRuleDefinition (com.tngtech.archunit.lang.syntax.ArchRuleDefinition)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 Immutable (org.eclipse.sirius.components.annotations.Immutable)2 Test (org.junit.jupiter.api.Test)2 PublicAPI (com.tngtech.archunit.PublicAPI)1 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)1