|
| 1 | +package dev.voidframework.core.utils; |
| 2 | + |
| 3 | +import com.esotericsoftware.kryo.Kryo; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.MethodOrderer; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.TestMethodOrder; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +import java.lang.reflect.Constructor; |
| 13 | +import java.lang.reflect.InvocationTargetException; |
| 14 | +import java.math.BigDecimal; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +@TestMethodOrder(MethodOrderer.MethodName.class) |
| 21 | +final class KryoUtilsTest { |
| 22 | + |
| 23 | + static Stream<Arguments> getDeserializeArguments() { |
| 24 | + return Stream.of( |
| 25 | + Arguments.of(new byte[]{1, -14, 20}, Integer.class, 1337), |
| 26 | + Arguments.of(new byte[]{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, -28}, String.class, "Hello World"), |
| 27 | + Arguments.of(new byte[]{1, 12, 2, 10, 0}, Optional.class, Optional.of(BigDecimal.TEN)), |
| 28 | + Arguments.of(new byte[]{1, 12, 3, 8, 0, 0}, Optional.class, Optional.of(BigDecimal.valueOf(2048))), |
| 29 | + Arguments.of(new byte[]{1, 2, 3, 65, 112, 112, 108, -27, 3, 66, 97, 110, 97, 110, -31}, List.class, List.of("Apple", "Banana")), |
| 30 | + Arguments.of(new byte[]{1, 1, 3, 65, 112, 112, 108, -27}, Set.class, Set.of("Apple")), |
| 31 | + Arguments.of(new byte[0], String.class, null), |
| 32 | + Arguments.of(new byte[]{0}, Set.class, null)); |
| 33 | + } |
| 34 | + |
| 35 | + static Stream<Arguments> getDeserializeWithoutExceptionArguments() { |
| 36 | + return Stream.of( |
| 37 | + Arguments.of(new byte[]{1, -14, 20}, Integer.class, 1337), |
| 38 | + Arguments.of(new byte[]{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, -28}, String.class, "Hello World"), |
| 39 | + Arguments.of(new byte[]{1, 12, 2, 10, 0}, Optional.class, Optional.of(BigDecimal.TEN)), |
| 40 | + Arguments.of(new byte[]{1, 12, 3, 8, 0, 0}, Optional.class, Optional.of(BigDecimal.valueOf(2048))), |
| 41 | + Arguments.of(new byte[]{1, 12, 3, 8, 0, 0}, TestDTO.class, null), // "null" because no serializer registered for "TestDTO" |
| 42 | + Arguments.of(new byte[]{1, 2, 3, 65, 112, 112, 108, -27, 3, 66, 97, 110, 97, 110, -31}, List.class, List.of("Apple", "Banana")), |
| 43 | + Arguments.of(new byte[]{1, 1, 3, 65, 112, 112, 108, -27}, Set.class, Set.of("Apple")), |
| 44 | + Arguments.of(new byte[0], String.class, null), |
| 45 | + Arguments.of(new byte[]{0}, Set.class, null)); |
| 46 | + } |
| 47 | + |
| 48 | + static Stream<Arguments> getSerializeArguments() { |
| 49 | + return Stream.of( |
| 50 | + Arguments.of(1337, new byte[]{1, -14, 20}), |
| 51 | + Arguments.of("Hello World", new byte[]{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, -28}), |
| 52 | + Arguments.of(Optional.of(BigDecimal.TEN), new byte[]{1, 12, 2, 10, 0}), |
| 53 | + Arguments.of(Optional.of(BigDecimal.valueOf(2048)), new byte[]{1, 12, 3, 8, 0, 0}), |
| 54 | + Arguments.of(new TestDTO("Clémence"), new byte[]{1, -119, 67, 108, -61, -87, 109, 101, 110, 99, 101}), |
| 55 | + Arguments.of(List.of("Apple", "Banana"), new byte[]{1, 2, 3, 65, 112, 112, 108, -27, 3, 66, 97, 110, 97, 110, -31}), |
| 56 | + Arguments.of(Set.of("Apple"), new byte[]{1, 1, 3, 65, 112, 112, 108, -27}), |
| 57 | + Arguments.of(null, new byte[]{0})); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void constructor() throws NoSuchMethodException { |
| 62 | + |
| 63 | + // Act |
| 64 | + final Constructor<KryoUtils> constructor = KryoUtils.class.getDeclaredConstructor(); |
| 65 | + constructor.setAccessible(true); |
| 66 | + |
| 67 | + final InvocationTargetException exception = Assertions.assertThrows(InvocationTargetException.class, constructor::newInstance); |
| 68 | + |
| 69 | + // Assert |
| 70 | + Assertions.assertNotNull(exception.getCause()); |
| 71 | + Assertions.assertEquals("This is a utility class and cannot be instantiated", exception.getCause().getMessage()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void kryo() { |
| 76 | + |
| 77 | + // Act |
| 78 | + final Kryo kryo = KryoUtils.kryo(); |
| 79 | + |
| 80 | + // Assert |
| 81 | + Assertions.assertNotNull(kryo); |
| 82 | + } |
| 83 | + |
| 84 | + @ParameterizedTest |
| 85 | + @MethodSource("getDeserializeArguments") |
| 86 | + void deserialize(final byte[] toDeserialize, final Class<?> outputClassType, final Object expected) { |
| 87 | + |
| 88 | + // Act |
| 89 | + final Object deserializedObject = KryoUtils.deserialize(toDeserialize, outputClassType); |
| 90 | + |
| 91 | + // Assert |
| 92 | + Assertions.assertEquals(expected, deserializedObject); |
| 93 | + } |
| 94 | + |
| 95 | + @ParameterizedTest |
| 96 | + @MethodSource("getDeserializeWithoutExceptionArguments") |
| 97 | + void deserializeWithoutException(final byte[] toDeserialize, final Class<?> outputClassType, final Object expected) { |
| 98 | + |
| 99 | + // Act |
| 100 | + final Object deserializedObject = KryoUtils.deserializeWithoutException(toDeserialize, outputClassType); |
| 101 | + |
| 102 | + // Assert |
| 103 | + Assertions.assertEquals(expected, deserializedObject); |
| 104 | + } |
| 105 | + |
| 106 | + @ParameterizedTest |
| 107 | + @MethodSource("getSerializeArguments") |
| 108 | + void serialize(final Object toSerialize, final byte[] expected) { |
| 109 | + |
| 110 | + // Act |
| 111 | + final byte[] serializedContent = KryoUtils.serialize(toSerialize); |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assertions.assertArrayEquals(expected, serializedContent); |
| 115 | + } |
| 116 | + |
| 117 | + @ParameterizedTest |
| 118 | + @MethodSource("getSerializeArguments") |
| 119 | + void serializeWithoutException(final Object toSerialize, final byte[] expected) { |
| 120 | + |
| 121 | + // Act |
| 122 | + final byte[] serializedContent = KryoUtils.serializeWithoutException(toSerialize); |
| 123 | + |
| 124 | + // Assert |
| 125 | + Assertions.assertArrayEquals(expected, serializedContent); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test DTO. |
| 130 | + */ |
| 131 | + public static final class TestDTO { |
| 132 | + |
| 133 | + final String name; |
| 134 | + |
| 135 | + public TestDTO(final String name) { |
| 136 | + |
| 137 | + this.name = name; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments