use of com.tngtech.archunit.core.domain.JavaClass in project flink by apache.
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.core.domain.JavaClass in project flink by apache.
the class Conditions method haveLeafArgumentTypes.
/**
* Tests leaf argument types of a method against the given predicate.
*
* <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
*/
public static ArchCondition<JavaMethod> haveLeafArgumentTypes(DescribedPredicate<JavaClass> typePredicate) {
return new ArchCondition<JavaMethod>("have leaf argument types" + typePredicate.getDescription()) {
@Override
public void check(JavaMethod method, ConditionEvents events) {
final List<JavaClass> leafArgumentTypes = method.getParameterTypes().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: Argument leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
events.add(SimpleConditionEvent.violated(method, message));
}
}
}
};
}
use of com.tngtech.archunit.core.domain.JavaClass in project gradle by gradle.
the class ArchUnitFixtureTest method checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes.
@NotNull
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.core.domain.JavaClass in project teammates by TEAMMATES.
the class ArchitectureTest method testArchitecture_logic_coreLogicCanOnlyAccessItsCorrespondingDb.
@Test
public void testArchitecture_logic_coreLogicCanOnlyAccessItsCorrespondingDb() {
for (JavaClass coreLogicClass : forClasses(LOGIC_CORE_PACKAGE)) {
String logicClassName = coreLogicClass.getSimpleName();
if ("DataBundleLogic".equals(logicClassName)) {
continue;
}
if (logicClassName.endsWith(TEST_FILE_SUFFIX)) {
continue;
}
String dbClassName = logicClassName.replace("Logic", "Db");
noClasses().that().resideInAPackage(includeSubpackages(LOGIC_CORE_PACKAGE)).and().doNotHaveSimpleName(logicClassName).and().doNotHaveSimpleName(logicClassName + TEST_FILE_SUFFIX).and().doNotHaveSimpleName("DataBundleLogic").should().accessClassesThat(new DescribedPredicate<>("") {
@Override
public boolean apply(JavaClass input) {
return input.getPackageName().startsWith(STORAGE_API_PACKAGE) && input.getSimpleName().equals(dbClassName);
}
}).check(forClasses(LOGIC_CORE_PACKAGE, STORAGE_API_PACKAGE));
}
}
use of com.tngtech.archunit.core.domain.JavaClass in project solarthing by wildmountainfarms.
the class ArchTest method testClassesThatMustImplementEquals.
@Test
void testClassesThatMustImplementEquals() {
JavaClasses importedClasses = new ClassFileImporter().importPackages("me.retrodaredevil.solarthing");
Collection<JavaClass> classesToCheck = new ArrayList<>();
// we want to be able to compare all execution reasons
classesToCheck.addAll(importedClasses.get(ExecutionReason.class).getAllSubclasses());
// required for OpenSourceExecutionReason
classesToCheck.addAll(importedClasses.get(OpenSourcePacket.class).getAllSubclasses());
classesToCheck.addAll(importedClasses.get(ActivePeriod.class).getAllSubclasses());
// DeleteAlterPacket uses this, and DeleteAlterPacket must implement equals correctly
classesToCheck.addAll(importedClasses.get(UpdateToken.class).getAllSubclasses());
for (JavaClass javaClass : classesToCheck) {
if (javaClass.isInterface()) {
continue;
}
boolean doesClassOverrideEquals = javaClass.getMethods().stream().anyMatch(method -> "equals".equals(method.getName()));
if (!doesClassOverrideEquals) {
fail("equals is not implemented for " + javaClass);
}
}
}
Aggregations