Search in sources :

Example 1 with HasAnnotations

use of com.tngtech.archunit.core.domain.properties.HasAnnotations in project arch-unit-build-plugin-core by societe-generale.

the class NoTestIgnoreRuleTest method notBeenIgnore.

public static ArchCondition<JavaClass> notBeenIgnore() {
    return new ArchCondition<JavaClass>(NO_JUNIT_IGNORE_VIOLATION_MESSAGE) {

        @Override
        @SuppressWarnings("squid:S1166")
        public void check(JavaClass item, ConditionEvents events) {
            // class level checks
            String violationMessageAtClassLevel = item.getName() + ", at class level";
            // class level checks
            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 (item.getAnnotationOfType(annotation) != null) {
                    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.
            }
            return Optional.empty();
        }
    };
}
Also used : HasAnnotations(com.tngtech.archunit.core.domain.properties.HasAnnotations) ConditionEvent(com.tngtech.archunit.lang.ConditionEvent) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) Ignore(org.junit.Ignore) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Optional(java.util.Optional) ArchCondition(com.tngtech.archunit.lang.ArchCondition) 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)

Example 2 with HasAnnotations

use of com.tngtech.archunit.core.domain.properties.HasAnnotations 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

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 ConditionEvent (com.tngtech.archunit.lang.ConditionEvent)2 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)2 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)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