[duration-improvement] isNegative, isZero Tests (#14791)

This commit is contained in:
Kai Yuan 2023-09-23 02:59:43 +02:00 committed by GitHub
parent 82c4b59059
commit 8ea2cc9703
1 changed files with 24 additions and 6 deletions

View File

@ -3,9 +3,11 @@ package com.baeldung.dateapi;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
@ -19,7 +21,7 @@ public class JavaDurationUnitTest {
LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30)); LocalTime finalTime = initialTime.plus(Duration.ofSeconds(30));
long seconds = Duration.between(initialTime, finalTime) long seconds = Duration.between(initialTime, finalTime)
.getSeconds(); .getSeconds();
assertThat(seconds).isEqualTo(30); assertThat(seconds).isEqualTo(30);
} }
@ -34,6 +36,22 @@ public class JavaDurationUnitTest {
assertThat(seconds).isEqualTo(30); assertThat(seconds).isEqualTo(30);
} }
@Test
public void givenADuration_whenCallingisZeroAndisNegative_thenGetExpectedResult() {
LocalDateTime start = LocalDateTime.parse("2020-01-01T08:00:00");
LocalDateTime end = LocalDateTime.parse("2020-01-01T12:00:00");
assertFalse(Duration.between(start, end)
.isNegative());
assertTrue(Duration.between(end, start)
.isNegative());
LocalDateTime theTime = LocalDateTime.parse("2023-09-09T08:00:00");
assertTrue(Duration.between(theTime, theTime)
.isZero());
assertFalse(Duration.between(theTime, theTime)
.isNegative());
}
@Test @Test
public void test2() { public void test2() {
Instant start = Instant.parse("2017-10-03T10:15:30.00Z"); Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
@ -53,14 +71,14 @@ public class JavaDurationUnitTest {
assertEquals(1, fromMinutes.toHours()); assertEquals(1, fromMinutes.toHours());
assertEquals(120, duration.plusSeconds(60) assertEquals(120, duration.plusSeconds(60)
.getSeconds()); .getSeconds());
assertEquals(30, duration.minusSeconds(30) assertEquals(30, duration.minusSeconds(30)
.getSeconds()); .getSeconds());
assertEquals(120, duration.plus(60, ChronoUnit.SECONDS) assertEquals(120, duration.plus(60, ChronoUnit.SECONDS)
.getSeconds()); .getSeconds());
assertEquals(30, duration.minus(30, ChronoUnit.SECONDS) assertEquals(30, duration.minus(30, ChronoUnit.SECONDS)
.getSeconds()); .getSeconds());
Duration fromChar1 = Duration.parse("P1DT1H10M10.5S"); Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
Duration fromChar2 = Duration.parse("PT10M"); Duration fromChar2 = Duration.parse("PT10M");