use of com.tngtech.archunit.lang.ArchCondition in project flink by splunk.
the class Conditions method haveLeafReturnTypes.
/**
* Tests leaf return types of a method against the given predicate.
*
* <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
*/
public static ArchCondition<JavaMethod> haveLeafReturnTypes(DescribedPredicate<JavaClass> typePredicate) {
return new ArchCondition<JavaMethod>("have leaf return types" + typePredicate.getDescription()) {
@Override
public void check(JavaMethod method, ConditionEvents events) {
for (JavaClass leafType : getLeafTypes(method.getReturnType())) {
if (!isJavaClass(leafType)) {
continue;
}
if (!typePredicate.apply(leafType)) {
final String message = String.format("%s: Returned leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
events.add(SimpleConditionEvent.violated(method, message));
}
}
}
};
}
use of com.tngtech.archunit.lang.ArchCondition in project ArchUnit by TNG.
the class CodeUnitsShouldTest method types_match_for_constructors.
@Test
public void types_match_for_constructors() {
EvaluationResult result = constructors().that().arePrivate().should(new ArchCondition<JavaMember>("exist") {
@Override
public void check(JavaMember item, ConditionEvents events) {
}
}).andShould(new ArchCondition<JavaConstructor>("not exist") {
@Override
public void check(JavaConstructor constructor, ConditionEvents events) {
events.add(SimpleConditionEvent.violated(constructor, "expected violation"));
}
}).evaluate(importClasses(ClassWithVariousMembers.class));
assertThat(Joiner.on(" ").join(result.getFailureReport().getDetails())).contains("expected violation");
}
use of com.tngtech.archunit.lang.ArchCondition in project ArchUnit by TNG.
the class GivenCodeUnitsTest method types_match_for_methods.
@Test
public void types_match_for_methods() {
EvaluationResult result = methods().that(new DescribedPredicate<JavaMember>("are there") {
@Override
public boolean test(JavaMember input) {
return true;
}
}).and(new DescribedPredicate<JavaMethod>("are there") {
@Override
public boolean test(JavaMethod input) {
return true;
}
}).should(new ArchCondition<JavaMethod>("not exist") {
@Override
public void check(JavaMethod method, ConditionEvents events) {
events.add(SimpleConditionEvent.violated(method, "expected violation"));
}
}).evaluate(importClasses(ClassWithVariousMembers.class));
assertThat(Joiner.on(" ").join(result.getFailureReport().getDetails())).contains("expected violation");
}
use of com.tngtech.archunit.lang.ArchCondition 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.ArchCondition 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);
}
Aggregations