blob: f8ead4317044c252ef7b17ad13a746e25d71d7f1 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.time;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.AbstractLangTest;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.jupiter.api.Test;
/**
* Tests {@link DurationUtils}.
*/
public class DurationUtilsTest extends AbstractLangTest {
@Test
public void testGetNanosOfMiili() {
assertEquals(0, DurationUtils.getNanosOfMiili(null));
assertEquals(0, DurationUtils.getNanosOfMiili(Duration.ZERO));
assertEquals(1, DurationUtils.getNanosOfMiili(Duration.ofNanos(1)));
assertEquals(10, DurationUtils.getNanosOfMiili(Duration.ofNanos(10)));
assertEquals(100, DurationUtils.getNanosOfMiili(Duration.ofNanos(100)));
assertEquals(1_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000)));
assertEquals(10_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(10_000)));
assertEquals(100_000, DurationUtils.getNanosOfMiili(Duration.ofNanos(100_000)));
assertEquals(0, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000_000)));
assertEquals(1, DurationUtils.getNanosOfMiili(Duration.ofNanos(1_000_001)));
}
@Test
public void testGetNanosOfMilli() {
assertEquals(0, DurationUtils.getNanosOfMilli(null));
assertEquals(0, DurationUtils.getNanosOfMilli(Duration.ZERO));
assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1)));
assertEquals(10, DurationUtils.getNanosOfMilli(Duration.ofNanos(10)));
assertEquals(100, DurationUtils.getNanosOfMilli(Duration.ofNanos(100)));
assertEquals(1_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000)));
assertEquals(10_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(10_000)));
assertEquals(100_000, DurationUtils.getNanosOfMilli(Duration.ofNanos(100_000)));
assertEquals(0, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_000)));
assertEquals(1, DurationUtils.getNanosOfMilli(Duration.ofNanos(1_000_001)));
}
@Test
public void testIsPositive() {
assertFalse(DurationUtils.isPositive(Duration.ZERO));
assertFalse(DurationUtils.isPositive(Duration.ofMillis(-1)));
assertTrue(DurationUtils.isPositive(Duration.ofMillis(1)));
}
@Test
public void testLongToIntRangeFit() {
assertEquals(0, DurationUtils.LONG_TO_INT_RANGE.fit(0L));
//
assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE));
assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE - 1));
assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MIN_VALUE - 2));
assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE));
assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE + 1));
assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(NumberUtils.LONG_INT_MAX_VALUE + 2));
//
assertEquals(Integer.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(Long.MIN_VALUE));
assertEquals(Integer.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit(Long.MAX_VALUE));
//
assertEquals(Short.MIN_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit((long) Short.MIN_VALUE));
assertEquals(Short.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit((long) Short.MAX_VALUE));
}
@Test
public void testOfConsumer() {
assertTrue(DurationUtils.of(start -> assertTrue(start.compareTo(Instant.now()) <= 0)).compareTo(Duration.ZERO) >= 0);
}
@Test
public void testOfRunnble() {
assertTrue(DurationUtils.of(this::testSince).compareTo(Duration.ZERO) >= 0);
}
@Test
public void testOfRunnbleThrowing() {
assertThrows(IOException.class, () -> DurationUtils.of(() -> {
throw new IOException();
}));
}
@Test
public void testSince() {
assertTrue(DurationUtils.since(Instant.EPOCH).compareTo(Duration.ZERO) >= 0);
assertTrue(DurationUtils.since(Instant.MIN).compareTo(Duration.ZERO) >= 0);
assertTrue(DurationUtils.since(Instant.MAX).compareTo(Duration.ZERO) <= 0);
}
@Test
public void testToDuration() {
assertEquals(Duration.ofDays(1), DurationUtils.toDuration(1, TimeUnit.DAYS));
assertEquals(Duration.ofHours(1), DurationUtils.toDuration(1, TimeUnit.HOURS));
assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1_000, TimeUnit.MICROSECONDS));
assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1, TimeUnit.MILLISECONDS));
assertEquals(Duration.ofMinutes(1), DurationUtils.toDuration(1, TimeUnit.MINUTES));
assertEquals(Duration.ofNanos(1), DurationUtils.toDuration(1, TimeUnit.NANOSECONDS));
assertEquals(Duration.ofSeconds(1), DurationUtils.toDuration(1, TimeUnit.SECONDS));
assertEquals(1, DurationUtils.toDuration(1, TimeUnit.MILLISECONDS).toMillis());
assertEquals(-1, DurationUtils.toDuration(-1, TimeUnit.MILLISECONDS).toMillis());
assertEquals(0, DurationUtils.toDuration(0, TimeUnit.SECONDS).toMillis());
}
@Test
public void testToMillisInt() {
assertEquals(0, DurationUtils.toMillisInt(Duration.ZERO));
assertEquals(1, DurationUtils.toMillisInt(Duration.ofMillis(1)));
//
assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(Integer.MIN_VALUE)));
assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(Integer.MAX_VALUE)));
assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MAX_VALUE + 1)));
assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MAX_VALUE + 2)));
assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MIN_VALUE - 1)));
assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofMillis(NumberUtils.LONG_INT_MIN_VALUE - 2)));
//
assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MIN_VALUE)));
assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MAX_VALUE)));
}
@Test
public void testToMillisIntNullDuration() {
assertThrows(NullPointerException.class, () -> DurationUtils.toMillisInt(null));
}
@Test
public void testZeroIfNull() {
assertEquals(Duration.ZERO, DurationUtils.zeroIfNull(null));
assertEquals(Duration.ofDays(1), DurationUtils.zeroIfNull(Duration.ofDays(1)));
}
}