diff --git a/src/test/java/com/avaje/tests/insert/TestSaveWithDaylightSavings.java b/src/test/java/com/avaje/tests/insert/TestSaveWithDaylightSavings.java new file mode 100644 index 000000000..7aca418fc --- /dev/null +++ b/src/test/java/com/avaje/tests/insert/TestSaveWithDaylightSavings.java @@ -0,0 +1,58 @@ +package com.avaje.tests.insert; + +import java.util.Date; +import java.util.TimeZone; + +import org.junit.Assert; +import org.junit.Test; + +import com.avaje.ebean.BaseTestCase; +import com.avaje.ebean.Ebean; +import com.avaje.tests.model.basic.EBasic; + +public class TestSaveWithDaylightSavings extends BaseTestCase { + + @Test + public void test() { + + // For it to fail, the time has to match the time at which the daylight saving changes + // are applied in that time zone. Therefore specify it explicitly. + + TimeZone defaultTimeZone = TimeZone.getDefault(); + try { + + TimeZone.setDefault(TimeZone.getTimeZone("EET")); + + // Run the code and see how there is a 3600 second change + Date daylightSavingDate = new Date(1351382400000l); + // On a second run comment in the following date and see + // how there is a 0 second change + // daylightSavingDate = new Date(1361382400000l); + + EBasic e = new EBasic(); + e.setSomeDate(daylightSavingDate); + + Ebean.save(e); + Assert.assertNotNull(e.getId()); + + // Reload the entity from database + EBasic e2 = Ebean.find(EBasic.class, e.getId()); + + long diffMillis = e2.getSomeDate().getTime() - e.getSomeDate().getTime(); + + System.out.println("The date I created " + daylightSavingDate); + System.out.println(" --- the date i put in : " + e.getSomeDate()); + System.out.println(" as millis : " + e.getSomeDate().getTime()); + System.out.println(" --- the date i get back : " + e2.getSomeDate()); + System.out.println(" as millis : " + e2.getSomeDate().getTime()); + System.out.println("The difference is " + diffMillis / 1000 + " seconds"); + + Assert.assertEquals(0L, diffMillis); + + } finally { + TimeZone.setDefault(defaultTimeZone); + } + + } + +} diff --git a/src/test/java/com/avaje/tests/model/basic/EBasic.java b/src/test/java/com/avaje/tests/model/basic/EBasic.java index 3eaca0b5c..a9f5d98c6 100644 --- a/src/test/java/com/avaje/tests/model/basic/EBasic.java +++ b/src/test/java/com/avaje/tests/model/basic/EBasic.java @@ -1,5 +1,7 @@ package com.avaje.tests.model.basic; +import java.util.Date; + import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @@ -7,59 +9,69 @@ import javax.persistence.Table; import com.avaje.ebean.annotation.EnumValue; @Entity -@Table(name="e_basic") +@Table(name = "e_basic") public class EBasic { - public enum Status { - @EnumValue("N") - NEW, - - @EnumValue("A") - ACTIVE, - - @EnumValue("I") - INACTIVE, - } - - @Id - Integer id; - - Status status; + public enum Status { + @EnumValue("N") + NEW, - String name; + @EnumValue("A") + ACTIVE, - String description; - - public Integer getId() { - return id; - } + @EnumValue("I") + INACTIVE, + } - public void setId(Integer id) { - this.id = id; - } + @Id + Integer id; - public Status getStatus() { - return status; - } + Status status; - public void setStatus(Status status) { - this.status = status; - } + String name; - public String getName() { - return name; - } + String description; - public void setName(String name) { - this.name = name; - } + Date someDate; - public String getDescription() { - return description; - } + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getSomeDate() { + return someDate; + } + + public void setSomeDate(Date someDate) { + this.someDate = someDate; + } - public void setDescription(String description) { - this.description = description; - } - }