mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Merge remote-tracking branch 'upstream/master'
# Conflicts: # ebean-ddl-generator/pom.xml # ebean-test/src/test/java/io/ebean/xtest/core/DatesAndTimesTest.java # ebean-test/src/test/java/io/ebean/xtest/core/DatesAndTimesWithJsonMillisTest.java # ebean-test/src/test/java/io/ebean/xtest/core/DatesAndTimesWithJsonNanosTest.java
This commit is contained in:
commit
0c9ec4ebfc
191 files changed
+2380
-1165
No files matched your search
@@ -1,63 +0,0 @@
|
||||
package io.ebean.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class UnderscoreNamingConventionTest {
|
||||
|
||||
private UnderscoreNamingConvention namingConvention = new UnderscoreNamingConvention();
|
||||
|
||||
@Test
|
||||
public void simple() {
|
||||
String col = namingConvention.getColumnFromProperty(null, "helloThere");
|
||||
assertThat(col).isEqualTo("hello_there");
|
||||
|
||||
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloThere");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void with_suffix_Id() {
|
||||
String col = namingConvention.getColumnFromProperty(null, "helloId");
|
||||
assertThat(col).isEqualTo("hello_id");
|
||||
|
||||
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloId");
|
||||
|
||||
col = namingConvention.getColumnFromProperty(null, "helloThereId");
|
||||
assertThat(col).isEqualTo("hello_there_id");
|
||||
|
||||
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloThereId");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void with_suffix_ID() {
|
||||
String col = namingConvention.getColumnFromProperty(null, "helloID");
|
||||
assertThat(col).isEqualTo("hello_id");
|
||||
|
||||
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloId");
|
||||
|
||||
col = namingConvention.getColumnFromProperty(null, "helloThereID");
|
||||
assertThat(col).isEqualTo("hello_there_id");
|
||||
|
||||
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloThereId");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumnFromProperty() {
|
||||
|
||||
String fkCol = "bridgetab_user_id";
|
||||
|
||||
String col = namingConvention.getColumnFromProperty(null, fkCol);
|
||||
assertThat(col).isEqualTo(fkCol);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getForeignKey() {
|
||||
|
||||
String fk = namingConvention.getForeignKey("billing_address", "id");
|
||||
assertThat(fk).isEqualTo("billing_address_id");
|
||||
|
||||
fk = namingConvention.getForeignKey("billing_address", "remoteIdProperty");
|
||||
assertThat(fk).isEqualTo("billing_address_remote_id_property");
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.MatchingNamingConvention;
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class DatabasePlatformTest {
|
||||
|
||||
@Test
|
||||
public void convertQuotedIdentifiers_when_allQuotedIdentifier_sqlServer() {
|
||||
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setAllQuotedIdentifiers(true);
|
||||
config.setNamingConvention(new MatchingNamingConvention());
|
||||
|
||||
DatabasePlatform dbPlatform = new SqlServer17Platform();
|
||||
dbPlatform.configure(config.getPlatformConfig(), config.isAllQuotedIdentifiers());
|
||||
|
||||
assertEquals(dbPlatform.convertQuotedIdentifiers("order"), "[order]");
|
||||
assertEquals(dbPlatform.convertQuotedIdentifiers("`order`"), "[order]");
|
||||
assertEquals(dbPlatform.convertQuotedIdentifiers("firstName"), "[firstName]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertQuotedIdentifiers() {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
|
||||
DatabasePlatform dbPlatform = new SqlServer17Platform();
|
||||
dbPlatform.configure(config.getPlatformConfig(), config.isAllQuotedIdentifiers());
|
||||
|
||||
assertEquals(dbPlatform.convertQuotedIdentifiers("order"), "order");
|
||||
assertEquals(dbPlatform.convertQuotedIdentifiers("`order`"), "[order]");
|
||||
assertEquals(dbPlatform.convertQuotedIdentifiers("firstName"), "firstName");
|
||||
|
||||
assertEquals(dbPlatform.unQuote("order"), "order");
|
||||
assertEquals(dbPlatform.unQuote("[order]"), "order");
|
||||
assertEquals(dbPlatform.unQuote("[firstName]"), "firstName");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultTypesForDecimalAndVarchar() {
|
||||
|
||||
DatabasePlatform dbPlatform = new DatabasePlatform();
|
||||
assertEquals(defaultDecimalDefn(dbPlatform), "decimal(16,3)");
|
||||
assertEquals(defaultDefn(DbType.VARCHAR, dbPlatform), "varchar(255)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configure_customType() {
|
||||
|
||||
PlatformConfig config = new PlatformConfig();
|
||||
config.addCustomMapping(DbType.VARCHAR, "text", Platform.POSTGRES);
|
||||
config.addCustomMapping(DbType.DECIMAL, "decimal(24,4)");
|
||||
|
||||
// PG renders custom decimal and varchar
|
||||
PostgresPlatform pgPlatform = new PostgresPlatform();
|
||||
pgPlatform.configure(config, false);
|
||||
assertEquals(defaultDecimalDefn(pgPlatform), "decimal(24,4)");
|
||||
assertEquals(defaultDefn(DbType.VARCHAR, pgPlatform), "text");
|
||||
|
||||
// H2 only renders custom decimal
|
||||
H2Platform h2Platform = new H2Platform();
|
||||
h2Platform.configure(config, false);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package io.ebean.config.dbplatform;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DbPlatformTypeLookupTest {
|
||||
|
||||
DbPlatformTypeLookup lookup = new DbPlatformTypeLookup();
|
||||
|
||||
@Test
|
||||
public void byName() throws Exception {
|
||||
|
||||
Assertions.assertEquals(lookup.byName("DECIMAL"), DbType.DECIMAL);
|
||||
assertEquals(lookup.byName("Decimal"), DbType.DECIMAL);
|
||||
assertEquals(lookup.byName("decimal"), DbType.DECIMAL);
|
||||
|
||||
assertEquals(lookup.byName("varchar"), DbType.VARCHAR);
|
||||
assertEquals(lookup.byName("varchar2"), DbType.VARCHAR);
|
||||
|
||||
assertEquals(lookup.byName("float"), DbType.REAL);
|
||||
assertEquals(lookup.byName("real"), DbType.REAL);
|
||||
|
||||
assertEquals(lookup.byName("uuid"), DbType.UUID);
|
||||
assertEquals(lookup.byName("hstore"), DbType.HSTORE);
|
||||
|
||||
assertEquals(lookup.byName("json"), DbType.JSON);
|
||||
assertEquals(lookup.byName("jsonb"), DbType.JSONB);
|
||||
assertEquals(lookup.byName("jsonclob"), DbType.JSONCLOB);
|
||||
assertEquals(lookup.byName("jsonblob"), DbType.JSONBLOB);
|
||||
assertEquals(lookup.byName("jsonVarchar"), DbType.JSONVARCHAR);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byId() throws Exception {
|
||||
|
||||
assertEquals(lookup.byId(Types.ARRAY), DbType.ARRAY);
|
||||
assertEquals(lookup.byId(Types.BIGINT), DbType.BIGINT);
|
||||
|
||||
assertEquals(lookup.byId(ExtraDbTypes.UUID), DbType.UUID);
|
||||
assertEquals(lookup.byId(ExtraDbTypes.HSTORE), DbType.HSTORE);
|
||||
|
||||
assertEquals(lookup.byId(ExtraDbTypes.JSON), DbType.JSON);
|
||||
assertEquals(lookup.byId(ExtraDbTypes.JSONB), DbType.JSONB);
|
||||
assertEquals(lookup.byId(ExtraDbTypes.JSONClob), DbType.JSONCLOB);
|
||||
assertEquals(lookup.byId(ExtraDbTypes.JSONBlob), DbType.JSONBLOB);
|
||||
assertEquals(lookup.byId(ExtraDbTypes.JSONVarchar), DbType.JSONVARCHAR);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package io.ebean.config.dbplatform;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MySqlPlatformTest {
|
||||
|
||||
@Test
|
||||
public void uuid_default() {
|
||||
|
||||
MySqlPlatform platform = new MySqlPlatform();
|
||||
platform.configure(new PlatformConfig(), false);
|
||||
|
||||
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
|
||||
assertThat(dbType.renderType(0, 0)).isEqualTo("varchar(40)");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void uuid_as_binary() {
|
||||
|
||||
MySqlPlatform platform = new MySqlPlatform();
|
||||
PlatformConfig config = new PlatformConfig();
|
||||
config.setDbUuid(PlatformConfig.DbUuid.AUTO_BINARY);
|
||||
platform.configure(config, false);
|
||||
|
||||
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
|
||||
assertThat(dbType.renderType(0, 0)).isEqualTo("binary(16)");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.oracle.Oracle11Platform;
|
||||
import io.ebean.config.dbplatform.oracle.Oracle12Platform;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class OraclePlatformTest {
|
||||
|
||||
@Test
|
||||
void columnAliasPrefix_Oracle11Platform() {
|
||||
Oracle11Platform platform11 = new Oracle11Platform();
|
||||
assertThat(platform11.columnAliasPrefix).isEqualTo("c");
|
||||
}
|
||||
|
||||
@Test
|
||||
void columnAliasPrefix_Oracle12Platform() {
|
||||
Oracle12Platform platform12 = new Oracle12Platform();
|
||||
assertThat(platform12.columnAliasPrefix).isEqualTo("c");
|
||||
}
|
||||
|
||||
@Test
|
||||
void columnAliasPrefix_OraclePlatform() {
|
||||
OraclePlatform platform = new OraclePlatform();
|
||||
assertThat(platform.columnAliasPrefix).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void uuid_default() {
|
||||
OraclePlatform platform = new OraclePlatform();
|
||||
platform.configure(new PlatformConfig(), false);
|
||||
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
|
||||
assertThat(dbType.renderType(0, 0)).isEqualTo("varchar2(40)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void uuid_as_binary() {
|
||||
OraclePlatform platform = new OraclePlatform();
|
||||
PlatformConfig config = new PlatformConfig();
|
||||
config.setDbUuid(PlatformConfig.DbUuid.AUTO_BINARY);
|
||||
|
||||
platform.configure(config, false);
|
||||
|
||||
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
|
||||
assertThat(dbType.renderType(0, 0)).isEqualTo("raw(16)");
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.Query;
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PostgresPlatformTest {
|
||||
|
||||
@Test
|
||||
public void testUuidType() {
|
||||
|
||||
PostgresPlatform platform = new PostgresPlatform();
|
||||
platform.configure(new PlatformConfig(), false);
|
||||
|
||||
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
|
||||
String columnDefn = dbType.renderType(0, 0);
|
||||
|
||||
assertThat(columnDefn).isEqualTo("uuid");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void default_forUpdate_expect_noKeyUsed() {
|
||||
|
||||
DatabasePlatform platform = new PostgresPlatform();
|
||||
|
||||
PlatformConfig config = new PlatformConfig();
|
||||
platform.configure(config);
|
||||
|
||||
assertThat(config.isForUpdateNoKey()).isFalse();
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.DEFAULT)).isEqualTo("X for update skip locked");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.DEFAULT)).isEqualTo("X for update nowait");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.DEFAULT)).isEqualTo("X for update");
|
||||
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.UPDATE)).isEqualTo("X for update skip locked");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.NO_KEY_UPDATE)).isEqualTo("X for no key update skip locked");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.SHARE)).isEqualTo("X for share skip locked");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.KEY_SHARE)).isEqualTo("X for key share skip locked");
|
||||
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.UPDATE)).isEqualTo("X for update nowait");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.NO_KEY_UPDATE)).isEqualTo("X for no key update nowait");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.SHARE)).isEqualTo("X for share nowait");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.KEY_SHARE)).isEqualTo("X for key share nowait");
|
||||
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.UPDATE)).isEqualTo("X for update");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.NO_KEY_UPDATE)).isEqualTo("X for no key update");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.SHARE)).isEqualTo("X for share");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.KEY_SHARE)).isEqualTo("X for key share");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lockWithKey_forUpdate() {
|
||||
|
||||
DatabasePlatform platform = new PostgresPlatform();
|
||||
|
||||
PlatformConfig config = new PlatformConfig();
|
||||
config.setForUpdateNoKey(true);
|
||||
platform.configure(config);
|
||||
|
||||
assertThat(config.isForUpdateNoKey()).isTrue();
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.DEFAULT)).isEqualTo("X for no key update skip locked");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.DEFAULT)).isEqualTo("X for no key update nowait");
|
||||
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.DEFAULT)).isEqualTo("X for no key update");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package io.ebean.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CamelCaseHelperTest {
|
||||
|
||||
@Test
|
||||
public void when_underscore() {
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there"), "helloThere");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_jim"), "helloThereJim");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_trailing_numbers() {
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_1"), "hello1");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_2"), "helloThere2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_numbers() {
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello1_id"), "hello1Id");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there2_foo"), "helloThere2Foo");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello1id"), "hello1id");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there2foo"), "helloThere2foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_numbersProceedUppercase() {
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello1_id"), "hello1Id");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there2_foo"), "helloThere2Foo");
|
||||
|
||||
assertEquals(CamelCaseHelper.toUnderscoreFromCamel("hello1Id"), "hello1_id");
|
||||
assertEquals(CamelCaseHelper.toUnderscoreFromCamel("helloThere2Foo"), "hello_there2_foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_already_camel() {
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThere"), "helloThere");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThereJim"), "helloThereJim");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello"), "hello");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("HELLO"), "hello");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package io.ebean.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.ebean.util.EncodeB64.enc;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestEncodeB64 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
assertEquals("A", enc(0));
|
||||
assertEquals("B", enc(1));
|
||||
assertEquals("Z", enc(25));
|
||||
assertEquals("a", enc(26));
|
||||
assertEquals("z", enc(51));
|
||||
assertEquals("0", enc(52));
|
||||
assertEquals("9", enc(61));
|
||||
assertEquals("-", enc(62));
|
||||
assertEquals("_", enc(63));
|
||||
assertEquals("BA", enc(64));
|
||||
assertEquals("Bk", enc(100));
|
||||
assertEquals("B9", enc(125));
|
||||
assertEquals("B-", enc(126));
|
||||
assertEquals("B_", enc(127));
|
||||
assertEquals("CA", enc(128));
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.PersistenceContext;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
import io.ebeaninternal.server.transaction.DefaultPersistenceContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Customer.Status;
|
||||
import org.tests.model.embedded.EAddress;
|
||||
import org.tests.model.embedded.EPerson;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class CacheBeanDataTest {
|
||||
|
||||
@Test
|
||||
public void extract_load_on_customer() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)DB.getDefault();
|
||||
BeanDescriptor<Customer> desc = server.descriptor(Customer.class);
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setId(98989);
|
||||
c.setName("Rob");
|
||||
c.setCretime(new Timestamp(System.currentTimeMillis()));
|
||||
c.setUpdtime(new Timestamp(System.currentTimeMillis()));
|
||||
c.setStatus(Status.ACTIVE);
|
||||
c.setSmallnote("somenote");
|
||||
|
||||
Address billingAddress = new Address();
|
||||
billingAddress.setId(12);
|
||||
billingAddress.setCity("Auckland");
|
||||
billingAddress.setCountry(server.reference(Country.class, "NZ"));
|
||||
billingAddress.setLine1("92 Someplace Else");
|
||||
c.setBillingAddress(billingAddress);
|
||||
|
||||
((EntityBean) c)._ebean_getIntercept().setLoaded();
|
||||
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, (EntityBean) c);
|
||||
|
||||
|
||||
assertNotNull(cacheData);
|
||||
|
||||
Customer newCustomer = new Customer();
|
||||
newCustomer.setId(c.getId());
|
||||
CachedBeanDataToBean.load(desc, (EntityBean) newCustomer, cacheData, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(c.getId(), newCustomer.getId());
|
||||
assertEquals(c.getName(), newCustomer.getName());
|
||||
assertEquals(c.getStatus(), newCustomer.getStatus());
|
||||
assertEquals(c.getSmallnote(), newCustomer.getSmallnote());
|
||||
assertEquals(c.getCretime(), newCustomer.getCretime());
|
||||
assertEquals(c.getUpdtime(), newCustomer.getUpdtime());
|
||||
assertEquals(c.getBillingAddress().getId(), newCustomer.getBillingAddress().getId());
|
||||
|
||||
assertNotNull(newCustomer.getId());
|
||||
assertNotNull(newCustomer.getName());
|
||||
assertNotNull(newCustomer.getStatus());
|
||||
assertNotNull(newCustomer.getSmallnote());
|
||||
assertNotNull(newCustomer.getCretime());
|
||||
assertNotNull(newCustomer.getUpdtime());
|
||||
assertNotNull(newCustomer.getBillingAddress());
|
||||
assertNotNull(newCustomer.getBillingAddress().getId());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void extract_load_withEmbeddedBean() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)DB.getDefault();
|
||||
BeanDescriptor<EPerson> desc = server.descriptor(EPerson.class);
|
||||
BeanPropertyAssocOne<?> addressBeanProperty = (BeanPropertyAssocOne<?>) desc.beanProperty("address");
|
||||
|
||||
EAddress address = new EAddress();
|
||||
address.setStreet("92 Someplace Else");
|
||||
address.setSuburb("Sandringham");
|
||||
address.setCity("Auckland");
|
||||
|
||||
EPerson person = new EPerson();
|
||||
person.setId(98989L);
|
||||
person.setName("Rob");
|
||||
person.setAddress(address);
|
||||
|
||||
CachedBeanData addressCacheData = (CachedBeanData) addressBeanProperty.getCacheDataValue((EntityBean) person);
|
||||
|
||||
PersistenceContext context = new DefaultPersistenceContext();
|
||||
|
||||
EPerson newPersonCheck = new EPerson();
|
||||
newPersonCheck.setId(98989L);
|
||||
addressBeanProperty.setCacheDataValue((EntityBean) newPersonCheck, addressCacheData, context);
|
||||
|
||||
EAddress newAddress = newPersonCheck.getAddress();
|
||||
assertEquals(address.getStreet(), newAddress.getStreet());
|
||||
assertEquals(address.getCity(), newAddress.getCity());
|
||||
assertEquals(address.getSuburb(), newAddress.getSuburb());
|
||||
|
||||
|
||||
CachedBeanData cacheData = desc.cacheEmbeddedBeanExtract((EntityBean) person);
|
||||
|
||||
assertNotNull(cacheData);
|
||||
|
||||
EPerson newPerson = (EPerson) desc.cacheEmbeddedBeanLoad(cacheData, context);
|
||||
|
||||
assertNotNull(newPerson.getId());
|
||||
assertNotNull(newPerson.getName());
|
||||
assertNotNull(newPerson.getAddress());
|
||||
|
||||
assertEquals(person.getId(), newPerson.getId());
|
||||
assertEquals(person.getName(), newPerson.getName());
|
||||
assertEquals(person.getAddress().getStreet(), newPerson.getAddress().getStreet());
|
||||
assertEquals(person.getAddress().getCity(), newPerson.getAddress().getCity());
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.transaction.DefaultPersistenceContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CachedBeanDataFromBeanTest {
|
||||
|
||||
private final SpiEbeanServer server = (SpiEbeanServer)DB.getDefault();
|
||||
|
||||
@Test
|
||||
public void extract() {
|
||||
|
||||
BeanDescriptor<Customer> desc = server.descriptor(Customer.class);
|
||||
|
||||
Date largeDate = new Date(9223372036825200000L);
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setId(42);
|
||||
customer.setName("Rob");
|
||||
customer.setAnniversary(largeDate);
|
||||
|
||||
Address billingAddress = new Address();
|
||||
billingAddress.setId(12);
|
||||
billingAddress.setCity("SomePlace");
|
||||
|
||||
customer.setBillingAddress(billingAddress);
|
||||
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, (EntityBean) customer);
|
||||
|
||||
assertEquals(cacheData.getData("id"), "42");
|
||||
assertEquals(cacheData.getData("name"), "Rob");
|
||||
assertEquals(cacheData.getData("billingAddress"), "12");
|
||||
assertEquals(cacheData.getData("anniversary"), "9223372036825200000");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void inheritance() {
|
||||
|
||||
Car car = new Car();
|
||||
car.setId(42);
|
||||
car.setDriver("Jimmy");
|
||||
car.setNotes("some notes");
|
||||
|
||||
BeanDescriptor<Car> carDesc = server.descriptor(Car.class);
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(carDesc, (EntityBean) car);
|
||||
|
||||
Car newCar = new Car();
|
||||
EntityBean entityBean = (EntityBean) newCar;
|
||||
CachedBeanDataToBean.load(carDesc, entityBean, cacheData, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(newCar.getId(), car.getId());
|
||||
assertEquals(newCar.getDriver(), car.getDriver());
|
||||
assertEquals(newCar.getNotes(), car.getNotes());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void dirtyScalar_expect_originalValueUsed() {
|
||||
|
||||
Contact contact = new Contact();
|
||||
contact.setId(42);
|
||||
contact.setLastName("Bygrave");
|
||||
contact.setFirstName("Foo");
|
||||
contact.setEmail("rob@email.com");
|
||||
|
||||
EntityBean entityBean = (EntityBean)contact;
|
||||
entityBean._ebean_getIntercept().setLoaded();
|
||||
|
||||
// mutate, dirty
|
||||
contact.setLastName("Banana");
|
||||
|
||||
final BeanDescriptor<Contact> desc = server.descriptor(Contact.class);
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, entityBean);
|
||||
|
||||
final Map<String, Object> data = cacheData.getData();
|
||||
assertThat(data.get("id")).isEqualTo("42");
|
||||
assertThat(data.get("lastName")).isEqualTo("Bygrave"); // ORIGINAL VALUE
|
||||
assertThat(data.get("firstName")).isEqualTo(contact.getFirstName());
|
||||
assertThat(data.get("email")).isEqualTo(contact.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dirtyManyToOne_expect_originalValueUsed() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setId(99);
|
||||
|
||||
Contact contact = new Contact();
|
||||
contact.setFirstName("Foo");
|
||||
contact.setLastName("Bygrave");
|
||||
contact.setEmail("rob@email.com");
|
||||
contact.setCustomer(customer);
|
||||
|
||||
EntityBean entityBean = (EntityBean)contact;
|
||||
entityBean._ebean_getIntercept().setLoaded();
|
||||
|
||||
// mutate, dirty
|
||||
Customer customer2 = new Customer();
|
||||
customer2.setId(108);
|
||||
contact.setCustomer(customer2);
|
||||
contact.setLastName("Banana");
|
||||
|
||||
final BeanDescriptor<Contact> desc = server.descriptor(Contact.class);
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, entityBean);
|
||||
|
||||
final Map<String, Object> data = cacheData.getData();
|
||||
assertThat(data.get("lastName")).isEqualTo("Bygrave"); // Original value
|
||||
assertThat(data.get("customer")).isEqualTo("99"); // Original value
|
||||
assertThat(data.get("firstName")).isEqualTo(contact.getFirstName());
|
||||
assertThat(data.get("email")).isEqualTo(contact.getEmail());
|
||||
}
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.transaction.DefaultPersistenceContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.TBytesOnly;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class CachedBeanDataSerializeTest {
|
||||
|
||||
private final SpiEbeanServer db = (SpiEbeanServer)DB.getDefault();
|
||||
|
||||
@Test
|
||||
public void write() throws IOException, ClassNotFoundException {
|
||||
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("name", "rob");
|
||||
map.put("some", "thing");
|
||||
map.put("whenCreated", "" + System.currentTimeMillis());
|
||||
|
||||
long version = System.currentTimeMillis();
|
||||
CachedBeanData write = new CachedBeanData(null, "C", map, version);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
write.writeExternal(oos);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
byte[] bytes = os.toByteArray();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
CachedBeanData read = new CachedBeanData();
|
||||
read.readExternal(ois);
|
||||
|
||||
assertEquals(read.getVersion(), write.getVersion());
|
||||
assertEquals(read.getWhenCreated(), write.getWhenCreated());
|
||||
assertEquals(read.getDiscValue(), write.getDiscValue());
|
||||
assertEquals(read.getData(), write.getData());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void fullBean() throws IOException, ClassNotFoundException {
|
||||
Customer customer = new Customer();
|
||||
customer.setId(42);
|
||||
customer.setName("FooBar");
|
||||
customer.setStatus(Customer.Status.INACTIVE);
|
||||
customer.setVersion(3L);
|
||||
|
||||
BeanDescriptor<Customer> desc = db.descriptor(Customer.class);
|
||||
CachedBeanData extract = CachedBeanDataFromBean.extract(desc, (EntityBean) customer);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
writeToStream(extract, os);
|
||||
|
||||
byte[] bytes = os.toByteArray();
|
||||
|
||||
CachedBeanData read = readFromStream(bytes);
|
||||
|
||||
assertEquals(read.getData(), extract.getData());
|
||||
|
||||
Customer loadCustomer = new Customer();
|
||||
CachedBeanDataToBean.load(desc, (EntityBean) loadCustomer, read, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(loadCustomer.getVersion(), customer.getVersion());
|
||||
assertEquals(loadCustomer.getId(), customer.getId());
|
||||
assertEquals(loadCustomer.getName(), customer.getName());
|
||||
assertEquals(loadCustomer.getStatus(), customer.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanWithByteArray() throws IOException, ClassNotFoundException {
|
||||
|
||||
String stringContent = "ThisIsSome";
|
||||
|
||||
TBytesOnly bean = new TBytesOnly();
|
||||
bean.setId(42);
|
||||
bean.setContent(stringContent.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
BeanDescriptor<TBytesOnly> desc = db.descriptor(TBytesOnly.class);
|
||||
CachedBeanData extract = CachedBeanDataFromBean.extract(desc, (EntityBean) bean);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
writeToStream(extract, os);
|
||||
byte[] bytes = os.toByteArray();
|
||||
|
||||
CachedBeanData read = readFromStream(bytes);
|
||||
byte[] extraContent = (byte[]) extract.getData("content");
|
||||
|
||||
assertEquals(stringContent, new String(extraContent));
|
||||
assertTrue(Arrays.equals(bean.getContent(), extraContent));
|
||||
|
||||
TBytesOnly loadBean = new TBytesOnly();
|
||||
CachedBeanDataToBean.load(desc, (EntityBean) loadBean, read, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(loadBean.getId(), bean.getId());
|
||||
assertTrue(Arrays.equals(loadBean.getContent(), bean.getContent()));
|
||||
}
|
||||
|
||||
private CachedBeanData readFromStream(byte[] bytes) throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
return (CachedBeanData) ois.readObject();
|
||||
}
|
||||
|
||||
private void writeToStream(CachedBeanData extract, ByteArrayOutputStream os) throws IOException {
|
||||
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
oos.writeObject(extract);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
}
|
||||
|
||||
}
|
||||
Vendored
+56
@@ -0,0 +1,56 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Article;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Product;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DefaultCacheHolder_getCacheOptions_Test {
|
||||
|
||||
private final DefaultCacheHolder cacheHolder;
|
||||
|
||||
public DefaultCacheHolder_getCacheOptions_Test() {
|
||||
|
||||
ServerCacheOptions defaultOptions = new ServerCacheOptions();
|
||||
defaultOptions.setMaxSize(10000);
|
||||
defaultOptions.setMaxSecsToLive(120);
|
||||
|
||||
CacheManagerOptions builder = new CacheManagerOptions(null, new DatabaseConfig(), true)
|
||||
.with(defaultOptions, defaultOptions);
|
||||
|
||||
this.cacheHolder = new DefaultCacheHolder(builder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanOptions_when_set() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Article.class, ServerCacheType.BEAN);
|
||||
assertEquals(options.getMaxSecsToLive(), 45);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanOptions_when_notSet_expect_default() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Order.class, ServerCacheType.BEAN);
|
||||
assertEquals(options.getMaxSecsToLive(), 120);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryOptions_when_set() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Product.class, ServerCacheType.QUERY);
|
||||
assertEquals(options.getMaxSecsToLive(), 15);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryOptions_when_notSet_expect_default() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Order.class, ServerCacheType.QUERY);
|
||||
assertEquals(options.getMaxSecsToLive(), 120);
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.config.ContainerConfig;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.server.cluster.ClusterManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class DefaultServerCacheManagerTest {
|
||||
|
||||
private final ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
class TdTenPro implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public Object currentId() {
|
||||
return tenantId.get();
|
||||
}
|
||||
}
|
||||
|
||||
private final ClusterManager clusterManager = new ClusterManager(new ContainerConfig());
|
||||
|
||||
private final DefaultServerCacheManager manager = new DefaultServerCacheManager(new CacheManagerOptions(clusterManager, new DatabaseConfig(), true));
|
||||
|
||||
private final DefaultServerCacheManager multiTenantManager;
|
||||
|
||||
public DefaultServerCacheManagerTest(){
|
||||
CacheManagerOptions builder = new CacheManagerOptions(clusterManager, new DatabaseConfig(), true);
|
||||
builder.with(new TdTenPro());
|
||||
this.multiTenantManager = new DefaultServerCacheManager(builder);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getCache_normal() {
|
||||
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(cache.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(manager, Customer.class);
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(manager, Contact.class);
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
|
||||
assertThat(cache2.getShortName()).isEqualTo("Contact_B");
|
||||
|
||||
|
||||
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class);
|
||||
assertThat(natKeyCache.getName()).isEqualTo("org.tests.model.basic.Customer_N");
|
||||
assertThat(natKeyCache.getShortName()).isEqualTo("Customer_N");
|
||||
|
||||
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class);
|
||||
assertThat(queryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
|
||||
assertThat(queryCache.getShortName()).isEqualTo("Customer_Q");
|
||||
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts");
|
||||
assertThat(collCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
|
||||
assertThat(collCache.getShortName()).isEqualTo("Customer.contacts_C");
|
||||
|
||||
cache.clearCount.reset();
|
||||
collCache.clearCount.reset();
|
||||
queryCache.clearCount.reset();
|
||||
natKeyCache.clearCount.reset();
|
||||
|
||||
manager.clear(Customer.class);
|
||||
|
||||
assertThat(cache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(natKeyCache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(queryCache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(collCache.clearCount.get(true)).isEqualTo(1);
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
return (DefaultServerCache) manager.getBeanCache(beanType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() {
|
||||
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId.set("ten2");
|
||||
|
||||
assertThat(cache.get("1")).isNull();
|
||||
|
||||
tenantId.set("ten1");
|
||||
|
||||
assertThat(cache.get("1")).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_singleTenant() {
|
||||
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId.set("ten2");
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("tenant1");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isLocalL2Caching() {
|
||||
|
||||
assertTrue(manager.isLocalL2Caching());
|
||||
assertTrue(multiTenantManager.isLocalL2Caching());
|
||||
}
|
||||
|
||||
}
|
||||
+358
@@ -0,0 +1,358 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.server.querydefn.DefaultOrmQuery;
|
||||
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultServer_createOrmQueryRequestTest {
|
||||
|
||||
Query<Order> query() {
|
||||
return server().find(Order.class);
|
||||
}
|
||||
|
||||
private Database server() {
|
||||
return DB.getDefault();
|
||||
}
|
||||
|
||||
OrmQueryRequest<Order> queryRequest(Query<Order> query) {
|
||||
return OrmQueryRequestTestHelper.queryRequest(query);
|
||||
}
|
||||
|
||||
OrmQueryDetail detail(Query<Order> query) {
|
||||
return queryRequest(query).query().getDetail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_empty_then_same() {
|
||||
|
||||
assertSame(detail(query()), detail(query()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_all_then_same() {
|
||||
|
||||
assertSame(detail(query().select("*")), detail(query().select("*")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_propertiesInOrder_then_same() {
|
||||
|
||||
assertSame(detail(query().select("id,name")), detail(query().select("id,name")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_propertiesInDifferentOrder_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name")), detail(query().select("name, id")));
|
||||
|
||||
// but same from autotune perspective
|
||||
assertThat(detail(query().select("id,name")).isAutoTuneEqual(detail(query().select("name, id")))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_additional_fetch_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name")), detail(query().select("id,name").fetch("details")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_same_fetch_then_same() {
|
||||
|
||||
assertSame(detail(query().select("id,name").fetch("details")), detail(query().select("id,name").fetch("details")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_fetch_order_different_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name").fetch("details").fetch("customer")),
|
||||
detail(query().select("id,name").fetch("customer").fetch("details")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_extra_queryFetchToMany_then_same() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name").fetch("customer")),
|
||||
detail(query().select("id,name").fetch("customer").fetch("details", FetchConfig.ofQuery())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_extra_queryToOne_fetch_then_different() {
|
||||
|
||||
// with the fetch of customer the foreign key must be added to the root query
|
||||
assertDifferent(detail(query().select("id,name")),
|
||||
detail(query().select("id,name").fetch("customer", FetchConfig.ofQuery())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_additional_fetch_V2_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name")), detail(query().select("id,name").fetch("customer", "id")));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void when_fetchConfig_then_differentPlan() throws Exception {
|
||||
|
||||
DefaultOrmQuery<Order> query1 = (DefaultOrmQuery<Order>) DB.find(Order.class)
|
||||
.select("status, shipDate")
|
||||
.fetchQuery("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name");
|
||||
|
||||
|
||||
DefaultOrmQuery<Order> query2 = (DefaultOrmQuery<Order>) DB.find(Order.class)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name");
|
||||
|
||||
assertDifferent(detail(query1), detail(query2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_fetchJoins_expect_detailJoinsPreserveOrder() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetch("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_fetchJoinsAndWhere_expect_fetchJoinsOnlyInFetchPaths() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("details")
|
||||
.where().eq("customer.name", "rob").query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_queryFetch_expect_getFetchPaths_doesNotIncludeQueryJoin() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchQuery("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_queryFetch_expect_getFetchPaths_doesNotIncludeQueryJoin_via_fetchQuery() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchQuery("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetch_expect_getFetchPaths_doesNotIncludeQueryJoin() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetch("details", FetchConfig.ofLazy());
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetch_expect_getFetchPaths_doesNotIncludeQueryJoin_via_fetchLazy() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchLazy("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetchAndHasChildren_expect_getFetchPaths_doesNotIncludeJoinOrChild() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchLazy("details")
|
||||
.fetch("details.product");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_fetchMany_expect_getFetchPaths_containsAllInOrder() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer", "name");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "details.product", "customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_queryJoin_expect_getFetchPaths_excludesQueryJoinAndChildren() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetchQuery("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer", "name");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeJoinToMany_when_multipleManyPaths() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts"); // second many path
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "details.product", "customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeAllJoinToMany_when_firstRow() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setFirstRow(1)
|
||||
.fetch("details") // many path
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts"); // many path
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeAllJoinToMany_when_maxRows() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setMaxRows(1)
|
||||
.fetch("details") // many path
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts"); // many path
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_filterMany_included() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.filterMany("details").eq("orderQuantity", 10)
|
||||
.query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "details.product", "customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_filterMany_excludedByOrdering() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.filterMany("details").eq("orderQuantity", 10)
|
||||
.query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "customer.contacts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_filterMany_excludedExplicitly() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetchQuery("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.filterMany("details").eq("orderQuantity", 10)
|
||||
.query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "customer.contacts");
|
||||
}
|
||||
|
||||
private void assertSame(OrmQueryDetail detail1, OrmQueryDetail detail2) {
|
||||
assertThat(hash(detail1)).isEqualTo(hash(detail2));
|
||||
}
|
||||
|
||||
private void assertDifferent(OrmQueryDetail detail1, OrmQueryDetail detail2) {
|
||||
assertThat(hash(detail1)).isNotEqualTo(hash(detail2));
|
||||
}
|
||||
|
||||
private String hash(OrmQueryDetail detail1) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
detail1.queryPlanHash(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
|
||||
public class OrmQueryRequestTestHelper {
|
||||
|
||||
static DefaultServer defaultServer = (DefaultServer) DB.getDefault();
|
||||
|
||||
/**
|
||||
* Create and return a OrmQueryRequest for the given query.
|
||||
*/
|
||||
public static <T> OrmQueryRequest<T> queryRequest(Query<T> query) {
|
||||
return (OrmQueryRequest<T>) defaultServer.createQueryRequest(SpiQuery.Type.LIST, query, null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.ValuePair;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Order.Status;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TestDiffHelpSimple {
|
||||
|
||||
long firstTime = System.currentTimeMillis() - 10000;
|
||||
long secondTime = System.currentTimeMillis();
|
||||
|
||||
Database server;
|
||||
BeanDescriptor<Order> orderDesc;
|
||||
|
||||
TestDiffHelpSimple() {
|
||||
server = DB.getDefault();
|
||||
SpiEbeanServer spiServer = (SpiEbeanServer) server;
|
||||
orderDesc = spiServer.descriptor(Order.class);
|
||||
}
|
||||
|
||||
private Order createBaseOrder(Database server) {
|
||||
Order order1 = new Order();
|
||||
order1.setId(12);
|
||||
order1.setCretime(new Timestamp(firstTime));
|
||||
order1.setCustomer(server.reference(Customer.class, 1234));
|
||||
order1.setStatus(Status.NEW);
|
||||
order1.setShipDate(new Date(firstTime));
|
||||
order1.setOrderDate(new Date(firstTime));
|
||||
return order1;
|
||||
}
|
||||
|
||||
@Test
|
||||
void diffWhenNull_expect_empty() {
|
||||
assertThat(DB.diff(null, null)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicChanges() {
|
||||
Order order1 = createBaseOrder(server);
|
||||
|
||||
Order order2 = new Order();
|
||||
order2.setId(14);
|
||||
order2.setCretime(new Timestamp(secondTime));
|
||||
order2.setCustomer(server.reference(Customer.class, 2133));
|
||||
order2.setStatus(Status.COMPLETE);
|
||||
order2.setShipDate(new Date(secondTime));
|
||||
order2.setOrderDate(new Date(secondTime));
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(5, diff.size());
|
||||
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("cretime"));
|
||||
assertTrue(keySet.contains("status"));
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
assertTrue(keySet.contains("orderDate"));
|
||||
assertTrue(keySet.contains("customer.id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicChanges_given_flatMode() {
|
||||
Order order1 = createBaseOrder(server);
|
||||
|
||||
Order order2 = new Order();
|
||||
order2.setId(14);
|
||||
order2.setCretime(new Timestamp(secondTime));
|
||||
order2.setCustomer(server.reference(Customer.class, 2133));
|
||||
order2.setStatus(Status.COMPLETE);
|
||||
order2.setShipDate(new Date(secondTime));
|
||||
order2.setOrderDate(new Date(secondTime));
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(5, diff.size());
|
||||
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("cretime"));
|
||||
assertTrue(keySet.contains("status"));
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
assertTrue(keySet.contains("orderDate"));
|
||||
assertTrue(keySet.contains("customer.id"));
|
||||
assertEquals(2133, diff.get("customer.id").getOldValue());
|
||||
assertEquals(1234, diff.get("customer.id").getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIdIgnored() {
|
||||
Order order1 = createBaseOrder(server);
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setId(14);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(0, diff.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSecondValueNull() {
|
||||
Order order1 = createBaseOrder(server);
|
||||
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setCustomer(server.reference(Customer.class, 2133));
|
||||
order2.setStatus(Status.COMPLETE);
|
||||
order2.setShipDate(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(3, diff.size());
|
||||
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("status"));
|
||||
assertTrue(keySet.contains("customer.id"));
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
|
||||
ValuePair shipDatePair = diff.get("shipDate");
|
||||
assertEquals(order1.getShipDate(), shipDatePair.getNewValue());
|
||||
assertEquals(order2.getShipDate(), shipDatePair.getOldValue());
|
||||
assertNull(shipDatePair.getOldValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFirstValueNull() {
|
||||
Order order1 = createBaseOrder(server);
|
||||
order1.setShipDate(null);
|
||||
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setShipDate(new Date(secondTime));
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(1, diff.size());
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
|
||||
ValuePair shipDatePair = diff.get("shipDate");
|
||||
assertEquals(order1.getShipDate(), shipDatePair.getNewValue());
|
||||
assertEquals(order2.getShipDate(), shipDatePair.getOldValue());
|
||||
assertNull(shipDatePair.getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBothNull() {
|
||||
Order order1 = createBaseOrder(server);
|
||||
order1.setShipDate(null);
|
||||
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setShipDate(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(0, diff.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.ValuePair;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.embedded.EMain;
|
||||
import org.tests.model.embedded.Eembeddable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestDiffHelpWithEmbedded {
|
||||
|
||||
Database server;
|
||||
|
||||
BeanDescriptor<EMain> emainDesc;
|
||||
|
||||
public TestDiffHelpWithEmbedded() {
|
||||
server = DB.getDefault();
|
||||
SpiEbeanServer spiServer = (SpiEbeanServer) server;
|
||||
emainDesc = spiServer.descriptor(EMain.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeExistingEmbedded() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
|
||||
emain2.getEmbeddable().setDescription("baz");
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
assertEquals("baz", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Same result as testChangeExistingEmbedded.
|
||||
*/
|
||||
@Test
|
||||
public void testSetViaNewEmbedded() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
|
||||
Eembeddable embeddable = new Eembeddable();
|
||||
embeddable.setDescription("baz");
|
||||
emain2.setEmbeddable(embeddable);
|
||||
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
assertEquals("baz", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiffEmbedded_flatMode() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
emain2.getEmbeddable().setDescription("baz");
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
assertEquals("baz", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFirstEmbeddedIsNull() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setEmbeddable(null);
|
||||
EMain emain2 = createEMain();
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertNull(valuePair.getNewValue());
|
||||
assertEquals("bar", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecondEmbeddedIsNull() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
emain2.setEmbeddable(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertNull(valuePair.getOldValue());
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecondEmbeddedIsNull_given_flatMode() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
emain2.setEmbeddable(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertNull(valuePair.getOldValue());
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBothEmbeddedIsNull() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setEmbeddable(null);
|
||||
EMain emain2 = createEMain();
|
||||
emain2.setEmbeddable(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(0, diff.size());
|
||||
}
|
||||
|
||||
private EMain createEMain() {
|
||||
EMain emain = new EMain();
|
||||
emain.setName("foo");
|
||||
emain.setVersion(13L);
|
||||
|
||||
Eembeddable embeddable = new Eembeddable();
|
||||
embeddable.setDescription("bar");
|
||||
emain.setEmbeddable(embeddable);
|
||||
return emain;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.Animal;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
public class BaseTest {
|
||||
|
||||
protected SpiEbeanServer db = (SpiEbeanServer)DB.getDefault();
|
||||
|
||||
protected <T> BeanDescriptor<T> getBeanDescriptor(Class<T> cls) {
|
||||
return db.descriptor(cls);
|
||||
}
|
||||
|
||||
protected SpiEbeanServer spiEbeanServer() {
|
||||
return db;
|
||||
}
|
||||
|
||||
protected Database server() {
|
||||
return db;
|
||||
}
|
||||
|
||||
protected void initTables() {
|
||||
try {
|
||||
db.find(Animal.class).findCount();
|
||||
} catch (PersistenceException e) {
|
||||
db.script().run("/h2-init.sql");
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isH2() {
|
||||
return isPlatform(Platform.H2);
|
||||
}
|
||||
protected boolean isPostgres() {
|
||||
return isPlatform(Platform.POSTGRES);
|
||||
}
|
||||
protected boolean isSqlServer() {
|
||||
return isPlatform(Platform.SQLSERVER);
|
||||
}
|
||||
protected boolean isMySql() {
|
||||
return isPlatform(Platform.MYSQL);
|
||||
}
|
||||
protected boolean isPlatform(Platform platform) {
|
||||
return db.platform().base().equals(platform);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.plugin.Property;
|
||||
import io.ebeaninternal.server.core.CacheOptions;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployIdentityMode;
|
||||
import io.ebeanservice.docstore.api.DocStoreBeanAdapter;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.*;
|
||||
import org.tests.model.bridge.BSite;
|
||||
import org.tests.model.bridge.BUser;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class BeanDescriptorTest extends BaseTest {
|
||||
|
||||
private BeanDescriptor<Customer> customerDesc = spiEbeanServer().descriptor(Customer.class);
|
||||
|
||||
@Test
|
||||
public void createReference() {
|
||||
|
||||
Customer bean = customerDesc.createReference(null, false, 42, null);
|
||||
assertThat(bean.getId()).isEqualTo(42);
|
||||
Assertions.assertThat(server().beanState(bean).isReadOnly()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_whenReadOnly() {
|
||||
|
||||
Customer bean = customerDesc.createReference(Boolean.TRUE, false, 42, null);
|
||||
Assertions.assertThat(server().beanState(bean).isReadOnly()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_whenNotReadOnly() {
|
||||
|
||||
Customer bean = customerDesc.createReference(Boolean.FALSE, false, 42, null);
|
||||
Assertions.assertThat(server().beanState(bean).isReadOnly()).isFalse();
|
||||
|
||||
bean = customerDesc.createReference(42, null);
|
||||
Assertions.assertThat(server().beanState(bean).isReadOnly()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_when_disabledLazyLoad() {
|
||||
|
||||
Customer bean = customerDesc.createReference(Boolean.FALSE, true, 42, null);
|
||||
Assertions.assertThat(server().beanState(bean).isDisableLazyLoad()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_with_inheritance() {
|
||||
initTables();
|
||||
|
||||
Cat cat = new Cat();
|
||||
cat.setName("Puss");
|
||||
DB.save(cat);
|
||||
|
||||
Dog dog = new Dog();
|
||||
dog.setRegistrationNumber("DOGGIE");
|
||||
DB.save(dog);
|
||||
|
||||
AnimalShelter shelter = new AnimalShelter();
|
||||
shelter.setName("My Animal Shelter");
|
||||
shelter.getAnimals().add(cat);
|
||||
shelter.getAnimals().add(dog);
|
||||
|
||||
DB.save(shelter);
|
||||
|
||||
BeanDescriptor<Animal> animalDesc = spiEbeanServer().descriptor(Animal.class);
|
||||
|
||||
Animal bean = animalDesc.createReference(Boolean.FALSE, false, dog.getId(), null);
|
||||
assertThat(bean.getId()).isEqualTo(dog.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allProperties() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
Collection<? extends Property> props = desc.allProperties();
|
||||
|
||||
assertThat(props).extracting("name").contains("id", "status", "orderDate", "shipDate");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchBaseTable() {
|
||||
BeanDescriptor<Customer> desc = getBeanDescriptor(Customer.class);
|
||||
assertTrue(desc.matchBaseTable("o_customer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchBaseTable_whenTableHasSchema_expect_matchRegardlessOfSchema() {
|
||||
|
||||
DeployBeanDescriptor<Customer> deploy = mockDeployCustomer();
|
||||
|
||||
when(deploy.getBaseTable()).thenReturn("foo.o_customer");
|
||||
BeanDescriptor<?> desc1 = new BeanDescriptor<>(mockOwner(), deploy);
|
||||
assertTrue(desc1.matchBaseTable("o_customer"));
|
||||
|
||||
when(deploy.getBaseTable()).thenReturn("bar.o_customer");
|
||||
BeanDescriptor<?> desc2 = new BeanDescriptor<>(mockOwner(), deploy);
|
||||
assertTrue(desc2.matchBaseTable("o_customer"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private DeployBeanDescriptor<Customer> mockDeployCustomer() {
|
||||
DeployBeanDescriptor<Customer> deploy = mock(DeployBeanDescriptor.class);
|
||||
when(deploy.getBeanType()).thenReturn(Customer.class);
|
||||
when(deploy.getIdentityMode()).thenReturn(DeployIdentityMode.auto());
|
||||
when(deploy.buildIdentityMode()).thenReturn(IdentityMode.NONE);
|
||||
when(deploy.getCacheOptions()).thenReturn(CacheOptions.NO_CACHING);
|
||||
return deploy;
|
||||
}
|
||||
|
||||
private BeanDescriptorMap mockOwner() {
|
||||
BeanDescriptorMap owner = mock(BeanDescriptorMap.class);
|
||||
when(owner.createDocStoreBeanAdapter(any(), any())).thenReturn(mock(DocStoreBeanAdapter.class));
|
||||
return owner;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void merge_when_empty() {
|
||||
|
||||
Customer from = new Customer();
|
||||
from.setId(42);
|
||||
from.setName("rob");
|
||||
|
||||
Customer to = new Customer();
|
||||
customerDesc.merge((EntityBean) from, (EntityBean) to);
|
||||
|
||||
assertThat(to.getId()).isEqualTo(42);
|
||||
assertThat(to.getName()).isEqualTo("rob");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_externalId() {
|
||||
|
||||
BeanDescriptor<Country> countryDesc = spiEbeanServer().descriptor(Country.class);
|
||||
assertThat(countryDesc.isIdGeneratedValue()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_platformGenerator_noGeneratedValueAnnotation() {
|
||||
|
||||
assertThat(customerDesc.isIdGeneratedValue()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_explicitGeneratedValue() {
|
||||
|
||||
BeanDescriptor<Contact> desc = spiEbeanServer().descriptor(Contact.class);
|
||||
assertThat(desc.isIdGeneratedValue()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_uuidGenerator_and_generatedValue() {
|
||||
|
||||
BeanDescriptor<BSite> desc = spiEbeanServer().descriptor(BSite.class);
|
||||
assertThat(desc.isIdGeneratedValue()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_uuidGenerator_and_noGeneratedValue() {
|
||||
|
||||
BeanDescriptor<BUser> desc = spiEbeanServer().descriptor(BUser.class);
|
||||
assertThat(desc.isIdGeneratedValue()).isFalse();
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebeanservice.docstore.api.mapping.DocPropertyAdapter;
|
||||
import io.ebeanservice.docstore.api.mapping.DocPropertyMapping;
|
||||
import io.ebeanservice.docstore.api.mapping.DocumentMapping;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BeanDescriptor_documentMappingTest extends BaseTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void docMapping() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
|
||||
DocumentMapping documentMapping = desc.docMapping();
|
||||
|
||||
DocPropertyMapping properties = documentMapping.getProperties();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void docMapping_visitor() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
|
||||
DocumentMapping documentMapping = desc.docMapping();
|
||||
|
||||
DocPropertyMapping properties = documentMapping.getProperties();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
TDVisitor tdVisitor = new TDVisitor();
|
||||
documentMapping.visit(tdVisitor);
|
||||
|
||||
assertThat(tdVisitor.sb.toString()).isEqualTo("{status,orderDate,shipDate, object{customer:id,name,}customerName, nested{details: [id,orderQty,shipQty,unitPrice,cretime,updtime,]}cretime,updtime,}");
|
||||
}
|
||||
|
||||
class TDVisitor extends DocPropertyAdapter {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void visitProperty(DocPropertyMapping property) {
|
||||
sb.append(property.getName() + ",");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBegin() {
|
||||
|
||||
sb.append("{");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEnd() {
|
||||
sb.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBeginObject(DocPropertyMapping property) {
|
||||
sb.append(" object{" + property.getName() + ":");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEndObject(DocPropertyMapping property) {
|
||||
sb.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBeginList(DocPropertyMapping property) {
|
||||
sb.append(" nested{" + property.getName() + ": [");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEndList(DocPropertyMapping property) {
|
||||
sb.append("]}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DatabaseFactory;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.event.AbstractBeanPersistListener;
|
||||
import io.ebean.event.BeanPersistAdapter;
|
||||
import io.ebean.event.BeanPersistListener;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasic;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class BeanDescriptor_registerTest {
|
||||
|
||||
@Test
|
||||
public void testRegisterDeregister() {
|
||||
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
|
||||
config.setName("h2other");
|
||||
config.loadFromProperties();
|
||||
config.setDdlExtra(false);
|
||||
config.setRegister(false);
|
||||
config.setDefaultServer(false);
|
||||
config.getClasses().add(EBasic.class);
|
||||
|
||||
SpiEbeanServer ebeanServer = (SpiEbeanServer)DatabaseFactory.create(config);
|
||||
try {
|
||||
BeanDescriptor<EBasic> desc = ebeanServer.descriptor(EBasic.class);
|
||||
persistListenerRegistrationTests(desc);
|
||||
persistControllerRegistrationTests(desc);
|
||||
} finally {
|
||||
ebeanServer.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private void persistControllerRegistrationTests(BeanDescriptor<EBasic> desc) {
|
||||
|
||||
Controller1 controller1 = new Controller1();
|
||||
|
||||
assertNull(desc.persistController());
|
||||
desc.register(controller1);
|
||||
assertSame(controller1, desc.persistController());
|
||||
|
||||
Controller2 controller2 = new Controller2();
|
||||
desc.register(controller2);
|
||||
|
||||
Assertions.assertEquals(2, ((ChainedBeanPersistController) desc.persistController()).size());
|
||||
|
||||
desc.deregister(controller1);
|
||||
assertEquals(1, ((ChainedBeanPersistController) desc.persistController()).size());
|
||||
|
||||
desc.deregister(controller2);
|
||||
assertEquals(0, ((ChainedBeanPersistController) desc.persistController()).size());
|
||||
}
|
||||
|
||||
private void persistListenerRegistrationTests(BeanDescriptor<EBasic> desc) {
|
||||
|
||||
Listener1 listener1 = new Listener1();
|
||||
|
||||
assertNull(desc.persistListener());
|
||||
desc.register(listener1);
|
||||
assertSame(listener1, desc.persistListener());
|
||||
|
||||
Listener2 listener2 = new Listener2();
|
||||
desc.register(listener2);
|
||||
|
||||
BeanPersistListener persistListener = desc.persistListener();
|
||||
assertTrue(persistListener instanceof ChainedBeanPersistListener);
|
||||
assertEquals(2, ((ChainedBeanPersistListener) persistListener).size());
|
||||
|
||||
desc.deregister(listener1);
|
||||
assertEquals(1, ((ChainedBeanPersistListener) desc.persistListener()).size());
|
||||
|
||||
desc.deregister(listener2);
|
||||
assertEquals(0, ((ChainedBeanPersistListener) desc.persistListener()).size());
|
||||
}
|
||||
|
||||
public static class Listener1 extends AbstractBeanPersistListener {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Listener2 extends AbstractBeanPersistListener {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Controller1 extends BeanPersistAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Controller2 extends BeanPersistAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class BeanDescriptor_whenCreatedPropertyTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
|
||||
BeanDescriptor<Customer> desc = server.descriptor(Customer.class);
|
||||
|
||||
BeanProperty whenCreatedProperty = desc.whenCreatedProperty();
|
||||
assertEquals("cretime", whenCreatedProperty.dbColumn());
|
||||
|
||||
BeanProperty whenModifiedProperty = desc.whenModifiedProperty();
|
||||
assertEquals("updtime", whenModifiedProperty.dbColumn());
|
||||
|
||||
|
||||
BeanDescriptor<EBasic> eBasicDesc = server.descriptor(EBasic.class);
|
||||
assertNull(eBasicDesc.whenCreatedProperty());
|
||||
assertNull(eBasicDesc.whenModifiedProperty());
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.common.BeanList;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class BeanPropertyAssocManyTest extends BaseTest {
|
||||
|
||||
private BeanDescriptor<Customer> customerDesc = spiEbeanServer().descriptor(Customer.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private BeanPropertyAssocMany<Customer> contacts() {
|
||||
return (BeanPropertyAssocMany<Customer>) customerDesc.beanProperty("contacts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReferenceIfNull_when_notBeanCollection_expect_null() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setContacts(new ArrayList<>());
|
||||
|
||||
BeanCollection<?> ref = contacts().createReferenceIfNull((EntityBean) customer);
|
||||
assertNull(ref);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReferenceIfNull_when_null_expect_ref() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setContacts(null);
|
||||
|
||||
BeanCollection<?> ref = contacts().createReferenceIfNull((EntityBean) customer);
|
||||
assertNotNull(ref);
|
||||
assertTrue(ref.isReference());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lazyLoadMany_addsToParentCollection() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setContacts(new BeanList<>());
|
||||
|
||||
Contact contact = new Contact();
|
||||
contact.setCustomer(customer);
|
||||
|
||||
contacts().lazyLoadMany((EntityBean) contact);
|
||||
|
||||
assertThat(customer.getContacts()).hasSize(1);
|
||||
assertThat(customer.getContacts().get(0)).isSameAs(contact);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void findIdsByParentId() {
|
||||
//
|
||||
// ResetBasicData.reset();
|
||||
//
|
||||
// List<Long> ids = DB.find(Customer.class).orderBy("id").setMaxRows(2).findIds();
|
||||
//
|
||||
// List<Object> customerIds = new ArrayList<>();
|
||||
// customerIds.add(ids.get(0));
|
||||
// customerIds.add(ids.get(1));
|
||||
//
|
||||
// List<Object> contactIdsForOne = contacts().findIdsByParentId(ids.get(0), null, null, null, true);
|
||||
//
|
||||
// List<Object> contactIdsForMultiple = contacts().findIdsByParentId(null, customerIds, null, null, true);
|
||||
//
|
||||
// assertThat(contactIdsForOne).isNotEmpty();
|
||||
// assertThat(contactIdsForMultiple).isNotEmpty();
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.BWithQIdent;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
public class DeployPropertyParserTest extends BaseTest {
|
||||
|
||||
private final BeanDescriptor<Customer> descriptor = getBeanDescriptor(Customer.class);
|
||||
|
||||
private final BeanDescriptor<Address> addressBeanDescriptor = getBeanDescriptor(Address.class);
|
||||
|
||||
private final BeanDescriptor<BWithQIdent> bWithQIdentDescriptor = getBeanDescriptor(BWithQIdent.class);
|
||||
|
||||
@Test
|
||||
public void from_prefix_expect_unchanged() {
|
||||
Assertions.assertThat(parser().parse("(select x from status join status)")).isEqualTo("(select x from status join status)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void depth0_path() {
|
||||
Assertions.assertThat(parser().parse("pre status post")).isEqualTo("pre ${}status post");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void depth1_path() {
|
||||
Assertions.assertThat(parser().parse("billingAddress.city")).isEqualTo("${billingAddress}city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void depth2_path() {
|
||||
Assertions.assertThat(parser().parse("max(billingAddress.country.name)")).isEqualTo("max(${billingAddress.country}name)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleMax() {
|
||||
Assertions.assertThat(parser().parse("max(name)")).isEqualTo("max(${}name)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combined() {
|
||||
Assertions.assertThat(parser().parse("sum(status * name)")).isEqualTo("sum(${}status * ${}name)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combined_withAtColumn() {
|
||||
Assertions.assertThat(addressParser().parse("concat(line1, line2, '-EA')")).isEqualTo("concat(${}line_1, ${}line_2, '-EA')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withExplicitQuote_all_platforms() {
|
||||
Assertions.assertThat(withQuoteParser().parse("t0.`CODE` like ?")).isEqualTo("t0.`CODE` like ?");
|
||||
Assertions.assertThat(withQuoteParser().parse("t0.[CODE] like ?")).isEqualTo("t0.[CODE] like ?");
|
||||
Assertions.assertThat(withQuoteParser().parse("t0.\"CODE\" like ?")).isEqualTo("t0.\"CODE\" like ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withQuote_when_match_h2() {
|
||||
if (isH2() || isPostgres()) {
|
||||
Assertions.assertThat(withQuoteParser().parse("name like ?")).isEqualTo("${}\"Name\" like ?");
|
||||
} else if (isSqlServer()) {
|
||||
Assertions.assertThat(withQuoteParser().parse("name like ?")).isEqualTo("${}[Name] like ?");
|
||||
} else if (isMySql()) {
|
||||
Assertions.assertThat(withQuoteParser().parse("name like ?")).isEqualTo("${}`Name` like ?");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unknown_path() {
|
||||
Assertions.assertThat(parser().parse(" foo ")).isEqualTo(" foo ");
|
||||
}
|
||||
|
||||
private DeployPropertyParser parser() {
|
||||
return descriptor.parser();
|
||||
}
|
||||
|
||||
private DeployPropertyParser addressParser() {
|
||||
return addressBeanDescriptor.parser();
|
||||
}
|
||||
|
||||
private DeployPropertyParser withQuoteParser() {
|
||||
return bWithQIdentDescriptor.parser();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class FormulaPropertyPathTest extends BaseTest {
|
||||
|
||||
private BeanDescriptor<Customer> customerDesc = getBeanDescriptor(Customer.class);
|
||||
|
||||
@Test
|
||||
public void isFormula() {
|
||||
|
||||
assertFormula("max(version)", "max", "version");
|
||||
assertFormula("min(name)", "min", "name");
|
||||
assertFormula("avg(id)", "avg", "id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isFormula_count() {
|
||||
assertFormula("count(status)", "count", "status");
|
||||
assertFormula("count(distinct name)", "count", "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concat() {
|
||||
|
||||
assertFormula("concat(name,'-end')", "concat", "name,'-end'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castFormula() {
|
||||
|
||||
assertFormula("concat(name,'-end')::String", "concat", "name,'-end'", "String", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cast_javaInstant() {
|
||||
|
||||
assertFormula("max(updtime)::Instant", "max", "updtime", "Instant", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alias() {
|
||||
assertFormula("concat(name,'-end') name", "concat", "name,'-end'", null, "name");
|
||||
assertFormula("concat(name,'-end') as name", "concat", "name,'-end'", null, "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castAndAlias() {
|
||||
assertFormula("concat(name,'-end')::String name", "concat", "name,'-end'", "String", "name");
|
||||
assertFormula("concat(name,'-end')::String as name", "concat", "name,'-end'", "String", "name");
|
||||
}
|
||||
|
||||
private void assertFormula(String input, String funcName, String expression) {
|
||||
assertFormula(input, funcName, expression, null, null);
|
||||
}
|
||||
|
||||
private void assertFormula(String input, String funcName, String expression, String cast, String alias) {
|
||||
|
||||
FormulaPropertyPath propertyPath = new FormulaPropertyPath(customerDesc, input, null);
|
||||
|
||||
assertThat(propertyPath.internalExpression()).isEqualTo(expression);
|
||||
assertThat(propertyPath.outerFunction()).isEqualTo(funcName);
|
||||
if (cast != null) {
|
||||
assertThat(propertyPath.cast()).isEqualTo(cast);
|
||||
} else {
|
||||
assertThat(propertyPath.cast()).isNull();
|
||||
}
|
||||
if (alias != null) {
|
||||
assertThat(propertyPath.alias()).isEqualTo(alias);
|
||||
} else {
|
||||
assertThat(propertyPath.alias()).isNull();
|
||||
}
|
||||
|
||||
assertThat(propertyPath.build()).isNotNull();
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.composite.RCustomer;
|
||||
import org.tests.model.composite.RCustomerKey;
|
||||
import org.tests.model.embedded.UserInterestLive;
|
||||
import org.tests.model.embedded.UserInterestLiveKey;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestBeanDescriptorHasIdProperty extends BaseTest {
|
||||
|
||||
SpiEbeanServer spiServer;
|
||||
|
||||
public TestBeanDescriptorHasIdProperty() {
|
||||
Database server = DB.getDefault();
|
||||
spiServer = (SpiEbeanServer) server;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasId() {
|
||||
|
||||
BeanDescriptor<Order> beanDescriptor = spiServer.descriptor(Order.class);
|
||||
assertNotNull(beanDescriptor.idProperty());
|
||||
assertEquals("id", beanDescriptor.idProperty().name());
|
||||
|
||||
assertNotNull(beanDescriptor.versionProperty());
|
||||
assertEquals("updtime", beanDescriptor.versionProperty().name());
|
||||
|
||||
Order order = new Order();
|
||||
|
||||
assertFalse(beanDescriptor.hasIdValue(entityBean(order)));
|
||||
assertFalse(beanDescriptor.hasVersionProperty(getIntercept(order)));
|
||||
|
||||
order.setId(23);
|
||||
order.setUpdtime(new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
assertTrue(beanDescriptor.hasIdValue(entityBean(order)));
|
||||
assertTrue(beanDescriptor.hasVersionProperty(getIntercept(order)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsReference() {
|
||||
|
||||
BeanDescriptor<Customer> beanDescriptor = spiServer.descriptor(Customer.class);
|
||||
|
||||
Customer order = new Customer();
|
||||
EntityBeanIntercept ebi = getIntercept(order);
|
||||
assertFalse(beanDescriptor.referenceIdPropertyOnly(ebi));
|
||||
|
||||
order.setId(23);
|
||||
assertTrue(beanDescriptor.referenceIdPropertyOnly(ebi));
|
||||
|
||||
order.setName("custName");
|
||||
assertFalse(beanDescriptor.referenceIdPropertyOnly(ebi));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isReference_withGeneratedOnInsertOnlyProperty_expect_false() {
|
||||
BeanDescriptor<UserInterestLive> descriptor = spiServer.descriptor(UserInterestLive.class);
|
||||
UserInterestLive bean = new UserInterestLive(new UserInterestLiveKey(1L, 2L));
|
||||
EntityBeanIntercept ebi = getIntercept(bean);
|
||||
assertFalse(descriptor.referenceIdPropertyOnly(ebi));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getIdForJson() {
|
||||
|
||||
BeanDescriptor<Order> orderDesc = spiServer.descriptor(Order.class);
|
||||
|
||||
Order order = new Order();
|
||||
order.setId(42);
|
||||
assertEquals(42, orderDesc.idForJson(order));
|
||||
|
||||
assertEquals(42, orderDesc.convertIdFromJson(42));
|
||||
assertEquals(42, orderDesc.convertIdFromJson("42"));
|
||||
assertEquals(42, orderDesc.convertIdFromJson(42L));
|
||||
|
||||
|
||||
RCustomerKey key = new RCustomerKey();
|
||||
key.setCompany("comp");
|
||||
key.setName("fred");
|
||||
|
||||
RCustomer rCustomer = new RCustomer();
|
||||
rCustomer.setKey(key);
|
||||
|
||||
BeanDescriptor<RCustomer> rcustDesc = spiServer.descriptor(RCustomer.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> idForJson = (Map<String, Object>) rcustDesc.idForJson(rCustomer);
|
||||
assertEquals("comp", idForJson.get("company"));
|
||||
assertEquals("fred", idForJson.get("name"));
|
||||
assertEquals(2, idForJson.size());
|
||||
|
||||
RCustomerKey keyVal = (RCustomerKey) rcustDesc.convertIdFromJson(idForJson);
|
||||
assertEquals("comp", keyVal.getCompany());
|
||||
assertEquals("fred", keyVal.getName());
|
||||
|
||||
}
|
||||
|
||||
private EntityBean entityBean(Object bean) {
|
||||
return (EntityBean) bean;
|
||||
}
|
||||
|
||||
private EntityBeanIntercept getIntercept(Object bean) {
|
||||
return ((EntityBean) bean)._ebean_getIntercept();
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestCollectionLoadedStatus {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
BeanDescriptor<Customer> custDesc = server.descriptor(Customer.class);
|
||||
|
||||
Customer customer = new Customer();
|
||||
EntityBean eb = (EntityBean) customer;
|
||||
EntityBeanIntercept ebi = eb._ebean_getIntercept();
|
||||
|
||||
BeanProperty contactsProperty = custDesc.beanProperty("contacts");
|
||||
assertFalse(ebi.isLoadedProperty(contactsProperty.propertyIndex()));
|
||||
|
||||
Object contactsViaInternal = contactsProperty.getValue(eb);
|
||||
assertNull(contactsViaInternal);
|
||||
assertFalse(ebi.isLoadedProperty(contactsProperty.propertyIndex()));
|
||||
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
assertNotNull(contacts);
|
||||
assertTrue(contacts instanceof BeanCollection);
|
||||
assertTrue(ebi.isLoadedProperty(contactsProperty.propertyIndex()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.Database;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.api.json.SpiJsonWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Order.Status;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class TestDiffHelpInsertSimple extends BaseTest {
|
||||
|
||||
private final long firstTime = System.currentTimeMillis() - 10000;
|
||||
|
||||
private final BeanDescriptor<Order> orderDesc;
|
||||
|
||||
public TestDiffHelpInsertSimple() {
|
||||
orderDesc = db.descriptor(Order.class);
|
||||
}
|
||||
|
||||
private Order createBaseOrder(Database server) {
|
||||
Order order1 = new Order();
|
||||
order1.setId(12);
|
||||
order1.setCretime(new Timestamp(firstTime));
|
||||
order1.setCustomer(server.reference(Customer.class, 1234));
|
||||
order1.setStatus(Status.NEW);
|
||||
//order1.setShipDate(new Date(firstTime));
|
||||
order1.setOrderDate(new Date(firstTime));
|
||||
return order1;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic() throws IOException {
|
||||
|
||||
Date date = Date.valueOf("2000-01-01");
|
||||
|
||||
Order order1 = createBaseOrder(db);
|
||||
order1.setOrderDate(date);
|
||||
|
||||
StringWriter buffer = new StringWriter();
|
||||
|
||||
SpiJsonWriter jsonWriter = spiEbeanServer().jsonExtended().createJsonWriter(buffer);
|
||||
|
||||
orderDesc.jsonWriteForInsert(jsonWriter, (EntityBean) order1);
|
||||
jsonWriter.flush();
|
||||
|
||||
String asJson = buffer.toString();
|
||||
|
||||
assertThat(asJson).startsWith("{\"status\":\"NEW\",\"orderDate\":\"2000-01-01\"");
|
||||
assertThat(asJson).endsWith(",\"customer\":{\"id\":1234}}");
|
||||
}
|
||||
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.api.json.SpiJsonWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.embedded.EMain;
|
||||
import org.tests.model.embedded.Eembeddable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestDiffHelpInsertWithEmbedded extends BaseTest {
|
||||
|
||||
private final BeanDescriptor<EMain> emainDesc;
|
||||
|
||||
public TestDiffHelpInsertWithEmbedded() {
|
||||
emainDesc = getBeanDescriptor(EMain.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simple() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"name\":\"foo\",\"version\":13,\"embeddable\":{\"description\":\"bar\"}}");
|
||||
|
||||
}
|
||||
|
||||
private String asInsertJson(EntityBean emain1) throws IOException {
|
||||
StringWriter buffer = new StringWriter();
|
||||
|
||||
SpiJsonWriter jsonWriter = spiEbeanServer().jsonExtended().createJsonWriter(buffer);
|
||||
|
||||
emainDesc.jsonWriteForInsert(jsonWriter, emain1);
|
||||
jsonWriter.flush();
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scalarPropertyAsNull() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setName(null);
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"version\":13,\"embeddable\":{\"description\":\"bar\"}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void embeddedAsNull() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setEmbeddable(null);
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"name\":\"foo\",\"version\":13}");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void embeddedPropertiesAsNull() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.getEmbeddable().setDescription(null);
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"name\":\"foo\",\"version\":13,\"embeddable\":{}}");
|
||||
}
|
||||
|
||||
private EMain createEMain() {
|
||||
EMain emain = new EMain();
|
||||
emain.setName("foo");
|
||||
emain.setVersion(13L);
|
||||
|
||||
Eembeddable embeddable = new Eembeddable();
|
||||
embeddable.setDescription("bar");
|
||||
emain.setEmbeddable(embeddable);
|
||||
return emain;
|
||||
}
|
||||
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AllEqualsExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", 10), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultiple() {
|
||||
|
||||
Assertions.assertThat(exp("a", 10, "b", "23").isSameByBind(exp("a", 10, "b", "23"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindValue() {
|
||||
|
||||
same(exp("a", 10), exp("a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_multiple() {
|
||||
|
||||
same(exp("a", 10, "b", 20, "c", 30), exp("a", 10, "b", 20, "c", 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_less() {
|
||||
|
||||
different(exp("a", 10, "b", 20, "c", 30), exp("a", 10, "b", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_more() {
|
||||
|
||||
different(exp("a", 10, "b", 20), exp("a", 10, "b", 20, "c", 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", 10), exp("b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindByNull_last() {
|
||||
|
||||
different(exp("a", 10), exp("a", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindByNull_first() {
|
||||
|
||||
different(exp("a", null), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_differentExpressionType() {
|
||||
|
||||
different(exp("a", null), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindByNull_last() {
|
||||
|
||||
Assertions.assertThat(exp("a", 10).isSameByBind(exp("a", null))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindByNull_first() {
|
||||
|
||||
Assertions.assertThat(exp("a", null).isSameByBind(exp("a", 10))).isFalse();
|
||||
}
|
||||
|
||||
private AllEqualsExpression exp(Map<String, Object> propMap) {
|
||||
return new AllEqualsExpression(propMap);
|
||||
}
|
||||
|
||||
AllEqualsExpression exp(String key0, Object val1) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
map.put(key0, val1);
|
||||
return exp(map);
|
||||
}
|
||||
|
||||
|
||||
AllEqualsExpression exp(String key0, Object val1, String key2, Object val2) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
map.put(key0, val1);
|
||||
map.put(key2, val2);
|
||||
return exp(map);
|
||||
}
|
||||
|
||||
AllEqualsExpression exp(String key0, Object val1, String key2, Object val2, String key3, Object val3) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
map.put(key0, val1);
|
||||
map.put(key2, val2);
|
||||
map.put(key3, val3);
|
||||
return exp(map);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.event.BeanQueryRequest;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import io.ebeaninternal.server.deploy.BaseTest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public abstract class BaseExpressionTest extends BaseTest {
|
||||
|
||||
protected DefaultExpressionRequest newExpressionRequest() {
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
return new DefaultExpressionRequest(desc);
|
||||
}
|
||||
|
||||
protected String hash(SpiExpression expression) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (expression != null) {
|
||||
expression.queryPlanHash(sb);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected void same(SpiExpression one, SpiExpression two) {
|
||||
assertThat(hash(one)).isEqualTo(hash(two));
|
||||
}
|
||||
|
||||
protected void different(SpiExpression one, SpiExpression two) {
|
||||
assertThat(hash(one)).isNotEqualTo(hash(two));
|
||||
}
|
||||
|
||||
/**
|
||||
* Request with Multi-Value support.
|
||||
*/
|
||||
protected InExpressionTest.TDQueryRequest<Customer> multi() {
|
||||
return MULTI_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request with NO Multi-Value support.
|
||||
*/
|
||||
protected InExpressionTest.TDQueryRequest<Customer> noMulti() {
|
||||
return NO_MULTI_VALUE;
|
||||
}
|
||||
|
||||
|
||||
private static final TDQueryRequest<Customer> MULTI_VALUE = new TDQueryRequest<>(true);
|
||||
private static final TDQueryRequest<Customer> NO_MULTI_VALUE = new TDQueryRequest<>(false);
|
||||
|
||||
static class TDQueryRequest<T> implements BeanQueryRequest<T> {
|
||||
|
||||
final boolean supported;
|
||||
|
||||
TDQueryRequest(boolean supported) {
|
||||
this.supported = supported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EbeanServer getEbeanServer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transaction transaction() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> query() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPadInExpression() {
|
||||
return supported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMultiValueIdSupported() {
|
||||
return supported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMultiValueSupported(Class<?> valueType) {
|
||||
return supported;
|
||||
}
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BetweenExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void addSql() {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
BetweenExpression exp = new BetweenExpression("startDate", 1, 2);
|
||||
exp.addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("startDate between ? and ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameInstance() {
|
||||
|
||||
BetweenExpression exp = new BetweenExpression("startDate", 1, 2);
|
||||
SpiExpression other = exp.copyForPlanKey();
|
||||
|
||||
assertThat(exp).isSameAs(other);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("startDate", 3, 4);
|
||||
|
||||
same(exp0, exp1);
|
||||
same(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_do_not_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("endDate", 1, 2);
|
||||
|
||||
different(exp0, exp1);
|
||||
different(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_do_not_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("startDate", 1, 3);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isFalse();
|
||||
assertThat(exp1.isSameByBind(exp0)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("startDate", 1, 2);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isTrue();
|
||||
assertThat(exp1.isSameByBind(exp0)).isTrue();
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BetweenPropertyExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private BetweenPropertyExpression exp(String lowProperty, String highProperty, Object value) {
|
||||
return new BetweenPropertyExpression(lowProperty, highProperty, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sqlExpression() {
|
||||
TDSpiExpressionRequest request = new TDSpiExpressionRequest(null);
|
||||
exp("a", "b", 10).addSql(request);
|
||||
Assertions.assertThat(request.getSql()).isEqualTo(" ? between a and b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
same(exp("a", "b", 10), exp("a", "b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffValue() {
|
||||
same(exp("a", "b", 10), exp("a", "b", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty() {
|
||||
different(exp("a", "b", 10), exp("a", "c", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
different(exp("a", "b", 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
assertThat(exp("a", "b", 10).isSameByBind(exp("a", "b", 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diff() {
|
||||
assertThat(exp("a", "b", 10).isSameByBind(exp("a", "b", 20))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CaseInsensitiveEqualExpressionTest extends BaseExpressionTest {
|
||||
|
||||
CaseInsensitiveEqualExpression exp(String propName, String value) {
|
||||
return new CaseInsensitiveEqualExpression(propName, value, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", "10"), exp("a", "10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", "10"), exp("a", "20"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", "10"), exp("b", "10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", "10"), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", "10").isSameByBind(exp("b", "10"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", "10").isSameByBind(exp("b", "20"))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.LikeType;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.event.BeanQueryRequest;
|
||||
import io.ebeaninternal.api.ManyWhereJoins;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
|
||||
public class DefaultExampleExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
Customer customer() {
|
||||
Address address = new Address();
|
||||
address.setCity("billingAddress.city");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("name");
|
||||
customer.setBillingAddress(address);
|
||||
return customer;
|
||||
}
|
||||
|
||||
DefaultExampleExpression exp() {
|
||||
Customer customer = customer();
|
||||
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
|
||||
}
|
||||
|
||||
DefaultExampleExpression expExtra() {
|
||||
Customer customer = customer();
|
||||
customer.setSmallnote("smallNote");
|
||||
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
|
||||
}
|
||||
|
||||
DefaultExampleExpression expDiffName() {
|
||||
Customer customer = customer();
|
||||
customer.setName("otherName");
|
||||
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
|
||||
}
|
||||
|
||||
BeanDescriptor<Customer> customerBeanDescriptor() {
|
||||
return getBeanDescriptor(Customer.class);
|
||||
}
|
||||
|
||||
DefaultExampleExpression prepare(DefaultExampleExpression expr) {
|
||||
|
||||
SpiQuery<Customer> query = (SpiQuery<Customer>) spiEbeanServer().find(Customer.class);
|
||||
BeanQueryRequest<?> request = create(query);
|
||||
expr.containsMany(customerBeanDescriptor(), new ManyWhereJoins());
|
||||
expr.prepareExpression(request);
|
||||
return expr;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DefaultExampleExpression expr = exp();
|
||||
|
||||
prepare(expr);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
expr.queryPlanHash(builder);
|
||||
|
||||
TDSpiExpressionRequest req = new TDSpiExpressionRequest(customerBeanDescriptor());
|
||||
expr.addBindValues(req);
|
||||
|
||||
Assertions.assertThat(req.bindValues).contains("name", "billingAddress.city");
|
||||
}
|
||||
|
||||
|
||||
private <T> OrmQueryRequest<T> create(SpiQuery<T> query) {
|
||||
return new OrmQueryRequest<>(null, null, query, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_whenSame() {
|
||||
|
||||
same(prepare(exp()), prepare(exp()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindValue_stillSame() {
|
||||
|
||||
same(prepare(exp()), prepare(expDiffName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_extraExpression_then_different() {
|
||||
|
||||
different(prepare(exp()), prepare(expExtra()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_lessExpression_then_different() {
|
||||
|
||||
different(prepare(expExtra()), prepare(exp()));
|
||||
}
|
||||
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Expression;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DefaultExpressionFactoryTest {
|
||||
|
||||
private String toQueryPlanHash(Expression expression) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
((SpiExpression) expression).queryPlanHash(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLowerILike() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ilike("name", "foo");
|
||||
assertThat(expression).isInstanceOf(LikeExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNativeILike() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, true);
|
||||
Expression expression = factory.ilike("name", "foo");
|
||||
assertThat(expression).isInstanceOf(NativeILikeExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEq() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.eq("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("Null[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNe() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ne("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("NotNull[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIeq() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ieq("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("Null[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIne() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ine("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("NotNull[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEq_with_equalsWithNullAsNoop() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
|
||||
Expression expression = factory.eq("name", null);
|
||||
assertThat(expression).isInstanceOf(NoopExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNe_with_equalsWithNullAsNoop() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
|
||||
Expression expression = factory.ne("name", null);
|
||||
assertThat(expression).isInstanceOf(NoopExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIeq_with_equalsWithNullAsNoop() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
|
||||
Expression expression = factory.ieq("name", null);
|
||||
assertThat(expression).isInstanceOf(NoopExpression.class);
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.ExpressionList;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultExpressionListTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp() {
|
||||
|
||||
return new DefaultExpressionList<>(null, new DefaultExpressionFactory(true, true), null);
|
||||
}
|
||||
|
||||
private <T> DefaultExpressionList<T> spi(ExpressionList<T> list) {
|
||||
return (DefaultExpressionList<T>) list;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("a", 10).eq("b", 20)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_less() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("a", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_lessEmptyLast() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_lessEmptyFirst() {
|
||||
|
||||
different(spi(exp())
|
||||
,spi(exp().eq("a", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_more() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("a", 10).eq("b", 20).eq("c", 30)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperties() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("c", 10).eq("b", 20)));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10).eq("b", 20))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffValues() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10).eq("b", 20))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 30)))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_less() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_more() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ExistsQueryExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private ExistsQueryExpression exp(boolean not, String sql, Object... bindValues) {
|
||||
return new ExistsQueryExpression(not, sql, Arrays.asList(bindValues));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp(true, "a", 10), exp(true, "a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp(true, "a", 10), exp(true, "a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNot() {
|
||||
|
||||
different(exp(true, "a", 10), exp(false, "a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffSql() {
|
||||
|
||||
different(exp(true, "a", 10), exp(true, "b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
same(exp(true, "a", 10), exp(true, "a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultipleBindValues() {
|
||||
|
||||
same(exp(true, "a", 10, "ABC", 20), exp(true, "a", 10, "ABC", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10, "ABC", 20).isSameByBind(exp(true, "a", 10, "ABC", 21))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValuesByOrder() {
|
||||
|
||||
assertThat(exp(true, "a", 10, "ABC", 20).isSameByBind(exp(true, "a", 10, 20, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10).isSameByBind(exp(true, "a", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10, 20).isSameByBind(exp(true, "a", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10).isSameByBind(exp(true, "a", 10, 20))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IdExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private IdExpression exp(Object value) {
|
||||
return new IdExpression(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp(10), exp(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp(10), exp(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp(10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp(20))).isFalse();
|
||||
assertThat(exp(10).isSameByBind(exp("junk"))).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IdInExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private IdInExpression exp(Object... values) {
|
||||
return new IdInExpression(Arrays.asList(values));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp(10), exp(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp(10), exp(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindCount_notPrepared() {
|
||||
|
||||
different(exp(10), exp(10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp(10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_mulitpleSameBindValues() {
|
||||
|
||||
assertThat(exp(10, "ABC", 20).isSameByBind(exp(10, "ABC", 20))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp("junk"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp(10, "ABC", 20).isSameByBind(exp(10, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp(10, "ABC").isSameByBind(exp(10, "ABC", 30))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffEmpty_should_differentPlanHash() {
|
||||
|
||||
List<Integer> emptyValues = values();
|
||||
|
||||
InExpression ex1 = new InExpression("foo", emptyValues, false, true);
|
||||
InExpression ex2 = new InExpression("foo", emptyValues, false);
|
||||
InExpression ex3 = new InExpression("foo", emptyValues, false, true);
|
||||
InExpression ex4 = new InExpression("foo", null, false, true);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
different(ex1, ex2);
|
||||
same(ex1, ex3); // same empty
|
||||
same(ex1, ex4); // same null
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffPropertyName_should_differentPlanHash() {
|
||||
|
||||
List<Integer> values = values(42, 92);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values, false);
|
||||
InExpression ex2 = new InExpression("bar", values, false);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
different(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffBindCount_should_differentPlanHash() {
|
||||
|
||||
List<Integer> values1 = values(42, 92);
|
||||
List<Integer> values2 = values(42, 92, 82);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values1, false);
|
||||
InExpression ex2 = new InExpression("foo", values2, false);
|
||||
|
||||
ex1.prepareExpression(noMulti());
|
||||
ex2.prepareExpression(noMulti());
|
||||
different(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffBindCount_withMultiSupport_samePlanHash() {
|
||||
|
||||
List<Integer> values1 = values(42, 92);
|
||||
List<Integer> values2 = values(42, 92, 82);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values1, false);
|
||||
InExpression ex2 = new InExpression("foo", values2, false);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
same(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffNotFlag_should_differentPlanHash() {
|
||||
|
||||
List<Integer> values = values(42, 92);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values, true);
|
||||
InExpression ex2 = new InExpression("foo", values, false);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
different(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_sameNotFlag_should_samePlanHash() {
|
||||
|
||||
List<Integer> values = values(42, 92);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values, true);
|
||||
InExpression ex2 = new InExpression("foo", values, true);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
same(ex1, ex2);
|
||||
}
|
||||
|
||||
private List<Integer> values(int... vals) {
|
||||
ArrayList<Integer> list = new ArrayList<>();
|
||||
for (int val : vals) {
|
||||
list.add(val);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private InExpression exp(String propName, boolean not, Object... values) {
|
||||
InExpression ex = new InExpression(propName, Arrays.asList(values), not);
|
||||
ex.prepareExpression(multi());
|
||||
return ex;
|
||||
}
|
||||
|
||||
private InExpression expNoMulti(String propName, boolean not, Object... values) {
|
||||
InExpression ex = new InExpression(propName, Arrays.asList(values), not);
|
||||
ex.prepareExpression(noMulti());
|
||||
return ex;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", false, 10), exp("a", false, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffPropertyName() {
|
||||
|
||||
different(exp("a", false, 10), exp("b", false, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNot() {
|
||||
|
||||
different(exp("a", false, 10), exp("a", true, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", false, 10), exp("a", false, 10, 20));
|
||||
different(expNoMulti("a", false, 10), expNoMulti("a", false, 10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindCount() {
|
||||
|
||||
same(exp("a", false, 10), exp("a", false, 10, 20));
|
||||
different(expNoMulti("a", false, 10), expNoMulti("a", false, 10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10).isSameByBind(exp("a", false, 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultipleBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10, "ABC", 20).isSameByBind(exp("a", false, 10, "ABC", 20))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10).isSameByBind(exp("a", false, "foo"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10, "ABC", 20).isSameByBind(exp("a", false, 10, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10, "ABC").isSameByBind(exp("a", false, 10, "ABC", 30))).isFalse();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Pairs;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class InPairsExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private Pairs pairs() {
|
||||
return pairs("sku", "code");
|
||||
}
|
||||
|
||||
private Pairs pairs(String property0, String property1) {
|
||||
Pairs pairs = new Pairs(property0, property1)
|
||||
.add("2", 1000)
|
||||
.add("2", 1001)
|
||||
.add("3", 1000);
|
||||
|
||||
return pairs;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void same_samePlan_sameBind() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs(), false);
|
||||
|
||||
same(e0, e1);
|
||||
|
||||
assertTrue(e0.isSameByBind(e1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void same_samePlan_diffBind() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().add("4", 1000), false);
|
||||
e0.prepareExpression(multi());
|
||||
e1.prepareExpression(multi());
|
||||
|
||||
// when multi() ... same as bind count not important
|
||||
same(e0, e1);
|
||||
assertFalse(e0.isSameByBind(e1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void same_noMulti_diffPlan() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().add("4", 1000), false);
|
||||
e0.prepareExpression(noMulti());
|
||||
e1.prepareExpression(noMulti());
|
||||
|
||||
// when noMulti() ... bind count different so different plan
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffProperty0_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs("k", "code"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffProperty1_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs("sku", "c"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffSeparator_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().concatSeparator(":"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffSuffix_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().concatSuffix(":"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InQueryExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private InQueryExpression exp(String propertyName, boolean not, String sql, Object... bindValues) {
|
||||
return new InQueryExpression(propertyName, not, sql, Arrays.asList(bindValues));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("name", true, "sql", 10), exp("name", true, "sql", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("name", true, "sql", 10), exp("name", true, "sql", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNPropertyName() {
|
||||
|
||||
different(exp("name", true, "sql", 10), exp("nameDiff", true, "sql", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNot() {
|
||||
|
||||
different(exp("name", true, "sql", 10), exp("name", false, "sql", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffSql() {
|
||||
|
||||
different(exp("name", true, "sql", 10), exp("name", true, "sqlDiff", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultipleBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, "ABC", 20))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, "ABC", 21))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValuesByOrder() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, 20, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, 20).isSameByBind(exp("name", true, "sql", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 10, 20))).isFalse();
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InRangeExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void addSql() {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
InRangeExpression exp = new InRangeExpression("startDate", 1, 2);
|
||||
exp.addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("(startDate >= ? and startDate < ?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameInstance() {
|
||||
|
||||
InRangeExpression exp = new InRangeExpression("startDate", 1, 2);
|
||||
SpiExpression other = exp.copyForPlanKey();
|
||||
|
||||
assertThat(exp).isSameAs(other);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("startDate", 3, 4);
|
||||
|
||||
same(exp0, exp1);
|
||||
same(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_do_not_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("endDate", 1, 2);
|
||||
|
||||
different(exp0, exp1);
|
||||
different(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_do_not_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("startDate", 1, 3);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isFalse();
|
||||
assertThat(exp1.isSameByBind(exp0)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("startDate", 1, 2);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isTrue();
|
||||
assertThat(exp1.isSameByBind(exp0)).isTrue();
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JsonPathExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private JsonPathExpression exp(String propertyName, String path, Op operator, Object value) {
|
||||
return new JsonPathExpression(propertyName, path, operator, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", "path", Op.EQ, 10), exp("a", "path", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", "path", Op.EQ, 10), exp("a", "path", Op.EQ, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffPath() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), exp("a", "pathDiff", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), exp("b", "path", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffOperator_diff() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), exp("a", "path", Op.LT, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.LT, 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameNullBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, null).isSameByBind(exp("a", "path", Op.LT, null))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.EQ, "junk"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffFirstNullBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, null).isSameByBind(exp("a", "path", Op.LT, 10))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffLastNullBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.LT, null))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Expr;
|
||||
import io.ebean.Expression;
|
||||
import io.ebean.Junction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JunctionExpressionTest extends BaseExpressionTest {
|
||||
|
||||
Expression eq(String propName, int value) {
|
||||
return Expr.eq(propName, value);
|
||||
}
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp(Expression... expressions) {
|
||||
|
||||
DefaultExpressionList<Object> list = new DefaultExpressionList<>(null, new DefaultExpressionFactory(true, false), null);
|
||||
for (Expression ex : expressions) {
|
||||
list.add(ex);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
<T> JunctionExpression<T> and(DefaultExpressionList<T> list) {
|
||||
return new JunctionExpression<>(Junction.Type.AND, list);
|
||||
}
|
||||
|
||||
<T> JunctionExpression<T> or(DefaultExpressionList<T> list) {
|
||||
return new JunctionExpression<>(Junction.Type.OR, list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(and(exp(eq("a", 10), eq("b", 10))),
|
||||
and(exp(eq("a", 10), eq("b", 10))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameByPlan_when_same() {
|
||||
|
||||
same(and(exp(eq("a", 10), eq("b", 10)).copyForPlanKey()),
|
||||
and(exp(eq("a", 10), eq("b", 10))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameByPlan_when_diff() {
|
||||
|
||||
different(and(exp(eq("a", 10), eq("b", 10)).copyForPlanKey()),
|
||||
and(exp(eq("a", 10), eq("c", 10))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType() {
|
||||
|
||||
different(and(exp(eq("a", 10), eq("b", 10))),
|
||||
or(exp(eq("a", 10), eq("b", 10))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.LikeType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LikeExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private LikeExpression exp(String propertyName, String value, boolean caseInsensitive, LikeType type) {
|
||||
return new LikeExpression(propertyName, value, caseInsensitive, type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "rob", true, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_then_stillSame() {
|
||||
|
||||
same(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "bor", true, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffCaseInsensitive() {
|
||||
|
||||
different(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "rob", false, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffLikeType() {
|
||||
|
||||
different(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "rob", true, LikeType.ENDS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty() {
|
||||
|
||||
different(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("b", "rob", true, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
.isSameByBind(exp("a", "rob", true, LikeType.STARTS_WITH))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
.isSameByBind(exp("a", "bor", true, LikeType.STARTS_WITH))).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Expr;
|
||||
import io.ebean.Expression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LogicExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private Expression eq(String propName, int value) {
|
||||
return Expr.eq(propName, value);
|
||||
}
|
||||
|
||||
private LogicExpression and(Expression a, Expression b) {
|
||||
return new LogicExpression.And(a, b);
|
||||
}
|
||||
|
||||
private LogicExpression or(Expression a, Expression b) {
|
||||
return new LogicExpression.Or(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql() {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
LogicExpression and = and(eq("a", 10), eq("b", 10));
|
||||
and.addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("(a = ? and b = ?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 10), eq("b", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_then_stillSame() {
|
||||
|
||||
same(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 20), eq("b", 20)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExp1_then_diff() {
|
||||
|
||||
different(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("c", 10), eq("b", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExp2_then_diff() {
|
||||
|
||||
different(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 10), eq("c", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_then_diff() {
|
||||
|
||||
different(or(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 10), eq("c", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
|
||||
different(or(eq("a", 10), eq("b", 10))
|
||||
, new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(and(eq("a", 10), eq("b", 10))
|
||||
.isSameByBind(and(eq("a", 10), eq("c", 10)))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(and(eq("a", 10), eq("b", 10))
|
||||
.isSameByBind(and(eq("a", 10), eq("c", 20)))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_notNested() {
|
||||
|
||||
LogicExpression and = and(eq("orderDate", 10), eq("shipDate", 10));
|
||||
|
||||
and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
|
||||
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_nestedSame() {
|
||||
|
||||
LogicExpression and = and(eq("details.orderQty", 10), eq("details.unitPrice", 10));
|
||||
|
||||
String path = and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(path).isEqualTo("details");
|
||||
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
|
||||
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_nestedDifferent() {
|
||||
|
||||
LogicExpression and = and(eq("details.orderQty", 10), eq("shipments.shipTime", 10));
|
||||
|
||||
String path = and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(path).isNull();
|
||||
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
|
||||
assertThat(((NestedPathWrapperExpression) and.expOne).nestedPath).isEqualTo("details");
|
||||
assertThat(and.expTwo).isInstanceOf(NestedPathWrapperExpression.class);
|
||||
assertThat(((NestedPathWrapperExpression) and.expTwo).nestedPath).isEqualTo("shipments");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_oneNested() {
|
||||
|
||||
LogicExpression and = and(eq("details.orderQty", 10), eq("orderDate", 10));
|
||||
|
||||
String path = and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(path).isNull();
|
||||
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
|
||||
assertThat(((NestedPathWrapperExpression) and.expOne).nestedPath).isEqualTo("details");
|
||||
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class NoopExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
initTables();
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().add(NoopExpression.INSTANCE)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
assertThat(generatedSql).contains("select t0.id from o_customer t0 where 1=1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_withPreAndPost() {
|
||||
initTables();
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().eq("name", null)
|
||||
.add(NoopExpression.INSTANCE)
|
||||
.ne("status", null)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
|
||||
assertThat(generatedSql).contains("select t0.id from o_customer t0 where t0.name is null and 1=1 and t0.status is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(new NoopExpression(), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
|
||||
different(new NoopExpression(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
|
||||
assertThat(new NoopExpression().isSameByBind(new NoopExpression())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffExpressionType() {
|
||||
|
||||
assertThat(new NoopExpression().isSameByBind(null)).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import io.ebean.Expression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.ebean.Expr.eq;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class NotExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
NotExpression not(Expression expression) {
|
||||
return new NotExpression(expression);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(not(eq("a", 10)), not(eq("a", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_sameByPlan() {
|
||||
|
||||
same(not(eq("a", 10)), not(eq("a", 20)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_different() {
|
||||
|
||||
different(not(eq("a", 10)), not(eq("b", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_differentExpressionType() {
|
||||
|
||||
different(not(eq("a", 10)), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
|
||||
assertThat(not(eq("a", 10))
|
||||
.isSameByBind(not(eq("b", 10)))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_different() {
|
||||
|
||||
assertThat(not(eq("a", 10))
|
||||
.isSameByBind(not(eq("a", 20)))).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.ManyWhereJoins;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class NullExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private NullExpression nullExp(String propertyName, boolean notNull) {
|
||||
NullExpression expr = new NullExpression(propertyName, notNull);
|
||||
expr.containsMany(getBeanDescriptor(Order.class), new ManyWhereJoins());
|
||||
return expr;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_notNull() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("id", true).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("id is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_null() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("id", false).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("id is null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_notNull_and_assocOne() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("customer", true).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("customer.id is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_null_and_assocOne() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("customer", false).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("customer.id is null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameInstance() throws Exception {
|
||||
|
||||
NullExpression exp = nullExp("customer.name", false);
|
||||
SpiExpression other = exp.copyForPlanKey();
|
||||
|
||||
assertThat(exp).isSameAs(other);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_true() throws Exception {
|
||||
|
||||
assertThat(nullExp("customer.name", false)
|
||||
.isSameByBind(nullExp("customer.name", false))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_true() throws Exception {
|
||||
|
||||
same(nullExp("customer.name", false),
|
||||
nullExp("customer.name", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_false_when_notNullDiff() throws Exception {
|
||||
|
||||
different(new NullExpression("customer.name", false),
|
||||
new NullExpression("customer.name", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_false_when_propertyNameDiff() throws Exception {
|
||||
|
||||
different(new NullExpression("customer.startDate", true),
|
||||
new NullExpression("customer.name", true));
|
||||
}
|
||||
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.ExpressionList;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import io.ebeaninternal.server.deploy.BaseTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class PrepareDocNestedTest extends BaseTest {
|
||||
@Test
|
||||
public void prepare() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("details.orderQty", 1)
|
||||
.query().where();
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 1);
|
||||
assertEquals(exp.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_multipleOfSamePath() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 2);
|
||||
assertEquals(exp.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_mixed() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("customer.id", 1)
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 2);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
DefaultExpressionList<?> second = (DefaultExpressionList<?>) underlyingList.get(1);
|
||||
assertEquals(second.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_nestedJunction() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.not()
|
||||
.gt("customer.id", 1)
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 1);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
JunctionExpression<?> junction = (JunctionExpression<?>) underlyingList.get(0);
|
||||
List<SpiExpression> junctionUnderlying = junction.exprList.getUnderlyingList();
|
||||
JunctionExpression<?> nestedNestedPath = (JunctionExpression<?>) junctionUnderlying.get(1);
|
||||
assertEquals(nestedNestedPath.exprList.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_nestedMultiple() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.isNotNull("shipments.shipTime")
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 2);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>) underlyingList.get(0);
|
||||
assertEquals(shipExpr.allDocNestedPath, "shipments");
|
||||
|
||||
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>) underlyingList.get(1);
|
||||
assertEquals(detailsExpr.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void prepare_when_manyMixed() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("customer.id", 1) // 0
|
||||
.isNotNull("shipments.shipTime") // shipments 0
|
||||
.isNotNull("status") // 1
|
||||
.gt("details.orderQty", 1) // details 0
|
||||
.isNotNull("orderDate") // 2
|
||||
.gt("details.unitPrice", 1) // details 1
|
||||
.query().where();
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 5);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>) underlyingList.get(3);
|
||||
assertEquals(shipExpr.allDocNestedPath, "shipments");
|
||||
|
||||
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>) underlyingList.get(4);
|
||||
assertEquals(detailsExpr.allDocNestedPath, "details");
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class RawExpressionBuilderTest {
|
||||
|
||||
@Test
|
||||
public void buildSingle_noop() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo = ?", 42);
|
||||
assertThat(exp.sql).isEqualTo("foo = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildSingle_noExpand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo = ?", asList(42, 43));
|
||||
assertThat(exp.sql).isEqualTo("foo = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildSingle_expand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo in (?1)", asList(42, 43));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?,?)");
|
||||
assertThat(exp.values).contains(42, 43);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildSingle_expand_more() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo in (?1)", asList(42, 43, 44, 45));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?,?,?,?)");
|
||||
assertThat(exp.values).contains(42, 43, 44, 45);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void buildSingle_expand_single() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo in (?1)", asList(42));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?)");
|
||||
assertThat(exp.values).contains(42);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void build_noop() {
|
||||
RawExpression exp = RawExpressionBuilder.build("foo = ?", asArray(42));
|
||||
assertThat(exp.sql).isEqualTo("foo = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_noExpand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo = ? and bar = any(?)", asArray(44, asList(42, 43)));
|
||||
assertThat(exp.sql).isEqualTo("foo = ? and bar = any(?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_expand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.build("foo in (?) and bar in (?2)", asArray(44, asList(42, 43)));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?) and bar in (?,?)");
|
||||
assertThat(exp.values).contains(44, 42, 43);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_expand2() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.build("foo in (?) and bar in (?2) (?3)", asArray(44, asList(42, 43), asList(91, 92, 93)));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?) and bar in (?,?) (?,?,?)");
|
||||
assertThat(exp.values).containsExactly(44, 42, 43, 91, 92, 93);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_expand3() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.build("foo in (?) and bar in (?2) (?3) and ?", asArray(44, asList(42, 43), asList(91, 92, 93), 87));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?) and bar in (?,?) (?,?,?) and ?");
|
||||
assertThat(exp.values).containsExactly(44, 42, 43, 91, 92, 93, 87);
|
||||
}
|
||||
|
||||
private Object[] asArray(Object... values) {
|
||||
return values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.BindValuesKey;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class RawExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private RawExpression exp(String sql, Object... values) {
|
||||
return new RawExpression(sql, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
same(exp("a", 10), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindValues() {
|
||||
same(exp("a", 10), exp("a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffSql() {
|
||||
different(exp("a", 10), exp("b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
assertThat(exp("a", 10).isSameByBind(exp("a", 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
assertThat(exp("a", 10).isSameByBind(exp("a", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
assertThat(exp("a", 10).isSameByBind(exp("a", 10, 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
assertThat(exp("a", 10, 20).isSameByBind(exp("a", 10))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryBindHash_when_sameBindValues() {
|
||||
assert_queryBindHash_isSame(exp("a", 10), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryBindHash_when_diffBindValues() {
|
||||
assert_queryBindHash_isDifferent(exp("a", 10), exp("a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryBindHash_when_diffBindValues2() {
|
||||
assert_queryBindHash_isDifferent(exp("a", 10), exp("a", 10, 11));
|
||||
}
|
||||
|
||||
public void assert_queryBindHash_isDifferent(RawExpression exp0, RawExpression exp1) {
|
||||
assertThat(bindKey(exp0)).isNotEqualTo(bindKey(exp1));
|
||||
}
|
||||
|
||||
public void assert_queryBindHash_isSame(RawExpression exp0, RawExpression exp1) {
|
||||
assertThat(bindKey(exp0)).isEqualTo(bindKey(exp1));
|
||||
}
|
||||
|
||||
private BindValuesKey bindKey(RawExpression query) {
|
||||
BindValuesKey bindValuesKey = new BindValuesKey();
|
||||
query.queryBindKey(bindValuesKey);
|
||||
return bindValuesKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class SameTest {
|
||||
|
||||
@Test
|
||||
public void sameByNull() throws Exception {
|
||||
|
||||
Assertions.assertThat(Same.sameByNull("a", "a")).isTrue();
|
||||
assertThat(Same.sameByNull("a", "b")).isTrue();
|
||||
assertThat(Same.sameByNull(null, null)).isTrue();
|
||||
assertThat(Same.sameByNull("a", null)).isFalse();
|
||||
assertThat(Same.sameByNull(null, "a")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameByValue() throws Exception {
|
||||
|
||||
assertThat(Same.sameByValue("a", "a")).isTrue();
|
||||
assertThat(Same.sameByValue("a", "b")).isFalse();
|
||||
assertThat(Same.sameByValue(null, null)).isTrue();
|
||||
assertThat(Same.sameByValue("a", null)).isFalse();
|
||||
assertThat(Same.sameByValue(null, "a")).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SimpleExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private SimpleExpression exp(String propertyName, Op operator, Object value) {
|
||||
return new SimpleExpression(propertyName, operator, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", Op.EQ, 10), exp("a", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", Op.EQ, 10), exp("a", Op.EQ, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", Op.EQ, 10), exp("b", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffOperator_diff() {
|
||||
|
||||
different(exp("a", Op.EQ, 10), exp("a", Op.LT, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", Op.EQ, 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", Op.EQ, 10).isSameByBind(exp("a", Op.LT, 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", Op.EQ, 10).isSameByBind(exp("a", Op.EQ, "junk"))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.core.SpiOrmQueryRequest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.expression.platform.DbExpressionHandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Test double for testing with SpiExpressionRequest.
|
||||
*/
|
||||
public class TDSpiExpressionRequest implements SpiExpressionRequest {
|
||||
|
||||
List<Object> bindValues = new ArrayList<>();
|
||||
|
||||
final BeanDescriptor<?> descriptor;
|
||||
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
public TDSpiExpressionRequest(BeanDescriptor<?> descriptor) {
|
||||
this.descriptor = descriptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DbExpressionHandler getDbPlatformHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parseDeploy(String logicalProp) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BeanDescriptor<?> getBeanDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiOrmQueryRequest<?> getQueryRequest() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiExpressionRequest append(String sqlExpression) {
|
||||
sql.append(sqlExpression);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBindEncryptKey(Object encryptKey) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBindValue(Object bindValue) {
|
||||
bindValues.add(bindValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSql() {
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Object> getBindValues() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextParameter() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLike(boolean rawLikeExpression) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String escapeLikeString(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendInExpression(boolean not, List<Object> bindValues) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package io.ebeaninternal.server.querydefn;
|
||||
|
||||
|
||||
import io.ebean.*;
|
||||
import io.ebeaninternal.api.BindValuesKey;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.server.core.DefaultServer;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequest;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequestTestHelper;
|
||||
import io.ebeaninternal.server.deploy.BaseTest;
|
||||
import io.ebeaninternal.server.expression.BaseExpressionTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultOrmQueryTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void when_forUpdate_then_excludeFromBeanCache() {
|
||||
|
||||
DefaultOrmQuery<Customer> q1 = (DefaultOrmQuery<Customer>) DB.find(Customer.class)
|
||||
.forUpdate().where().eq("id", 42).query();
|
||||
|
||||
assertThat(q1.getUseBeanCache()).isSameAs(CacheMode.OFF);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForId_when_eqId_then_translatedTo_setId() {
|
||||
|
||||
DefaultOrmQuery<Order> q1 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().eq("id", 42).query();
|
||||
assertThat(q1.getWhereExpressions()).isNotNull();
|
||||
assertThat(q1.getId()).isNull();
|
||||
|
||||
assertThat(q1.isFindById()).isTrue();
|
||||
|
||||
assertThat(q1.getId()).isEqualTo(42);
|
||||
assertThat(q1.getWhereExpressions()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForId_when_idEq_ok() {
|
||||
|
||||
DefaultOrmQuery<Order> q1 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().idEq(42).query();
|
||||
assertThat(q1.getId()).isEqualTo(42);
|
||||
assertThat(q1.isFindById()).isTrue();
|
||||
assertThat(q1.getId()).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForId_when_setId_ok() {
|
||||
|
||||
DefaultOrmQuery<Order> q1 = (DefaultOrmQuery<Order>) DB.find(Order.class).setId(42);
|
||||
assertThat(q1.getId()).isEqualTo(42);
|
||||
assertThat(q1.isFindById()).isTrue();
|
||||
assertThat(q1.getId()).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_addWhere_then_planChanges() {
|
||||
|
||||
DefaultOrmQuery<Order> q1 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().in("name", "a", "b", "c").query();
|
||||
DefaultOrmQuery<Order> q2 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().in("id", 2, 2, 3).query();
|
||||
|
||||
prepare(q1, q2);
|
||||
assertThat(q1.createQueryPlanKey()).isNotEqualTo(q2.createQueryPlanKey());
|
||||
assertThat(bindKey(q1)).isNotEqualTo(bindKey(q2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_sameWhereWithDiffBindValues_then_planSame_bindDiff() {
|
||||
|
||||
DefaultOrmQuery<Order> q1 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().in("id", 1, 2, 3).query();
|
||||
DefaultOrmQuery<Order> q2 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().in("id", 2, 2, 3).query();
|
||||
|
||||
prepare(q1, q2);
|
||||
assertThat(q1.createQueryPlanKey()).isEqualTo(q2.createQueryPlanKey());
|
||||
assertThat(bindKey(q1)).isNotEqualTo(bindKey(q2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_sameWhereAndBindValues_then_planSameAndBind() {
|
||||
|
||||
DefaultOrmQuery<Order> q1 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().in("id", 1, 2, 3).query();
|
||||
DefaultOrmQuery<Order> q2 = (DefaultOrmQuery<Order>) DB.find(Order.class).where().in("id", 1, 2, 3).query();
|
||||
|
||||
prepare(q1, q2);
|
||||
assertThat(q1.createQueryPlanKey()).isEqualTo(q2.createQueryPlanKey());
|
||||
assertThat(bindKey(q1)).isEqualTo(bindKey(q2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_diffFirstMaxRows_then_differentPlan() {
|
||||
|
||||
DefaultOrmQuery<Order> query1 = (DefaultOrmQuery<Order>) DB.find(Order.class)
|
||||
.setFirstRow(0)
|
||||
.setMaxRows(31);
|
||||
|
||||
DefaultOrmQuery<Order> query2 = (DefaultOrmQuery<Order>) DB.find(Order.class)
|
||||
.setFirstRow(1)
|
||||
.setMaxRows(0);
|
||||
|
||||
prepare(query1, query2);
|
||||
assertThat(query1.createQueryPlanKey()).isNotEqualTo(query2.createQueryPlanKey());
|
||||
}
|
||||
|
||||
private <T> void prepare(DefaultOrmQuery<T> q1, DefaultOrmQuery<T> q2) {
|
||||
|
||||
OrmQueryRequest<T> r1 = OrmQueryRequestTestHelper.queryRequest(q1);
|
||||
q1.prepare(r1);
|
||||
|
||||
OrmQueryRequest<T> r2 = OrmQueryRequestTestHelper.queryRequest(q2);
|
||||
q2.prepare(r2);
|
||||
}
|
||||
|
||||
private BindValuesKey bindKey(DefaultOrmQuery<Order> query) {
|
||||
BindValuesKey key = new BindValuesKey();
|
||||
query.queryBindKey(key);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
package io.ebeaninternal.server.querydefn;
|
||||
|
||||
import io.ebeaninternal.server.deploy.BaseTest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class OrmQueryDetailTest extends BaseTest {
|
||||
|
||||
OrmQueryDetail parse(String query) {
|
||||
return new OrmQueryDetailParser(query).parse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty_when_empty() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
assertThat(detail.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty_when_hasFetch_expect_false() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("customer", null, null);
|
||||
assertThat(detail.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty_when_hasSelect_expect_false() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.select("name");
|
||||
assertThat(detail.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutoTuneEqual_when_fetchOrderIsDifferent_then_stillEqual() {
|
||||
|
||||
OrmQueryDetail detail1 = parse("select (id,name) fetch customer (name) fetch details (code)");
|
||||
OrmQueryDetail detail2 = parse("select (id,name) fetch details (code) fetch customer (name)");
|
||||
|
||||
assertTrue(detail1.isAutoTuneEqual(detail2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutoTuneEqual_when_different_select() {
|
||||
|
||||
OrmQueryDetail detail1 = parse("select (id,name) fetch customer (name) fetch details (code)");
|
||||
OrmQueryDetail detail2 = parse("select (id) fetch details (code) fetch customer (name)");
|
||||
|
||||
assertFalse(detail1.isAutoTuneEqual(detail2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutoTuneEqual_when_different_selectInFetch() {
|
||||
|
||||
OrmQueryDetail detail1 = parse("select (id,name) fetch customer (name) fetch details (code)");
|
||||
OrmQueryDetail detail2 = parse("select (id,name) fetch customer (id,name) fetch details (code)");
|
||||
|
||||
assertFalse(detail1.isAutoTuneEqual(detail2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutoTuneEqual_when_different_additionalFetch() {
|
||||
|
||||
OrmQueryDetail detail1 = parse("select (id,name) fetch customer (name) fetch details (code)");
|
||||
OrmQueryDetail detail2 = parse("select (id,name) fetch customer (name) fetch details (code) fetch customer.contacts");
|
||||
|
||||
assertFalse(detail1.isAutoTuneEqual(detail2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutoTuneEqual_when_different_removedFetch() {
|
||||
|
||||
OrmQueryDetail detail1 = parse("select (id,name) fetch customer (name) fetch details (code) fetch customer.contacts");
|
||||
OrmQueryDetail detail2 = parse("select (id,name) fetch customer (name) fetch details (code)");
|
||||
|
||||
assertFalse(detail1.isAutoTuneEqual(detail2));
|
||||
assertFalse(detail2.isAutoTuneEqual(detail1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutoTuneEqual_when_different_removedFetchProperty() {
|
||||
|
||||
OrmQueryDetail detail1 = parse("select (id,name) fetch customer (name)");
|
||||
OrmQueryDetail detail2 = parse("select (id,name) fetch customer ");
|
||||
|
||||
assertFalse(detail1.isAutoTuneEqual(detail2));
|
||||
assertFalse(detail2.isAutoTuneEqual(detail1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAutoTuneEqual_when_different_path() {
|
||||
|
||||
OrmQueryDetail detail1 = parse("select (id,name) fetch customer (name)");
|
||||
OrmQueryDetail detail2 = parse("select (id,name) fetch details (id) ");
|
||||
|
||||
assertFalse(detail1.isAutoTuneEqual(detail2));
|
||||
assertFalse(detail2.isAutoTuneEqual(detail1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_whenMultiple() throws Exception {
|
||||
|
||||
OrmQueryDetail other = new OrmQueryDetail();
|
||||
other.select("id,name");
|
||||
|
||||
OrmQueryProperties root = other.getChunk(null, false);
|
||||
assertNull(root.getPath());
|
||||
assertThat(root.getIncluded()).containsExactly("id", "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_whenOne() throws Exception {
|
||||
|
||||
OrmQueryDetail other = new OrmQueryDetail();
|
||||
other.select("name");
|
||||
|
||||
OrmQueryProperties root = other.getChunk(null, false);
|
||||
assertNull(root.getPath());
|
||||
assertThat(root.getIncluded()).containsExactly("name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFetchPaths_when_noFetches_then_expect_empty() {
|
||||
|
||||
assertThat(new OrmQueryDetail().getFetchPaths()).isEmpty();
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.select("foo");
|
||||
|
||||
assertThat(detail.getFetchPaths()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFetchPaths_when_oneFetch() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.select("foo");
|
||||
detail.fetch("customer", null, null);
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFetchPaths_when_multipleFetch_expect_preserveOrder() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.select("foo");
|
||||
detail.fetch("customer", null, null);
|
||||
detail.fetch("details", null, null);
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFetchPaths_when_multipleFetch_expect_preserveOrder_v2() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.select("foo");
|
||||
detail.fetch("details", null, null);
|
||||
detail.fetch("customer", null, null);
|
||||
detail.fetch("details.product", null, null);
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "customer", "details.product");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowOne_expect_stillFetchJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, true, true);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowNone_expect_queryJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, false, true);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowOneButSecond_expect_queryJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
detail.fetch("customer.contacts", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, true, true);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isFalse();
|
||||
assertThat(detail.getChunk("customer.contacts", false).isQueryFetch()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markQueryJoins_when_allowNone_expect_bothQueryJoin() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("details", null, null);
|
||||
detail.fetch("customer.contacts", null, null);
|
||||
|
||||
detail.markQueryJoins(orderDesc(), null, false, true);
|
||||
|
||||
assertThat(detail.getChunk("details", false).isQueryFetch()).isTrue();
|
||||
assertThat(detail.getChunk("customer.contacts", false).isQueryFetch()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortFetchPaths_when_missingParent_expect_addsMissing() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("customer.contacts", null, null);
|
||||
|
||||
detail.sortFetchPaths(orderDesc());
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "customer.contacts");
|
||||
assertThat(detail.getChunk("customer", false).getIncluded()).containsExactly("id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortFetchPaths_when_outOfOrder_expect_correctOrder() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.fetch("customer.contacts", "email", null);
|
||||
detail.fetch("customer", "name", null);
|
||||
|
||||
detail.sortFetchPaths(orderDesc());
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "customer.contacts");
|
||||
assertThat(detail.getChunk("customer", false).getIncluded()).containsExactly("name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortFetchPaths_when_empty_expect_stillEmpty() {
|
||||
|
||||
OrmQueryDetail detail = new OrmQueryDetail();
|
||||
detail.sortFetchPaths(orderDesc());
|
||||
|
||||
assertThat(detail.getFetchPaths()).isEmpty();
|
||||
}
|
||||
|
||||
BeanDescriptor<Order> orderDesc() {
|
||||
return getBeanDescriptor(Order.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
package io.ebeaninternal.server.querydefn;
|
||||
|
||||
|
||||
import io.ebean.ExpressionList;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.api.CQueryPlanKey;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.server.deploy.BaseTest;
|
||||
import io.ebeaninternal.server.deploy.TableJoin;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployTableJoin;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployTableJoinColumn;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class OrmQueryPlanKeyTest extends BaseTest {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private DefaultOrmQuery<Customer> query() {
|
||||
return (DefaultOrmQuery) server().find(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_defaults() {
|
||||
|
||||
assertSame(query().createQueryPlanKey(), query().createQueryPlanKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffTableJoinNull() {
|
||||
|
||||
DefaultOrmQuery<Customer> q1 = query();
|
||||
q1.setM2MIncludeJoin(tableJoin("table", "id", "customer_id"));
|
||||
|
||||
assertDifferent(q1, query());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffTableJoin() {
|
||||
|
||||
DefaultOrmQuery<Customer> q1 = query();
|
||||
q1.setM2MIncludeJoin(tableJoin("one", "cid", "customer_id"));
|
||||
|
||||
DefaultOrmQuery<Customer> q2 = query();
|
||||
q2.setM2MIncludeJoin(tableJoin("two", "cid", "customer_id"));
|
||||
|
||||
assertDifferent(q1, q2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_sameTableJoin() {
|
||||
|
||||
DefaultOrmQuery<Customer> q1 = query();
|
||||
q1.setM2MIncludeJoin(tableJoin("one", "id", "cust_id"));
|
||||
|
||||
DefaultOrmQuery<Customer> q2 = query();
|
||||
q2.setM2MIncludeJoin(tableJoin("one", "id", "cust_id"));
|
||||
|
||||
assertSame(q1, q2);
|
||||
}
|
||||
|
||||
private TableJoin tableJoin(String table, String col1, String col2) {
|
||||
DeployTableJoin deploy = new DeployTableJoin();
|
||||
deploy.setTable(table);
|
||||
deploy.addJoinColumn(new DeployTableJoinColumn(col1, col2));
|
||||
return new TableJoin(deploy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffQueryType() {
|
||||
|
||||
DefaultOrmQuery<Customer> q1 = query();
|
||||
q1.setType(SpiQuery.Type.BEAN);
|
||||
|
||||
DefaultOrmQuery<Customer> q2 = query();
|
||||
q2.setType(SpiQuery.Type.LIST);
|
||||
|
||||
assertDifferent(q1, q2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_firstRowsDifferent() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setFirstRow(10));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
key1 = planKey(query().setFirstRow(10));
|
||||
key2 = planKey(query().setFirstRow(9));
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
key1 = planKey(query());
|
||||
key2 = planKey(query().setFirstRow(9));
|
||||
assertDifferent(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_maxRowsDifferent() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setMaxRows(10));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
key1 = planKey(query().setMaxRows(10));
|
||||
key2 = planKey(query().setMaxRows(9));
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
key1 = planKey(query());
|
||||
key2 = planKey(query().setMaxRows(9));
|
||||
assertDifferent(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_firstRowsMaxRowsSame() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setMaxRows(10).setFirstRow(20));
|
||||
CQueryPlanKey key2 = planKey(query().setFirstRow(20).setMaxRows(10));
|
||||
assertSame(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffDisableLazyLoading() {
|
||||
|
||||
assertDifferent(query().setDisableLazyLoading(true), query());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffOrderByNull() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().order("id"));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
key1 = planKey(query().order().asc("id"));
|
||||
key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_orderBySame() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().order("id, name"));
|
||||
CQueryPlanKey key2 = planKey(query().order("id, name"));
|
||||
assertSame(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffDistinct() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setDistinct(true));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_sameDistinct() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setDistinct(true));
|
||||
CQueryPlanKey key2 = planKey(query().setDistinct(true));
|
||||
assertSame(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_useDocStore() {
|
||||
CQueryPlanKey key1 = planKey(query().setUseDocStore(true));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
|
||||
assertDifferent(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffMapKey() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setMapKey("name"));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
CQueryPlanKey key3 = planKey(query().setMapKey("email"));
|
||||
assertDifferent(key1, key3);
|
||||
|
||||
CQueryPlanKey key4 = planKey(query().setMapKey("name"));
|
||||
assertSame(key1, key4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffIdNull() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setId(42));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_idBothGiven() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().setId(42));
|
||||
CQueryPlanKey key2 = planKey(query().setId(23));
|
||||
assertSame(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffTemporalMode() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query());
|
||||
CQueryPlanKey key2 = planKey(query().asDraft());
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
CQueryPlanKey key3 = planKey(query().asOf(new Timestamp(System.currentTimeMillis())));
|
||||
assertDifferent(key1, key3);
|
||||
|
||||
CQueryPlanKey key4 = planKey(query().setIncludeSoftDeletes());
|
||||
assertDifferent(key1, key4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffForUpdate() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().forUpdate());
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
CQueryPlanKey key3 = planKey(query().forUpdateNoWait());
|
||||
assertDifferent(key1, key3);
|
||||
|
||||
CQueryPlanKey key4 = planKey(query().forUpdateSkipLocked());
|
||||
assertDifferent(key1, key4);
|
||||
|
||||
CQueryPlanKey key5 = planKey(query().forUpdate());
|
||||
assertSame(key1, key5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffRootAliasNull() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().alias("alias"));
|
||||
CQueryPlanKey key2 = planKey(query());
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
CQueryPlanKey key3 = planKey(query().alias("diff"));
|
||||
assertDifferent(key1, key3);
|
||||
|
||||
CQueryPlanKey key4 = planKey(query().alias("alias"));
|
||||
assertSame(key1, key4);
|
||||
}
|
||||
|
||||
private DefaultOrmQuery<Customer> list_id_eq_42() {
|
||||
return (DefaultOrmQuery<Customer>) server().find(Customer.class)
|
||||
.where().eq("id", 42).query();
|
||||
}
|
||||
|
||||
private DefaultOrmQuery<Customer> list_id_eq_43() {
|
||||
return (DefaultOrmQuery<Customer>) server().find(Customer.class)
|
||||
.where().eq("id", 43).query();
|
||||
}
|
||||
|
||||
private DefaultOrmQuery<Customer> list_id_eq_42_and_name_eq_rob() {
|
||||
return (DefaultOrmQuery<Customer>) server().find(Customer.class)
|
||||
.where().eq("id", 43).eq("name", "rob").query();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void equals_when_sameWhere() {
|
||||
|
||||
assertSame(list_id_eq_42(), list_id_eq_43());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void equals_when_diffWhere() {
|
||||
|
||||
assertDifferent(list_id_eq_42(), list_id_eq_42_and_name_eq_rob());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffWhereNullLast() {
|
||||
|
||||
assertDifferent(list_id_eq_42(), query());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffHaving() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().having().eq("id", 42));
|
||||
CQueryPlanKey key2 = planKey(query().having().eq("id", 42).eq("name", "Rob"));
|
||||
assertDifferent(key1, key2);
|
||||
|
||||
CQueryPlanKey key3 = planKey(query().where().eq("id", 42));
|
||||
assertDifferent(key1, key3);
|
||||
|
||||
CQueryPlanKey key4 = planKey(query().having().eq("name", "Rob"));
|
||||
assertDifferent(key1, key4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_sameHaving() {
|
||||
|
||||
CQueryPlanKey key1 = planKey(query().having().eq("id", 42));
|
||||
CQueryPlanKey key2 = planKey(query().having().eq("id", 43));
|
||||
assertSame(key1, key2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_manualId_andSelectClause() {
|
||||
DefaultOrmQuery<Customer> q1 = (DefaultOrmQuery<Customer>)query().select("name");
|
||||
q1.setManualId();
|
||||
|
||||
assertDifferent(q1, query().select("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_manualId_andNoSelectClause() {
|
||||
DefaultOrmQuery<Customer> q1 = query();
|
||||
q1.setManualId();
|
||||
|
||||
assertSame(q1, query());
|
||||
}
|
||||
|
||||
private CQueryPlanKey planKey(ExpressionList<Customer> id) {
|
||||
return planKey(id.query());
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
private CQueryPlanKey planKey(Query<Customer> query) {
|
||||
return ((DefaultOrmQuery) query).createQueryPlanKey();
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private void assertDifferent(Query q1, Query q2) {
|
||||
assertDifferent(planKey(q1), planKey(q2));
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private void assertSame(Query q1, Query q2) {
|
||||
assertSame(planKey(q1), planKey(q2));
|
||||
}
|
||||
|
||||
private void assertDifferent(CQueryPlanKey key1, CQueryPlanKey key2) {
|
||||
assertThat(key1).isNotEqualTo(key2);
|
||||
assertThat(key1.hashCode()).isNotEqualTo(key2.hashCode());
|
||||
}
|
||||
|
||||
private void assertSame(CQueryPlanKey key1, CQueryPlanKey key2) {
|
||||
assertThat(key1).isEqualTo(key2);
|
||||
assertThat(key1.hashCode()).isEqualTo(key2.hashCode());
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import org.postgresql.util.PGobject;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
void assertBindNullTo_Null(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.wasSetNull);
|
||||
}
|
||||
|
||||
void assertBindNullTo_EmptyArray(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
|
||||
assertTrue(bind.wasEmptyArray);
|
||||
}
|
||||
|
||||
void assertBindNullTo_EmptyString(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.setEmptyString);
|
||||
}
|
||||
|
||||
void assertBindNullTo_PGObjectNull(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.wasPgoNull);
|
||||
}
|
||||
void assertBindNullTo_PGObjectEmpty(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.wasPgoEmpty);
|
||||
}
|
||||
|
||||
static class TestDoubleDataBind extends DataBind {
|
||||
|
||||
boolean setEmptyString;
|
||||
boolean wasSetNull;
|
||||
boolean setArray;
|
||||
boolean wasNull;
|
||||
boolean wasEmptyArray;
|
||||
boolean wasPgoNull;
|
||||
boolean wasPgoEmpty;
|
||||
|
||||
TestDoubleDataBind() {
|
||||
super(null, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNull(int jdbcType) {
|
||||
wasSetNull = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setString(String s) {
|
||||
setEmptyString = "[]".equals(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setObject(Object value) {
|
||||
wasNull = value == null;
|
||||
if (!wasNull) {
|
||||
if (value instanceof Object[]) {
|
||||
wasEmptyArray = ((Object[]) value).length == 0;
|
||||
} else {
|
||||
PGobject pgo = (PGobject) value;
|
||||
wasPgoEmpty = "[]".equals(pgo.getValue());
|
||||
wasPgoNull = pgo.getValue() == null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setArray(String arrayType, Object[] elements) {
|
||||
setArray = true;
|
||||
wasNull = elements == null;
|
||||
wasEmptyArray = (elements != null && elements.length == 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ConvertInetAddressTest {
|
||||
|
||||
@Test
|
||||
public void forString() {
|
||||
|
||||
InetAddress addr = ConvertInetAddresses.forString("128.1.10.23");
|
||||
assertEquals("128.1.10.23", addr.getHostAddress());
|
||||
|
||||
String ip6addr = "2001:db8:85a3:0:0:8a2e:370:7334";
|
||||
InetAddress addr6 = ConvertInetAddresses.forString(ip6addr);
|
||||
String uriAddr6 = ConvertInetAddresses.toUriString(addr6);
|
||||
assertEquals("[" + ip6addr + "]", uriAddr6);
|
||||
assertEquals(ip6addr, addr6.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ipv6_fromHost_getHostAddress() {
|
||||
InetAddress addr2 = ConvertInetAddresses.fromHost("2001:4f8:3:ba:2e0:81ff:fe22:d1f1");
|
||||
assertEquals("2001:4f8:3:ba:2e0:81ff:fe22:d1f1", addr2.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ipv6_fromHost_getHostAddress_2() {
|
||||
InetAddress addr2 = ConvertInetAddresses.fromHost("2001:db8:85a3:0:0:8a2e:370:7334");
|
||||
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", addr2.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUriString() {
|
||||
|
||||
InetAddress addr = ConvertInetAddresses.forString("128.1.10.23");
|
||||
assertEquals("128.1.10.23", addr.getHostAddress());
|
||||
|
||||
String uriAddr = ConvertInetAddresses.toUriString(addr);
|
||||
assertEquals("128.1.10.23", uriAddr);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ConvertInetAddressesTest {
|
||||
|
||||
@Test
|
||||
public void testForString() throws Exception {
|
||||
|
||||
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
|
||||
|
||||
String uri = ConvertInetAddresses.toUriString(loopbackAddress);
|
||||
InetAddress inetAddress = ConvertInetAddresses.forString(uri);
|
||||
|
||||
assertEquals(loopbackAddress, inetAddress);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsInetAddress() throws Exception {
|
||||
|
||||
assertTrue(ConvertInetAddresses.isInetAddress("127.0.0.1"));
|
||||
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("junk"));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("127.0.0.junk"));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("junk.0.0.23"));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress(""));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("127.0.0"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToUriString() throws Exception {
|
||||
|
||||
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
|
||||
|
||||
String uri = ConvertInetAddresses.toUriString(loopbackAddress);
|
||||
InetAddress inetAddress = ConvertInetAddresses.forUriString(uri);
|
||||
|
||||
assertEquals(loopbackAddress, inetAddress);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DecimalUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testToDecimal() throws Exception {
|
||||
|
||||
Instant now = Instant.now();
|
||||
BigDecimal value = DecimalUtils.toDecimal(now);
|
||||
|
||||
Instant instant = DecimalUtils.toInstant(value);
|
||||
Timestamp timestamp = DecimalUtils.toTimestamp(value);
|
||||
BigDecimal decimal = DecimalUtils.toDecimal(timestamp);
|
||||
|
||||
Instant instant1 = timestamp.toInstant();
|
||||
|
||||
assertEquals(instant, instant1);
|
||||
assertEquals(value, decimal);
|
||||
assertEquals(now, instant);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDuration() throws Exception {
|
||||
|
||||
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
|
||||
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
|
||||
Duration duration1 = DecimalUtils.toDuration(bigDecimal);
|
||||
|
||||
assertEquals(duration, duration1);
|
||||
assertEquals("PT5M23.0015S", duration1.toString());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
||||
public class DefaultTypeFactoryTest {
|
||||
|
||||
DefaultTypeFactory defaultTypeFactory = new DefaultTypeFactory(null);
|
||||
|
||||
@Test
|
||||
public void testCreateBoolean() throws Exception {
|
||||
|
||||
ScalarType<Boolean> stIntBoolean = defaultTypeFactory.createBoolean("0", "1");
|
||||
assertEquals(Types.INTEGER, stIntBoolean.getJdbcType());
|
||||
|
||||
stIntBoolean = defaultTypeFactory.createBoolean("1", "2");
|
||||
assertEquals(Types.INTEGER, stIntBoolean.getJdbcType());
|
||||
|
||||
ScalarType<Boolean> stStringBoolean = defaultTypeFactory.createBoolean("Y", "N");
|
||||
assertEquals(Types.VARCHAR, stStringBoolean.getJdbcType());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
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.jupiter.api.Test;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.IntEnum;
|
||||
import org.tests.model.basic.VarcharEnum;
|
||||
|
||||
import javax.persistence.EnumType;
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.Month;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class DefaultTypeManagerTest {
|
||||
|
||||
private DefaultTypeManager create() {
|
||||
DatabaseConfig serverConfig = new DatabaseConfig();
|
||||
serverConfig.setDatabasePlatform(new PostgresPlatform());
|
||||
BootupClasses bootupClasses = new BootupClasses();
|
||||
return new DefaultTypeManager(serverConfig, bootupClasses);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIntegerType() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
assertTrue(typeManager.isIntegerType("1"));
|
||||
assertTrue(typeManager.isIntegerType("0"));
|
||||
|
||||
assertFalse(typeManager.isIntegerType("A"));
|
||||
assertFalse(typeManager.isIntegerType("01"));
|
||||
assertFalse(typeManager.isIntegerType(" 01"));
|
||||
assertFalse(typeManager.isIntegerType(" 0"));
|
||||
assertFalse(typeManager.isIntegerType(" 1"));
|
||||
assertFalse(typeManager.isIntegerType(" A"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void enumDayMonth_builtIn_overrideAsString() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(Month.class, null);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class).as("built in type");
|
||||
|
||||
// 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");
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
|
||||
}
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enumDayOfWeek_builtIn_overrideAsString() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(DayOfWeek.class, null);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class).as("built in type");
|
||||
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
||||
@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'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IsoJsonDateTimeParserTest {
|
||||
|
||||
private IsoJsonDateTimeParser parser = new IsoJsonDateTimeParser();
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_hasMillis() {
|
||||
parseAndFormat("2016-02-28T20:39:00.123Z", "2016-02-28T20:39:00.123Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_noMillis() {
|
||||
parseAndFormat("2016-02-28T20:39:00Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_1dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.0Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_2dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.00Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_3dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.000Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_3dp_2() {
|
||||
parseAndFormat("2016-02-28T20:39:32.999000Z", "2016-02-28T20:39:32.999Z");
|
||||
}
|
||||
|
||||
private void parseAndFormat(String input, String expected) {
|
||||
Instant timestamp = parser.parseIso(input);
|
||||
String format = parser.formatIso(timestamp);
|
||||
assertThat(format).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Base class to help json testing.
|
||||
*/
|
||||
public class JsonTester<T> {
|
||||
|
||||
protected JsonFactory factory = new JsonFactory();
|
||||
|
||||
protected ScalarType<T> type;
|
||||
|
||||
public JsonTester(ScalarType<T> type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String test(T value) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
JsonGenerator generator = factory.createGenerator(writer);
|
||||
generator.writeStartObject();
|
||||
|
||||
WriteJson writeJson = new WriteJson(generator, JsonConfig.Include.ALL);
|
||||
writeJson.writeFieldName("key");
|
||||
type.jsonWrite(generator, value);
|
||||
generator.writeEndObject();
|
||||
generator.flush();
|
||||
|
||||
JsonParser parser = factory.createParser(writer.toString());
|
||||
JsonToken token = parser.nextToken();
|
||||
assertEquals(JsonToken.START_OBJECT, token);
|
||||
token = parser.nextToken();
|
||||
assertEquals(JsonToken.FIELD_NAME, token);
|
||||
parser.nextToken();
|
||||
|
||||
T val1 = type.jsonRead(parser);
|
||||
assertEquals(value, val1);
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class JsonTrimTest {
|
||||
|
||||
@Test
|
||||
void trim_array() {
|
||||
final String trimmed = JsonTrim.trim("[{\"name\": \"one\", \"along\": 1, \"timestamp\": 1629609021559}, {\"name\": \"two\", \"along\": 2, \"timestamp\": 1629609021559}]");
|
||||
assertThat(trimmed).isEqualTo("[{\"name\":\"one\",\"along\":1,\"timestamp\":1629609021559},{\"name\":\"two\",\"along\":2,\"timestamp\":1629609021559}]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object() {
|
||||
final String trimmed = JsonTrim.trim("{\"name\": \"one\",\t \t \"along\": 1, \"timestamp\": 1629609021559}");
|
||||
assertThat(trimmed).isEqualTo("{\"name\":\"one\",\"along\":1,\"timestamp\":1629609021559}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object_embeddedEscapeSlash() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\nf\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\nf\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_escapedTabNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_leadingEscapedNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"\\no\\t\\nf\\n\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"\\no\\t\\nf\\n\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_trailingEscapedNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\\n\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\\n\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_trailingEscapedSlash() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\\\\\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\\\\\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object_embeddedEscaped() {
|
||||
final String trimmed = JsonTrim.trim("{\"name\": \"one\nfoo\nbar\\bazz\tboo\",\t \t \n \"along\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"name\":\"one\nfoo\nbar\\bazz\tboo\",\"along\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_escaped() {
|
||||
final String trimmed = JsonTrim.trim(" \t \n {\"a\": \n \"\\t1\\t2\\n3\\\\\",\t \n \"b\": \"\\t1\\t2\\n3\\\\\" , \t \n \"c\": \"\\t1\\t2\\n3\\\\\" \t \n }");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"\\t1\\t2\\n3\\\\\",\"b\":\"\\t1\\t2\\n3\\\\\",\"c\":\"\\t1\\t2\\n3\\\\\"}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareFlag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModifyAwareFlagTest {
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
ModifyAwareFlag flag = new ModifyAwareFlag();
|
||||
flag.setMarkedDirty(true);
|
||||
oos.writeObject(flag);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
ModifyAwareFlag read = (ModifyAwareFlag)ois.readObject();
|
||||
assertThat(read.isMarkedDirty()).isTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareList;
|
||||
import io.ebeaninternal.json.ModifyAwareSet;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class ModifyAwareListTest {
|
||||
|
||||
private ModifyAwareList<String> createList() {
|
||||
return new ModifyAwareList<>(new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E")));
|
||||
}
|
||||
|
||||
private ModifyAwareList<String> createEmptyList() {
|
||||
return new ModifyAwareList<>(new ArrayList<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() {
|
||||
|
||||
assertEquals(5, createList().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
|
||||
assertFalse(createList().isEmpty());
|
||||
assertTrue(createEmptyList().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
|
||||
assertTrue(createList().contains("B"));
|
||||
assertFalse(createList().contains("Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
Iterator<String> iterator = list.iterator();
|
||||
assertTrue(iterator.hasNext());
|
||||
assertEquals("A", iterator.next());
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
iterator.remove();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToArray() {
|
||||
|
||||
Object[] objects = createList().toArray();
|
||||
assertEquals(5, objects.length);
|
||||
assertEquals("A", objects[0]);
|
||||
assertEquals("E", objects[4]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToArray1() {
|
||||
|
||||
String[] objects = createList().toArray(new String[5]);
|
||||
assertEquals(5, objects.length);
|
||||
assertEquals("A", objects[0]);
|
||||
assertEquals("E", objects[4]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.add("F");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.remove("A");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
|
||||
assertTrue(list.containsAll(Arrays.asList("A", "B")));
|
||||
assertFalse(list.containsAll(Arrays.asList("A", "B", "Z")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertTrue(list.addAll(Arrays.asList("F", "G")));
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRemoveAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertTrue(list.removeAll(Arrays.asList("A", "G")));
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertTrue(list.retainAll(Arrays.asList("A", "B")));
|
||||
assertTrue(list.isMarkedDirty());
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.clear();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
assertEquals(0, list.size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
|
||||
assertEquals("A", list.get(0));
|
||||
assertEquals("B", list.get(1));
|
||||
assertEquals("E", list.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.set(0, "Z");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
assertEquals(5, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndexOf() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertEquals(2, list.indexOf("C"));
|
||||
assertEquals(-1, list.indexOf("Z"));
|
||||
assertFalse(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastIndexOf() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertEquals(2, list.lastIndexOf("C"));
|
||||
assertEquals(-1, list.lastIndexOf("Z"));
|
||||
assertFalse(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIterator() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ListIterator<String> iterator = list.listIterator();
|
||||
assertTrue(iterator.hasNext());
|
||||
assertEquals("A", iterator.next());
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
iterator.remove();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIterator1() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ListIterator<String> iterator = list.listIterator(2);
|
||||
assertTrue(iterator.hasNext());
|
||||
assertEquals("C", iterator.next());
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
iterator.remove();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubList() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
List<String> sub = list.subList(1, 3);
|
||||
assertEquals("B", sub.get(0));
|
||||
assertEquals("C", sub.get(1));
|
||||
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
sub.remove("C");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsSet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ModifyAwareSet<String> set = list.asSet();
|
||||
assertFalse(set.isMarkedDirty());
|
||||
|
||||
set.add("next");
|
||||
|
||||
assertTrue(set.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
ModifyAwareList<String> orig = createList();
|
||||
oos.writeObject(orig);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareList<String> read = (ModifyAwareList<String>)ois.readObject();
|
||||
assertThat(read).contains("A", "B", "C", "D", "E");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareList<String> listA = createList();
|
||||
ModifyAwareList<String> listB = createList();
|
||||
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareList<String> listA = createList();
|
||||
ModifyAwareList<String> listB = createList();
|
||||
listB.add("F");
|
||||
|
||||
assertThat(listA).isNotEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isNotEqualTo(listB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareList<String> listA = createEmptyList();
|
||||
ArrayList<String> listB = new ArrayList<>();
|
||||
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
|
||||
listA.add("foo");
|
||||
assertThat(listA).isNotEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isNotEqualTo(listB.hashCode());
|
||||
|
||||
listB.add("foo");
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModifyAwareMapTest {
|
||||
|
||||
private ModifyAwareMap<String, Integer> createMap() {
|
||||
HashMap<String, Integer> set = new HashMap<>();
|
||||
set.put("A", 1);
|
||||
set.put("B", 2);
|
||||
set.put("C", 3);
|
||||
set.put("D", 4);
|
||||
set.put("E", 5);
|
||||
return new ModifyAwareMap<>(set);
|
||||
}
|
||||
|
||||
private ModifyAwareMap<String, Integer> createEmptyMap() {
|
||||
HashMap<String, Integer> set = new HashMap<>();
|
||||
return new ModifyAwareMap<>(set);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
oos.writeObject(createMap());
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareMap<String, Integer> read = (ModifyAwareMap<String, Integer>) ois.readObject();
|
||||
assertThat(read).containsKeys("A", "B", "C", "D", "E").containsValues(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareMap<String, Integer> setA = createMap();
|
||||
ModifyAwareMap<String, Integer> setB = createMap();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareMap<String, Integer> setA = createMap();
|
||||
ModifyAwareMap<String, Integer> setB = createMap();
|
||||
setB.put("F", 6);
|
||||
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareMap<String, Integer> setA = createEmptyMap();
|
||||
HashMap<String, Integer> setB = new HashMap<>();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
|
||||
setA.put("foo", 42);
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
|
||||
setB.put("foo", 42);
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareSet;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModifyAwareSetTest {
|
||||
|
||||
private ModifyAwareSet<String> createSet() {
|
||||
HashSet<String> set = new HashSet<>();
|
||||
set.addAll(Arrays.asList("A", "B", "C", "D", "E"));
|
||||
return new ModifyAwareSet<>(set);
|
||||
}
|
||||
|
||||
private ModifyAwareSet<String> createEmptySet() {
|
||||
HashSet<String> set = new HashSet<>();
|
||||
return new ModifyAwareSet<>(set);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
oos.writeObject(createSet());
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareSet<String> read = (ModifyAwareSet<String>)ois.readObject();
|
||||
assertThat(read).contains("A", "B", "C", "D", "E");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareSet<String> setA = createSet();
|
||||
ModifyAwareSet<String> setB = createSet();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareSet<String> setA = createSet();
|
||||
ModifyAwareSet<String> setB = createSet();
|
||||
setB.add("F");
|
||||
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareSet<String> setA = createEmptySet();
|
||||
HashSet<String> setB = new HashSet<>();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
|
||||
setA.add("foo");
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
|
||||
setB.add("foo");
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArrayListH2Test extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArrayListH2.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.text.json.EJson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArrayListTest extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArrayList.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read_when_null() throws SQLException {
|
||||
|
||||
DataReader mock = Mockito.mock(DataReader.class);
|
||||
Mockito.when(mock.getArray()).thenReturn(null);
|
||||
|
||||
ScalarType<?> scalarType = ScalarTypeArrayList.factory().typeFor(Long.class, true);
|
||||
scalarType.read(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withUuidType_expect_uuidTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
List<UUID> input = new ArrayList<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withIntegerType_expect_longIntegerReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Integer.class, true);
|
||||
|
||||
List<Integer> input = new ArrayList<>();
|
||||
input.add(2);
|
||||
input.add(4);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withLongType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Long.class, true);
|
||||
|
||||
List<Long> input = new ArrayList<>();
|
||||
input.add(2L);
|
||||
input.add(4L);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withDoubleType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Double.class, true);
|
||||
|
||||
List<Double> input = new ArrayList<>();
|
||||
input.add(2D);
|
||||
input.add(4D);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonRead_withUuidType() throws IOException {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
List<UUID> input = new ArrayList<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
String asJson = EJson.write(input);
|
||||
|
||||
JsonParser parser = DB.json().createParser(new StringReader(asJson));
|
||||
|
||||
Object parsed = scalarType.jsonRead(parser);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.text.json.EJson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArraySetH2Test extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArraySetH2.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withUuidType_expect_uuidTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
Set<UUID> input = new LinkedHashSet<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Set<UUID> parsed = (Set<UUID>)scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withIntegerType_expect_longIntegerReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Integer.class, true);
|
||||
|
||||
Set<Integer> input = new LinkedHashSet<>();
|
||||
input.add(2);
|
||||
input.add(4);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Set<Integer> parsed = (Set<Integer>)scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withLongType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Long.class, true);
|
||||
|
||||
Set<Long> input = new LinkedHashSet<>();
|
||||
input.add(2L);
|
||||
input.add(4L);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Set<Long> parsed = (Set<Long>)scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withDoubleType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Double.class, true);
|
||||
|
||||
Set<Double> input = new LinkedHashSet<>();
|
||||
input.add(2D);
|
||||
input.add(4D);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonRead_withUuidType() throws IOException {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
Set<UUID> input = new LinkedHashSet<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
String asJson = EJson.write(input);
|
||||
|
||||
JsonParser parser = DB.json().createParser(new StringReader(asJson));
|
||||
|
||||
Object parsed = scalarType.jsonRead(parser);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArraySetTest extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArraySet.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.text.json.JsonContext;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.TOne;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeBooleanTest {
|
||||
|
||||
JsonContext jsonContext = DB.getDefault().json();
|
||||
|
||||
@Test
|
||||
public void json_true() {
|
||||
|
||||
TOne bean = new TOne();
|
||||
bean.setId(42);
|
||||
bean.setActive(true);
|
||||
|
||||
String json = jsonContext.toJson(bean);
|
||||
TOne tOne = jsonContext.toBean(TOne.class, json);
|
||||
|
||||
Assertions.assertTrue(tOne.isActive());
|
||||
assertEquals(json, "{\"id\":42,\"active\":true}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json_false() {
|
||||
|
||||
TOne bean = new TOne();
|
||||
bean.setId(42);
|
||||
bean.setActive(false);
|
||||
|
||||
String json = jsonContext.toJson(bean);
|
||||
TOne tOne = jsonContext.toBean(TOne.class, json);
|
||||
|
||||
Assertions.assertFalse(tOne.isActive());
|
||||
assertEquals(json, "{\"id\":42,\"active\":false}");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void IntBoolean_isLogicalBoolean() {
|
||||
|
||||
ScalarTypeBoolean.IntBoolean intBoolean = new ScalarTypeBoolean.IntBoolean(1, 0);
|
||||
assertThat(intBoolean).isInstanceOf(ScalarTypeLogicalType.class);
|
||||
|
||||
assertThat(intBoolean.getLogicalType()).isEqualTo(Types.BOOLEAN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.Calendar;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeCalendarTest {
|
||||
|
||||
private ScalarTypeCalendar type = new ScalarTypeCalendar(JsonConfig.DateTime.ISO8601, Types.TIMESTAMP);
|
||||
|
||||
@Test
|
||||
public void toJsonISO8601() {
|
||||
|
||||
Calendar instance = Calendar.getInstance();
|
||||
String asUtc = type.toJsonISO8601(instance);
|
||||
Calendar calendar = type.fromJsonISO8601(asUtc);
|
||||
|
||||
assertThat(instance).isEqualTo(calendar);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeDateTest {
|
||||
|
||||
private ScalarTypeDate type = new ScalarTypeDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void formatParse_PG_DATE_POSITIVE_INFINITY() {
|
||||
|
||||
Date postgresInfinityDate = new Date(9223372036825200000L);
|
||||
|
||||
String format = type.formatValue(postgresInfinityDate);
|
||||
Date parsed = type.parse(format);
|
||||
assertEquals(parsed, postgresInfinityDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
|
||||
Date val = new Date(1557360000000L);
|
||||
|
||||
JsonTester<Date> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557360000000}");
|
||||
|
||||
JsonTester<Date> jsonIso = new JsonTester<>(new ScalarTypeDate(JsonConfig.Date.ISO8601));
|
||||
Date val1 = jsonIso.type.parse("2019-05-09");
|
||||
assertThat(jsonIso.test(val1)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeDurationTest {
|
||||
|
||||
ScalarTypeDuration type = new ScalarTypeDuration();
|
||||
|
||||
@Test
|
||||
public void testReadData() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(1234);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(os);
|
||||
|
||||
type.writeData(out, duration);
|
||||
type.writeData(out, null);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream in = new ObjectInputStream(is);
|
||||
|
||||
Duration val1 = type.readData(in);
|
||||
Duration val2 = type.readData(in);
|
||||
|
||||
assertEquals(duration, val1);
|
||||
assertNull(val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(1234);
|
||||
long seconds = duration.getSeconds();
|
||||
|
||||
Object val1 = type.toJdbcType(duration);
|
||||
Object val2 = type.toJdbcType(seconds);
|
||||
|
||||
assertEquals(seconds, val1);
|
||||
assertEquals(seconds, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(1234);
|
||||
long seconds = duration.getSeconds();
|
||||
|
||||
Duration val1 = type.toBeanType(duration);
|
||||
Duration val2 = type.toBeanType(seconds);
|
||||
Duration val3 = type.toBeanType((int) seconds);
|
||||
|
||||
assertEquals(duration, val1);
|
||||
assertEquals(duration, val2);
|
||||
assertEquals(duration, val3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatValue() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(1234);
|
||||
String formatValue = type.formatValue(duration);
|
||||
assertEquals("PT20M34S", formatValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
Duration duration = type.parse("PT20M34S");
|
||||
assertEquals(Duration.ofSeconds(1234), duration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(1234);
|
||||
|
||||
JsonTester<Duration> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(duration);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeDurationWithNanosTest {
|
||||
|
||||
ScalarTypeDurationWithNanos type = new ScalarTypeDurationWithNanos();
|
||||
|
||||
@Test
|
||||
public void testReadData() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(os);
|
||||
|
||||
type.writeData(out, duration);
|
||||
type.writeData(out, null);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream in = new ObjectInputStream(is);
|
||||
|
||||
Duration val1 = type.readData(in);
|
||||
Duration val2 = type.readData(in);
|
||||
|
||||
assertEquals(duration, val1);
|
||||
assertNull(val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
|
||||
|
||||
Object val1 = type.toJdbcType(duration);
|
||||
Object val2 = type.toJdbcType(bigDecimal);
|
||||
|
||||
assertEquals(bigDecimal, val1);
|
||||
assertEquals(bigDecimal, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
|
||||
|
||||
Duration val1 = type.toBeanType(duration);
|
||||
Duration val2 = type.toBeanType(bigDecimal);
|
||||
|
||||
assertEquals(duration, val1);
|
||||
assertEquals(duration, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatValue() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
String formatValue = type.formatValue(duration);
|
||||
assertEquals("PT5M23.0015S", formatValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
Duration val1 = type.parse("PT5M23.0015S");
|
||||
assertEquals(duration, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
|
||||
JsonTester<Duration> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(duration);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeInstantTest {
|
||||
|
||||
ScalarTypeInstant type = new ScalarTypeInstant(JsonConfig.DateTime.MILLIS);
|
||||
|
||||
private Instant now() {
|
||||
// in JDK11 Instant.now() returns a nanosecond precise instant.
|
||||
// as ScalarTypeInstant is only milliSecond precise, tests may fail with
|
||||
// expected:<2019-02-10T15:39:36.702700200Z> but was:<2019-02-10T15:39:36.702Z>
|
||||
// if we use Instant.now() - so we use this workaround here.
|
||||
return Instant.ofEpochMilli(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadData() throws Exception {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(os);
|
||||
|
||||
Instant now = now();
|
||||
type.writeData(out, now);
|
||||
type.writeData(out, null);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream in = new ObjectInputStream(is);
|
||||
|
||||
Instant val1 = type.readData(in);
|
||||
Instant val2 = type.readData(in);
|
||||
|
||||
assertEquals(now, val1);
|
||||
assertNull(val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Timestamp timestamp = Timestamp.from(now);
|
||||
Object val1 = type.toJdbcType(now);
|
||||
Object val2 = type.toJdbcType(timestamp);
|
||||
|
||||
assertEquals(timestamp, val1);
|
||||
assertEquals(timestamp, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Instant val1 = type.toBeanType(now);
|
||||
Instant val2 = type.toBeanType(Timestamp.from(now));
|
||||
|
||||
assertEquals(now, val1);
|
||||
assertEquals(now, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatValue() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Timestamp timestamp = Timestamp.from(now);
|
||||
String formatted = type.formatValue(now);
|
||||
assertEquals("" + timestamp.getTime(), formatted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse_when_epochMillis() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Timestamp timestamp = Timestamp.from(now);
|
||||
Instant val1 = type.parse("" + timestamp.getTime());
|
||||
assertEquals(now, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse_when_timestampForm() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Timestamp timestamp = Timestamp.from(now);
|
||||
Instant val1 = type.parse(timestamp.toString());
|
||||
assertEquals(now, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatAndParse() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
|
||||
String format = type.format(now);
|
||||
Instant val1 = type.parse(format);
|
||||
assertEquals(now, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() throws Exception {
|
||||
|
||||
assertTrue(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Instant val = type.convertFromMillis(now.toEpochMilli());
|
||||
assertEquals(now, val);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
|
||||
JsonTester<Instant> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeInstant typeNanos = new ScalarTypeInstant(JsonConfig.DateTime.NANOS);
|
||||
jsonTester = new JsonTester<>(typeNanos);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeInstant typeIso = new ScalarTypeInstant(JsonConfig.DateTime.ISO8601);
|
||||
jsonTester = new JsonTester<>(typeIso);
|
||||
jsonTester.test(now);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isoJsonParseFormat() {
|
||||
|
||||
ScalarTypeInstant typeIso = new ScalarTypeInstant(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
Instant instant = Instant.now();
|
||||
String asJson = typeIso.toJsonISO8601(instant);
|
||||
|
||||
Instant value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(instant).isEqualTo(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeIntegerTest {
|
||||
|
||||
private final ScalarTypeInteger type = new ScalarTypeInteger();
|
||||
|
||||
@Test
|
||||
public void format_when_string() {
|
||||
assertThat(type.format("1")).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void format_when_integer() {
|
||||
assertThat(type.format(1)).isEqualTo("1");
|
||||
}
|
||||
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class ScalarTypeJodaLocalDateTest {
|
||||
|
||||
private ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void convertToMillis_convertFromMillis() {
|
||||
|
||||
LocalDate localDate = new LocalDate();
|
||||
long millis = type.convertToMillis(localDate);
|
||||
LocalDate localDate1 = type.convertFromMillis(millis);
|
||||
|
||||
assertThat(localDate).isEqualTo(localDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertToDate_convertFromDate_westOfUtc() {
|
||||
final TimeZone originaDefaultTimezone = TimeZone.getDefault();
|
||||
try {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
|
||||
|
||||
convertDate(new LocalDate());
|
||||
convertDate(new LocalDate(1899, 12, 1));
|
||||
convertDate(new LocalDate(1900, 1, 1));
|
||||
convertDate(new LocalDate(2021, 2, 8));
|
||||
|
||||
} finally {
|
||||
TimeZone.setDefault(originaDefaultTimezone);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertToDate_convertFromDate() {
|
||||
|
||||
convertDate(new LocalDate());
|
||||
convertDate(new LocalDate(1899, 12, 1));
|
||||
convertDate(new LocalDate(1900, 1, 1));
|
||||
convertDate(new LocalDate(2021, 2, 8));
|
||||
}
|
||||
|
||||
private void convertDate(LocalDate localDate) {
|
||||
|
||||
Date dateValue = type.convertToDate(localDate);
|
||||
LocalDate localDate1 = type.convertFromDate(dateValue);
|
||||
|
||||
assertThat(localDate).isEqualTo(localDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJdbcType() {
|
||||
|
||||
LocalDate localDate = new LocalDate();
|
||||
Object jdbcType = type.toJdbcType(localDate);
|
||||
Date dateValue = type.convertToDate(localDate);
|
||||
|
||||
assertThat(jdbcType).isEqualTo(dateValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanType() {
|
||||
|
||||
LocalDate localDate = new LocalDate();
|
||||
Date dateValue = type.convertToDate(localDate);
|
||||
LocalDate beanType = type.toBeanType(dateValue);
|
||||
|
||||
assertThat(beanType).isEqualTo(localDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
LocalDate val = new LocalDate(2019, 5, 9);
|
||||
|
||||
JsonTester<LocalDate> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557360000000}");
|
||||
|
||||
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeJodaLocalDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeJodaLocalDateTimeTest {
|
||||
|
||||
ScalarTypeJodaLocalDateTime type = new ScalarTypeJodaLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
@Test
|
||||
public void testConvertFromTimestamp() throws Exception {
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
Timestamp nowTs = new Timestamp(now);
|
||||
|
||||
LocalDateTime ldt1 = type.convertFromTimestamp(nowTs);
|
||||
LocalDateTime ldt2 = localConvertFromTimestamp(nowTs);
|
||||
|
||||
assertEquals(ldt1, ldt2);
|
||||
|
||||
Timestamp ts1 = type.convertToTimestamp(ldt1);
|
||||
Timestamp ts2 = localConvertToTimestamp(ldt2);
|
||||
|
||||
assertEquals(ts1, ts2);
|
||||
}
|
||||
|
||||
|
||||
LocalDateTime localConvertFromTimestamp(Timestamp ts) {
|
||||
return new LocalDateTime(ts.getTime(), DateTimeZone.getDefault());
|
||||
}
|
||||
|
||||
|
||||
Timestamp localConvertToTimestamp(LocalDateTime t) {
|
||||
return new Timestamp(t.toDateTime(DateTimeZone.getDefault()).getMillis());
|
||||
}
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.DB;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.TJodaEntity;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeJodaLocalTimeTest {
|
||||
|
||||
ScalarTypeJodaLocalTime type = new ScalarTypeJodaLocalTime();
|
||||
|
||||
@Test
|
||||
public void toJdbcType_toBeanType() {
|
||||
|
||||
LocalTime localTime0 = new LocalTime().withMillisOfSecond(0);
|
||||
Object time = type.toJdbcType(localTime0);
|
||||
LocalTime localTime1 = type.toBeanType(time);
|
||||
|
||||
assertThat(localTime0).isEqualTo(localTime1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
//DateTimeZone timeZone = DateTimeZone.getDefault();
|
||||
//ISOChronology instance = ISOChronology.getInstance();
|
||||
|
||||
LocalDateTime ldt1 = new LocalDateTime(now, DateTimeZone.getDefault());
|
||||
LocalDateTime ldt2 = new LocalDateTime(now);
|
||||
|
||||
assertEquals(ldt1, ldt2);
|
||||
|
||||
Timestamp ts1 = new Timestamp(ldt1.toDateTime(DateTimeZone.getDefault()).getMillis());
|
||||
Timestamp ts2 = new Timestamp(ldt2.toDateTime().getMillis());
|
||||
|
||||
assertEquals(ts1, ts2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJson() {
|
||||
|
||||
LocalTime now = new LocalTime();
|
||||
|
||||
TJodaEntity bean = new TJodaEntity();
|
||||
bean.setId(42);
|
||||
bean.setLocalTime(now);
|
||||
|
||||
String json = DB.json().toJson(bean);
|
||||
TJodaEntity bean1 = DB.json().toBean(TJodaEntity.class, json);
|
||||
|
||||
Assertions.assertEquals(bean1.getLocalTime(), now);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.joda.time.Period;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeJodaPeriodTest {
|
||||
|
||||
private ScalarTypeJodaPeriod scalarType = new ScalarTypeJodaPeriod();
|
||||
|
||||
@Test
|
||||
public void getLength() {
|
||||
assertThat(scalarType.getLength()).isEqualTo(50);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParse() {
|
||||
|
||||
Period original = Period.years(1).plusMonths(2).plusDays(4)
|
||||
.plusHours(12).plusMinutes(19).plusSeconds(20);
|
||||
|
||||
String value = scalarType.formatValue(original);
|
||||
assertThat(value).isEqualTo("P1Y2M4DT12H19M20S");
|
||||
|
||||
Period period = scalarType.parse(value);
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void convertFromDbString() {
|
||||
|
||||
Period original = Period.years(1).plusMonths(2).plusDays(4)
|
||||
.plusHours(23).plusMinutes(19).plusSeconds(20).plusMillis(987);
|
||||
|
||||
String stringVal = scalarType.convertToDbString(original);
|
||||
Period period = scalarType.convertFromDbString(stringVal);
|
||||
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.dbplatform.ExtraDbTypes;
|
||||
import io.ebean.core.type.DocPropertyType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ScalarTypeJsonListTest extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
@Test
|
||||
public void typeFor_expect_nullToEmpty_when_postgresNonNull() throws SQLException {
|
||||
|
||||
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, false, false));
|
||||
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, false, false));
|
||||
|
||||
assertBindNullTo_EmptyString(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, false, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeFor_expect_nullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, true, false));
|
||||
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, true, false));
|
||||
|
||||
assertBindNullTo_Null(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, true, false));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ScalarTypeLocalDateTest {
|
||||
|
||||
ScalarTypeLocalDate type = new ScalarTypeLocalDate(JsonConfig.Date.ISO8601);
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() {
|
||||
|
||||
LocalDate date = LocalDate.of(2014, 5, 20);
|
||||
long millis = type.convertToMillis(date);
|
||||
|
||||
LocalDate parseDate = type.convertFromMillis(millis);
|
||||
assertEquals(date, parseDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromDate() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Date date = Date.valueOf(localDate);
|
||||
|
||||
LocalDate localDate1 = type.convertFromDate(date);
|
||||
assertEquals(localDate, localDate1);
|
||||
|
||||
Date date1 = type.convertToDate(localDate);
|
||||
assertEquals(date, date1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Object o = type.toJdbcType(localDate);
|
||||
assertTrue(o instanceof Date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void olderDates_ToFromJdbcType() {
|
||||
LocalDate localDate = LocalDate.of(1850,12,1);
|
||||
Object o = type.toJdbcType(localDate);
|
||||
LocalDate localDate1 = type.toBeanType(o);
|
||||
assertThat(localDate1).isEqualTo(localDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Date date = Date.valueOf(localDate);
|
||||
|
||||
LocalDate localDate1 = type.toBeanType(date);
|
||||
assertEquals(localDate, localDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
LocalDate val = LocalDate.of(2019, 5, 9);
|
||||
|
||||
JsonTester<LocalDate> jsonMillis = new JsonTester<>(new ScalarTypeLocalDate(JsonConfig.Date.MILLIS));
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557360000000}");
|
||||
|
||||
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeLocalDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import io.ebean.config.JsonConfig;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeLocalDateTimeTest {
|
||||
|
||||
private final ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS);
|
||||
|
||||
private final JsonFactory factory = new JsonFactory();
|
||||
|
||||
// warm up
|
||||
private final LocalDateTime warmUp = LocalDateTime.now();
|
||||
|
||||
@Test
|
||||
public void testNowToMillis() {
|
||||
|
||||
warmUp.hashCode();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long toMillis = type.convertToMillis(LocalDateTime.now());
|
||||
assertTrue(toMillis - now < 30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() {
|
||||
|
||||
LocalDateTime now = LocalDateTime.now().withNano(123_000_000); // jdk11 workaround
|
||||
long asMillis = type.convertToMillis(now);
|
||||
LocalDateTime fromMillis = type.convertFromMillis(asMillis);
|
||||
|
||||
assertEquals(now, fromMillis);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromTimestamp() {
|
||||
|
||||
Timestamp now = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
LocalDateTime localDateTime = type.convertFromTimestamp(now);
|
||||
Timestamp timestamp = type.convertToTimestamp(localDateTime);
|
||||
|
||||
assertEquals(now, timestamp);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
Object jdbcType = type.toJdbcType(LocalDateTime.now());
|
||||
assertTrue(jdbcType instanceof Timestamp);
|
||||
|
||||
jdbcType = type.toJdbcType(new Timestamp(System.currentTimeMillis()));
|
||||
assertTrue(jdbcType instanceof Timestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||
LocalDateTime val1 = type.toBeanType(timestamp);
|
||||
assertNotNull(val1);
|
||||
|
||||
LocalDateTime val2 = type.toBeanType(timestamp.toLocalDateTime());
|
||||
assertNotNull(val2);
|
||||
|
||||
Timestamp timestamp1 = type.convertToTimestamp(val1);
|
||||
assertEquals(timestamp, timestamp1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testJsonRaw() throws Exception {
|
||||
|
||||
final LocalDateTime of = LocalDateTime.of(2020, 5, 4, 13, 20, 40);
|
||||
|
||||
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonGenerator generator = factory.createGenerator(writer);
|
||||
|
||||
typeIso.jsonWrite(generator, of);
|
||||
generator.flush();
|
||||
|
||||
assertThat(of.toString()).isEqualTo("2020-05-04T13:20:40");
|
||||
assertThat(writer.toString()).isEqualTo("\"2020-05-04T13:20:40\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
|
||||
LocalDateTime now = LocalDateTime.now().withNano(123_000_000); // jdk11 workaround
|
||||
|
||||
JsonTester<LocalDateTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeLocalDateTime typeNanos = new ScalarTypeLocalDateTime(JsonConfig.DateTime.NANOS);
|
||||
jsonTester = new JsonTester<>(typeNanos);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
jsonTester = new JsonTester<>(typeIso);
|
||||
jsonTester.test(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isoJsonFormatParse() {
|
||||
|
||||
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
LocalDateTime localDateTime = LocalDateTime.now();
|
||||
String asJson = typeIso.toJsonISO8601(localDateTime);
|
||||
|
||||
LocalDateTime value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(localDateTime).isEqualToIgnoringNanos(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Does not work @github due different timezone")
|
||||
// Expecting actual:
|
||||
// 2018-02-03T03:05:06 (java.time.LocalDateTime)
|
||||
// to have same year, month, day, hour, minute and second as:
|
||||
// 2018-02-03T04:05:06 (java.time.LocalDateTime)
|
||||
public void testParseEbean11() {
|
||||
|
||||
ScalarTypeLocalDateTime typeDefault = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
LocalDateTime fromMillis = typeDefault.parse("1517627106000");
|
||||
LocalDateTime fromIso= typeDefault.parse("2018-02-03T04:05:06");
|
||||
|
||||
assertThat(fromMillis).isEqualToIgnoringNanos(fromIso);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.sql.Time;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeLocalTimeTest {
|
||||
|
||||
ScalarTypeLocalTime type = new ScalarTypeLocalTime();
|
||||
|
||||
@Test
|
||||
public void testReadData() throws Exception {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(os);
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
type.writeData(out, localTime);
|
||||
type.writeData(out, null);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream in = new ObjectInputStream(is);
|
||||
|
||||
LocalTime val1 = type.readData(in);
|
||||
LocalTime val2 = type.readData(in);
|
||||
|
||||
assertEquals(localTime, val1);
|
||||
assertNull(val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45, 123);
|
||||
Time time = Time.valueOf(localTime);
|
||||
|
||||
Object val1 = type.toJdbcType(localTime);
|
||||
Object val2 = type.toJdbcType(time);
|
||||
|
||||
assertEquals(time, val1);
|
||||
assertEquals(time, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
LocalTime val1 = type.toBeanType(localTime);
|
||||
LocalTime val2 = type.toBeanType(Time.valueOf(localTime));
|
||||
|
||||
assertEquals(localTime, val1);
|
||||
assertEquals(localTime, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatValue() throws Exception {
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
String formatted = type.formatValue(localTime);
|
||||
assertEquals("09:23:45", formatted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
LocalTime val1 = type.parse("09:23:45");
|
||||
assertEquals(localTime, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1234));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
|
||||
JsonTester<LocalTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(localTime);
|
||||
}
|
||||
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeLocalTimeWithNanosTest {
|
||||
|
||||
ScalarTypeLocalTimeWithNanos type = new ScalarTypeLocalTimeWithNanos();
|
||||
|
||||
@Test
|
||||
public void testReadData() throws Exception {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(os);
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45, 115);
|
||||
type.writeData(out, localTime);
|
||||
type.writeData(out, null);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream in = new ObjectInputStream(is);
|
||||
|
||||
LocalTime val1 = type.readData(in);
|
||||
LocalTime val2 = type.readData(in);
|
||||
|
||||
assertEquals(localTime, val1);
|
||||
assertNull(val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45, 115);
|
||||
long asNanos = localTime.toNanoOfDay();
|
||||
Object val1 = type.toJdbcType(localTime);
|
||||
Object val2 = type.toJdbcType(asNanos);
|
||||
|
||||
assertEquals(asNanos, val1);
|
||||
assertEquals(asNanos, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
LocalTime val1 = type.toBeanType(localTime);
|
||||
LocalTime val2 = type.toBeanType(localTime.toNanoOfDay());
|
||||
|
||||
assertEquals(localTime, val1);
|
||||
assertEquals(localTime, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatValue() throws Exception {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
String formatted = type.formatValue(localTime);
|
||||
assertEquals("09:23:45", formatted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
LocalTime val1 = type.parse("09:23:45");
|
||||
assertEquals(localTime, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1234));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
|
||||
JsonTester<LocalTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(localTime);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeLongTest {
|
||||
|
||||
private final ScalarTypeLong type = new ScalarTypeLong();
|
||||
|
||||
@Test
|
||||
public void format_when_string() {
|
||||
assertThat(type.format("1")).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void format_when_long() {
|
||||
assertThat(type.format(1L)).isEqualTo("1");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.MonthDay;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeMonthDayTest {
|
||||
|
||||
ScalarTypeMonthDay type = new ScalarTypeMonthDay();
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
Date date = Date.valueOf(LocalDate.of(2000, 4, 29));
|
||||
|
||||
Object val1 = type.toJdbcType(value);
|
||||
Object val2 = type.toJdbcType(date);
|
||||
|
||||
assertEquals(date, val1);
|
||||
assertEquals(date, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
Date date = Date.valueOf(LocalDate.of(2000, 4, 29));
|
||||
|
||||
MonthDay val1 = type.toBeanType(value);
|
||||
MonthDay val2 = type.toBeanType(date);
|
||||
|
||||
assertEquals(value, val1);
|
||||
assertEquals(value, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatParse() {
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
String val1 = type.formatValue(value);
|
||||
MonthDay monthDay = type.parse(val1);
|
||||
|
||||
assertEquals("--04-29", val1);
|
||||
assertEquals(value, monthDay);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(RuntimeException.class, () -> type.convertFromMillis(1203));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
JsonTester<MonthDay> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(value);
|
||||
}
|
||||
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeOffsetDateTimeTest {
|
||||
|
||||
|
||||
ScalarTypeOffsetDateTime type = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.MILLIS, ZoneOffset.systemDefault());
|
||||
|
||||
OffsetDateTime warmUp = OffsetDateTime.now();
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() {
|
||||
|
||||
warmUp.hashCode();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long toMillis = type.convertToMillis(OffsetDateTime.now());
|
||||
|
||||
assertTrue(toMillis - now < 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertFromInstant_with_UTC_expect_matchingZoneOffset() {
|
||||
final TimeZone timeZoneToUse = TimeZone.getTimeZone("UTC");
|
||||
final ZoneOffset expectedZoneOffset = ZoneOffset.UTC;
|
||||
|
||||
convertFromInstantWithConfiguredTimeZone(timeZoneToUse, expectedZoneOffset);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertFromInstant_with_EST_expect_matchingZoneOffset() {
|
||||
final TimeZone timeZoneToUse = TimeZone.getTimeZone("EST");
|
||||
final ZoneOffset expectedOffset = OffsetDateTime.now(timeZoneToUse.toZoneId()).getOffset();
|
||||
|
||||
convertFromInstantWithConfiguredTimeZone(timeZoneToUse, expectedOffset);
|
||||
}
|
||||
|
||||
private void convertFromInstantWithConfiguredTimeZone(TimeZone timeZoneToUse, ZoneOffset expectedZoneOffset) {
|
||||
TimeZone previous = TimeZone.getDefault();
|
||||
try {
|
||||
OffsetDateTime dateTime = OffsetDateTime.parse("2021-01-01T00:00:00+11:00");
|
||||
|
||||
// test ScalarTypeOffsetDateTime with the configured timeZone to use
|
||||
ScalarTypeOffsetDateTime type = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.MILLIS, timeZoneToUse.toZoneId());
|
||||
|
||||
// effectively we desire to ignore the system timezone and use the configured one
|
||||
TimeZone.setDefault(timeZoneToUse);
|
||||
|
||||
final OffsetDateTime offsetDateTime = type.convertFromInstant(dateTime.toInstant());
|
||||
|
||||
assertEquals(expectedZoneOffset, offsetDateTime.getOffset());
|
||||
|
||||
} finally {
|
||||
TimeZone.setDefault(previous);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromTimestamp() {
|
||||
|
||||
Timestamp now = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
OffsetDateTime localDateTime = type.convertFromTimestamp(now);
|
||||
Timestamp timestamp = type.convertToTimestamp(localDateTime);
|
||||
|
||||
assertEquals(now, timestamp);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
Object jdbcType = type.toJdbcType(OffsetDateTime.now());
|
||||
assertTrue(jdbcType instanceof Timestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||
OffsetDateTime localDateTime = type.toBeanType(timestamp);
|
||||
|
||||
assertNotNull(localDateTime);
|
||||
|
||||
Timestamp timestamp1 = type.convertToTimestamp(localDateTime);
|
||||
assertEquals(timestamp, timestamp1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
|
||||
OffsetDateTime now = OffsetDateTime.now().withNano(123_000_000); // jdk11 workaround
|
||||
|
||||
JsonTester<OffsetDateTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeOffsetDateTime typeNanos = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.NANOS, ZoneOffset.systemDefault());
|
||||
jsonTester = new JsonTester<>(typeNanos);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeOffsetDateTime typeIso = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.ISO8601, ZoneOffset.systemDefault());
|
||||
jsonTester = new JsonTester<>(typeIso);
|
||||
jsonTester.test(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isoJsonFormatParse() {
|
||||
|
||||
ScalarTypeOffsetDateTime typeIso = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.ISO8601, ZoneOffset.systemDefault());
|
||||
|
||||
OffsetDateTime now = OffsetDateTime.now();
|
||||
String asJson = typeIso.toJsonISO8601(now);
|
||||
|
||||
OffsetDateTime value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(now).isEqualToIgnoringNanos(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeOffsetTimeTest {
|
||||
|
||||
ScalarTypeOffsetTime type = new ScalarTypeOffsetTime();
|
||||
|
||||
@Test
|
||||
public void testGetLength() throws Exception {
|
||||
|
||||
assertEquals(25, type.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatParse() throws Exception {
|
||||
|
||||
OffsetTime now = OffsetTime.now();
|
||||
String value = type.formatValue(now);
|
||||
OffsetTime offsetTime = type.parse(value);
|
||||
|
||||
assertEquals(now, offsetTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertDb() throws Exception {
|
||||
|
||||
OffsetTime now = OffsetTime.now();
|
||||
String value = type.convertToDbString(now);
|
||||
OffsetTime offsetTime = type.convertFromDbString(value);
|
||||
|
||||
assertEquals(now, offsetTime);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypePathTest {
|
||||
private static final String TEMP_PATH = new File("/tmp").getAbsolutePath();
|
||||
|
||||
private ScalarTypePath type = new ScalarTypePath();
|
||||
|
||||
@Test
|
||||
public void convertFromDbString() throws Exception {
|
||||
|
||||
Path path = Paths.get(TEMP_PATH);
|
||||
|
||||
String asString = type.convertToDbString(path); // "/tmp" will be converted to "file://c:/tmp" on windows
|
||||
Path converted = type.convertFromDbString(asString);
|
||||
|
||||
assertEquals(path, converted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParse() throws Exception {
|
||||
|
||||
Path path = Paths.get(TEMP_PATH);
|
||||
|
||||
String asString = type.formatValue(path);
|
||||
Path converted = type.parse(asString);
|
||||
|
||||
assertEquals(path, converted);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Period;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypePeriodTest {
|
||||
|
||||
private ScalarTypePeriod scalarType = new ScalarTypePeriod();
|
||||
|
||||
@Test
|
||||
public void getLength() {
|
||||
assertThat(scalarType.getLength()).isEqualTo(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParse() {
|
||||
|
||||
Period original = Period.of(1,2, 4);
|
||||
|
||||
String value = scalarType.formatValue(original);
|
||||
assertThat(value).isEqualTo("P1Y2M4D");
|
||||
|
||||
Period period = scalarType.parse(value);
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void convertFromDbString() {
|
||||
|
||||
Period original = Period.of(1, 2, 4);
|
||||
|
||||
String stringVal = scalarType.convertToDbString(original);
|
||||
Period period = scalarType.convertFromDbString(stringVal);
|
||||
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
}
|
||||
Loaded 100 of 191 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user