use of com.tngtech.archunit.core.domain.JavaMember 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.core.domain.JavaMember 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.core.domain.JavaMember in project ArchUnit by TNG.
the class CodeUnitsShouldTest method types_match_for_methods.
@Test
public void types_match_for_methods() {
EvaluationResult result = methods().that().arePrivate().should(new ArchCondition<JavaMember>("exist") {
@Override
public void check(JavaMember item, ConditionEvents events) {
}
}).andShould(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.core.domain.JavaMember in project ArchUnit by TNG.
the class PublicAPIRules method notBePublic.
private static ArchCondition<JavaMember> notBePublic() {
return new ArchCondition<JavaMember>("not be public") {
@Override
public void check(JavaMember member, ConditionEvents events) {
boolean satisfied = !member.getModifiers().contains(PUBLIC);
events.add(new SimpleConditionEvent(member, satisfied, String.format("member %s.%s is %spublic in %s", member.getOwner().getName(), member.getName(), satisfied ? "not " : "", member.getSourceCodeLocation())));
}
};
}
use of com.tngtech.archunit.core.domain.JavaMember in project ArchUnit by TNG.
the class PublicAPIRules method bePubliclyAccessible.
private static ArchCondition<JavaMember> bePubliclyAccessible() {
return new ArchCondition<JavaMember>("be publicly accessible") {
@Override
public void check(JavaMember member, ConditionEvents events) {
boolean declaringClassIsPublic = getAllEnclosingClasses(member).allMatch(c -> c.getModifiers().contains(PUBLIC));
boolean satisfied = member.getModifiers().contains(PUBLIC) && declaringClassIsPublic;
events.add(new SimpleConditionEvent(member, satisfied, String.format("member %s.%s is %sdeclared in public location in %s", member.getOwner().getName(), member.getName(), satisfied ? "" : "not ", member.getSourceCodeLocation())));
}
private Stream<JavaClass> getAllEnclosingClasses(JavaMember member) {
List<JavaClass> enclosingClasses = newArrayList(member.getOwner());
while (getLast(enclosingClasses).getEnclosingClass().isPresent()) {
enclosingClasses.add(getLast(enclosingClasses).getEnclosingClass().get());
}
return enclosingClasses.stream();
}
};
}
Aggregations