BAEL-5178:An introduction to InstantSource (#11646)

* BAEL-5178:An introduction to InstantSource

* BAEL-5178:PR fixes

Co-authored-by: Pablo Ubal <pablo.ubal@flexit.es>
This commit is contained in:
pabloubal 2022-01-13 16:12:54 +01:00 committed by GitHub
parent 4000ebcf65
commit eb05d3fec3
3 changed files with 147 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}