补充单元测试

pull/1/head
ZhouXY108 2024-12-04 01:17:42 +08:00
parent b4115aae95
commit ab318136a8
3 changed files with 437 additions and 0 deletions

View File

@ -0,0 +1,189 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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
*
* https://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 xyz.zhouxy.plusone.commons.util;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
public
class NumbersTests {
// TODO 【优化】 检查测试用例
@Test
public void sum_ShortArray_ReturnsCorrectSum() {
short[] numbers = {1, 2, 3, 4};
int result = Numbers.sum(numbers);
assertEquals(10, result);
}
@Test
public void sum_IntArray_ReturnsCorrectSum() {
int[] numbers = {1, 2, 3, 4};
long result = Numbers.sum(numbers);
assertEquals(10L, result);
}
@Test
public void sum_LongArray_ReturnsCorrectSum() {
long[] numbers = {1, 2, 3, 4};
long result = Numbers.sum(numbers);
assertEquals(10L, result);
}
@Test
public void sum_FloatArray_ReturnsCorrectSum() {
float[] numbers = {1.5f, 2.5f, 3.5f};
double result = Numbers.sum(numbers);
assertEquals(7.5, result);
}
@Test
public void sum_DoubleArray_ReturnsCorrectSum() {
double[] numbers = {1.5, 2.5, 3.5};
double result = Numbers.sum(numbers);
assertEquals(7.5, result);
}
@Test
public void sum_BigIntegerArray_ReturnsCorrectSum() {
BigInteger[] numbers = {new BigInteger("1"), new BigInteger("2"), new BigInteger("3")};
BigInteger result = Numbers.sum(numbers);
assertEquals(new BigInteger("6"), result);
}
@Test
public void sum_BigDecimalArray_ReturnsCorrectSum() {
BigDecimal[] numbers = {new BigDecimal("1.5"), new BigDecimal("2.5"), new BigDecimal("3.5")};
BigDecimal result = Numbers.sum(numbers);
assertEquals(new BigDecimal("7.5"), result);
}
@Test
public void nullToZero_ByteNotNull_ReturnsSameValue() {
Byte value = 5;
byte result = Numbers.nullToZero(value);
assertEquals(5, result);
}
@Test
public void nullToZero_ByteNull_ReturnsZero() {
Byte value = null;
byte result = Numbers.nullToZero(value);
assertEquals(0, result);
}
@Test
public void nullToZero_ShortNotNull_ReturnsSameValue() {
Short value = 5;
short result = Numbers.nullToZero(value);
assertEquals(5, result);
}
@Test
public void nullToZero_ShortNull_ReturnsZero() {
Short value = null;
short result = Numbers.nullToZero(value);
assertEquals(0, result);
}
@Test
public void nullToZero_IntegerNotNull_ReturnsSameValue() {
Integer value = 5;
int result = Numbers.nullToZero(value);
assertEquals(5, result);
}
@Test
public void nullToZero_IntegerNull_ReturnsZero() {
Integer value = null;
int result = Numbers.nullToZero(value);
assertEquals(0, result);
}
@Test
public void nullToZero_LongNotNull_ReturnsSameValue() {
Long value = 5L;
long result = Numbers.nullToZero(value);
assertEquals(5L, result);
}
@Test
public void nullToZero_LongNull_ReturnsZero() {
Long value = null;
long result = Numbers.nullToZero(value);
assertEquals(0L, result);
}
@Test
public void nullToZero_FloatNotNull_ReturnsSameValue() {
Float value = 5.0F;
float result = Numbers.nullToZero(value);
assertEquals(5.0F, result);
}
@Test
public void nullToZero_FloatNull_ReturnsZero() {
Float value = null;
float result = Numbers.nullToZero(value);
assertEquals(0.0F, result);
}
@Test
public void nullToZero_DoubleNotNull_ReturnsSameValue() {
Double value = 5.0;
double result = Numbers.nullToZero(value);
assertEquals(5.0, result);
}
@Test
public void nullToZero_DoubleNull_ReturnsZero() {
Double value = null;
double result = Numbers.nullToZero(value);
assertEquals(0.0, result);
}
@Test
public void nullToZero_BigIntegerNotNull_ReturnsSameValue() {
BigInteger value = new BigInteger("5");
BigInteger result = Numbers.nullToZero(value);
assertEquals(new BigInteger("5"), result);
}
@Test
public void nullToZero_BigIntegerNull_ReturnsZero() {
BigInteger value = null;
BigInteger result = Numbers.nullToZero(value);
assertEquals(BigInteger.ZERO, result);
}
@Test
public void nullToZero_BigDecimalNotNull_ReturnsSameValue() {
BigDecimal value = new BigDecimal("5.0");
BigDecimal result = Numbers.nullToZero(value);
assertEquals(new BigDecimal("5.0"), result);
}
@Test
public void nullToZero_BigDecimalNull_ReturnsZero() {
BigDecimal value = null;
BigDecimal result = Numbers.nullToZero(value);
assertEquals(BigDecimal.ZERO, result);
}
}

View File

