diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/instantsource/InstantExample.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/instantsource/InstantExample.java new file mode 100644 index 0000000000..0b7fd569f0 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/instantsource/InstantExample.java @@ -0,0 +1,21 @@ +package com.baeldung.instantsource; + +import java.time.Instant; +import java.time.InstantSource; + +public class InstantExample { + InstantWrapper instantWrapper; + InstantSource instantSource; + + public InstantExample(InstantWrapper instantWrapper, InstantSource instantSource) { + this.instantWrapper = instantWrapper; + this.instantSource = instantSource; + } + + public Instant getCurrentInstantFromWrapper() { + return instantWrapper.instant(); + } + public Instant getCurrentInstantFromInstantSource() { + return instantSource.instant(); + } +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/instantsource/InstantWrapper.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/instantsource/InstantWrapper.java new file mode 100644 index 0000000000..d88a8b5a8a --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/instantsource/InstantWrapper.java @@ -0,0 +1,39 @@ +package com.baeldung.instantsource; + +import java.time.Clock; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; + +public class InstantWrapper { + Clock clock; + + private InstantWrapper() { + this.clock = Clock.systemDefaultZone(); + } + + private InstantWrapper(ZonedDateTime zonedDateTime) { + this.clock = Clock.fixed(zonedDateTime.toInstant(), zonedDateTime.getZone()); + } + + private InstantWrapper(LocalDateTime localDateTime) { + this.clock = Clock.fixed(localDateTime.toInstant(ZoneOffset.UTC), ZoneOffset.UTC); + } + + public static InstantWrapper of() { + return new InstantWrapper(); + } + + public static InstantWrapper of(ZonedDateTime zonedDateTime) { + return new InstantWrapper(zonedDateTime); + } + + public static InstantWrapper of(LocalDateTime localDateTime) { + return new InstantWrapper(localDateTime); + } + + public Instant instant() { + return clock.instant(); + } +} diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/instantsource/InstantExampleUnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/instantsource/InstantExampleUnitTest.java new file mode 100644 index 0000000000..68bc084ba6 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/instantsource/InstantExampleUnitTest.java @@ -0,0 +1,87 @@ +package com.baeldung.instantsource; + +import org.junit.jupiter.api.Test; + +import java.time.Clock; +import java.time.Instant; +import java.time.InstantSource; +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class InstantExampleUnitTest { + + @Test + void getCurrentInstantFromWrapper_shouldReturnCurrentInstantBasedOnSystemClock() { + //given + InstantExample tested = new InstantExample(InstantWrapper.of(), null); + Instant currentInstant = Clock.systemDefaultZone().instant(); + //when + Instant returnedInstant = tested.getCurrentInstantFromWrapper(); + + //then + assertTrue(returnedInstant.isAfter(currentInstant)); + } + + @Test + void getCurrentInstantFromWrapper_shouldReturnFixedInstant() { + //given + LocalDateTime now = LocalDateTime.now(); + InstantExample tested = new InstantExample(InstantWrapper.of(now), null); + Instant currentInstant = now.toInstant(ZoneOffset.UTC); + //when + Instant returnedInstant = tested.getCurrentInstantFromWrapper(); + + //then + assertEquals(currentInstant, returnedInstant); + } + + @Test + void getCurrentInstantFromInstantSource_shouldReturnCurrentInstantBasedOnSystemClock() { + //given + InstantSource instantSource = InstantSource.system(); + InstantExample tested = new InstantExample(null, instantSource); + Instant currentInstant = instantSource.instant(); + + //when + Instant returnedInstant = tested.getCurrentInstantFromInstantSource(); + + //then + assertTrue(returnedInstant.isAfter(currentInstant)); + } + + @Test + void getCurrentInstantFromInstantSource_shouldReturnFixedInstant() { + //given + LocalDateTime now = LocalDateTime.now(); + InstantSource instantSource = InstantSource.fixed(now.toInstant(ZoneOffset.UTC)); + InstantExample tested = new InstantExample(null, instantSource); + Instant currentInstant = instantSource.instant(); + + LocalDateTime fixed = LocalDateTime.of(2022, 01, 01, 00, 00); + Instant i = InstantSource.fixed(fixed.toInstant(ZoneOffset.UTC)).instant(); + System.out.println(i); + + //when + Instant returnedInstant = tested.getCurrentInstantFromInstantSource(); + + //then + assertEquals(currentInstant, returnedInstant); + } + + @Test + void getCurrentInstantFromInstantSource_shouldMatchGivenTimezone() { + //given + LocalDateTime now = LocalDateTime.now(); + InstantSource instantSource = InstantSource.fixed(now.toInstant(ZoneOffset.of("-4"))); + InstantExample tested = new InstantExample(null, instantSource); + + //when + Instant returnedInstant = tested.getCurrentInstantFromInstantSource(); + + //then + assertEquals(now.atOffset(ZoneOffset.of("-4")).toInstant(), returnedInstant); + } +} \ No newline at end of file