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);
}
}
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);
}
};
}
Aggregations