@ -0,0 +1,161 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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
*
* https://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 xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import org.junit.jupiter.api.Test;
public
class OptionalToolsTests {
// TODO 【优化】 检查测试用例
@Test
void optionalOf_NullInteger_ReturnsEmptyOptionalInt() {
OptionalInt result = OptionalTools.optionalOf((Integer) null);
assertFalse(result.isPresent());
}
@Test
void optionalOf_ValidInteger_ReturnsOptionalIntWithValue() {
OptionalInt result = OptionalTools.optionalOf(10);
assertTrue(result.isPresent());
assertEquals(10, result.getAsInt());
}
@Test
void toOptionalInt_NullOptionalInteger_ReturnsEmptyOptionalInt() {
OptionalInt result = OptionalTools.toOptionalInt(Optional.ofNullable(null));
assertFalse(result.isPresent());
}
@Test
void toOptionalInt_ValidOptionalInteger_ReturnsOptionalIntWithValue() {
OptionalInt result = OptionalTools.toOptionalInt(Optional.of(10));
assertTrue(result.isPresent());
assertEquals(10, result.getAsInt());
}
@Test
void optionalOf_NullLong_ReturnsEmptyOptionalLong() {
OptionalLong result = OptionalTools.optionalOf((Long) null);
assertFalse(result.isPresent());
}
@Test
void optionalOf_ValidLong_ReturnsOptionalLongWithValue() {
OptionalLong result = OptionalTools.optionalOf(10L);
assertTrue(result.isPresent());
assertEquals(10L, result.getAsLong());
}
@Test
void toOptionalLong_NullOptionalLong_ReturnsEmptyOptionalLong() {
OptionalLong result = OptionalTools.toOptionalLong(Optional.ofNullable(null));
assertFalse(result.isPresent());
}
@Test
void toOptionalLong_ValidOptionalLong_ReturnsOptionalLongWithValue() {
OptionalLong result = OptionalTools.toOptionalLong(Optional.of(10L));
assertTrue(result.isPresent());
assertEquals(10L, result.getAsLong());
}
@Test
void optionalOf_NullDouble_ReturnsEmptyOptionalDouble() {
OptionalDouble result = OptionalTools.optionalOf((Double) null);
assertFalse(result.isPresent());
}
@Test
void optionalOf_ValidDouble_ReturnsOptionalDoubleWithValue() {
OptionalDouble result = OptionalTools.optionalOf(10.0);
assertTrue(result.isPresent());
assertEquals(10.0, result.getAsDouble(), 0.0001);
}
@Test
void toOptionalDouble_NullOptionalDouble_ReturnsEmptyOptionalDouble() {
OptionalDouble result = OptionalTools.toOptionalDouble(Optional.ofNullable(null));
assertFalse(result.isPresent());
}
@Test
void toOptionalDouble_ValidOptionalDouble_ReturnsOptionalDoubleWithValue() {
OptionalDouble result = OptionalTools.toOptionalDouble(Optional.of(10.0));
assertTrue(result.isPresent());
assertEquals(10.0, result.getAsDouble(), 0.0001);
}
@Test
void orElseNull_NullOptional_ReturnsNull() {
Object result = OptionalTools.orElseNull(Optional.ofNullable(null));
assertNull(result);
}
@Test
void orElseNull_PresentOptional_ReturnsValue() {
Object result = OptionalTools.orElseNull(Optional.of("test"));
assertEquals("test", result);
}
@Test
void toInteger_NullOptionalInt_ReturnsNull() {
Integer result = OptionalTools.toInteger(OptionalInt.empty());
assertNull(result);
}
@Test
void toInteger_PresentOptionalInt_ReturnsValue() {
Integer result = OptionalTools.toInteger(OptionalInt.of(10));
assertEquals(10, result);
}
@Test
void toLong_NullOptionalLong_ReturnsNull() {
Long result = OptionalTools.toLong(OptionalLong.empty());
assertNull(result);
}
@Test
void toLong_PresentOptionalLong_ReturnsValue() {
Long result = OptionalTools.toLong(OptionalLong.of(10L));
assertEquals(10L, result);
}
@Test
void toDouble_NullOptionalDouble_ReturnsNull() {
Double result = OptionalTools.toDouble(OptionalDouble.empty());
assertNull(result);
}
@Test
void toDouble_PresentOptionalDouble_ReturnsValue() {
Double result = OptionalTools.toDouble(OptionalDouble.of(10.0));
assertEquals(10.0, result, 0.0001);
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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
*
* https://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 xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public
class StringToolsTests {
@Test
void isNotBlank_NullString_ReturnsFalse() {
assertFalse(StringTools.isNotBlank(null));
}
@Test
void isNotBlank_EmptyString_ReturnsFalse() {
assertFalse(StringTools.isNotBlank(""));
}
@Test
void isNotBlank_WhitespaceString_ReturnsFalse() {
assertFalse(StringTools.isNotBlank(" "));
}
@Test
void isNotBlank_NonWhitespaceString_ReturnsTrue() {
assertTrue(StringTools.isNotBlank("Hello"));
}
@Test
void repeat_NullString_ThrowsException() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
StringTools.repeat(null, 2);
});
assertNull(exception.getMessage());
}
@Test
void repeat_EmptyString_ReturnsEmptyString() {
assertEquals("", StringTools.repeat("", 2));
}
@Test
void repeat_RepeatOnce_ReturnsOriginalString() {
assertEquals("Hello", StringTools.repeat("Hello", 1));
}
@Test
void repeat_RepeatMultipleTimes_ReturnsRepeatedString() {
assertEquals("HelloHelloHello", StringTools.repeat("Hello", 3));
}
@Test
void repeat_ExceedsMaxLength_ReturnsTruncatedString() {
assertEquals("HelloHello", StringTools.repeat("Hello", 2, 14));
assertEquals("HelloHel", StringTools.repeat("Hello", 2, 8));
assertEquals("He", StringTools.repeat("Hello", 2, 2));
assertEquals("", StringTools.repeat("Hello", 0, 2));
assertThrows(IllegalArgumentException.class, () -> StringTools.repeat("Hello", -1, 2));
assertThrows(IllegalArgumentException.class, () -> StringTools.repeat("Hello", 5, -1));
}
@Test
void repeat_ZeroTimes_ReturnsEmptyString() {
assertEquals("", StringTools.repeat("Hello", 0));
}
}