diff --git a/ebean-test/src/test/java/org/tests/timezone/LocalTimeTest.java b/ebean-test/src/test/java/org/tests/timezone/LocalTimeTest.java new file mode 100644 index 000000000..6f5b7e0f9 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/LocalTimeTest.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.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/MLocalTime.java b/ebean-test/src/test/java/org/tests/timezone/MLocalTime.java new file mode 100644 index 000000000..4a2d30313 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/timezone/MLocalTime.java @@ -0,0 +1,28 @@ +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; + } + +}