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.apache.commons.lang3.LangAssertions.assertNullPointerException;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.io.IOException;
27 import java.time.Duration;
28 import java.time.Instant;
29 import java.time.temporal.ChronoUnit;
30 import java.util.concurrent.TimeUnit;
31
32 import org.apache.commons.lang3.AbstractLangTest;
33 import org.apache.commons.lang3.math.NumberUtils;
34 import org.junit.jupiter.api.Test;
35 import org.junitpioneer.jupiter.SetSystemProperty;
36 import org.junitpioneer.jupiter.SetSystemProperty.SetSystemProperties;
37
38
39
40
41 class DurationUtilsTest extends AbstractLangTest {
42
43 @Test
44 @SetSystemProperties({
45 @SetSystemProperty(key = "Seconds1", value = "1"),
46 @SetSystemProperty(key = "Seconds2", value = "9223372036854775807") })
47 void testGet() {
48
49 assertEquals(Duration.ofSeconds(0), DurationUtils.get(null, ChronoUnit.SECONDS, 0));
50 assertEquals(Duration.ofSeconds(0), DurationUtils.get("", ChronoUnit.SECONDS, 0));
51 assertEquals(Duration.ofSeconds(1), DurationUtils.get("Seconds1", ChronoUnit.SECONDS, 0));
52 assertEquals(Duration.ofSeconds(Long.MAX_VALUE), DurationUtils.get("Seconds2", ChronoUnit.SECONDS, 0));
53
54 assertEquals(Duration.ofMillis(0), DurationUtils.get(null, ChronoUnit.MILLIS, 0));
55 assertEquals(Duration.ofMillis(0), DurationUtils.get("", ChronoUnit.MILLIS, 0));
56 assertEquals(Duration.ofMillis(1), DurationUtils.get("Seconds1", ChronoUnit.MILLIS, 0));
57 assertEquals(Duration.ofMillis(Long.MAX_VALUE), DurationUtils.get("Seconds2", ChronoUnit.MILLIS, 0));
58 }
59
60 @Test
61 @SetSystemProperties({
62 @SetSystemProperty(key = "Seconds1", value = "1"),
63 @SetSystemProperty(key = "Seconds2", value = "9223372036854775807") })
64 void testGetMilliseconds() {
65 assertEquals(Duration.ofMillis(0), DurationUtils.getMillis(null, 0));
66 assertEquals(Duration.ofMillis(0), DurationUtils.getMillis("", 0));
67 assertEquals(Duration.ofMillis(1), DurationUtils.getMillis("Seconds1", 0));
68 assertEquals(Duration.ofMillis(Long.MAX_VALUE), DurationUtils.getMillis("Seconds2", 0));
69 }
70
71 @Test
72 void testGetNanosOfMiili() {
73 assertEquals(0, DurationUtils.getNanosOfMiili(null));
74 assertEquals(0, DurationUtils.getNanosOfMiili(Duration.ZERO));
75 assertEquals(1, DurationUtils.getNanosOfMiili(Duration.ofNanos(1)));
76 assertEquals(10, DurationUtils.getNanosOfMiili(Duration.ofNanos(10)));
77 assertEquals(100, DurationUtils.getNanosOfMiili(Duration.ofNanos(100)));
78 assertEquals(1_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000)));
79 assertEquals(10_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(10_000)));
80 assertEquals(100_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(100_000)));
81 assertEquals(0, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000_000)));
82 assertEquals(1, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000_001)));
83 }
84
85 @Test
86 void testGetNanosOfMilli() {
87 assertEquals(0, DurationUtils.getNanosOfMilli(null));
88 assertEquals(0, DurationUtils.getNanosOfMilli(Duration.ZERO));
89 assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1)));
90 assertEquals(10, DurationUtils.getNanosOfMilli(Duration.ofNanos(10)));
91 assertEquals(100, DurationUtils.getNanosOfMilli(Duration.ofNanos(100)));
92 assertEquals(1_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000)));
93 assertEquals(10_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(10_000)));
94 assertEquals(100_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(100_000)));
95 assertEquals(0, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_000)));
96 assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_001)));
97 }
98
99 @Test
100 @SetSystemProperties({
101 @SetSystemProperty(key = "Seconds1", value = "1"),
102 @SetSystemProperty(key = "Seconds2", value = "9223372036854775807") })
103 void testGetSeconds() {
104 assertEquals(Duration.ofSeconds(0), DurationUtils.getSeconds(null, 0));
105 assertEquals(Duration.ofSeconds(0), DurationUtils.getSeconds("", 0));
106 assertEquals(Duration.ofSeconds(1), DurationUtils.getSeconds("Seconds1", 0));
107 assertEquals(Duration.ofSeconds(Long.MAX_VALUE), DurationUtils.getSeconds("Seconds2", 0));
108 }
109
110 @Test
111 void testIsPositive() {
112 assertFalse(DurationUtils.isPositive(Duration.ZERO));
113 assertFalse(DurationUtils.isPositive(Duration.ofMillis(-1)));
114 assertTrue(DurationUtils.isPositive(Duration.ofMillis(1)));
115 }
116
117 @Test
118 void testLongToIntRangeFit() {
119 assertEquals(0, DurationUtils.LONG_TO_INT_RANGE.fit(0L));
120
121 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE));
122 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE - 1));
123 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE - 2));
124 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE));
125 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE + 1));
126 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE + 2));
127
128 assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(Long.MIN_VALUE));
129 assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(Long.MAX_VALUE));
130
131 assertEquals(Short.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit((long) Short.MIN_VALUE));
132 assertEquals(Short.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit((long) Short.MAX_VALUE));
133 }
134
135 @Test
136 void testOfConsumer() {
137 assertTrue(DurationUtils.of(start -> assertTrue(start.compareTo(Instant.now()) <= 0)).compareTo(Duration.ZERO) >= 0);
138 final Instant before = Instant.now();
139 DurationUtils.of(start -> assertTrue(start.compareTo(before) >= 0));
140 }
141
142 @Test
143 void testOfRunnble() {
144 assertTrue(DurationUtils.of(this::testSince).compareTo(Duration.ZERO) >= 0);
145 }
146
147 @Test
148 void testOfRunnbleThrowing() {
149 assertThrows(IOException.class, () -> DurationUtils.of(() -> {
150 throw new IOException();
151 }));
152 }
153
154 @Test
155 void testSince() {
156 assertTrue(DurationUtils.since(Instant.EPOCH).compareTo(Duration.ZERO) >= 0);
157 assertTrue(DurationUtils.since(Instant.MIN).compareTo(Duration.ZERO) >= 0);
158 assertTrue(DurationUtils.since(Instant.MAX).compareTo(Duration.ZERO) <= 0);
159 }
160
161 @Test
162 void testToDuration() {
163 assertEquals(Duration.ofDays(1), DurationUtils.toDuration(1, TimeUnit.DAYS));
164 assertEquals(Duration.ofHours(1), DurationUtils.toDuration(1, TimeUnit.HOURS));
165 assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1_000, TimeUnit.MICROSECONDS));
166 assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1, TimeUnit.MILLISECONDS));
167 assertEquals(Duration.ofMinutes(1), DurationUtils.toDuration(1, TimeUnit.MINUTES));
168 assertEquals(Duration.ofNanos(1), DurationUtils.toDuration(1, TimeUnit.NANOSECONDS));
169 assertEquals(Duration.ofSeconds(1), DurationUtils.toDuration(1, TimeUnit.SECONDS));
170 assertEquals(1, DurationUtils.toDuration(1, TimeUnit.MILLISECONDS).toMillis());
171 assertEquals(-1, DurationUtils.toDuration(-1, TimeUnit.MILLISECONDS).toMillis());
172 assertEquals(0, DurationUtils.toDuration(0, TimeUnit.SECONDS).toMillis());
173 }
174
175 @Test
176 void testToMillisInt() {
177 assertEquals(0, DurationUtils.toMillisInt(Duration.ZERO));
178 assertEquals(1, DurationUtils.toMillisInt(Duration.ofMillis(1)));
179
180 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(Integer.MIN_VALUE)));
181 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(Integer.MAX_VALUE)));
182 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MAX_VALUE + 1)));
183 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MAX_VALUE + 2)));
184 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MIN_VALUE - 1)));
185 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MIN_VALUE - 2)));
186
187 assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MIN_VALUE)));
188 assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MAX_VALUE)));
189 }
190
191 @Test
192 void testToMillisIntNullDuration() {
193 assertNullPointerException(() -> DurationUtils.toMillisInt(null));
194 }
195
196 @Test
197 void testZeroIfNull() {
198 assertEquals(Duration.ZERO, DurationUtils.zeroIfNull(null));
199 assertEquals(Duration.ofDays(1), DurationUtils.zeroIfNull(Duration.ofDays(1)));
200 }
201 }