From 29d88d7de8bdee184887928102008bf0befb6f4b Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Thu, 20 Sep 2018 10:23:46 +0200 Subject: [PATCH] FIX: NPE in StalarTypeLocalTime and StalarTypeMonthDay (#1479) --- .../server/type/ScalarTypeLocalTime.java | 1 + .../type/ScalarTypeLocalTimeWithNanos.java | 1 + .../server/type/ScalarTypeMonthDay.java | 1 + .../tests/model/types/SomeNewTypesBean.java | 34 ++++++++++++++++ .../java/org/tests/types/TestNewTypes.java | 39 +++++++++++++++++++ 5 files changed, 76 insertions(+) diff --git a/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTime.java b/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTime.java index f4449b718..49a217951 100644 --- a/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTime.java +++ b/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTime.java @@ -51,6 +51,7 @@ public class ScalarTypeLocalTime extends ScalarTypeBase { @Override public LocalTime toBeanType(Object value) { if (value instanceof LocalTime) return (LocalTime) value; + if (value == null) return null; return BasicTypeConverter.toTime(value).toLocalTime(); } diff --git a/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java b/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java index 9a39e3222..780820f02 100644 --- a/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java +++ b/src/main/java/io/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java @@ -39,6 +39,7 @@ public class ScalarTypeLocalTimeWithNanos extends ScalarTypeLocalTime { @Override public LocalTime toBeanType(Object value) { if (value instanceof LocalTime) return (LocalTime) value; + if (value == null) return null; return LocalTime.ofNanoOfDay(BasicTypeConverter.toLong(value)); } diff --git a/src/main/java/io/ebeaninternal/server/type/ScalarTypeMonthDay.java b/src/main/java/io/ebeaninternal/server/type/ScalarTypeMonthDay.java index 0afa5e865..3a3acdb3e 100644 --- a/src/main/java/io/ebeaninternal/server/type/ScalarTypeMonthDay.java +++ b/src/main/java/io/ebeaninternal/server/type/ScalarTypeMonthDay.java @@ -72,6 +72,7 @@ public class ScalarTypeMonthDay extends ScalarTypeBase { @Override public MonthDay toBeanType(Object value) { if (value instanceof MonthDay) return (MonthDay) value; + if (value == null) return null; return convertFromDate((Date) value); } diff --git a/src/test/java/org/tests/model/types/SomeNewTypesBean.java b/src/test/java/org/tests/model/types/SomeNewTypesBean.java index 64f0a2220..35a133d8f 100644 --- a/src/test/java/org/tests/model/types/SomeNewTypesBean.java +++ b/src/test/java/org/tests/model/types/SomeNewTypesBean.java @@ -6,10 +6,13 @@ import javax.persistence.Id; import javax.persistence.Version; import java.nio.file.Path; import java.time.DayOfWeek; +import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.Month; +import java.time.MonthDay; import java.time.OffsetDateTime; import java.time.Period; import java.time.Year; @@ -39,6 +42,9 @@ public class SomeNewTypesBean { @Column(name = "yr_mth") YearMonth yearMonth; + @Column(name = "month_day") + MonthDay monthDay; + LocalDate localDate; LocalDateTime localDateTime; @@ -47,6 +53,8 @@ public class SomeNewTypesBean { ZonedDateTime zonedDateTime; + LocalTime localTime; + Instant instant; ZoneId zoneId; @@ -57,6 +65,8 @@ public class SomeNewTypesBean { Period period; + Duration duration; + public Long getId() { return id; } @@ -105,6 +115,14 @@ public class SomeNewTypesBean { this.yearMonth = yearMonth; } + public MonthDay getMonthDay() { + return monthDay; + } + + public void setMonthDay(MonthDay monthDay) { + this.monthDay = monthDay; + } + public LocalDate getLocalDate() { return localDate; } @@ -137,6 +155,14 @@ public class SomeNewTypesBean { this.zonedDateTime = zonedDateTime; } + public LocalTime getLocalTime() { + return localTime; + } + + public void setLocalTime(LocalTime localTime) { + this.localTime = localTime; + } + public Instant getInstant() { return instant; } @@ -176,4 +202,12 @@ public class SomeNewTypesBean { public void setPeriod(Period period) { this.period = period; } + + public Duration getDuration() { + return duration; + } + + public void setDuration(Duration duration) { + this.duration = duration; + } } diff --git a/src/test/java/org/tests/types/TestNewTypes.java b/src/test/java/org/tests/types/TestNewTypes.java index 4f2f15981..45578f6b1 100644 --- a/src/test/java/org/tests/types/TestNewTypes.java +++ b/src/test/java/org/tests/types/TestNewTypes.java @@ -11,10 +11,13 @@ import org.tests.model.types.SomeNewTypesBean; import java.io.File; import java.nio.file.Paths; import java.time.DayOfWeek; +import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.Month; +import java.time.MonthDay; import java.time.OffsetDateTime; import java.time.Period; import java.time.Year; @@ -37,6 +40,7 @@ public class TestNewTypes extends BaseTestCase { bean.setLocalDateTime(LocalDateTime.now()); bean.setOffsetDateTime(OffsetDateTime.now()); bean.setZonedDateTime(ZonedDateTime.now()); + bean.setLocalTime(LocalTime.now()); bean.setInstant(Instant.now()); bean.setYear(Year.now()); bean.setMonth(Month.APRIL); @@ -44,8 +48,10 @@ public class TestNewTypes extends BaseTestCase { bean.setZoneId(ZoneId.systemDefault()); bean.setZoneOffset(ZonedDateTime.now().getOffset()); bean.setYearMonth(YearMonth.of(2014, 9)); + bean.setMonthDay(MonthDay.of(9,22)); bean.setPath(Paths.get(TEMP_PATH)); bean.setPeriod(Period.of(4,3,2)); + bean.setDuration(Duration.ofMinutes(5)); Ebean.save(bean); @@ -70,6 +76,9 @@ public class TestNewTypes extends BaseTestCase { list = Ebean.find(SomeNewTypesBean.class).where().lt("zonedDateTime", ZonedDateTime.now()).findList(); assertTrue(!list.isEmpty()); + list = Ebean.find(SomeNewTypesBean.class).where().le("localTime", LocalTime.now()).findList(); + assertTrue(!list.isEmpty()); + list = Ebean.find(SomeNewTypesBean.class).where().eq("zoneId", ZoneId.systemDefault().getId()).findList(); assertTrue(!list.isEmpty()); @@ -79,6 +88,9 @@ public class TestNewTypes extends BaseTestCase { list = Ebean.find(SomeNewTypesBean.class).where().le("yearMonth", YearMonth.of(2014, 9)).findList(); assertTrue(!list.isEmpty()); + list = Ebean.find(SomeNewTypesBean.class).where().le("monthDay", MonthDay.of(9,22)).findList(); + assertTrue(!list.isEmpty()); + list = Ebean.find(SomeNewTypesBean.class).where().le("year", Year.now()).findList(); assertTrue(!list.isEmpty()); @@ -91,6 +103,9 @@ public class TestNewTypes extends BaseTestCase { list = Ebean.find(SomeNewTypesBean.class).where().eq("period", Period.of(4,3,2)).findList(); assertTrue(!list.isEmpty()); + list = Ebean.find(SomeNewTypesBean.class).where().eq("duration", Duration.ofMinutes(5)).findList(); + assertTrue(!list.isEmpty()); + SomeNewTypesBean fetched = Ebean.find(SomeNewTypesBean.class, bean.getId()); assertEquals(bean.getZoneId(), fetched.getZoneId()); @@ -98,12 +113,15 @@ public class TestNewTypes extends BaseTestCase { assertEquals(bean.getMonth(), fetched.getMonth()); assertEquals(bean.getYear(), fetched.getYear()); assertEquals(bean.getYearMonth(), fetched.getYearMonth()); + assertEquals(bean.getMonthDay(), fetched.getMonthDay()); assertEquals(bean.getLocalDate(), fetched.getLocalDate()); assertThat(fetched.getLocalDateTime()).isEqualToIgnoringNanos(bean.getLocalDateTime()); assertThat(fetched.getOffsetDateTime()).isEqualToIgnoringNanos(bean.getOffsetDateTime()); + assertThat(fetched.getLocalTime().toSecondOfDay()).isEqualTo(bean.getLocalTime().toSecondOfDay()); assertEquals(bean.getInstant().toEpochMilli() / 1000, fetched.getInstant().toEpochMilli() / 1000); assertEquals(bean.getPath(), fetched.getPath()); assertEquals(bean.getPeriod(), fetched.getPeriod()); + assertEquals(bean.getDuration(), fetched.getDuration()); String asJson = Ebean.json().toJson(fetched); @@ -115,13 +133,16 @@ public class TestNewTypes extends BaseTestCase { assertEquals(bean.getMonth(), toBean.getMonth()); assertEquals(bean.getYear(), toBean.getYear()); assertEquals(bean.getYearMonth(), toBean.getYearMonth()); + assertEquals(bean.getMonthDay(), toBean.getMonthDay()); assertEquals(bean.getLocalDate(), toBean.getLocalDate()); assertThat(toBean.getLocalDateTime()).isEqualToIgnoringNanos(bean.getLocalDateTime()); assertThat(toBean.getOffsetDateTime()).isEqualToIgnoringNanos(bean.getOffsetDateTime()); + assertEquals(bean.getLocalTime().toSecondOfDay(), toBean.getLocalTime().toSecondOfDay()); assertEquals(bean.getInstant().toEpochMilli() / 1000, toBean.getInstant().toEpochMilli() / 1000); // FIXME: This test fails on Windows with: expected:<\tmp> but was: assertEquals(bean.getPath(), toBean.getPath()); assertEquals(bean.getPeriod(), toBean.getPeriod()); + assertEquals(bean.getDuration(), toBean.getDuration()); } @@ -139,12 +160,15 @@ public class TestNewTypes extends BaseTestCase { assertNull(fetched.getMonth()); assertNull(fetched.getYear()); assertNull(fetched.getYearMonth()); + assertNull(fetched.getMonthDay()); assertNull(fetched.getLocalDate()); assertNull(fetched.getLocalDateTime()); assertNull(fetched.getOffsetDateTime()); + assertNull(fetched.getLocalTime()); assertNull(fetched.getInstant()); assertNull(fetched.getPath()); assertNull(fetched.getPeriod()); + assertNull(fetched.getDuration()); } @Test @@ -154,6 +178,7 @@ public class TestNewTypes extends BaseTestCase { refBean.setLocalDateTime(LocalDateTime.now()); refBean.setOffsetDateTime(OffsetDateTime.now()); refBean.setZonedDateTime(ZonedDateTime.now()); + refBean.setLocalTime(LocalTime.now()); refBean.setInstant(Instant.now()); refBean.setYear(Year.now()); refBean.setMonth(Month.APRIL); @@ -161,8 +186,10 @@ public class TestNewTypes extends BaseTestCase { refBean.setZoneId(ZoneId.systemDefault()); refBean.setZoneOffset(ZonedDateTime.now().getOffset()); refBean.setYearMonth(YearMonth.of(2014, 9)); + refBean.setMonthDay(MonthDay.of( 9, 22)); refBean.setPath(Paths.get(TEMP_PATH)); refBean.setPeriod(Period.of(4,3,2)); + refBean.setDuration(Duration.ofMinutes(5)); testSetGetPath(refBean); } @@ -179,6 +206,7 @@ public class TestNewTypes extends BaseTestCase { ExpressionPath localDateTime = beanType.getExpressionPath("localDateTime"); ExpressionPath offsetDateTime = beanType.getExpressionPath("offsetDateTime"); ExpressionPath zonedDateTime = beanType.getExpressionPath("zonedDateTime"); + ExpressionPath localTime = beanType.getExpressionPath("localTime"); ExpressionPath instant = beanType.getExpressionPath("instant"); ExpressionPath year = beanType.getExpressionPath("year"); ExpressionPath month = beanType.getExpressionPath("month"); @@ -186,8 +214,10 @@ public class TestNewTypes extends BaseTestCase { ExpressionPath zoneId = beanType.getExpressionPath("zoneId"); ExpressionPath zoneOffset = beanType.getExpressionPath("zoneOffset"); ExpressionPath yearMonth = beanType.getExpressionPath("yearMonth"); + ExpressionPath monthDay = beanType.getExpressionPath("monthDay"); ExpressionPath path = beanType.getExpressionPath("path"); ExpressionPath period = beanType.getExpressionPath("period"); + ExpressionPath duration = beanType.getExpressionPath("duration"); localDate.pathSet(testBean, refBean.getLocalDate()); assertThat(localDate.pathGet(testBean)).isEqualTo(refBean.getLocalDate()); @@ -201,6 +231,9 @@ public class TestNewTypes extends BaseTestCase { zonedDateTime.pathSet(testBean, refBean.getZonedDateTime()); assertThat(zonedDateTime.pathGet(testBean)).isEqualTo(refBean.getZonedDateTime()); + localTime.pathSet(testBean, refBean.getLocalTime()); + assertThat(localTime.pathGet(testBean)).isEqualTo(refBean.getLocalTime()); + instant.pathSet(testBean, refBean.getInstant()); assertThat(instant.pathGet(testBean)).isEqualTo(refBean.getInstant()); @@ -222,12 +255,18 @@ public class TestNewTypes extends BaseTestCase { yearMonth.pathSet(testBean, refBean.getYearMonth()); assertThat(yearMonth.pathGet(testBean)).isEqualTo(refBean.getYearMonth()); + monthDay.pathSet(testBean, refBean.getMonthDay()); + assertThat(monthDay.pathGet(testBean)).isEqualTo(refBean.getMonthDay()); + path.pathSet(testBean, refBean.getPath()); assertThat(path.pathGet(testBean)).isEqualTo(refBean.getPath()); period.pathSet(testBean, refBean.getPeriod()); assertThat(period.pathGet(testBean)).isEqualTo(refBean.getPeriod()); + duration.pathSet(testBean, refBean.getDuration()); + assertThat(duration.pathGet(testBean)).isEqualTo(refBean.getDuration()); + Ebean.save(refBean); Ebean.save(testBean); }