1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3.time;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.IOException;
26 import java.time.Duration;
27 import java.time.Instant;
28 import java.util.concurrent.TimeUnit;
29
30 import org.apache.commons.lang3.AbstractLangTest;
31 import org.apache.commons.lang3.math.NumberUtils;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class DurationUtilsTest extends AbstractLangTest {
38
39 @Test
40 public void testGetNanosOfMiili() {
41 assertEquals(0, DurationUtils.getNanosOfMiili(null));
42 assertEquals(0, DurationUtils.getNanosOfMiili(Duration.ZERO));
43 assertEquals(1, DurationUtils.getNanosOfMiili(Duration.ofNanos(1)));
44 assertEquals(10, DurationUtils.getNanosOfMiili(Duration.ofNanos(10)));
45 assertEquals(100, DurationUtils.getNanosOfMiili(Duration.ofNanos(100)));
46 assertEquals(1_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000)));
47 assertEquals(10_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(10_000)));
48 assertEquals(100_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(100_000)));
49 assertEquals(0, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000_000)));
50 assertEquals(1, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000_001)));
51 }
52
53 @Test
54 public void testGetNanosOfMilli() {
55 assertEquals(0, DurationUtils.getNanosOfMilli(null));
56 assertEquals(0, DurationUtils.getNanosOfMilli(Duration.ZERO));
57 assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1)));
58 assertEquals(10, DurationUtils.getNanosOfMilli(Duration.ofNanos(10)));
59 assertEquals(100, DurationUtils.getNanosOfMilli(Duration.ofNanos(100)));
60 assertEquals(1_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000)));
61 assertEquals(10_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(10_000)));
62 assertEquals(100_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(100_000)));
63 assertEquals(0, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_000)));
64 assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_001)));
65 }
66
67 @Test
68 public void testIsPositive() {
69 assertFalse(DurationUtils.isPositive(Duration.ZERO));
70 assertFalse(DurationUtils.isPositive(Duration.ofMillis(-1)));
71 assertTrue(DurationUtils.isPositive(Duration.ofMillis(1)));
72 }
73
74 @Test
75 public void testLongToIntRangeFit() {
76 assertEquals(0, DurationUtils.LONG_TO_INT_RANGE.fit(0L));
77
78 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE));
79 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE - 1));
80 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE - 2));
81 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE));
82 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE + 1));
83 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE + 2));
84
85 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(Long.MIN_VALUE));
86 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(Long.MAX_VALUE));
87
88 assertEquals(Short.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit((long) Short.MIN_VALUE));
89 assertEquals(Short.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit((long) Short.MAX_VALUE));
90 }
91
92 @Test
93 public void testOfConsumer() {
94 assertTrue(DurationUtils.of(start -> assertTrue(start.compareTo(Instant.now()) <= 0)).compareTo(Duration.ZERO) >= 0);
95 }
96
97 @Test
98 public void testOfRunnble() {
99 assertTrue(DurationUtils.of(this::testSince).compareTo(Duration.ZERO) >= 0);
100 }
101
102 @Test
103 public void testOfRunnbleThrowing() {
104 assertThrows(IOException.class, () -> DurationUtils.of(() -> {
105 throw new IOException();
106 }));
107 }
108
109 @Test
110 public void testSince() {
111 assertTrue(DurationUtils.since(Instant.EPOCH).compareTo(Duration.ZERO) >= 0);
112 assertTrue(DurationUtils.since(Instant.MIN).compareTo(Duration.ZERO) >= 0);
113 assertTrue(DurationUtils.since(Instant.MAX).compareTo(Duration.ZERO) <= 0);
114 }
115
116 @Test
117 public void testToDuration() {
118 assertEquals(Duration.ofDays(1), DurationUtils.toDuration(1, TimeUnit.DAYS));
119 assertEquals(Duration.ofHours(1), DurationUtils.toDuration(1, TimeUnit.HOURS));
120 assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1_000, TimeUnit.MICROSECONDS));
121 assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1, TimeUnit.MILLISECONDS));
122 assertEquals(Duration.ofMinutes(1), DurationUtils.toDuration(1, TimeUnit.MINUTES));
123 assertEquals(Duration.ofNanos(1), DurationUtils.toDuration(1, TimeUnit.NANOSECONDS));
124 assertEquals(Duration.ofSeconds(1), DurationUtils.toDuration(1, TimeUnit.SECONDS));
125 assertEquals(1, DurationUtils.toDuration(1, TimeUnit.MILLISECONDS).toMillis());
126 assertEquals(-1, DurationUtils.toDuration(-1, TimeUnit.MILLISECONDS).toMillis());
127 assertEquals(0, DurationUtils.toDuration(0, TimeUnit.SECONDS).toMillis());
128 }
129
130 @Test
131 public void testToMillisInt() {
132 assertEquals(0, DurationUtils.toMillisInt(Duration.ZERO));
133 assertEquals(1, DurationUtils.toMillisInt(Duration.ofMillis(1)));
134
135 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(Integer.MIN_VALUE)));
136 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(Integer.MAX_VALUE)));
137 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MAX_VALUE + 1)));
138 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MAX_VALUE + 2)));
139 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MIN_VALUE - 1)));
140 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MIN_VALUE - 2)));
141
142 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MIN_VALUE)));
143 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MAX_VALUE)));
144 }
145
146 @Test
147 public void testToMillisIntNullDuration() {
148 assertThrows(NullPointerException.class, () -> DurationUtils.toMillisInt(null));
149 }
150
151 @Test
152 public void testZeroIfNull() {
153 assertEquals(Duration.ZERO, DurationUtils.zeroIfNull(null));
154 assertEquals(Duration.ofDays(1), DurationUtils.zeroIfNull(Duration.ofDays(1)));
155 }
156 }