From 4652607bbd714174417faf42cc4a980cbc355ad2 Mon Sep 17 00:00:00 2001 From: Noemi Praml Date: Tue, 31 May 2022 15:51:47 +0200 Subject: [PATCH 1/2] some other tests to other data types --- .../java/org/tests/timezone/InstantTest.java | 70 +++++++++++++++++++ .../org/tests/timezone/LocalDateTest.java | 69 ++++++++++++++++++ .../org/tests/timezone/LocalDateTimeTest.java | 62 ++++++++++++++++ .../java/org/tests/timezone/MInstant.java | 28 ++++++++ .../java/org/tests/timezone/MLocalDate.java | 28 ++++++++ .../org/tests/timezone/MLocalDateTime.java | 28 ++++++++ .../java/org/tests/timezone/MTimestamp.java | 28 ++++++++ .../org/tests/timezone/TimestampTest.java | 70 +++++++++++++++++++ 8 files changed, 383 insertions(+) create mode 100644 ebean-test/src/test/java/org/tests/timezone/InstantTest.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/MInstant.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/MLocalDate.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/MTimestamp.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/TimestampTest.java diff --git a/ebean-test/src/test/java/org/tests/timezone/InstantTest.java b/ebean-test/src/test/java/org/tests/timezone/InstantTest.java new file mode 100644 index 0000000000..07f5b08a5d --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/InstantTest.java @@ -0,0 +1,70 @@ +package org.tests.timezone; + +import io.ebean.Database; +import io.ebean.DatabaseFactory; +import io.ebean.config.DatabaseConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.time.Instant; + +import static org.assertj.core.api.Assertions.assertThat; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class InstantTest { + + protected String platform="h2"; + protected Database db; + + @BeforeAll + public void startTest() { + db = createServer("GMT"); // test uses GMT database + } + + @AfterAll + public void shutdown() { + if (db != null) { + db.find(MInstant.class).delete(); + db.shutdown(); + } + } + + /** + * The test checks the write and read of LocalTime values. The database is in GMT time zone. + * In order to verify the test in different java time zones (where the application runs), + * use the -Duser.timezone as JVM argument, + * e.g. -Duser.timezone="America/New_York" or -Duser.timezone="PST"> + * or any other timezone: https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/. + */ + @Test + public void testInstant() { + Instant inst = Instant.parse("2021-11-21T05:15:15Z"); + + assertThat(db.find(MInstant.class).findCount()).isEqualTo(0); + db.sqlUpdate("insert into minstant (id, instant) values (1, '2021-11-21 05:15:15')").execute(); + + int count = db.find(MInstant.class).where().eq("instant", inst).findCount(); + assertThat(count).isEqualTo(1); + + MInstant dbModel = db.find(MInstant.class).where().eq("instant", inst).findOne(); + assertThat(dbModel.getInstant().toString()).isEqualTo(inst.toString()); + } + + private Database createServer(String dbTimeZone) { + DatabaseConfig config = new DatabaseConfig(); + config.setName(platform); + config.loadFromProperties(); + config.setDdlExtra(false); + config.setDefaultServer(false); + config.setRegister(false); + config.setChangeLogAsync(false); + config.addClass(MInstant.class); + + config.setDumpMetricsOnShutdown(false); + config.setDataTimeZone(dbTimeZone); + + return DatabaseFactory.create(config); + } +} diff --git a/ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java b/ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java new file mode 100644 index 0000000000..db552ba981 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java @@ -0,0 +1,69 @@ +package org.tests.timezone; + +import io.ebean.Database; +import io.ebean.DatabaseFactory; +import io.ebean.config.DatabaseConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.time.LocalDate; + +import static org.assertj.core.api.Assertions.assertThat; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class LocalDateTest { + + protected String platform="h2"; + protected Database db; + + @BeforeAll + public void startTest() { + db = createServer("GMT"); // test uses GMT database + } + + @AfterAll + public void shutdown() { + if (db != null) { + db.find(MLocalDate.class).delete(); + db.shutdown(); + } + } + + /** + * The test checks the write and read of LocalDate values. The database is in GMT time zone. + * In order to verify the test in different java time zones (where the application runs), + * use the -Duser.timezone as JVM argument, + * e.g. -Duser.timezone="America/New_York" or -Duser.timezone="PST"> + * or any other timezone: https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/. + */ + @Test + public void testLocalDate() { + LocalDate ld = LocalDate.parse("2021-11-21"); + assertThat(db.find(MLocalDate.class).findCount()).isEqualTo(0); + db.sqlUpdate("insert into mlocal_date (id, local_date) values (1, '2021-11-21')").execute(); + + int count = db.find(MLocalDate.class).where().eq("local_date", ld).findCount(); + assertThat(count).isEqualTo(1); + + MLocalDate dbModel = db.find(MLocalDate.class).where().eq("local_date", ld).findOne(); + assertThat(dbModel.getLocalDate().toString()).isEqualTo(ld.toString()); + } + + private Database createServer(String dbTimeZone) { + DatabaseConfig config = new DatabaseConfig(); + config.setName(platform); + config.loadFromProperties(); + config.setDdlExtra(false); + config.setDefaultServer(false); + config.setRegister(false); + config.setChangeLogAsync(false); + config.addClass(MLocalDate.class); + + config.setDumpMetricsOnShutdown(false); + config.setDataTimeZone(dbTimeZone); + + return DatabaseFactory.create(config); + } +} diff --git a/ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java b/ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java new file mode 100644 index 0000000000..bdb366b1c2 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java @@ -0,0 +1,62 @@ +package org.tests.timezone; + +import io.ebean.Database; +import io.ebean.DatabaseFactory; +import io.ebean.config.DatabaseConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThat; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class LocalDateTimeTest { + + protected String platform="h2"; + protected Database db; + + @BeforeAll + public void startTest() { + db = createServer("GMT"); // test uses GMT database + } + + @AfterAll + public void shutdown() { + if (db != null) { + db.find(MLocalDateTime.class).delete(); + db.shutdown(); + } + } + + @Test + public void testLocalDateTime() { + LocalDateTime ldt = LocalDateTime.parse("2021-11-21T05:15:15"); + assertThat(db.find(MLocalDateTime.class).findCount()).isEqualTo(0); + db.sqlUpdate("insert into mlocal_date_time (id, local_date_time) values (1, '2021-11-21 05:15:15')").execute(); + + int count = db.find(MLocalDateTime.class).where().eq("local_date_time", ldt).findCount(); + assertThat(count).isEqualTo(1); + + MLocalDateTime dbModel = db.find(MLocalDateTime.class).where().eq("local_date_time", ldt).findOne(); + assertThat(dbModel.getLocalDateTime().toString()).isEqualTo(ldt.toString()); + } + + private Database createServer(String dbTimeZone) { + DatabaseConfig config = new DatabaseConfig(); + config.setName(platform); + config.loadFromProperties(); + config.setDdlExtra(false); + config.setDefaultServer(false); + config.setRegister(false); + config.setChangeLogAsync(false); + config.addClass(MLocalDateTime.class); + + config.setDumpMetricsOnShutdown(false); + config.setDataTimeZone(dbTimeZone); + + return DatabaseFactory.create(config); + } +} diff --git a/ebean-test/src/test/java/org/tests/timezone/MInstant.java b/ebean-test/src/test/java/org/tests/timezone/MInstant.java new file mode 100644 index 0000000000..519156743e --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/MInstant.java @@ -0,0 +1,28 @@ +package org.tests.timezone; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.persistence.Id; +import java.time.Instant; + +@Entity +public class MInstant { + + @Id + private Integer id; + + @Nullable + private Instant instant; + + @Nullable + public Instant getInstant() { + return instant; + } + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + +} diff --git a/ebean-test/src/test/java/org/tests/timezone/MLocalDate.java b/ebean-test/src/test/java/org/tests/timezone/MLocalDate.java new file mode 100644 index 0000000000..c902b3415e --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/MLocalDate.java @@ -0,0 +1,28 @@ +package org.tests.timezone; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.persistence.Id; +import java.time.LocalDate; + +@Entity +public class MLocalDate { + + @Id + private Integer id; + + @Nullable + private LocalDate localDate; + + @Nullable + public LocalDate getLocalDate() { + return localDate; + } + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + +} diff --git a/ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java b/ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java new file mode 100644 index 0000000000..e55060bfa4 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java @@ -0,0 +1,28 @@ +package org.tests.timezone; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.persistence.Id; +import java.time.LocalDateTime; + +@Entity +public class MLocalDateTime { + + @Id + private Integer id; + + @Nullable + private LocalDateTime localDateTime; + + @Nullable + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + +} diff --git a/ebean-test/src/test/java/org/tests/timezone/MTimestamp.java b/ebean-test/src/test/java/org/tests/timezone/MTimestamp.java new file mode 100644 index 0000000000..43a4dbed55 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/MTimestamp.java @@ -0,0 +1,28 @@ +package org.tests.timezone; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.persistence.Id; +import java.sql.Timestamp; + +@Entity +public class MTimestamp { + + @Id + private Integer id; + + @Nullable + private Timestamp timestamp; + + @Nullable + public Timestamp getTimestamp() { + return timestamp; + } + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + +} diff --git a/ebean-test/src/test/java/org/tests/timezone/TimestampTest.java b/ebean-test/src/test/java/org/tests/timezone/TimestampTest.java new file mode 100644 index 0000000000..958d4db41c --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/TimestampTest.java @@ -0,0 +1,70 @@ +package org.tests.timezone; + +import io.ebean.Database; +import io.ebean.DatabaseFactory; +import io.ebean.config.DatabaseConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.sql.Timestamp; + +import static org.assertj.core.api.Assertions.assertThat; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class TimestampTest { + + protected String platform="h2"; + protected Database db; + + @BeforeAll + public void startTest() { + db = createServer("GMT"); // test uses GMT database + } + + @AfterAll + public void shutdown() { + if (db != null) { + db.find(MTimestamp.class).delete(); + db.shutdown(); + } + } + + /** + * The test checks the write and read of LocalTime values. The database is in GMT time zone. + * In order to verify the test in different java time zones (where the application runs), + * use the -Duser.timezone as JVM argument, + * e.g. -Duser.timezone="America/New_York" or -Duser.timezone="PST"> + * or any other timezone: https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/. + */ + @Test + public void testTimestamp() { + Timestamp ts = new Timestamp(2021 - 1900, 11 - 1, 21, 5, 15, 15, 0); + + assertThat(db.find(MTimestamp.class).findCount()).isEqualTo(0); + db.sqlUpdate("insert into mtimestamp (id, timestamp) values (1, '2021-11-21 05:15:15')").execute(); + + int count = db.find(MTimestamp.class).where().eq("timestamp", ts).findCount(); + assertThat(count).isEqualTo(1); + + MTimestamp dbModel = db.find(MTimestamp.class).where().eq("timestamp", ts).findOne(); + assertThat(dbModel.getTimestamp().toString()).isEqualTo(ts.toString()); + } + + private Database createServer(String dbTimeZone) { + DatabaseConfig config = new DatabaseConfig(); + config.setName(platform); + config.loadFromProperties(); + config.setDdlExtra(false); + config.setDefaultServer(false); + config.setRegister(false); + config.setChangeLogAsync(false); + config.addClass(MTimestamp.class); + + config.setDumpMetricsOnShutdown(false); + config.setDataTimeZone(dbTimeZone); + + return DatabaseFactory.create(config); + } +} From b58c496f9295b3913cff4b441ae49ecf038e6044 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Thu, 10 Aug 2023 10:29:37 +0200 Subject: [PATCH 2/2] Rewrote the test --- .../java/org/tests/timezone/InstantTest.java | 70 ------- .../org/tests/timezone/LocalDateTest.java | 69 ------- .../org/tests/timezone/LocalDateTimeTest.java | 62 ------ .../org/tests/timezone/LocalTimeTest.java | 69 ------- .../java/org/tests/timezone/MInstant.java | 28 --- .../java/org/tests/timezone/MLocalDate.java | 28 --- .../org/tests/timezone/MLocalDateTime.java | 28 --- .../java/org/tests/timezone/MLocalTime.java | 28 --- .../java/org/tests/timezone/MTimeTest.java | 83 ++++++++ .../java/org/tests/timezone/MTimestamp.java | 28 --- .../org/tests/timezone/TimeTypesTest.java | 184 ++++++++++++++++++ .../org/tests/timezone/TimestampTest.java | 70 ------- 12 files changed, 267 insertions(+), 480 deletions(-) delete mode 100644 ebean-test/src/test/java/org/tests/timezone/InstantTest.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/LocalTimeTest.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/MInstant.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/MLocalDate.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/MLocalTime.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/MTimeTest.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/MTimestamp.java create mode 100644 ebean-test/src/test/java/org/tests/timezone/TimeTypesTest.java delete mode 100644 ebean-test/src/test/java/org/tests/timezone/TimestampTest.java diff --git a/ebean-test/src/test/java/org/tests/timezone/InstantTest.java b/ebean-test/src/test/java/org/tests/timezone/InstantTest.java deleted file mode 100644 index 07f5b08a5d..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/InstantTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.tests.timezone; - -import io.ebean.Database; -import io.ebean.DatabaseFactory; -import io.ebean.config.DatabaseConfig; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; - -import java.time.Instant; - -import static org.assertj.core.api.Assertions.assertThat; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class InstantTest { - - protected String platform="h2"; - protected Database db; - - @BeforeAll - public void startTest() { - db = createServer("GMT"); // test uses GMT database - } - - @AfterAll - public void shutdown() { - if (db != null) { - db.find(MInstant.class).delete(); - db.shutdown(); - } - } - - /** - * The test checks the write and read of LocalTime values. The database is in GMT time zone. - * In order to verify the test in different java time zones (where the application runs), - * use the -Duser.timezone as JVM argument, - * e.g. -Duser.timezone="America/New_York" or -Duser.timezone="PST"> - * or any other timezone: https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/. - */ - @Test - public void testInstant() { - Instant inst = Instant.parse("2021-11-21T05:15:15Z"); - - assertThat(db.find(MInstant.class).findCount()).isEqualTo(0); - db.sqlUpdate("insert into minstant (id, instant) values (1, '2021-11-21 05:15:15')").execute(); - - int count = db.find(MInstant.class).where().eq("instant", inst).findCount(); - assertThat(count).isEqualTo(1); - - MInstant dbModel = db.find(MInstant.class).where().eq("instant", inst).findOne(); - assertThat(dbModel.getInstant().toString()).isEqualTo(inst.toString()); - } - - private Database createServer(String dbTimeZone) { - DatabaseConfig config = new DatabaseConfig(); - config.setName(platform); - config.loadFromProperties(); - config.setDdlExtra(false); - config.setDefaultServer(false); - config.setRegister(false); - config.setChangeLogAsync(false); - config.addClass(MInstant.class); - - config.setDumpMetricsOnShutdown(false); - config.setDataTimeZone(dbTimeZone); - - return DatabaseFactory.create(config); - } -} diff --git a/ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java b/ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java deleted file mode 100644 index db552ba981..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/LocalDateTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.tests.timezone; - -import io.ebean.Database; -import io.ebean.DatabaseFactory; -import io.ebean.config.DatabaseConfig; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; - -import java.time.LocalDate; - -import static org.assertj.core.api.Assertions.assertThat; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class LocalDateTest { - - protected String platform="h2"; - protected Database db; - - @BeforeAll - public void startTest() { - db = createServer("GMT"); // test uses GMT database - } - - @AfterAll - public void shutdown() { - if (db != null) { - db.find(MLocalDate.class).delete(); - db.shutdown(); - } - } - - /** - * The test checks the write and read of LocalDate values. The database is in GMT time zone. - * In order to verify the test in different java time zones (where the application runs), - * use the -Duser.timezone as JVM argument, - * e.g. -Duser.timezone="America/New_York" or -Duser.timezone="PST"> - * or any other timezone: https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/. - */ - @Test - public void testLocalDate() { - LocalDate ld = LocalDate.parse("2021-11-21"); - assertThat(db.find(MLocalDate.class).findCount()).isEqualTo(0); - db.sqlUpdate("insert into mlocal_date (id, local_date) values (1, '2021-11-21')").execute(); - - int count = db.find(MLocalDate.class).where().eq("local_date", ld).findCount(); - assertThat(count).isEqualTo(1); - - MLocalDate dbModel = db.find(MLocalDate.class).where().eq("local_date", ld).findOne(); - assertThat(dbModel.getLocalDate().toString()).isEqualTo(ld.toString()); - } - - private Database createServer(String dbTimeZone) { - DatabaseConfig config = new DatabaseConfig(); - config.setName(platform); - config.loadFromProperties(); - config.setDdlExtra(false); - config.setDefaultServer(false); - config.setRegister(false); - config.setChangeLogAsync(false); - config.addClass(MLocalDate.class); - - config.setDumpMetricsOnShutdown(false); - config.setDataTimeZone(dbTimeZone); - - return DatabaseFactory.create(config); - } -} diff --git a/ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java b/ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java deleted file mode 100644 index bdb366b1c2..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/LocalDateTimeTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.tests.timezone; - -import io.ebean.Database; -import io.ebean.DatabaseFactory; -import io.ebean.config.DatabaseConfig; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; - -import java.time.LocalDateTime; - -import static org.assertj.core.api.Assertions.assertThat; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class LocalDateTimeTest { - - protected String platform="h2"; - protected Database db; - - @BeforeAll - public void startTest() { - db = createServer("GMT"); // test uses GMT database - } - - @AfterAll - public void shutdown() { - if (db != null) { - db.find(MLocalDateTime.class).delete(); - db.shutdown(); - } - } - - @Test - public void testLocalDateTime() { - LocalDateTime ldt = LocalDateTime.parse("2021-11-21T05:15:15"); - assertThat(db.find(MLocalDateTime.class).findCount()).isEqualTo(0); - db.sqlUpdate("insert into mlocal_date_time (id, local_date_time) values (1, '2021-11-21 05:15:15')").execute(); - - int count = db.find(MLocalDateTime.class).where().eq("local_date_time", ldt).findCount(); - assertThat(count).isEqualTo(1); - - MLocalDateTime dbModel = db.find(MLocalDateTime.class).where().eq("local_date_time", ldt).findOne(); - assertThat(dbModel.getLocalDateTime().toString()).isEqualTo(ldt.toString()); - } - - private Database createServer(String dbTimeZone) { - DatabaseConfig config = new DatabaseConfig(); - config.setName(platform); - config.loadFromProperties(); - config.setDdlExtra(false); - config.setDefaultServer(false); - config.setRegister(false); - config.setChangeLogAsync(false); - config.addClass(MLocalDateTime.class); - - config.setDumpMetricsOnShutdown(false); - config.setDataTimeZone(dbTimeZone); - - return DatabaseFactory.create(config); - } -} diff --git a/ebean-test/src/test/java/org/tests/timezone/LocalTimeTest.java b/ebean-test/src/test/java/org/tests/timezone/LocalTimeTest.java deleted file mode 100644 index 6f5b7e0f91..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/LocalTimeTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.tests.timezone; - -import io.ebean.Database; -import io.ebean.DatabaseFactory; -import io.ebean.config.DatabaseConfig; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; - -import java.time.LocalTime; - -import static org.assertj.core.api.Assertions.assertThat; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class LocalTimeTest { - - protected String platform="h2"; - protected Database db; - - @BeforeAll - public void startTest() { - db = createServer("GMT"); // test uses GMT database - } - - @AfterAll - public void shutdown() { - if (db != null) { - db.find(MLocalTime.class).delete(); - db.shutdown(); - } - } - - /** - * The test checks the write and read of LocalTime values. The database is in GMT time zone. - * In order to verify the test in different java time zones (where the application runs), - * use the -Duser.timezone as JVM argument, - * e.g. -Duser.timezone="America/New_York" or -Duser.timezone="PST"> - * or any other timezone: https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/. - */ - @Test - public void testLocalTime() { - LocalTime lt = LocalTime.of(5, 15, 15); - assertThat(db.find(MLocalTime.class).findCount()).isEqualTo(0); - db.sqlUpdate("insert into mlocal_time (id, local_time) values (1, '05:15:15')").execute(); - - int count = db.find(MLocalTime.class).where().eq("local_time", lt).findCount(); - assertThat(count).isEqualTo(1); - - MLocalTime dbModel = db.find(MLocalTime.class).where().eq("local_time", lt).findOne(); - assertThat(dbModel.getLocalTime().toString()).isEqualTo(lt.toString()); - } - - private Database createServer(String dbTimeZone) { - DatabaseConfig config = new DatabaseConfig(); - config.setName(platform); - config.loadFromProperties(); - config.setDdlExtra(false); - config.setDefaultServer(false); - config.setRegister(false); - config.setChangeLogAsync(false); - config.addClass(MLocalTime.class); - - config.setDumpMetricsOnShutdown(false); - config.setDataTimeZone(dbTimeZone); - - return DatabaseFactory.create(config); - } -} diff --git a/ebean-test/src/test/java/org/tests/timezone/MInstant.java b/ebean-test/src/test/java/org/tests/timezone/MInstant.java deleted file mode 100644 index 519156743e..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/MInstant.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.tests.timezone; - -import javax.annotation.Nullable; -import javax.persistence.Entity; -import javax.persistence.Id; -import java.time.Instant; - -@Entity -public class MInstant { - - @Id - private Integer id; - - @Nullable - private Instant instant; - - @Nullable - public Instant getInstant() { - return instant; - } - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - -} diff --git a/ebean-test/src/test/java/org/tests/timezone/MLocalDate.java b/ebean-test/src/test/java/org/tests/timezone/MLocalDate.java deleted file mode 100644 index c902b3415e..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/MLocalDate.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.tests.timezone; - -import javax.annotation.Nullable; -import javax.persistence.Entity; -import javax.persistence.Id; -import java.time.LocalDate; - -@Entity -public class MLocalDate { - - @Id - private Integer id; - - @Nullable - private LocalDate localDate; - - @Nullable - public LocalDate getLocalDate() { - return localDate; - } - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - -} diff --git a/ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java b/ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java deleted file mode 100644 index e55060bfa4..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/MLocalDateTime.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.tests.timezone; - -import javax.annotation.Nullable; -import javax.persistence.Entity; -import javax.persistence.Id; -import java.time.LocalDateTime; - -@Entity -public class MLocalDateTime { - - @Id - private Integer id; - - @Nullable - private LocalDateTime localDateTime; - - @Nullable - public LocalDateTime getLocalDateTime() { - return localDateTime; - } - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - -} diff --git a/ebean-test/src/test/java/org/tests/timezone/MLocalTime.java b/ebean-test/src/test/java/org/tests/timezone/MLocalTime.java deleted file mode 100644 index 4a2d303137..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/MLocalTime.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.tests.timezone; - -import javax.annotation.Nullable; -import javax.persistence.Entity; -import javax.persistence.Id; -import java.time.LocalTime; - -@Entity -public class MLocalTime { - - @Id - private Integer id; - - @Nullable - private LocalTime localTime; - - @Nullable - public LocalTime getLocalTime() { - return localTime; - } - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - -} diff --git a/ebean-test/src/test/java/org/tests/timezone/MTimeTest.java b/ebean-test/src/test/java/org/tests/timezone/MTimeTest.java new file mode 100644 index 0000000000..238806a250 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/MTimeTest.java @@ -0,0 +1,83 @@ +package org.tests.timezone; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.persistence.Id; +import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +/** + * Test model with various time types. + */ +@Entity +public class MTimeTest { + + @Id + private Integer id; + @Nullable + private Instant instant; + @Nullable + private LocalDate localDate; + @Nullable + private LocalTime localTime; + @Nullable + private LocalDateTime localDateTime; + @Nullable + private Timestamp timestamp; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Nullable + public Instant getInstant() { + return instant; + } + + public void setInstant(@Nullable Instant instant) { + this.instant = instant; + } + + @Nullable + public LocalDate getLocalDate() { + return localDate; + } + + public void setLocalDate(@Nullable LocalDate localDate) { + this.localDate = localDate; + } + + @Nullable + public LocalTime getLocalTime() { + return localTime; + } + + public void setLocalTime(@Nullable LocalTime localTime) { + this.localTime = localTime; + } + + @Nullable + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(@Nullable LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + + @Nullable + public Timestamp getTimestamp() { + return timestamp; + } + + public void setTimestamp(@Nullable Timestamp timestamp) { + this.timestamp = timestamp; + } +} diff --git a/ebean-test/src/test/java/org/tests/timezone/MTimestamp.java b/ebean-test/src/test/java/org/tests/timezone/MTimestamp.java deleted file mode 100644 index 43a4dbed55..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/MTimestamp.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.tests.timezone; - -import javax.annotation.Nullable; -import javax.persistence.Entity; -import javax.persistence.Id; -import java.sql.Timestamp; - -@Entity -public class MTimestamp { - - @Id - private Integer id; - - @Nullable - private Timestamp timestamp; - - @Nullable - public Timestamp getTimestamp() { - return timestamp; - } - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - -} diff --git a/ebean-test/src/test/java/org/tests/timezone/TimeTypesTest.java b/ebean-test/src/test/java/org/tests/timezone/TimeTypesTest.java new file mode 100644 index 0000000000..ab3dbba8ed --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/TimeTypesTest.java @@ -0,0 +1,184 @@ +package org.tests.timezone; + +import io.ebean.Database; +import io.ebean.DatabaseFactory; +import io.ebean.config.DatabaseConfig; +import io.ebean.xtest.BaseTestCase; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.*; + +import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.function.Function; + +import static org.assertj.core.api.Assertions.assertThat; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class TimeTypesTest extends BaseTestCase { + + protected Database dbGmt; + protected Database dbEurope; + protected Database dbPST; + + @BeforeAll + public void startTest() { + // both DBs are connected to the same datasource + dbGmt = createServer("GMT"); + dbEurope = createServer("Europe/Berlin"); + dbPST = createServer("PST"); + } + + @AfterAll + public void shutdown() { + if (dbGmt != null) { + dbGmt.shutdown(); + } + if (dbEurope != null) { + dbEurope.shutdown(); + } + if (dbPST != null) { + dbPST.shutdown(); + } + } + + @BeforeEach + public void clearDb() { + dbGmt.find(MTimeTest.class).delete(); + } + + /** + * Test for instant. + */ + @Test + public void testInstant() { + + dbGmt.sqlUpdate("insert into mtime_test (id, instant) values (1, '2021-11-21 05:15:15')").execute(); + + Instant inst = Instant.parse("2021-11-21T05:15:15Z"); + testTimeType(MTimeTest.class, "instant", MTimeTest::getInstant, inst); + + assertThat(dbEurope.find(MTimeTest.class).findOne().getInstant()) // we get 04:15:15/GMT here, as this is equal to 05:15:15/Europe + .isEqualTo(inst.minusSeconds(3600)); + + assertThat(dbPST.find(MTimeTest.class).findOne().getInstant()) + .isEqualTo(inst.plusSeconds(8 * 3600)); + } + + @Test + public void testLocalDate() { + dbGmt.sqlUpdate("insert into mtime_test (id, local_date) values (1, '2021-11-21')").execute(); + + LocalDate ld = LocalDate.parse("2021-11-21"); + testTimeType(MTimeTest.class, "local_date", MTimeTest::getLocalDate, ld); + + assertThat(dbEurope.find(MTimeTest.class).findOne().getLocalDate()).isEqualTo(ld); + + assertThat(dbPST.find(MTimeTest.class).findOne().getLocalDate()).isEqualTo(ld); + } + + @Test + public void testLocalDateTime() { + dbGmt.sqlUpdate("insert into mtime_test (id, local_date_time) values (1, '2021-11-21 05:15:15')").execute(); + + LocalDateTime ldt = LocalDateTime.parse("2021-11-21T05:15:15"); + + // Expected: + // testTimeType(MTimeTest.class, "local_date_time", MTimeTest::getLocalDateTime, ldt); + // assertThat(dbEurope.find(MTimeTest.class).findOne().getLocalDateTime()).isEqualTo(ldt); + // assertThat(dbPST.find(MTimeTest.class).findOne().getLocalDateTime()).isEqualTo(ldt); + + // Actual (if machine runs in Europe/Berlin): + // This is something I would not expect, or at least has to be discussed + // A LocalDateTime mapped to a DB column should IMHO be never adjusted + // Arguments: A LocalDateTime = LocalDate + LocalTime. Having these types in separate columns, they are also not adjusted + assertThat(dbGmt.find(MTimeTest.class).findOne().getLocalDateTime()).isEqualTo(ldt.plusSeconds(3600)); + assertThat(dbEurope.find(MTimeTest.class).findOne().getLocalDateTime()).isEqualTo(ldt); + assertThat(dbPST.find(MTimeTest.class).findOne().getLocalDateTime()).isEqualTo(ldt.plusSeconds(9 * 3600)); + + } + + @Test + public void testLocalTime() { + dbGmt.sqlUpdate("insert into mtime_test (id, local_time) values (1, '05:15:15')").execute(); + + LocalTime lt = LocalTime.of(5, 15, 15); + + testTimeType(MTimeTest.class, "local_time", MTimeTest::getLocalTime, lt); + + assertThat(dbEurope.find(MTimeTest.class).findOne().getLocalTime()).isEqualTo(lt); + + assertThat(dbPST.find(MTimeTest.class).findOne().getLocalTime()).isEqualTo(lt); + + } + + @Test + public void testTimestamp() { + dbGmt.sqlUpdate("insert into mtime_test (id, timestamp) values (1, '2021-11-21 05:15:15')").execute(); + + Timestamp ts = Timestamp.from(Instant.parse("2021-11-21T05:15:15Z")); + + testTimeType(MTimeTest.class, "timestamp", MTimeTest::getTimestamp, ts); + + + assertThat(dbEurope.find(MTimeTest.class).findOne().getTimestamp()).isEqualTo(new Timestamp(ts.getTime() - 3600_000)); + + assertThat(dbPST.find(MTimeTest.class).findOne().getTimestamp()).isEqualTo(new Timestamp(ts.getTime() + 8 * 3600_000)); + } + + /** + * This checks for each datatype, if it can be read properly from DB. + *
    + *
  1. Check, if the read value is the same as ref
  2. + *
  3. Perform a findcount with eq(field, ref)
  4. + *
  5. Perform a findOnw with eq(field, ref)
  6. + *
  7. Perform a findOnw with in(field, ref)
  8. + *
+ */ + void testTimeType(Class modelClass, String field, Function getter, T ref) { + SoftAssertions softly = new SoftAssertions(); + // Check 1: There should be ONE model in the DB + M dbModel = dbGmt.find(modelClass).findOne(); + softly.assertThat(getter.apply(dbModel).toString()) + .as("test read value") + .isEqualTo(ref.toString()); + + int count = dbGmt.find(modelClass).where().eq(field, ref).findCount(); + softly.assertThat(count) + .as("test findcount with eq") + .isEqualTo(1); + + dbModel = dbGmt.find(modelClass).where().eq(field, ref).findOne(); + softly.assertThat(dbModel) + .as("test find with eq") + .isNotNull(); + + dbModel = dbGmt.find(modelClass).where().in(field, ref).findOne(); + softly.assertThat(dbModel) + .as("test find with in") + .isNotNull(); + softly.assertAll(); + } + + private Database createServer(String dbTimeZone) { + + DatabaseConfig config = new DatabaseConfig(); +// create a clone from server() with different TZ + config.setName(server().name()); + config.loadFromProperties(server().pluginApi().config().getProperties()); + config.setDataSource(server().dataSource()); + config.setReadOnlyDataSource(server().dataSource()); + config.setDdlGenerate(false); + config.setDdlRun(false); + config.setRegister(false); + config.setDdlExtra(false); + config.setDefaultServer(false); + config.setChangeLogAsync(false); + config.setDumpMetricsOnShutdown(false); + config.setDataTimeZone(dbTimeZone); + + return DatabaseFactory.create(config); + } +} diff --git a/ebean-test/src/test/java/org/tests/timezone/TimestampTest.java b/ebean-test/src/test/java/org/tests/timezone/TimestampTest.java deleted file mode 100644 index 958d4db41c..0000000000 --- a/ebean-test/src/test/java/org/tests/timezone/TimestampTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.tests.timezone; - -import io.ebean.Database; -import io.ebean.DatabaseFactory; -import io.ebean.config.DatabaseConfig; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; - -import java.sql.Timestamp; - -import static org.assertj.core.api.Assertions.assertThat; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class TimestampTest { - - protected String platform="h2"; - protected Database db; - - @BeforeAll - public void startTest() { - db = createServer("GMT"); // test uses GMT database - } - - @AfterAll - public void shutdown() { - if (db != null) { - db.find(MTimestamp.class).delete(); - db.shutdown(); - } - } - - /** - * The test checks the write and read of LocalTime values. The database is in GMT time zone. - * In order to verify the test in different java time zones (where the application runs), - * use the -Duser.timezone as JVM argument, - * e.g. -Duser.timezone="America/New_York" or -Duser.timezone="PST"> - * or any other timezone: https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/. - */ - @Test - public void testTimestamp() { - Timestamp ts = new Timestamp(2021 - 1900, 11 - 1, 21, 5, 15, 15, 0); - - assertThat(db.find(MTimestamp.class).findCount()).isEqualTo(0); - db.sqlUpdate("insert into mtimestamp (id, timestamp) values (1, '2021-11-21 05:15:15')").execute(); - - int count = db.find(MTimestamp.class).where().eq("timestamp", ts).findCount(); - assertThat(count).isEqualTo(1); - - MTimestamp dbModel = db.find(MTimestamp.class).where().eq("timestamp", ts).findOne(); - assertThat(dbModel.getTimestamp().toString()).isEqualTo(ts.toString()); - } - - private Database createServer(String dbTimeZone) { - DatabaseConfig config = new DatabaseConfig(); - config.setName(platform); - config.loadFromProperties(); - config.setDdlExtra(false); - config.setDefaultServer(false); - config.setRegister(false); - config.setChangeLogAsync(false); - config.addClass(MTimestamp.class); - - config.setDumpMetricsOnShutdown(false); - config.setDataTimeZone(dbTimeZone); - - return DatabaseFactory.create(config); - } -}