diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/collections/DateRange.java b/li.strolch.utils/src/main/java/li/strolch/utils/collections/DateRange.java index d1e231a2e..4e826a39d 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/collections/DateRange.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/collections/DateRange.java @@ -104,26 +104,38 @@ public class DateRange { } public ZonedDateTime getFromDateZdt() { + if (this.fromDate == null) + throw new IllegalStateException("fromDate is unbounded, check with isFromBounded()"); return this.fromDate; } public ZonedDateTime getToDateZdt() { + if (this.toDate == null) + throw new IllegalStateException("toDate is unbounded, check with isToBounded()"); return this.toDate; } public LocalDateTime getFromDateLdt() { + if (this.fromDate == null) + throw new IllegalStateException("fromDate is unbounded, check with isFromBounded()"); return this.fromDate.toLocalDateTime(); } public LocalDateTime getToDateLdt() { + if (this.toDate == null) + throw new IllegalStateException("toDate is unbounded, check with isToBounded()"); return this.toDate.toLocalDateTime(); } public Date getFromDate() { + if (this.fromDate == null) + throw new IllegalStateException("fromDate is unbounded, check with isFromBounded()"); return Date.from(this.fromDate.toInstant()); } public Date getToDate() { + if (this.toDate == null) + throw new IllegalStateException("toDate is unbounded, check with isToBounded()"); return Date.from(this.toDate.toInstant()); } diff --git a/li.strolch.utils/src/test/java/li/strolch/utils/collections/DateRangeTest.java b/li.strolch.utils/src/test/java/li/strolch/utils/collections/DateRangeTest.java index 85bfd40c8..5fa8d1b45 100644 --- a/li.strolch.utils/src/test/java/li/strolch/utils/collections/DateRangeTest.java +++ b/li.strolch.utils/src/test/java/li/strolch/utils/collections/DateRangeTest.java @@ -35,7 +35,13 @@ public class DateRangeTest { DateRange dateRange = new DateRange(); dateRange.from(now, true); assertEquals(now, dateRange.getFromDate()); - assertNull(dateRange.getToDate()); + try { + dateRange.getToDate(); + fail("Should fail, as toDate is not set!"); + } catch (IllegalStateException e) { + if (!e.getMessage().equals("toDate is unbounded, check with isToBounded()")) + fail("Should fail with different exception message, as toDate is not set!"); + } assertFalse(dateRange.isUnbounded()); assertFalse(dateRange.isBounded()); } @@ -49,7 +55,13 @@ public class DateRangeTest { DateRange dateRange = new DateRange(); dateRange.to(now, true); assertEquals(now, dateRange.getToDate()); - assertNull(dateRange.getFromDate()); + try { + dateRange.getFromDate(); + fail("Should fail, as fromDate is not set!"); + } catch (IllegalStateException e) { + if (!e.getMessage().equals("fromDate is unbounded, check with isFromBounded()")) + fail("Should fail with different exception message, as fromDate is not set!"); + } assertFalse(dateRange.isUnbounded()); assertFalse(dateRange.isBounded()); }