From 395b4b98cc24914a6ab06440bbc1e5e26fcecdd8 Mon Sep 17 00:00:00 2001 From: Duckelekuuk Date: Mon, 28 Aug 2023 12:19:50 +0200 Subject: [PATCH] Add more test for argument mapper --- meteor-core/pom.xml | 18 ++- .../meteormsg/core/utils/ArgumentMapper.java | 2 + .../core/utils/ArgumentMapperTest.java | 127 ++++++++++++++++++ .../core/utils/ArgumentTransformerTest.java | 68 ---------- .../redis/RedisPacketListenerTest.java | 1 - 5 files changed, 141 insertions(+), 75 deletions(-) create mode 100644 meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentMapperTest.java delete mode 100644 meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentTransformerTest.java diff --git a/meteor-core/pom.xml b/meteor-core/pom.xml index 7f9ff48..b18eb5d 100644 --- a/meteor-core/pom.xml +++ b/meteor-core/pom.xml @@ -19,12 +19,6 @@ - - org.junit.jupiter - junit-jupiter - test - - io.netty netty-buffer @@ -35,6 +29,18 @@ meteor-common ${meteor-serializer.version} + + + org.junit.jupiter + junit-jupiter + test + + + + org.mockito + mockito-junit-jupiter + test + diff --git a/meteor-core/src/main/java/com/meteormsg/core/utils/ArgumentMapper.java b/meteor-core/src/main/java/com/meteormsg/core/utils/ArgumentMapper.java index 8074071..7a77a6e 100644 --- a/meteor-core/src/main/java/com/meteormsg/core/utils/ArgumentMapper.java +++ b/meteor-core/src/main/java/com/meteormsg/core/utils/ArgumentMapper.java @@ -3,6 +3,7 @@ import java.lang.reflect.Array; import java.lang.reflect.Method; import java.util.Map; +import java.util.Objects; public class ArgumentMapper { @@ -28,6 +29,7 @@ public static Class ensureBoxedClass(Class primitiveClass) { } public static Class resolvePrimitive(final String className) { + Objects.requireNonNull(className, "className cannot be null"); switch (className) { case "boolean": return boolean.class; diff --git a/meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentMapperTest.java b/meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentMapperTest.java new file mode 100644 index 0000000..c6b16fb --- /dev/null +++ b/meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentMapperTest.java @@ -0,0 +1,127 @@ +package com.meteormsg.core.utils; + +import com.meteormsg.core.Meteor; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.lang.reflect.Method; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +class ArgumentMapperTest { + + @ParameterizedTest + @MethodSource + void ensureBoxedClass_success(Class primitive, Class boxed) { + Class result = ArgumentMapper.ensureBoxedClass(primitive); + + assertEquals(boxed, result); + } + + static Stream ensureBoxedClass_success() { + return Stream.of( + Arguments.of(byte.class, Byte.class), + Arguments.of(boolean.class, Boolean.class), + Arguments.of(void.class, Void.class), + Arguments.of(double.class, Double.class), + Arguments.of(char.class, Character.class) + ); + } + + @Test + void ensureBoxedClass_withNull() { + assertThrowsExactly(NullPointerException.class, () -> { + ArgumentMapper.ensureBoxedClass(null); + }); + } + + @Test + void ensureBoxedClass_withNonPrimitiveClass() { + Class result = ArgumentMapper.ensureBoxedClass(Meteor.class); + + assertEquals(Meteor.class, result); + } + + + @ParameterizedTest + @ValueSource(strings = {"boolean", "byte", "short", "int", "long", "float", "double", "char", "void"}) + void testResolvePrimitive_success(String className) { + Class result = ArgumentMapper.resolvePrimitive(className); + assertNotNull(result); + assertTrue(result.isPrimitive()); + } + + @Test + void testResolvePrimitive_withFullQualifiedName() { + Class result = ArgumentMapper.resolvePrimitive("java.lang.String"); + assertNotNull(result); + assertEquals(String.class, result); + } + + @Test + void testResolvePrimitive_withSimpleClassName() { + Class result = ArgumentMapper.resolvePrimitive("String"); + assertNotNull(result); + assertEquals(String.class, result); + } + + @Test + void testResolvePrimitive_classNotFound() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + ArgumentMapper.resolvePrimitive("UnknownClass"); + }); + assertTrue(exception.getMessage().contains("Class not found: java.lang.UnknownClass")); + } + + @Test + void testResolvePrimitive_nullValue() { + Exception exception = assertThrows(NullPointerException.class, () -> { + ArgumentMapper.resolvePrimitive(null); + }); + assertEquals("className cannot be null", exception.getMessage()); + } + + + static class Example { + private void singleParamMethod(Integer integer) { } + private void multipleParamsMethod(Integer integer, String str, Double dd) { } + private void oneArrayMethod(int[] a) { } + private void optionalArrayMethod(int a, int... b) {} + } + + private static Stream provideArgumentsForTest() throws NoSuchMethodException { + Method singleParamMethod = Example.class.getDeclaredMethod("singleParamMethod", Integer.class); + Method multiParamsMethod = Example.class.getDeclaredMethod("multipleParamsMethod", Integer.class, String.class, Double.class); + Method oneArrayMethod = Example.class.getDeclaredMethod("oneArrayMethod", int[].class); + Method optionalArrayMethod = Example.class.getDeclaredMethod("optionalArrayMethod", int.class, int[].class); + + return Stream.of( + Arguments.of(singleParamMethod, new Object[]{1}, new Object[]{1}), + Arguments.of(multiParamsMethod, new Object[]{1, "test", 2.0}, new Object[]{1, "test", 2.0}), + Arguments.of(singleParamMethod, new Object[]{null}, new Object[]{null}), + Arguments.of(multiParamsMethod, new Object[]{1, null, 2.0}, new Object[]{1, null, 2.0}), + Arguments.of(oneArrayMethod, new Object[]{1, 2, 3}, new Object[]{new int[]{1, 2, 3}}), + Arguments.of(optionalArrayMethod, new Object[]{1, 2, 3, 4}, new Object[]{1, new int[]{2, 3, 4}}) + + ); + } + + @ParameterizedTest + @MethodSource("provideArgumentsForTest") + void testOverflowArguments(Method method, Object[] allArguments, Object[] expected) { + Object[] output = ArgumentMapper.overflowArguments(method, allArguments); + + assertArrayEquals(expected, output); + } + + @ParameterizedTest + @NullAndEmptySource + void testOverflowArguments_WithEmptyOrNullExceptions(Object[] allArguments) { + assertThrows(NullPointerException.class, () -> ArgumentMapper.overflowArguments(null, allArguments)); + } +} \ No newline at end of file diff --git a/meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentTransformerTest.java b/meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentTransformerTest.java deleted file mode 100644 index 4adf83e..0000000 --- a/meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentTransformerTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.meteormsg.core.utils; - -import org.junit.jupiter.api.Test; - -import java.lang.reflect.Method; - -import static org.junit.jupiter.api.Assertions.*; - -public class ArgumentTransformerTest { - - @Test - public void testExactMatch() { - Method method = getMethod("aMethodWithThreeArguments"); - Object[] arguments = new Object[]{1, 2, 3}; - Object[] transformedArguments = ArgumentMapper.overflowArguments(method, arguments); - assertArrayEquals(arguments, transformedArguments); - } - - @Test - public void testOverflow() { - Method method = getMethod("aMethodWithThreeArguments"); - Object[] arguments = new Object[]{1, 2, 3}; - Object[] transformedArguments = ArgumentMapper.overflowArguments(method, arguments); - assertArrayEquals(new Object[]{1, 2, 3}, transformedArguments); - } - - @Test - public void testOverflowWithArray() { - Method method = getMethod("aMethodWithOneArrayArgument"); - Object[] arguments = new Object[]{1, 2, 3, 4, 5}; - Object[] transformedArguments = ArgumentMapper.overflowArguments(method, arguments); - assertArrayEquals(new int[][]{{1, 2, 3, 4, 5}}, transformedArguments); - } - - @Test - public void testOverflowWithOptionalArray() { - Method method = getMethod("aMethodWithAnOptionalArrayArgument"); - Object[] arguments = new Object[]{1, 2, 3, 4, 5}; - Object[] transformedArguments = ArgumentMapper.overflowArguments(method, arguments); - assertArrayEquals(new Object[]{1, new int[]{2, 3, 4, 5}}, transformedArguments); - } - - private Method getMethod(String name) { - for (Method declaredMethod : getClass().getDeclaredMethods()) { - if (declaredMethod.getName().equals(name)) - return declaredMethod; - } - fail("Method not found"); - return null; - } - - void aMethodWithThreeArguments(int a, int b, int c) { - - } - - void aMethodWithOneArgument(int a) { - - } - - void aMethodWithOneArrayArgument(int[] a) { - - } - - void aMethodWithAnOptionalArrayArgument(int a, int... b) { - - } - -} \ No newline at end of file diff --git a/meteor-jedis/src/test/java/com/meteormsg/transport/redis/RedisPacketListenerTest.java b/meteor-jedis/src/test/java/com/meteormsg/transport/redis/RedisPacketListenerTest.java index 000f788..5e137c6 100644 --- a/meteor-jedis/src/test/java/com/meteormsg/transport/redis/RedisPacketListenerTest.java +++ b/meteor-jedis/src/test/java/com/meteormsg/transport/redis/RedisPacketListenerTest.java @@ -57,7 +57,6 @@ void stop() { @Test void getCustomSubscribedChannels_ThenSuccess() { String topic = "test"; - String message = "message"; RedisPacketListener redisPacketListener = new RedisPacketListener(subscriptionHandler, topic, Logger.getAnonymousLogger());