完成 EnumTools 单元测试

1.x.x
ZhouXY108 2024-12-26 11:42:13 +08:00
parent cee24b3d10
commit 6bc32ce379
3 changed files with 260 additions and 9 deletions

View File

@ -1,6 +1,6 @@
[ ] 未开始测试 - 15 (21.43%)
[-] 测试未完成 - 12 (17.14%)
[Y] 测试完成 - 22 (31.43%)
[-] 测试未完成 - 11 (15.71%)
[Y] 测试完成 - 23 (32.86%)
[x] 无需测试 - 21 (30.00%)
xyz.zhouxy.plusone.commons
@ -89,7 +89,7 @@ xyz.zhouxy.plusone.commons
ConcurrentHashMapTools.java [-]
DateTimeTools.java [-]
Enumeration.java [Y]
EnumTools.java [-]
EnumTools.java [Y]
IdGenerator.java [ ]
IdWorker.java [ ]
Numbers.java [Y]

View File

@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.commons.util;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
@ -41,14 +42,28 @@ public final class EnumTools {
* @deprecated 使 ordinal
*/
@Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
private static <E extends Enum<?>> E valueOfInternal(@Nonnull Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
E[] values = enumType.getEnumConstants();
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return values[ordinal];
}
/**
* ordinal
*
* @param <E>
* @param enumType
* @param ordinal
* @return
* @deprecated 使 ordinal
*/
@Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
return valueOfInternal(enumType, ordinal);
}
/**
* ordinal
*
@ -62,7 +77,8 @@ public final class EnumTools {
@Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用,但不删掉
@Nullable Integer ordinal, E defaultValue) {
return null == ordinal ? defaultValue : valueOf(enumType, ordinal);
AssertTools.checkNotNull(enumType);
return null == ordinal ? defaultValue : valueOfInternal(enumType, ordinal);
}
/**
@ -80,7 +96,9 @@ public final class EnumTools {
Class<E> enumType,
@Nullable Integer ordinal,
Supplier<E> defaultValue) {
return null == ordinal ? defaultValue.get() : valueOf(enumType, ordinal);
AssertTools.checkNotNull(enumType);
AssertTools.checkNotNull(defaultValue);
return null == ordinal ? defaultValue.get() : valueOfInternal(enumType, ordinal);
}
/**
@ -134,7 +152,7 @@ public final class EnumTools {
*/
@Nullable
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> enumType, @Nullable Integer ordinal) {
return checkOrdinalOrDefault(enumType, ordinal, null);
return checkOrdinalOrDefault(enumType, ordinal, (Integer) null);
}
/**
@ -163,6 +181,30 @@ public final class EnumTools {
Class<E> enumType,
@Nullable Integer ordinal,
@Nullable Integer defaultValue) {
return ordinal != null ? checkOrdinal(enumType, ordinal) : defaultValue;
AssertTools.checkNotNull(enumType);
return checkOrdinalOrGetInternal(enumType, ordinal, () -> checkOrdinalOrDefaultInternal(enumType, defaultValue, null));
}
/**
* {@code ordinal} {@code defaultValue}
*/
@Nullable
private static <E extends Enum<?>> Integer checkOrdinalOrDefaultInternal(
@Nonnull Class<E> enumType,
@Nullable Integer ordinal,
@Nullable Integer defaultValue) {
return ordinal != null
? checkOrdinal(enumType, ordinal)
: defaultValue;
}
@Nullable
private static <E extends Enum<?>> Integer checkOrdinalOrGetInternal(
@Nonnull Class<E> enumType,
@Nullable Integer ordinal,
@Nonnull Supplier<Integer> defaultValueSupplier) {
return ordinal != null
? checkOrdinal(enumType, ordinal)
: defaultValueSupplier.get();
}
}

View File

