DatabaseClassValidator implements ClassVisitor { void validateEntityClass() { validator.validate(); if (!foundPrimaryKey) throw new Error("db entity class must have @PrimaryKey, class=" + validator.instanceClass.getCanonicalName()); } DatabaseClassValidator(Class<?> entityClass); @Override void visitClass(Class<?> objectClass, String path); @Override void visitField(Field field, String parentPath); @Override void visitEnum(Class<?> enumClass);  }
@Test void validateEntityClass() { new DatabaseClassValidator(AssignedIdEntity.class).validateEntityClass(); new DatabaseClassValidator(AutoIncrementIdEntity.class).validateEntityClass(); new DatabaseClassValidator(CompositeKeyEntity.class).validateEntityClass(); }
@Test void withInvalidPrimaryKeyType() { assertThatThrownBy(() -> new DatabaseClassValidator(TestEntityWithInvalidPrimaryKey.class).validateEntityClass()) .isInstanceOf(Error.class) .hasMessageContaining("primary key must be Integer or Long"); }
@Test void withDefaultValue() { assertThatThrownBy(() -> new DatabaseClassValidator(TestEntityWithDefaultValue.class).validateEntityClass()) .isInstanceOf(Error.class) .hasMessageContaining("db entity field must not have default value"); }
@Test void withNotNullAnnotationPrimaryKey() { assertThatThrownBy(() -> new DatabaseClassValidator(TestEntityWithNotNullAnnotationPK.class).validateEntityClass()) .isInstanceOf(Error.class) .hasMessageContaining("db @PrimaryKey field must not have @NotNull"); }
@Test void withPropertyAnnotation() { assertThatThrownBy(() -> new DatabaseClassValidator(TestEntityWithPropertyAnnotation.class).validateEntityClass()) .isInstanceOf(Error.class) .hasMessageContaining("db entity field must not have json annotation"); }