Skip to content

Commit

Permalink
Add more test for argument mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Duckelekuuk committed Aug 28, 2023
1 parent f55decd commit 395b4b9
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 75 deletions.
18 changes: 12 additions & 6 deletions meteor-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
Expand All @@ -35,6 +29,18 @@
<artifactId>meteor-common</artifactId>
<version>${meteor-serializer.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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<Arguments> 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));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void stop() {
@Test
void getCustomSubscribedChannels_ThenSuccess() {
String topic = "test";
String message = "message";

RedisPacketListener redisPacketListener = new RedisPacketListener(subscriptionHandler, topic, Logger.getAnonymousLogger());

Expand Down

0 comments on commit 395b4b9

Please sign in to comment.