use of com.tngtech.archunit.lang.ConditionEvents in project gradle by gradle.
the class ArchUnitFixtureTest method checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes.
@Nonnull
private ConditionEvent checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes(String methodName, Class<?>... arguments) {
ArchCondition<JavaMethod> archCondition = haveOnlyArgumentsOrReturnTypesThatAre(resideInAnyPackage("java.lang").or(primitive).or(resideInAnyPackage("java.util")).as("allowed"));
JavaClass javaClass = new ClassFileImporter().importClass(AllowedMethodTypesClass.class);
JavaMethod validMethod = javaClass.getMethod(methodName, arguments);
ConditionEvents events = new ConditionEvents();
archCondition.check(validMethod, events);
assertThat(events).hasSize(1);
return events.iterator().next();
}
use of com.tngtech.archunit.lang.ConditionEvents in project flink by splunk.
the class Conditions method haveLeafExceptionTypes.
/**
* Tests leaf exception types of a method against the given predicate.
*
* <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
*/
public static ArchCondition<JavaMethod> haveLeafExceptionTypes(DescribedPredicate<JavaClass> typePredicate) {
return new ArchCondition<JavaMethod>("have leaf exception types" + typePredicate.getDescription()) {
@Override
public void check(JavaMethod method, ConditionEvents events) {
final List<JavaClass> leafArgumentTypes = method.getExceptionTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
for (JavaClass leafType : leafArgumentTypes) {
if (!isJavaClass(leafType)) {
continue;
}
if (!typePredicate.apply(leafType)) {
final String message = String.format("%s: Exception 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.ConditionEvents in project ArchUnit by TNG.
the class GivenCodeUnitsTest method types_match_for_constructors.
@Test
public void types_match_for_constructors() {
EvaluationResult result = constructors().that(new DescribedPredicate<JavaMember>("are there") {
@Override
public boolean test(JavaMember input) {
return true;
}
}).and(new DescribedPredicate<JavaConstructor>("are there") {
@Override
public boolean test(JavaConstructor input) {
return true;
}
}).should(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.ConditionEvents in project ArchUnit by TNG.
the class GivenMembersTest method beAnnotatedWith.
static ArchCondition<JavaMember> beAnnotatedWith(final Class<? extends Annotation> annotationType) {
return new ArchCondition<JavaMember>("be annotated with @%s", annotationType.getSimpleName()) {
@Override
public void check(JavaMember member, ConditionEvents events) {
boolean satisfied = member.isAnnotatedWith(annotationType);
String message = String.format("Member '%s' %s @%s", formatMember(member), satisfied ? "is annotated with" : "is not annotated with", annotationType.getSimpleName());
events.add(new SimpleConditionEvent(member, satisfied, message));
}
};
}
use of com.tngtech.archunit.lang.ConditionEvents in project ArchUnit by TNG.
the class NeverCondition method check.
@Override
public void check(T item, ConditionEvents events) {
ConditionEvents subEvents = new ConditionEvents();
condition.check(item, subEvents);
for (ConditionEvent event : subEvents) {
event.addInvertedTo(events);
}
}
Aggregations