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