@ -0,0 +1,209 @@
package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@SuppressWarnings("deprecation")
public
class EnumToolsTests {
private enum MyEnum {
VALUE_0,
VALUE_1,
VALUE_2,
VALUE_3,
}
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3 })
void testCheckOrdinal_success(int ordinal) {
assertEquals(ordinal, EnumTools.checkOrdinal(MyEnum.class, ordinal));
}
@ParameterizedTest
@ValueSource(ints = { -1, 4 })
void testCheckOrdinal_EnumConstantNotPresentException(int ordinal) {
assertThrows(EnumConstantNotPresentException.class,
() -> EnumTools.checkOrdinal(MyEnum.class, ordinal));
}
@Test
void testCheckOrdinal_null() {
assertThrows(NullPointerException.class,
() -> EnumTools.checkOrdinal(MyEnum.class, null));
assertThrows(NullPointerException.class,
() -> EnumTools.checkOrdinal(null, 0));
assertThrows(NullPointerException.class,
() -> EnumTools.checkOrdinal(null, null));
}
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3 })
void testCheckOrdinalNullable_success(int ordinal) {
assertEquals(ordinal, EnumTools.checkOrdinalNullable(MyEnum.class, ordinal));
}
@ParameterizedTest
@ValueSource(ints = { -1, 4 })
void testCheckOrdinalNullable_EnumConstantNotPresentException(int ordinal) {
assertThrows(EnumConstantNotPresentException.class, () -> {
EnumTools.checkOrdinalNullable(MyEnum.class, ordinal);
});
}
@Test
void testCheckOrdinalNullable_null() {
assertNull(EnumTools.checkOrdinalNullable(MyEnum.class, null));
assertThrows(NullPointerException.class, () -> {
EnumTools.checkOrdinalNullable(null, 0);
});
assertThrows(NullPointerException.class, () -> {
EnumTools.checkOrdinalNullable(null, null);
});
}
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3 })
void testCheckOrdinalOrDefault_0To3(int ordinal) {
assertEquals(ordinal, EnumTools.checkOrdinalOrDefault(MyEnum.class, ordinal));
}
@ParameterizedTest
@ValueSource(ints = { -1, 4 })
void testCheckOrdinalOrDefault_EnumConstantNotPresentException(int ordinal) {
assertThrows(EnumConstantNotPresentException.class, () -> {
EnumTools.checkOrdinalOrDefault(MyEnum.class, ordinal);
});
assertThrows(EnumConstantNotPresentException.class, () -> {
EnumTools.checkOrdinalOrDefault(MyEnum.class, null, ordinal);
});
}
@Test
void testCheckOrdinalOrDefault_null() {
assertEquals(0, EnumTools.checkOrdinalOrDefault(MyEnum.class, null));
assertEquals(1, EnumTools.checkOrdinalOrDefault(MyEnum.class, null, 1));
assertNull(EnumTools.checkOrdinalOrDefault(MyEnum.class, null, null));
}
@Test
void testGetValueNullable_0To3() {
assertSame(MyEnum.VALUE_0, EnumTools.getValueNullable(MyEnum.class, 0));
assertSame(MyEnum.VALUE_1, EnumTools.getValueNullable(MyEnum.class, 1));
assertSame(MyEnum.VALUE_2, EnumTools.getValueNullable(MyEnum.class, 2));
assertSame(MyEnum.VALUE_3, EnumTools.getValueNullable(MyEnum.class, 3));
}
@ParameterizedTest
@ValueSource(ints = { -1, 4 })
void testGetValueNullable_EnumConstantNotPresentException(int ordinal) {
assertThrows(EnumConstantNotPresentException.class, () -> {
EnumTools.getValueNullable(MyEnum.class, ordinal);
});
}
@Test
void testGetValueNullable_null() {
assertNull(EnumTools.getValueNullable(MyEnum.class, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueNullable(null, 0));
assertThrows(NullPointerException.class, () -> EnumTools.getValueNullable(null, null));
}
@Test
void testGetValueOrDefault_0To3() {
assertSame(MyEnum.VALUE_0, EnumTools.getValueOrDefault(MyEnum.class, 0));
assertSame(MyEnum.VALUE_1, EnumTools.getValueOrDefault(MyEnum.class, 1));
assertSame(MyEnum.VALUE_2, EnumTools.getValueOrDefault(MyEnum.class, 2));
assertSame(MyEnum.VALUE_3, EnumTools.getValueOrDefault(MyEnum.class, 3));
assertSame(MyEnum.VALUE_0, EnumTools.getValueOrDefault(MyEnum.class, 0, () -> MyEnum.VALUE_1));
assertSame(MyEnum.VALUE_1, EnumTools.getValueOrDefault(MyEnum.class, 1, () -> MyEnum.VALUE_0));
assertSame(MyEnum.VALUE_2, EnumTools.getValueOrDefault(MyEnum.class, 2, () -> MyEnum.VALUE_0));
assertSame(MyEnum.VALUE_3, EnumTools.getValueOrDefault(MyEnum.class, 3, () -> MyEnum.VALUE_0));
assertSame(MyEnum.VALUE_0, EnumTools.getValueOrDefault(MyEnum.class, 0, () -> null));
assertSame(MyEnum.VALUE_1, EnumTools.getValueOrDefault(MyEnum.class, 1, () -> null));
assertSame(MyEnum.VALUE_2, EnumTools.getValueOrDefault(MyEnum.class, 2, () -> null));
assertSame(MyEnum.VALUE_3, EnumTools.getValueOrDefault(MyEnum.class, 3, () -> null));
}
@ParameterizedTest
@ValueSource(ints = { -1, 4 })
void testGetValueOrDefault_EnumConstantNotPresentException(int ordinal) {
assertThrows(EnumConstantNotPresentException.class, () -> {
EnumTools.getValueOrDefault(MyEnum.class, ordinal);
});
assertThrows(EnumConstantNotPresentException.class, () -> {
EnumTools.getValueOrDefault(MyEnum.class, ordinal, () -> MyEnum.VALUE_0);
});
}
@Test
void testGetValueOrDefault_null() {
assertSame(MyEnum.VALUE_0, EnumTools.getValueOrDefault(MyEnum.class, null));
assertSame(MyEnum.VALUE_0, EnumTools.getValueOrDefault(MyEnum.class, null, () -> MyEnum.VALUE_0));
assertNull(EnumTools.getValueOrDefault(MyEnum.class, null, () -> null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, -1));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, 0));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, 4));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, null, () -> MyEnum.VALUE_0));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, -1, () -> MyEnum.VALUE_1));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, 0, () -> MyEnum.VALUE_1));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, 4, () -> MyEnum.VALUE_0));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(MyEnum.class, null, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(MyEnum.class, -1, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(MyEnum.class, 0, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(MyEnum.class, 4, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, null, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, -1, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, 0, null));
assertThrows(NullPointerException.class, () -> EnumTools.getValueOrDefault(null, 4, null));
}
@Test
void testValueOf_0To3() {
assertSame(MyEnum.VALUE_0, EnumTools.valueOf(MyEnum.class, 0));
assertSame(MyEnum.VALUE_1, EnumTools.valueOf(MyEnum.class, 1));
assertSame(MyEnum.VALUE_2, EnumTools.valueOf(MyEnum.class, 2));
assertSame(MyEnum.VALUE_3, EnumTools.valueOf(MyEnum.class, 3));
assertSame(MyEnum.VALUE_0, EnumTools.valueOf(MyEnum.class, 0, MyEnum.VALUE_1));
assertSame(MyEnum.VALUE_1, EnumTools.valueOf(MyEnum.class, 1, MyEnum.VALUE_0));
assertSame(MyEnum.VALUE_2, EnumTools.valueOf(MyEnum.class, 2, MyEnum.VALUE_0));
assertSame(MyEnum.VALUE_3, EnumTools.valueOf(MyEnum.class, 3, MyEnum.VALUE_0));
assertSame(MyEnum.VALUE_0, EnumTools.valueOf(MyEnum.class, 0, null));
assertSame(MyEnum.VALUE_1, EnumTools.valueOf(MyEnum.class, 1, null));
assertSame(MyEnum.VALUE_2, EnumTools.valueOf(MyEnum.class, 2, null));
assertSame(MyEnum.VALUE_3, EnumTools.valueOf(MyEnum.class, 3, null));
}
@ParameterizedTest
@ValueSource(ints = { -1, 4 })
void testValueOf_EnumConstantNotPresentException(int ordinal) {
assertThrows(EnumConstantNotPresentException.class, () -> EnumTools.valueOf(MyEnum.class, ordinal));
assertThrows(EnumConstantNotPresentException.class, () -> EnumTools.valueOf(MyEnum.class, ordinal, MyEnum.VALUE_0));
assertThrows(EnumConstantNotPresentException.class, () -> EnumTools.valueOf(MyEnum.class, ordinal, null));
}
@Test
void testValueOf_null() {
assertThrows(NullPointerException.class, () -> EnumTools.valueOf(null, 0));
assertSame(MyEnum.VALUE_0, EnumTools.valueOf(MyEnum.class, null, MyEnum.VALUE_0));
assertNull(EnumTools.valueOf(MyEnum.class, null, null));
}
}