use of com.tngtech.archunit.core.domain.JavaClass in project taskana by Taskana.
the class ArchitectureTest method notUseCurrentTimestampSqlFunction.
private static ArchCondition<JavaClass> notUseCurrentTimestampSqlFunction() {
Function<JavaMethod, List<String>> getSqlStringsFromMethod = CheckedFunction.wrap((method) -> {
List<String> values = new ArrayList<>();
final Optional<Select> selectAnnotation = method.tryGetAnnotationOfType(Select.class);
final Optional<Update> updateAnnotation = method.tryGetAnnotationOfType(Update.class);
final Optional<Insert> insertAnnotation = method.tryGetAnnotationOfType(Insert.class);
final Optional<Delete> deleteAnnotation = method.tryGetAnnotationOfType(Delete.class);
final Optional<SelectProvider> selectProviderAnnotation = method.tryGetAnnotationOfType(SelectProvider.class);
final Optional<UpdateProvider> updateProviderAnnotation = method.tryGetAnnotationOfType(UpdateProvider.class);
final Optional<InsertProvider> insertProviderAnnotation = method.tryGetAnnotationOfType(InsertProvider.class);
final Optional<DeleteProvider> deleteProviderAnnotation = method.tryGetAnnotationOfType(DeleteProvider.class);
if (selectAnnotation.isPresent()) {
values.addAll(Arrays.asList(selectAnnotation.get().value()));
}
if (updateAnnotation.isPresent()) {
values.addAll(Arrays.asList(updateAnnotation.get().value()));
}
if (insertAnnotation.isPresent()) {
values.addAll(Arrays.asList(insertAnnotation.get().value()));
}
if (deleteAnnotation.isPresent()) {
values.addAll(Arrays.asList(deleteAnnotation.get().value()));
}
if (selectProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(selectProviderAnnotation.get().type(), selectProviderAnnotation.get().method()));
}
if (updateProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(updateProviderAnnotation.get().type(), updateProviderAnnotation.get().method()));
}
if (insertProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(insertProviderAnnotation.get().type(), insertProviderAnnotation.get().method()));
}
if (deleteProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(deleteProviderAnnotation.get().type(), deleteProviderAnnotation.get().method()));
}
return values;
});
return new ArchCondition<>("not use the SQL function 'CURRENT_TIMESTAMP'") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
for (JavaMethod method : javaClass.getAllMethods()) {
List<String> sqlStrings = getSqlStringsFromMethod.apply(method);
if (sqlStrings.isEmpty() && !method.tryGetAnnotationOfType(SelectProvider.class).isPresent()) {
String message = String.format("Method '%s#%s' does not contain any MyBatis SQL annotation", javaClass.getName(), method.getName());
events.add(SimpleConditionEvent.violated(javaClass, message));
}
if (sqlStrings.stream().anyMatch(s -> s.contains("CURRENT_TIMESTAMP"))) {
String message = String.format("Method '%s#%s' uses 'CURRENT_TIMESTAMP' SQL function", javaClass.getName(), method.getName());
events.add(SimpleConditionEvent.violated(javaClass, message));
}
}
}
};
}
use of com.tngtech.archunit.core.domain.JavaClass in project solarthing by wildmountainfarms.
the class ArchTest method classes_contain_both_equals_and_hash_code_or_contain_neither.
@Test
void classes_contain_both_equals_and_hash_code_or_contain_neither() {
// TODO Note this only checks the method name, so someone could overload equals or hashCode and cause errors
JavaClasses importedClasses = new ClassFileImporter().importPackages("me.retrodaredevil");
for (JavaClass javaClass : importedClasses) {
boolean overridesEquals = javaClass.getMethods().stream().anyMatch(method -> "equals".equals(method.getName()));
boolean overridesHashCode = javaClass.getMethods().stream().anyMatch(method -> "hashCode".equals(method.getName()));
if (overridesEquals != overridesHashCode) {
if (overridesEquals) {
fail(javaClass + " overrides equals but not hashCode()!");
} else {
fail(javaClass + " overrides hashCode() but not equals()!");
}
}
}
}
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);
}
}
}
use of com.tngtech.archunit.core.domain.JavaClass 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.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));
}
}
Aggregations