mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复EnglishNumberFormatter.format小数问题
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package org.dromara.hutool.core.math;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class EnglishNumberFormatterTest {
|
||||
@Test
|
||||
public void testFormatNull() {
|
||||
// 测试传入null值的情况
|
||||
final String result = EnglishNumberFormatter.format(null);
|
||||
Assertions.assertEquals("", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatInteger() {
|
||||
// 测试传入整数的情况
|
||||
String result = EnglishNumberFormatter.format(1234);
|
||||
Assertions.assertEquals("ONE THOUSAND TWO HUNDRED AND THIRTY FOUR ONLY", result);
|
||||
|
||||
result = EnglishNumberFormatter.format(1204);
|
||||
Assertions.assertEquals("ONE THOUSAND TWO HUNDRED AND FOUR ONLY", result);
|
||||
|
||||
result = EnglishNumberFormatter.format(1004);
|
||||
Assertions.assertEquals("ONE THOUSAND FOUR ONLY", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatDecimal() {
|
||||
// 测试传入小数的情况
|
||||
final String result = EnglishNumberFormatter.format(1234.56);
|
||||
Assertions.assertEquals("ONE THOUSAND TWO HUNDRED AND THIRTY FOUR AND CENTS FIFTY SIX ONLY", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatLargeNumber() {
|
||||
// 测试传入大数字的情况
|
||||
final String result = EnglishNumberFormatter.format(1234567890123L);
|
||||
Assertions.assertEquals("ONE TRILLION TWO HUNDRED AND THIRTY FOUR BILLION FIVE HUNDRED AND SIXTY SEVEN MILLION EIGHT HUNDRED AND NINETY THOUSAND ONE HUNDRED AND TWENTY THREE ONLY", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatNonNumeric() {
|
||||
Assertions.assertThrows(NumberFormatException.class, ()->{
|
||||
// 测试传入非数字字符串的情况
|
||||
EnglishNumberFormatter.format("non-numeric");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issue3579Test() {
|
||||
Assertions.assertEquals("ZERO AND CENTS TEN ONLY", EnglishNumberFormatter.format(0.1));
|
||||
Assertions.assertEquals("ZERO AND CENTS ONE ONLY", EnglishNumberFormatter.format(0.01));
|
||||
}
|
||||
}
|
@@ -17,15 +17,14 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* test for {@link MethodMatcher}
|
||||
*
|
||||
* @author huangchengxing
|
||||
*/
|
||||
class MethodMatcherTest {
|
||||
|
||||
private final MethodMatcher matchToString = t -> "toString".equals(t.getName());
|
||||
private final Predicate<Method> matchToString = t -> "toString".equals(t.getName());
|
||||
|
||||
@SneakyThrows
|
||||
@Test
|
||||
@@ -41,7 +40,7 @@ class MethodMatcherTest {
|
||||
void and() {
|
||||
final Method toString = Object.class.getDeclaredMethod("toString");
|
||||
Assertions.assertTrue(matchToString.test(toString));
|
||||
final MethodMatcher newMatcher = matchToString.and(t -> t.getReturnType() == String.class);
|
||||
final Predicate<Method> newMatcher = matchToString.and(t -> t.getReturnType() == String.class);
|
||||
Assertions.assertTrue(newMatcher.test(toString));
|
||||
}
|
||||
|
||||
@@ -50,14 +49,14 @@ class MethodMatcherTest {
|
||||
void negate() {
|
||||
final Method toString = Object.class.getDeclaredMethod("toString");
|
||||
Assertions.assertTrue(matchToString.test(toString));
|
||||
final MethodMatcher newMatcher = matchToString.negate();
|
||||
final Predicate<Method> newMatcher = matchToString.negate();
|
||||
Assertions.assertFalse(newMatcher.test(toString));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void or() {
|
||||
final MethodMatcher newMatcher = matchToString.or(t -> "hashCode".equals(t.getName()));
|
||||
final Predicate<Method> newMatcher = matchToString.or(t -> "hashCode".equals(t.getName()));
|
||||
final Method toString = Object.class.getDeclaredMethod("toString");
|
||||
final Method hashCode = Object.class.getDeclaredMethod("hashCode");
|
||||
Assertions.assertTrue(newMatcher.test(toString));
|
||||
@@ -68,8 +67,8 @@ class MethodMatcherTest {
|
||||
@Test
|
||||
void inspect() {
|
||||
final Method toString = Object.class.getDeclaredMethod("toString");
|
||||
Assertions.assertTrue(matchToString.inspect(toString));
|
||||
Assertions.assertTrue(matchToString.test(toString));
|
||||
final Method hashCode = Object.class.getDeclaredMethod("hashCode");
|
||||
Assertions.assertNull(matchToString.inspect(hashCode));
|
||||
Assertions.assertFalse(matchToString.test(hashCode));
|
||||
}
|
||||
}
|
||||
|
@@ -29,6 +29,7 @@ import java.lang.reflect.Modifier;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* test for {@link MethodMatcherUtil}
|
||||
@@ -59,14 +60,14 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void testForName() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forName("noneReturnNoArgs");
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forName("noneReturnNoArgs");
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
}
|
||||
|
||||
@Test
|
||||
void forNameIgnoreCase() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forNameIgnoreCase("noneReturnNoArgs");
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forNameIgnoreCase("noneReturnNoArgs");
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
// if name is upper case, it will be ignored
|
||||
@@ -77,7 +78,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forNoneReturnType() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forNoneReturnType();
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forNoneReturnType();
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -88,7 +89,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forReturnType() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forReturnType(Collection.class);
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forReturnType(Collection.class);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -99,7 +100,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forStrictReturnType() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forStrictReturnType(Collection.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forStrictReturnType(Collection.class);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -118,7 +119,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forParameterCount() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forParameterCount(2);
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forParameterCount(2);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -127,7 +128,7 @@ class MethodMatcherUtilsTest {
|
||||
@Test
|
||||
void forMostSpecificParameterTypes() {
|
||||
// match none args method
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forMostSpecificParameterTypes();
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forMostSpecificParameterTypes();
|
||||
Assertions.assertFalse(methodMatcher.test(returnTwoArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
|
||||
@@ -159,7 +160,7 @@ class MethodMatcherUtilsTest {
|
||||
@Test
|
||||
void forMostSpecificStrictParameterTypes() {
|
||||
// match none args method
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forMostSpecificStrictParameterTypes();
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forMostSpecificStrictParameterTypes();
|
||||
Assertions.assertFalse(methodMatcher.test(returnTwoArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
|
||||
@@ -194,7 +195,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forParameterTypes() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forParameterTypes();
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forParameterTypes();
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -213,7 +214,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forStrictParameterTypes() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forStrictParameterTypes();
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forStrictParameterTypes();
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -242,7 +243,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void noneMatch() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.noneMatch();
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.noneMatch();
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -261,7 +262,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void anyMatch() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.anyMatch();
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.anyMatch();
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -281,7 +282,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void allMatch() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.allMatch();
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.allMatch();
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -304,7 +305,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void isPublic() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.isPublic();
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.isPublic();
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -315,7 +316,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void isStatic() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.isStatic();
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.isStatic();
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -326,7 +327,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void isPublicStatic() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.isPublicStatic();
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.isPublicStatic();
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -337,7 +338,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forModifiers() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -348,7 +349,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forNameAndParameterTypes() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forNameAndParameterTypes("noneReturnTwoArgs", CharSequence.class, List.class);
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forNameAndParameterTypes("noneReturnTwoArgs", CharSequence.class, List.class);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -359,7 +360,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forNameAndStrictParameterTypes() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forNameAndStrictParameterTypes("noneReturnTwoArgs", CharSequence.class, List.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forNameAndStrictParameterTypes("noneReturnTwoArgs", CharSequence.class, List.class);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -378,7 +379,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forNameIgnoreCaseAndParameterTypes() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forNameIgnoreCaseAndParameterTypes("NONEReturnTWOArgs", CharSequence.class, List.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forNameIgnoreCaseAndParameterTypes("NONEReturnTWOArgs", CharSequence.class, List.class);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -397,7 +398,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forNameIgnoreCaseAndStrictParameterTypes() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forNameIgnoreCaseAndStrictParameterTypes("NONEReturnTWOArgs", CharSequence.class, List.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forNameIgnoreCaseAndStrictParameterTypes("NONEReturnTWOArgs", CharSequence.class, List.class);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -416,7 +417,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forStrictMethodSignature() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forStrictMethodSignature("noneReturnTwoArgs", null, CharSequence.class, Collection.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forStrictMethodSignature("noneReturnTwoArgs", null, CharSequence.class, Collection.class);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs2));
|
||||
@@ -435,7 +436,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forStrictMethodSignatureWithMethod() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forStrictMethodSignature(noneReturnTwoArgs);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forStrictMethodSignature(noneReturnTwoArgs);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -454,7 +455,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forMethodSignatureWithMethod() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forMethodSignature(noneReturnTwoArgs2);
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forMethodSignature(noneReturnTwoArgs2);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnOneArgs));
|
||||
Assertions.assertTrue(methodMatcher.test(noneReturnTwoArgs));
|
||||
@@ -466,7 +467,7 @@ class MethodMatcherUtilsTest {
|
||||
|
||||
@Test
|
||||
void forMethodSignature() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forMethodSignature(
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.forMethodSignature(
|
||||
"noneReturnTwoArgs", null, CharSequence.class, Collection.class
|
||||
);
|
||||
Assertions.assertFalse(methodMatcher.test(noneReturnNoArgs));
|
||||
@@ -481,77 +482,77 @@ class MethodMatcherUtilsTest {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void forGetterMethodWithField() {
|
||||
Field nameField = Foo.class.getDeclaredField("name");
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forGetterMethod(nameField);
|
||||
Method getName = Foo.class.getMethod("getName");
|
||||
final Field nameField = Foo.class.getDeclaredField("name");
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forGetterMethod(nameField);
|
||||
final Method getName = Foo.class.getMethod("getName");
|
||||
Assertions.assertTrue(methodMatcher.test(getName));
|
||||
|
||||
Field flagField = Foo.class.getDeclaredField("flag");
|
||||
final Field flagField = Foo.class.getDeclaredField("flag");
|
||||
methodMatcher = MethodMatcherUtil.forGetterMethod(flagField);
|
||||
Method isFlag = Foo.class.getMethod("isFlag");
|
||||
final Method isFlag = Foo.class.getMethod("isFlag");
|
||||
Assertions.assertTrue(methodMatcher.test(isFlag));
|
||||
|
||||
Field objectField = Foo.class.getDeclaredField("object");
|
||||
final Field objectField = Foo.class.getDeclaredField("object");
|
||||
methodMatcher = MethodMatcherUtil.forGetterMethod(objectField);
|
||||
Method object = Foo.class.getMethod("object");
|
||||
final Method object = Foo.class.getMethod("object");
|
||||
Assertions.assertTrue(methodMatcher.test(object));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void forGetterMethod() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forGetterMethod("name", String.class);
|
||||
Method getName = Foo.class.getMethod("getName");
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forGetterMethod("name", String.class);
|
||||
final Method getName = Foo.class.getMethod("getName");
|
||||
Assertions.assertTrue(methodMatcher.test(getName));
|
||||
|
||||
methodMatcher = MethodMatcherUtil.forGetterMethod("flag", boolean.class);
|
||||
Method isFlag = Foo.class.getMethod("isFlag");
|
||||
final Method isFlag = Foo.class.getMethod("isFlag");
|
||||
Assertions.assertTrue(methodMatcher.test(isFlag));
|
||||
|
||||
methodMatcher = MethodMatcherUtil.forGetterMethod("object", Object.class);
|
||||
Method object = Foo.class.getMethod("object");
|
||||
final Method object = Foo.class.getMethod("object");
|
||||
Assertions.assertTrue(methodMatcher.test(object));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void forSetterMethodWithField() {
|
||||
Field nameField = Foo.class.getDeclaredField("name");
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forSetterMethod(nameField);
|
||||
Method setName = Foo.class.getMethod("setName", String.class);
|
||||
final Field nameField = Foo.class.getDeclaredField("name");
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forSetterMethod(nameField);
|
||||
final Method setName = Foo.class.getMethod("setName", String.class);
|
||||
Assertions.assertTrue(methodMatcher.test(setName));
|
||||
|
||||
Field flagField = Foo.class.getDeclaredField("flag");
|
||||
final Field flagField = Foo.class.getDeclaredField("flag");
|
||||
methodMatcher = MethodMatcherUtil.forSetterMethod(flagField);
|
||||
Method setFlag = Foo.class.getMethod("setFlag", boolean.class);
|
||||
final Method setFlag = Foo.class.getMethod("setFlag", boolean.class);
|
||||
Assertions.assertTrue(methodMatcher.test(setFlag));
|
||||
|
||||
Field objectField = Foo.class.getDeclaredField("object");
|
||||
final Field objectField = Foo.class.getDeclaredField("object");
|
||||
methodMatcher = MethodMatcherUtil.forSetterMethod(objectField);
|
||||
Method object = Foo.class.getMethod("object", Object.class);
|
||||
final Method object = Foo.class.getMethod("object", Object.class);
|
||||
Assertions.assertTrue(methodMatcher.test(object));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void forSetterMethod() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.forSetterMethod("name", String.class);
|
||||
Method setName = Foo.class.getMethod("setName", String.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.forSetterMethod("name", String.class);
|
||||
final Method setName = Foo.class.getMethod("setName", String.class);
|
||||
Assertions.assertTrue(methodMatcher.test(setName));
|
||||
|
||||
methodMatcher = MethodMatcherUtil.forSetterMethod("flag", boolean.class);
|
||||
Method setFlag = Foo.class.getMethod("setFlag", boolean.class);
|
||||
final Method setFlag = Foo.class.getMethod("setFlag", boolean.class);
|
||||
Assertions.assertTrue(methodMatcher.test(setFlag));
|
||||
|
||||
methodMatcher = MethodMatcherUtil.forSetterMethod("object", Object.class);
|
||||
Method object = Foo.class.getMethod("object", Object.class);
|
||||
final Method object = Foo.class.getMethod("object", Object.class);
|
||||
Assertions.assertTrue(methodMatcher.test(object));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void hasDeclaredAnnotation() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.hasDeclaredAnnotation(GrandParentAnnotation.class);
|
||||
final Predicate<Method> methodMatcher = MethodMatcherUtil.hasDeclaredAnnotation(GrandParentAnnotation.class);
|
||||
Assertions.assertFalse(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByChildAnnotation")));
|
||||
Assertions.assertFalse(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByParentAnnotation")));
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByGrandParentAnnotation")));
|
||||
@@ -561,7 +562,7 @@ class MethodMatcherUtilsTest {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void hasAnnotation() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.hasAnnotation(GrandParentAnnotation.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.hasAnnotation(GrandParentAnnotation.class);
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByChildAnnotation")));
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByParentAnnotation")));
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByGrandParentAnnotation")));
|
||||
@@ -577,7 +578,7 @@ class MethodMatcherUtilsTest {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void hasAnnotationOnDeclaringClass() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.hasAnnotationOnDeclaringClass(GrandParentAnnotation.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.hasAnnotationOnDeclaringClass(GrandParentAnnotation.class);
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByChildAnnotation")));
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByParentAnnotation")));
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByGrandParentAnnotation")));
|
||||
@@ -599,7 +600,7 @@ class MethodMatcherUtilsTest {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void hasAnnotationOnMethodOrDeclaringClass() {
|
||||
MethodMatcher methodMatcher = MethodMatcherUtil.hasAnnotationOnMethodOrDeclaringClass(GrandParentAnnotation.class);
|
||||
Predicate<Method> methodMatcher = MethodMatcherUtil.hasAnnotationOnMethodOrDeclaringClass(GrandParentAnnotation.class);
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByChildAnnotation")));
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByParentAnnotation")));
|
||||
Assertions.assertTrue(methodMatcher.test(AnnotatedClass.class.getDeclaredMethod("annotatedByGrandParentAnnotation")));
|
||||
@@ -652,23 +653,23 @@ class MethodMatcherUtilsTest {
|
||||
@Getter
|
||||
private boolean flag;
|
||||
private Object object;
|
||||
public void setName(String name, Void none) { }
|
||||
public void setName(final String name, final Void none) { }
|
||||
|
||||
public Object object() {
|
||||
return object;
|
||||
}
|
||||
|
||||
public Foo object(Object object) {
|
||||
public Foo object(final Object object) {
|
||||
this.object = object;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private void noneReturnNoArgs() { }
|
||||
private static void noneReturnOneArgs(String arg1) { }
|
||||
public static void noneReturnTwoArgs(String arg1, List<String> stringList) { }
|
||||
public static void noneReturnTwoArgs(CharSequence arg1, Collection<String> stringList) { }
|
||||
private static void noneReturnOneArgs(final String arg1) { }
|
||||
public static void noneReturnTwoArgs(final String arg1, final List<String> stringList) { }
|
||||
public static void noneReturnTwoArgs(final CharSequence arg1, final Collection<String> stringList) { }
|
||||
public List<String> returnNoArgs() { return null; }
|
||||
public Set<String> returnOneArgs(String arg1) { return null; }
|
||||
public List<String> returnTwoArgs(String arg1, List<String> stringList) { return null; }
|
||||
public Set<String> returnOneArgs(final String arg1) { return null; }
|
||||
public List<String> returnTwoArgs(final String arg1, final List<String> stringList) { return null; }
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* test for {@link MethodScanner}
|
||||
* test for {@link MethodScanner2}
|
||||
*
|
||||
* @author huangchengxing
|
||||
*/
|
||||
@@ -35,52 +35,52 @@ class MethodScannerTest {
|
||||
|
||||
@Test
|
||||
void testGetMethods() {
|
||||
Assertions.assertEquals(0, MethodScanner.getMethods(null).length);
|
||||
final Method[] actual = MethodScanner.getMethods(Child.class);
|
||||
Assertions.assertSame(actual, MethodScanner.getMethods(Child.class));
|
||||
Assertions.assertEquals(0, MethodScanner2.getMethods(null).length);
|
||||
final Method[] actual = MethodScanner2.getMethods(Child.class);
|
||||
Assertions.assertSame(actual, MethodScanner2.getMethods(Child.class));
|
||||
final Method[] expected = Child.class.getMethods();
|
||||
Assertions.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetDeclaredMethods() {
|
||||
Assertions.assertEquals(0, MethodScanner.getDeclaredMethods(null).length);
|
||||
final Method[] actual = MethodScanner.getDeclaredMethods(Child.class);
|
||||
Assertions.assertSame(actual, MethodScanner.getDeclaredMethods(Child.class));
|
||||
Assertions.assertEquals(0, MethodScanner2.getDeclaredMethods(null).length);
|
||||
final Method[] actual = MethodScanner2.getDeclaredMethods(Child.class);
|
||||
Assertions.assertSame(actual, MethodScanner2.getDeclaredMethods(Child.class));
|
||||
final Method[] expected = Child.class.getDeclaredMethods();
|
||||
Assertions.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllMethods() {
|
||||
Assertions.assertEquals(0, MethodScanner.getAllMethods(null).length);
|
||||
final Method[] actual = MethodScanner.getAllMethods(Child.class);
|
||||
Assertions.assertEquals(0, MethodScanner2.getAllMethods(null).length);
|
||||
final Method[] actual = MethodScanner2.getAllMethods(Child.class);
|
||||
// get declared method from child、parent、grandparent
|
||||
final Method[] expected = Stream.of(Child.class, Parent.class, Interface.class, Object.class)
|
||||
.flatMap(c -> Stream.of(MethodScanner.getDeclaredMethods(c)))
|
||||
.flatMap(c -> Stream.of(MethodScanner2.getDeclaredMethods(c)))
|
||||
.toArray(Method[]::new);
|
||||
Assertions.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearCaches() {
|
||||
final Method[] declaredMethods = MethodScanner.getDeclaredMethods(Child.class);
|
||||
Assertions.assertSame(declaredMethods, MethodScanner.getDeclaredMethods(Child.class));
|
||||
final Method[] methods = MethodScanner.getMethods(Child.class);
|
||||
Assertions.assertSame(methods, MethodScanner.getMethods(Child.class));
|
||||
final Method[] declaredMethods = MethodScanner2.getDeclaredMethods(Child.class);
|
||||
Assertions.assertSame(declaredMethods, MethodScanner2.getDeclaredMethods(Child.class));
|
||||
final Method[] methods = MethodScanner2.getMethods(Child.class);
|
||||
Assertions.assertSame(methods, MethodScanner2.getMethods(Child.class));
|
||||
|
||||
// clear method cache
|
||||
MethodScanner.clearCaches();
|
||||
Assertions.assertNotSame(declaredMethods, MethodScanner.getDeclaredMethods(Child.class));
|
||||
Assertions.assertNotSame(methods, MethodScanner.getMethods(Child.class));
|
||||
MethodScanner2.clearCaches();
|
||||
Assertions.assertNotSame(declaredMethods, MethodScanner2.getDeclaredMethods(Child.class));
|
||||
Assertions.assertNotSame(methods, MethodScanner2.getMethods(Child.class));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindWithMetadataFromSpecificMethods() {
|
||||
Assertions.assertTrue(MethodScanner.findWithMetadataFromSpecificMethods(null, m -> m.getAnnotation(Annotation.class)).isEmpty());
|
||||
final Method[] methods = MethodScanner.getMethods(Child.class);
|
||||
final Map<Method, Annotation> actual = MethodScanner.findWithMetadataFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertTrue(MethodScanner2.findWithMetadataFromSpecificMethods(null, m -> m.getAnnotation(Annotation.class)).isEmpty());
|
||||
final Method[] methods = MethodScanner2.getMethods(Child.class);
|
||||
final Map<Method, Annotation> actual = MethodScanner2.findWithMetadataFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -95,8 +95,8 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindFromSpecificMethods() {
|
||||
final Method[] methods = MethodScanner.getMethods(Child.class);
|
||||
final Set<Method> actual = MethodScanner.findFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class));
|
||||
final Method[] methods = MethodScanner2.getMethods(Child.class);
|
||||
final Set<Method> actual = MethodScanner2.findFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -108,8 +108,9 @@ class MethodScannerTest {
|
||||
@Test
|
||||
void testGetWithMetadataFromSpecificMethods() {
|
||||
// find first oneArgMethod method
|
||||
final Method[] methods = MethodScanner.getMethods(Child.class);
|
||||
final Map.Entry<Method, Boolean> actual = MethodScanner.getWithMetadataFromSpecificMethods(methods, MethodMatcherUtil.forName("oneArgMethod"));
|
||||
final Method[] methods = MethodScanner2.getMethods(Child.class);
|
||||
final Map.Entry<Method, Boolean> actual = MethodScanner2.getWithMetadataFromSpecificMethods(methods,
|
||||
(method -> method.getName().equals("oneArgMethod") ? true : null));
|
||||
Assertions.assertNotNull(actual);
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual.getKey());
|
||||
@@ -120,8 +121,9 @@ class MethodScannerTest {
|
||||
@Test
|
||||
void testGetFromSpecificMethods() {
|
||||
// find first oneArgMethod method
|
||||
final Method[] methods = MethodScanner.getMethods(Child.class);
|
||||
final Method actual = MethodScanner.getFromSpecificMethods(methods, MethodMatcherUtil.forName("oneArgMethod"));
|
||||
final Method[] methods = MethodScanner2.getMethods(Child.class);
|
||||
final Method actual = MethodScanner2.getFromSpecificMethods(methods, method ->
|
||||
method.getName().equals("oneArgMethod") ? true : null);
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual);
|
||||
}
|
||||
@@ -129,7 +131,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindWithMetadataFromMethods() {
|
||||
final Map<Method, Annotation> actual = MethodScanner.findWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Map<Method, Annotation> actual = MethodScanner2.findWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -144,7 +146,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindFromMethods() {
|
||||
final Set<Method> actual = MethodScanner.findFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Set<Method> actual = MethodScanner2.findFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -155,7 +157,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testGetWithMetadataFromMethods() {
|
||||
final Map.Entry<Method, Annotation> actual = MethodScanner.getWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Map.Entry<Method, Annotation> actual = MethodScanner2.getWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertNotNull(actual);
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual.getKey());
|
||||
@@ -166,7 +168,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testGetFromMethods() {
|
||||
final Method actual = MethodScanner.getFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Method actual = MethodScanner2.getFromMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual);
|
||||
}
|
||||
@@ -174,7 +176,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindWithMetadataFromDeclaredMethods() {
|
||||
final Map<Method, Annotation> actual = MethodScanner.findWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Map<Method, Annotation> actual = MethodScanner2.findWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -189,7 +191,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindFromDeclaredMethods() {
|
||||
final Set<Method> actual = MethodScanner.findFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Set<Method> actual = MethodScanner2.findFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -200,7 +202,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testGetWithMetadataFromDeclaredMethods() {
|
||||
final Map.Entry<Method, Annotation> actual = MethodScanner.getWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Map.Entry<Method, Annotation> actual = MethodScanner2.getWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertNotNull(actual);
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual.getKey());
|
||||
@@ -211,7 +213,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testGetFromDeclaredMethods() {
|
||||
final Method actual = MethodScanner.getFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Method actual = MethodScanner2.getFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual);
|
||||
}
|
||||
@@ -219,7 +221,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindWithMetadataFromAllMethods() {
|
||||
final Map<Method, Annotation> actual = MethodScanner.findWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Map<Method, Annotation> actual = MethodScanner2.findWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -234,7 +236,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testFindFromAllMethods() {
|
||||
final Set<Method> actual = MethodScanner.findFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Set<Method> actual = MethodScanner2.findFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertEquals(1, actual.size());
|
||||
|
||||
// check method
|
||||
@@ -245,8 +247,8 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testGetWithMetadataFromAllMethods() {
|
||||
Assertions.assertNull(MethodScanner.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Alias.class)));
|
||||
final Map.Entry<Method, Annotation> actual = MethodScanner.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertNull(MethodScanner2.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Alias.class)));
|
||||
final Map.Entry<Method, Annotation> actual = MethodScanner2.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
Assertions.assertNotNull(actual);
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual.getKey());
|
||||
@@ -257,7 +259,7 @@ class MethodScannerTest {
|
||||
@SneakyThrows
|
||||
@Test
|
||||
void testGetFromAllMethods() {
|
||||
final Method actual = MethodScanner.getFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Method actual = MethodScanner2.getFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class));
|
||||
final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class);
|
||||
Assertions.assertEquals(expectedMethod, actual);
|
||||
}
|
||||
|
Reference in New Issue
Block a user