-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f55decd
commit 395b4b9
Showing
5 changed files
with
141 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
68 changes: 0 additions & 68 deletions
68
meteor-core/src/test/java/com/meteormsg/core/utils/ArgumentTransformerTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters