use of com.tngtech.archunit.lang.ConditionEvents in project ArchUnit by TNG.
the class ContainsOnlyConditionTest method if_there_are_no_input_events_no_ContainsOnlyEvent_is_added.
@Test
public void if_there_are_no_input_events_no_ContainsOnlyEvent_is_added() {
ConditionEvents events = new ConditionEvents();
containOnlyElementsThat(IS_SERIALIZABLE).check(emptyList(), events);
assertThat(events.isEmpty()).as("events are empty").isTrue();
}
use of com.tngtech.archunit.lang.ConditionEvents 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();
}
};
}
use of com.tngtech.archunit.lang.ConditionEvents 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);
}
use of com.tngtech.archunit.lang.ConditionEvents 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);
}
};
}
use of com.tngtech.archunit.lang.ConditionEvents 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));
}
}
};
}
Aggregations