|
26 | 26 | import java.io.IOException; |
27 | 27 | import java.time.Duration; |
28 | 28 | import java.time.Instant; |
| 29 | +import java.time.temporal.ChronoUnit; |
29 | 30 | import java.util.concurrent.TimeUnit; |
30 | 31 |
|
31 | 32 | import org.apache.commons.lang3.AbstractLangTest; |
32 | 33 | import org.apache.commons.lang3.math.NumberUtils; |
33 | 34 | import org.junit.jupiter.api.Test; |
| 35 | +import org.junitpioneer.jupiter.SetSystemProperty; |
| 36 | +import org.junitpioneer.jupiter.SetSystemProperty.SetSystemProperties; |
34 | 37 |
|
35 | 38 | /** |
36 | 39 | * Tests {@link DurationUtils}. |
37 | 40 | */ |
38 | 41 | class DurationUtilsTest extends AbstractLangTest { |
39 | 42 |
|
| 43 | + @Test |
| 44 | + @SetSystemProperties({ |
| 45 | + @SetSystemProperty(key = "Seconds1", value = "1"), |
| 46 | + @SetSystemProperty(key = "Seconds2", value = "9223372036854775807") }) // Long.MAX_VALUE |
| 47 | + void testGet() { |
| 48 | + // ChronoUnit.SECONDS |
| 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 | + // ChronoUnit.MILLIS |
| 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") }) // Long.MAX_VALUE |
| 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 | + |
40 | 71 | @Test |
41 | 72 | void testGetNanosOfMiili() { |
42 | 73 | assertEquals(0, DurationUtils.getNanosOfMiili(null)); |
@@ -65,6 +96,17 @@ void testGetNanosOfMilli() { |
65 | 96 | assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_001))); |
66 | 97 | } |
67 | 98 |
|
| 99 | + @Test |
| 100 | + @SetSystemProperties({ |
| 101 | + @SetSystemProperty(key = "Seconds1", value = "1"), |
| 102 | + @SetSystemProperty(key = "Seconds2", value = "9223372036854775807") }) // Long.MAX_VALUE |
| 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 | + |
68 | 110 | @Test |
69 | 111 | void testIsPositive() { |
70 | 112 | assertFalse(DurationUtils.isPositive(Duration.ZERO)); |
|
0 commit comments