From 4cf401f0a820034e574b71273b20d2af34a9dbee Mon Sep 17 00:00:00 2001 From: Duckelekuuk Date: Tue, 29 Aug 2023 19:01:37 +0200 Subject: [PATCH] Improved code coverage in core --- .../executor/ImplementationWrapperTest.java | 95 +++++++++++++++++++ .../meteor/core/utils/ArgumentMapperTest.java | 17 ++++ pom.xml | 2 +- 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 meteor-core/src/test/java/dev/pixelib/meteor/core/executor/ImplementationWrapperTest.java diff --git a/meteor-core/src/test/java/dev/pixelib/meteor/core/executor/ImplementationWrapperTest.java b/meteor-core/src/test/java/dev/pixelib/meteor/core/executor/ImplementationWrapperTest.java new file mode 100644 index 0000000..d1f4956 --- /dev/null +++ b/meteor-core/src/test/java/dev/pixelib/meteor/core/executor/ImplementationWrapperTest.java @@ -0,0 +1,95 @@ +package dev.pixelib.meteor.core.executor; + +import dev.pixelib.meteor.core.transport.packets.InvocationDescriptor; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.*; + +class ImplementationWrapperTest { + + @Test + void invokeOn_success() throws NoSuchMethodException { + + ImplementationWrapper wrapper = new ImplementationWrapper(new MathFunctionImplementation(), "namespace"); + Class[] argTypes = new Class[] { int.class, int.class }; + + InvocationDescriptor descriptor = new InvocationDescriptor("math", MathFunctionImplementation.class, "add", new Object[]{1, 2},argTypes, int.class); + assertEquals(3, wrapper.invokeOn(descriptor, int.class)); + } + + @Test + void invokeOn_withBoxParams() throws NoSuchMethodException { + ImplementationWrapper wrapper = new ImplementationWrapper(new MathFunctionImplementation(), "namespace"); + Class[] argTypes = new Class[] { Integer.class, Integer.class }; + + InvocationDescriptor descriptor = new InvocationDescriptor("math", MathFunctionImplementation.class, "add", new Object[]{1, 2},argTypes, int.class); + assertEquals(3, wrapper.invokeOn(descriptor, int.class)); + } + + @Test + void invokeOn_unknownMethod() { + ImplementationWrapper wrapper = new ImplementationWrapper(new MathFunctionImplementation(), "namespace"); + Class[] argTypes = new Class[] { Integer.class, Integer.class }; + + InvocationDescriptor descriptor = new InvocationDescriptor("math", MathFunctionImplementation.class, "unknown", new Object[]{1, 2},argTypes, int.class); + NoSuchMethodException noSuchMethodException = assertThrowsExactly(NoSuchMethodException.class, () -> { + wrapper.invokeOn(descriptor, int.class); + }); + + assertEquals("No method found with name unknown and compatible arguments (on " + MathFunctionImplementation.class.getName() + ").", noSuchMethodException.getMessage()); + } + @Test + void getImplementation_success() { + MathFunctionImplementation implementation = new MathFunctionImplementation(); + + ImplementationWrapper implementationWrapper = new ImplementationWrapper(implementation, "math"); + + assertSame(implementation, implementationWrapper.getImplementation()); + } + + @Test + void getNamespace_success() { + ImplementationWrapper implementationWrapper = new ImplementationWrapper(new MathFunctionImplementation(), "math"); + assertEquals("math", implementationWrapper.getNamespace()); + } + + static class MathFunctionImplementation implements MathFunction { + @Override + public int add(int a, int b) { + return a + b; + } + + @Override + public int add(int... a) { + return Arrays.stream(a).sum(); + } + + @Override + public int add(int a, Integer... b) { + return a + Arrays.stream(b).mapToInt(Integer::intValue).sum(); + } + + @Override + public int add(int a, Double... b) { + return a + Arrays.stream(b).mapToInt(Double::intValue).sum(); + } + + @Override + public int sub(int a, int b) { + return a - b; + } + } + + public interface MathFunction { + int add(int a, int b); + int add(int... a); + int add(int a, Integer... b); + + + int add(int a, Double... b); + + int sub(int a, int b); + } +} \ No newline at end of file diff --git a/meteor-core/src/test/java/dev/pixelib/meteor/core/utils/ArgumentMapperTest.java b/meteor-core/src/test/java/dev/pixelib/meteor/core/utils/ArgumentMapperTest.java index e10c5d5..7dcd609 100644 --- a/meteor-core/src/test/java/dev/pixelib/meteor/core/utils/ArgumentMapperTest.java +++ b/meteor-core/src/test/java/dev/pixelib/meteor/core/utils/ArgumentMapperTest.java @@ -1,6 +1,7 @@ package dev.pixelib.meteor.core.utils; import dev.pixelib.meteor.core.Meteor; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -92,6 +93,7 @@ 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 void sampleMethod(String a, Integer... b) {} } private static Stream provideArgumentsForTest() throws NoSuchMethodException { @@ -124,4 +126,19 @@ void testOverflowArguments(Method method, Object[] allArguments, Object[] expect void testOverflowArguments_WithEmptyOrNullExceptions(Object[] allArguments) { assertThrows(NullPointerException.class, () -> ArgumentMapper.overflowArguments(null, allArguments)); } + + @Test + void testOverflowArguments() throws NoSuchMethodException { + Method method = Example.class.getDeclaredMethod("sampleMethod", String.class, Integer[].class); + final Object[] allArguments = new Object[] { "test", new Integer[] {1, 2, 3}, new Integer[] {4, 5, 6}}; + + //Valid path + Assertions.assertDoesNotThrow(() -> ArgumentMapper.overflowArguments(method, allArguments)); + + //Invalid path - Type mismatch + Object[] allArguments2 = new Object[] { "test", new Boolean[] {true, false}, new Integer[] {4, 5, 6}}; + Assertions.assertThrows(RuntimeException.class, + () -> ArgumentMapper.overflowArguments(method, allArguments2), + "Argument type mismatch. java.lang.Integer expected, got java.lang.Boolean instead for argument 0."); + } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4bff9a2..62af2f6 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ - localbuild + 1.0.0-localbuild 17 17 UTF-8