Search in sources :

Example 11 with ConditionEvent

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

the class NeverCondition method finish.

@Override
public void finish(ConditionEvents events) {
    ConditionEvents subEvents = new ConditionEvents();
    condition.finish(subEvents);
    for (ConditionEvent event : subEvents) {
        event.addInvertedTo(events);
    }
}
Also used : ConditionEvent(com.tngtech.archunit.lang.ConditionEvent) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 12 with ConditionEvent

use of com.tngtech.archunit.lang.ConditionEvent in project arch-unit-build-plugin-core by societe-generale.

the class NoTestIgnoreWithoutCommentRuleTest method notBeIgnoredWithoutAComment.

public static ArchCondition<JavaClass> notBeIgnoredWithoutAComment() {
    return new ArchCondition<JavaClass>(NO_JUNIT_IGNORE_WITHOUT_COMMENT_VIOLATION_MESSAGE) {

        @Override
        @SuppressWarnings("squid:S1166")
        public void check(JavaClass item, ConditionEvents events) {
            // class level checks
            String violationMessageAtClassLevel = item.getName() + ", at class level";
            addViolationEvent(buildViolationIfAnnotationWithNoValueFound(item, Ignore.class, violationMessageAtClassLevel), events);
            addViolationEvent(buildViolationIfAnnotationWithNoValueFound(item, Disabled.class, violationMessageAtClassLevel), events);
            // method level checks
            for (JavaMethod method : item.getMethods()) {
                String violationMessageAtMethodLevel = item.getName() + " - " + method.getName() + ", at method level";
                addViolationEvent(buildViolationIfAnnotationWithNoValueFound(method, Ignore.class, violationMessageAtMethodLevel), events);
                addViolationEvent(buildViolationIfAnnotationWithNoValueFound(method, Disabled.class, violationMessageAtMethodLevel), events);
            }
        }

        private void addViolationEvent(Optional<ConditionEvent> violation, ConditionEvents events) {
            if (violation.isPresent()) {
                events.add(violation.get());
            }
        }

        private Optional<ConditionEvent> buildViolationIfAnnotationWithNoValueFound(HasAnnotations item, Class annotation, String violationMessage) {
            try {
                if (getAnnotationValue(item, annotation).isEmpty()) {
                    return Optional.of(SimpleConditionEvent.violated(item, violationMessage));
                }
            } catch (IllegalArgumentException e) {
            // if there's no Ignore annotation, IllegalArgument exception is thrown.
            // we swallow it, as it means there's no annotation at class level.
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            // this won't happen, as we use reflection on a type that we know has the method we are looking for
            }
            return Optional.empty();
        }

        private String getAnnotationValue(HasAnnotations annotatedObj, Class annotation) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            Annotation ann = annotatedObj.getAnnotationOfType(annotation);
            Method valueMethod = ann.getClass().getDeclaredMethod("value");
            return (String) valueMethod.invoke(ann);
        }
    };
}
Also used : HasAnnotations(com.tngtech.archunit.core.domain.properties.HasAnnotations) Ignore(org.junit.Ignore) Optional(java.util.Optional) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Annotation(java.lang.annotation.Annotation) ConditionEvent(com.tngtech.archunit.lang.ConditionEvent) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Disabled(org.junit.jupiter.api.Disabled) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Aggregations

ConditionEvent (com.tngtech.archunit.lang.ConditionEvent)12 Test (org.junit.jupiter.api.Test)8 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)4 JavaClass (com.tngtech.archunit.core.domain.JavaClass)2 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)2 HasAnnotations (com.tngtech.archunit.core.domain.properties.HasAnnotations)2 ArchCondition (com.tngtech.archunit.lang.ArchCondition)2 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)2 File (java.io.File)2 Optional (java.util.Optional)2 Ignore (org.junit.Ignore)2 Disabled (org.junit.jupiter.api.Disabled)2 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1