|
11 | 11 | import java.time.temporal.TemporalAccessor; |
12 | 12 | import java.util.regex.Pattern; |
13 | 13 |
|
| 14 | +import static java.util.concurrent.TimeUnit.DAYS; |
| 15 | +import static java.util.concurrent.TimeUnit.MINUTES; |
14 | 16 | import static org.assertj.core.api.Assertions.assertThat; |
15 | 17 | import static org.assertj.core.api.Assertions.assertThatCode; |
16 | 18 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
17 | 19 |
|
18 | 20 | public class TimeTest { |
19 | 21 |
|
20 | 22 | private static final Pattern RE_TIME_BETWEEN = Pattern.compile("[0-2][0-9]:[0-5][0-9]:[0-5][0-9]"); |
21 | | - private static final long NANOSECONDS_IN_DAY = 24L * 60 * 60 * 1000 * 1000_000L; |
22 | | - private static final long NANOSECONDS_IN_MINUTE = 60 * 1000 * 1000_000L; |
| 23 | + private static final long SECONDS_IN_DAY = DAYS.toSeconds(1); |
| 24 | + private static final long SECONDS_IN_MINUTE = MINUTES.toSeconds(1); |
23 | 25 | private final Faker faker = new Faker(); |
24 | 26 |
|
25 | 27 | @Test |
@@ -97,14 +99,15 @@ void testBetweenThenLargerThanNow() { |
97 | 99 | @RepeatedTest(10) |
98 | 100 | void testBetweenWithMask() { |
99 | 101 | String pattern = "HH:mm:ss"; |
100 | | - LocalTime now = LocalTime.ofNanoOfDay((long) (Math.random() * (NANOSECONDS_IN_DAY - NANOSECONDS_IN_MINUTE - 1))); |
| 102 | + long moment = faker.random().nextLong(0, SECONDS_IN_DAY - SECONDS_IN_MINUTE); |
| 103 | + LocalTime now = LocalTime.ofSecondOfDay(moment); |
101 | 104 | LocalTime then = now.plusMinutes(1); |
102 | 105 |
|
103 | 106 | String result = faker.time().between(now, then, pattern); |
104 | 107 | assertThat(result).matches(RE_TIME_BETWEEN); |
105 | 108 | TemporalAccessor timeBetween = DateTimeFormatter.ofPattern(pattern).parse(result); |
106 | | - assertThat(timeBetween.query(LocalTime::from)).isAfter(now.minusSeconds(1)); |
107 | | - assertThat(timeBetween.query(LocalTime::from)).isBefore(then.plusSeconds(1)); |
| 109 | + assertThat(timeBetween.query(LocalTime::from)).isAfterOrEqualTo(now); |
| 110 | + assertThat(timeBetween.query(LocalTime::from)).isBeforeOrEqualTo(then); |
108 | 111 | } |
109 | 112 |
|
110 | 113 | @Test |
|
0 commit comments