Merge branch 'BAEL-16646' into BAEL-16646-2. Flatten additional datetime modules and create new time-measurements module with 3 articles.
# Conflicts: # core-java-modules/core-java-datetime/README.md # core-java-modules/core-java-datetime/pom.xml
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,159 @@
|
||||
package com.baeldung.java.clock;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ClockUnitTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ClockUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenClock_withSytemUTC_retrievesInstant() {
|
||||
|
||||
Clock clockUTC = Clock.systemUTC();
|
||||
|
||||
assertEquals(clockUTC.getZone(), ZoneOffset.UTC);
|
||||
assertEquals(clockUTC.instant().equals(null), false);
|
||||
|
||||
LOGGER.debug("UTC instant :: " + clockUTC.instant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_withSytem_retrievesInstant() {
|
||||
|
||||
Clock clockSystem = Clock.system(ZoneId.of("Asia/Calcutta"));
|
||||
|
||||
assertEquals(clockSystem.getZone(), ZoneId.of("Asia/Calcutta"));
|
||||
assertEquals(clockSystem.instant().equals(null), false);
|
||||
|
||||
LOGGER.debug("System zone :: " + clockSystem.getZone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_withSytemDefaultZone_retrievesInstant() {
|
||||
|
||||
Clock clockSystemDefault = Clock.systemDefaultZone();
|
||||
|
||||
assertEquals(clockSystemDefault.getZone().equals(null), false);
|
||||
assertEquals(clockSystemDefault.instant().equals(null), false);
|
||||
|
||||
LOGGER.debug("System Default instant :: " + clockSystemDefault.instant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_withSytemUTC_retrievesTimeInMillis() {
|
||||
|
||||
Clock clockMillis = Clock.systemDefaultZone();
|
||||
|
||||
assertEquals(clockMillis.instant().equals(null), false);
|
||||
assertTrue(clockMillis.millis() > 0);
|
||||
|
||||
LOGGER.debug("System Default millis :: " + clockMillis.millis());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_usingOffset_retrievesFutureDate() {
|
||||
|
||||
Clock baseClock = Clock.systemDefaultZone();
|
||||
|
||||
// result clock will be later than baseClock
|
||||
Clock futureClock = Clock.offset(baseClock, Duration.ofHours(72));
|
||||
|
||||
assertEquals(futureClock.instant().equals(null), false);
|
||||
assertTrue(futureClock.millis() > baseClock.millis());
|
||||
|
||||
LOGGER.debug("Future Clock instant :: " + futureClock.instant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_usingOffset_retrievesPastDate() {
|
||||
Clock baseClock = Clock.systemDefaultZone();
|
||||
|
||||
// result clock will be later than baseClock
|
||||
Clock pastClock = Clock.offset(baseClock, Duration.ofHours(-72));
|
||||
|
||||
assertEquals(pastClock.instant().equals(null), false);
|
||||
assertTrue(pastClock.millis() < baseClock.millis());
|
||||
|
||||
LOGGER.debug("Past Clock instant :: " + pastClock.instant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_usingTick_retrievesInstant() {
|
||||
Clock clockDefaultZone = Clock.systemDefaultZone();
|
||||
Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(300));
|
||||
|
||||
assertEquals(clockDefaultZone.instant().equals(null), false);
|
||||
assertEquals(clocktick.instant().equals(null), false);
|
||||
assertTrue(clockDefaultZone.millis() > clocktick.millis());
|
||||
|
||||
LOGGER.debug("Clock Default Zone instant : " + clockDefaultZone.instant());
|
||||
LOGGER.debug("Clock tick instant: " + clocktick.instant());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void givenClock_usingTickDurationNegative_throwsException() {
|
||||
|
||||
Clock clockDefaultZone = Clock.systemDefaultZone();
|
||||
Clock.tick(clockDefaultZone, Duration.ofSeconds(-300));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_usingTickSeconds_retrievesInstant() {
|
||||
ZoneId zoneId = ZoneId.of("Asia/Calcutta");
|
||||
Clock tickSeconds = Clock.tickSeconds(zoneId);
|
||||
|
||||
assertEquals(tickSeconds.instant().equals(null), false);
|
||||
LOGGER.debug("Clock tick seconds instant :: " + tickSeconds.instant());
|
||||
|
||||
tickSeconds = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofSeconds(100));
|
||||
assertEquals(tickSeconds.instant().equals(null), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_usingTickMinutes_retrievesInstant() {
|
||||
|
||||
Clock tickMinutes = Clock.tickMinutes(ZoneId.of("Asia/Calcutta"));
|
||||
|
||||
assertEquals(tickMinutes.instant().equals(null), false);
|
||||
LOGGER.debug("Clock tick seconds instant :: " + tickMinutes.instant());
|
||||
|
||||
tickMinutes = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofMinutes(5));
|
||||
assertEquals(tickMinutes.instant().equals(null), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_usingWithZone_retrievesInstant() {
|
||||
|
||||
ZoneId zoneSingapore = ZoneId.of("Asia/Singapore");
|
||||
Clock clockSingapore = Clock.system(zoneSingapore);
|
||||
|
||||
assertEquals(clockSingapore.instant().equals(null), false);
|
||||
LOGGER.debug("clockSingapore instant : " + clockSingapore.instant());
|
||||
|
||||
ZoneId zoneCalcutta = ZoneId.of("Asia/Calcutta");
|
||||
Clock clockCalcutta = clockSingapore.withZone(zoneCalcutta);
|
||||
assertEquals(clockCalcutta.instant().equals(null), false);
|
||||
LOGGER.debug("clockCalcutta instant : " + clockSingapore.instant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClock_usingGetZone_retrievesZoneId() {
|
||||
|
||||
Clock clockDefaultZone = Clock.systemDefaultZone();
|
||||
ZoneId zone = clockDefaultZone.getZone();
|
||||
|
||||
assertEquals(zone.getId().equals(null), false);
|
||||
LOGGER.debug("Default zone instant : " + clockDefaultZone.instant());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ElapsedTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenRunningTask_whenMeasuringTimeWithCurrentTimeMillis_thenGetElapsedTime() throws InterruptedException {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
simulateRunningTask();
|
||||
|
||||
long finish = System.currentTimeMillis();
|
||||
|
||||
long timeElapsed = finish - start;
|
||||
|
||||
assertEquals(true, (2000L <= timeElapsed) && (timeElapsed <= 3000L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giveRunningTask_whenMeasuringTimeWithNanoTime_thenGetElapsedTime() throws InterruptedException {
|
||||
long start = System.nanoTime();
|
||||
|
||||
simulateRunningTask();
|
||||
|
||||
long finish = System.nanoTime();
|
||||
|
||||
long timeElapsed = finish - start;
|
||||
|
||||
assertEquals(true, (2000000000L <= timeElapsed) && (timeElapsed <= 3000000000L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRunningTask_whenMeasuringTimeWithStopWatch_thenGetElapsedTime() throws InterruptedException {
|
||||
StopWatch watch = new StopWatch();
|
||||
watch.start();
|
||||
|
||||
simulateRunningTask();
|
||||
|
||||
watch.stop();
|
||||
|
||||
long timeElapsed = watch.getTime();
|
||||
|
||||
assertEquals(true, (2000L <= timeElapsed) && (timeElapsed <= 3000L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRunningTask_whenMeasuringTimeWithInstantClass_thenGetElapsedTime() throws InterruptedException {
|
||||
Instant start = Instant.now();
|
||||
|
||||
simulateRunningTask();
|
||||
|
||||
Instant finish = Instant.now();
|
||||
|
||||
long timeElapsed = Duration.between(start, finish).toMillis();
|
||||
|
||||
assertEquals(true, (2000L <= timeElapsed) && (timeElapsed <= 3000L));
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate task running for 2.5 seconds.
|
||||
*
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private static void simulateRunningTask() throws InterruptedException {
|
||||
TimeUnit.MILLISECONDS.sleep(2500); // 2.5 seconds
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ Instant.class })
|
||||
public class InstantUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInstantMock_whenNow_thenGetFixedInstant() {
|
||||
String instantExpected = "2014-12-22T10:15:30Z";
|
||||
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
|
||||
Instant instant = Instant.now(clock);
|
||||
mockStatic(Instant.class);
|
||||
when(Instant.now()).thenReturn(instant);
|
||||
|
||||
Instant now = Instant.now();
|
||||
|
||||
assertThat(now.toString()).isEqualTo(instantExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFixedClock_whenNow_thenGetFixedInstant() {
|
||||
String instantExpected = "2014-12-22T10:15:30Z";
|
||||
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
|
||||
|
||||
Instant instant = Instant.now(clock);
|
||||
|
||||
assertThat(instant.toString()).isEqualTo(instantExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Mock;
|
||||
import mockit.MockUp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InstantWithJMockUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInstantWithJMock_whenNow_thenGetFixedInstant() {
|
||||
String instantExpected = "2014-12-21T10:15:30Z";
|
||||
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
|
||||
new MockUp<Instant>() {
|
||||
@Mock
|
||||
public Instant now() {
|
||||
return Instant.now(clock);
|
||||
}
|
||||
};
|
||||
|
||||
Instant now = Instant.now();
|
||||
|
||||
assertThat(now.toString()).isEqualTo(instantExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInstantWithExpectations_whenNow_thenGetFixedInstant() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-23T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
Instant instantExpected = Instant.now(clock);
|
||||
new Expectations(Instant.class) {
|
||||
{
|
||||
Instant.now();
|
||||
result = instantExpected;
|
||||
}
|
||||
};
|
||||
|
||||
Instant now = Instant.now();
|
||||
|
||||
assertThat(now).isEqualTo(instantExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ LocalDateTime.class })
|
||||
public class LocalDateTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeMock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
LocalDateTime dateTime = LocalDateTime.now(clock);
|
||||
mockStatic(LocalDateTime.class);
|
||||
when(LocalDateTime.now()).thenReturn(dateTime);
|
||||
String dateTimeExpected = "2014-12-22T10:15:30";
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFixedClock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
String dateTimeExpected = "2014-12-22T10:15:30";
|
||||
|
||||
LocalDateTime dateTime = LocalDateTime.now(clock);
|
||||
|
||||
assertThat(dateTime).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Mock;
|
||||
import mockit.MockUp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LocalDateTimeWithJMockUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeWithJMock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-21T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
new MockUp<LocalDateTime>() {
|
||||
@Mock
|
||||
public LocalDateTime now() {
|
||||
return LocalDateTime.now(clock);
|
||||
}
|
||||
};
|
||||
String dateTimeExpected = "2014-12-21T10:15:30";
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeWithExpectations_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-23T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
LocalDateTime dateTimeExpected = LocalDateTime.now(clock);
|
||||
new Expectations(LocalDateTime.class) {
|
||||
{
|
||||
LocalDateTime.now();
|
||||
result = dateTimeExpected;
|
||||
}
|
||||
};
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user