#824 - Refactor com.avaje.ebean.config.dbplatform DbType & DbPlatformType - support custom DbType mapping options

This commit is contained in:
Robin Bygrave
2016-10-03 20:52:53 +13:00
parent abfe58fb06
commit d26203db1f
12 changed files with 323 additions and 20 deletions
@@ -1,6 +1,5 @@
package com.avaje.ebean.config;
import com.avaje.ebean.config.dbplatform.DbPlatformName;
import org.junit.Test;
import java.util.Properties;
@@ -25,9 +24,9 @@ public class PropertiesWrapperTest {
properties.put("platform","postgres");
PropertiesWrapper pw = new PropertiesWrapper("pref", "myserver", properties);
assertEquals(DbPlatformName.POSTGRES, pw.getEnum(DbPlatformName.class, "platform", DbPlatformName.H2));
assertEquals(DbPlatformName.H2, pw.getEnum(DbPlatformName.class, "junk", DbPlatformName.H2));
assertNull(pw.getEnum(DbPlatformName.class, "junk", null));
assertEquals(Platform.POSTGRES, pw.getEnum(Platform.class, "platform", Platform.H2));
assertEquals(Platform.H2, pw.getEnum(Platform.class, "junk", Platform.H2));
assertNull(pw.getEnum(Platform.class, "junk", null));
}
@Test
@@ -0,0 +1,46 @@
package com.avaje.ebean.config.dbplatform;
import com.avaje.ebean.config.Platform;
import com.avaje.ebean.config.ServerConfig;
import org.junit.Test;
import static org.junit.Assert.*;
public class DatabasePlatformTest {
@Test
public void defaultTypesForDecimalAndVarchar() throws Exception {
DatabasePlatform dbPlatform = new DatabasePlatform();
assertEquals(defaultDecimalDefn(dbPlatform), "decimal(38)");
assertEquals(defaultDefn(DbType.VARCHAR, dbPlatform), "varchar(255)");
}
@Test
public void configure_customType() throws Exception {
ServerConfig serverConfig = new ServerConfig();
serverConfig.addCustomMapping(DbType.VARCHAR, "text", Platform.POSTGRES);
serverConfig.addCustomMapping(DbType.DECIMAL, "decimal(24,4)");
// PG renders custom decimal and varchar
PostgresPlatform pgPlatform = new PostgresPlatform();
pgPlatform.configure(serverConfig);
assertEquals(defaultDecimalDefn(pgPlatform), "decimal(24,4)");
assertEquals(defaultDefn(DbType.VARCHAR, pgPlatform), "text");
// H2 only renders custom decimal
H2Platform h2Platform = new H2Platform();
h2Platform.configure(serverConfig);
assertEquals(defaultDecimalDefn(h2Platform), "decimal(24,4)");
assertEquals(defaultDefn(DbType.VARCHAR, h2Platform), "varchar(255)");
}
private String defaultDecimalDefn(DatabasePlatform dbPlatform) {
return defaultDefn(DbType.DECIMAL, dbPlatform);
}
private String defaultDefn(DbType type, DatabasePlatform dbPlatform) {
return dbPlatform.getDbTypeMap().get(type).renderType(0, 0);
}
}
@@ -0,0 +1,67 @@
package com.avaje.ebean.config.dbplatform;
import org.junit.Test;
import static org.junit.Assert.*;
public class DbPlatformTypeParserTest {
@Test
public void parse_text() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("text");
assertEquals(type.getName(), "text");
assertEquals(type.getDefaultLength(), 0);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "text");
assertEquals(type.renderType(40, 0), "text(40)");
}
@Test
public void parse_varchar20() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("varchar(20)");
assertEquals(type.getName(), "varchar");
assertEquals(type.getDefaultLength(), 20);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "varchar(20)");
assertEquals(type.renderType(40, 0), "varchar(40)");
}
@Test
public void parse_decimal_18_6() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("decimal(18,6)");
assertEquals(type.getName(), "decimal");
assertEquals(type.getDefaultLength(), 18);
assertEquals(type.getDefaultScale(), 6);
assertEquals(type.renderType(0, 0), "decimal(18,6)");
}
@Test
public void parse_something() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("something(asd,6)");
assertEquals(type.getName(), "something(asd,6)");
assertEquals(type.getDefaultLength(), 0);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "something(asd,6)");
}
@Test
public void parse_invalid() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("something(asd");
assertEquals(type.getName(), "something(asd");
assertEquals(type.getDefaultLength(), 0);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "something(asd");
}
}