#2120 - DbEnumValue without database constraint

This commit is contained in:
rob bygrave committed 2020-12-07 21:53:03 +13:00
1 parent dbe273bb56
commit d68f477e8e
5 files changed
+49 -89

No files matched your search

@@ -1,10 +1,13 @@
package io.ebeaninternal.server.type;
import io.ebean.config.ServerConfig;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
import io.ebean.core.type.ScalarType;
import io.ebeaninternal.server.core.bootup.BootupClasses;
import org.junit.Test;
import org.tests.model.array.IntEnum;
import org.tests.model.array.VarcharEnum;
import org.tests.model.basic.Car;
import javax.persistence.EnumType;
import java.time.DayOfWeek;
@@ -17,7 +20,7 @@ import static org.junit.Assert.assertTrue;
public class DefaultTypeManagerTest {
private DefaultTypeManager create() {
ServerConfig serverConfig = new ServerConfig();
DatabaseConfig serverConfig = new DatabaseConfig();
serverConfig.setDatabasePlatform(new PostgresPlatform());
BootupClasses bootupClasses = new BootupClasses();
return new DefaultTypeManager(serverConfig, bootupClasses);
@@ -25,7 +28,6 @@ public class DefaultTypeManagerTest {
@Test
public void isIntegerType() {
DefaultTypeManager typeManager = create();
assertTrue(typeManager.isIntegerType("1"));
@@ -42,7 +44,6 @@ public class DefaultTypeManagerTest {
@Test
public void enumDayMonth_builtIn_overrideAsString() {
DefaultTypeManager typeManager = create();
ScalarType<?> type = typeManager.createEnumScalarType(Month.class, null);
@@ -51,7 +52,6 @@ public class DefaultTypeManagerTest {
// mapped explicitly as JPA EnumType.STRING
type = typeManager.createEnumScalarType(Month.class, EnumType.STRING);
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.StringEnum.class).as("override built in type");
try {
typeManager.createEnumScalarType(Month.class, EnumType.ORDINAL);
assertThat(true).isFalse().as("never get here");
@@ -63,17 +63,14 @@ public class DefaultTypeManagerTest {
@Test
public void enumMonth_builtIn_overrideAsOrdinal() {
DefaultTypeManager typeManager = create();
// mapped explicitly as JPA EnumType.STRING
ScalarType<?> type = typeManager.createEnumScalarType(Month.class, EnumType.ORDINAL);
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.OrdinalEnum.class).as("override built in type");
try {
typeManager.createEnumScalarType(Month.class, EnumType.STRING);
assertThat(true).isFalse().as("never get here");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
}
@@ -81,7 +78,6 @@ public class DefaultTypeManagerTest {
@Test
public void enumDayOfWeek_builtIn_overrideAsString() {
DefaultTypeManager typeManager = create();
ScalarType<?> type = typeManager.createEnumScalarType(DayOfWeek.class, null);
@@ -90,11 +86,9 @@ public class DefaultTypeManagerTest {
// mapped explicitly as JPA EnumType.STRING
type = typeManager.createEnumScalarType(DayOfWeek.class, EnumType.STRING);
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.StringEnum.class).as("override built in type");
try {
typeManager.createEnumScalarType(DayOfWeek.class, EnumType.ORDINAL);
assertThat(true).isFalse().as("never get here");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
}
@@ -102,19 +96,40 @@ public class DefaultTypeManagerTest {
@Test
public void enumDayOfWeek_builtIn_overrideAsOrdinal() {
DefaultTypeManager typeManager = create();
// mapped explicitly as JPA EnumType.STRING
ScalarType<?> type = typeManager.createEnumScalarType(DayOfWeek.class, EnumType.ORDINAL);
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.OrdinalEnum.class).as("override built in type");
try {
typeManager.createEnumScalarType(DayOfWeek.class, EnumType.STRING);
assertThat(true).isFalse().as("never get here");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
}
}
@Test
public void createEnumScalarTypePerExtentions() {
DefaultTypeManager typeManager = create();
ScalarType<?> type = typeManager.createEnumScalarType(VarcharEnum.class, EnumType.ORDINAL);
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class);
// withConstraint false
assertThat(((ScalarTypeEnumWithMapping) type).getDbCheckConstraintValues()).isNull();
type = typeManager.createEnumScalarType(IntEnum.class, EnumType.ORDINAL);
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class);
ScalarTypeEnumWithMapping enumWithMapping = (ScalarTypeEnumWithMapping) type;
// withConstraint true
assertThat(enumWithMapping.getDbCheckConstraintValues()).hasSize(3);
assertThat(enumWithMapping.getDbCheckConstraintValues()).contains("100", "101", "102");
type = typeManager.createEnumScalarType(Car.Size.class, EnumType.ORDINAL);
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class);
enumWithMapping = (ScalarTypeEnumWithMapping) type;
// withConstraint true
assertThat(enumWithMapping.getDbCheckConstraintValues()).hasSize(2);
assertThat(enumWithMapping.getDbCheckConstraintValues()).contains("'L'", "'S'");
}
}
@@ -6,7 +6,7 @@ import io.ebean.annotation.DbEnumValue;
public enum VarcharEnum {
ZERO, ONE, TWO;
@DbEnumValue(storage = DbEnumType.VARCHAR)
@DbEnumValue(storage = DbEnumType.VARCHAR, withConstraint = false)
public String dbValue() {
return "xXx" + name();
}