Add a unit test for getMostSpecificMethod of ClassUtils
See gh-24633
This commit is contained in:
parent
3c5dc19da3
commit
e7071c329a
|
@ -391,6 +391,32 @@ class ClassUtilsTests {
|
|||
assertThat(ClassUtils.determineCommonAncestor(String.class, List.class)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMostSpecificMethod() throws NoSuchMethodException {
|
||||
Method defaultPrintMethod = ClassUtils.getMethod(MethodsInterface.class, "defaultPrint");
|
||||
assertThat(ClassUtils.getMostSpecificMethod(defaultPrintMethod, MethodsInterfaceImplementation.class)).isEqualTo(defaultPrintMethod);
|
||||
assertThat(ClassUtils.getMostSpecificMethod(defaultPrintMethod, SubMethodsInterfaceImplementation.class)).isEqualTo(defaultPrintMethod);
|
||||
|
||||
Method printMethod = ClassUtils.getMethod(MethodsInterface.class, "print", String.class);
|
||||
assertThat(ClassUtils.getMostSpecificMethod(printMethod, MethodsInterfaceImplementation.class)).isNotEqualTo(printMethod);
|
||||
assertThat(ClassUtils.getMostSpecificMethod(printMethod, MethodsInterfaceImplementation.class))
|
||||
.isEqualTo(ClassUtils.getMethod(MethodsInterfaceImplementation.class, "print", String.class));
|
||||
assertThat(ClassUtils.getMostSpecificMethod(printMethod, SubMethodsInterfaceImplementation.class))
|
||||
.isEqualTo(ClassUtils.getMethod(MethodsInterfaceImplementation.class, "print", String.class));
|
||||
|
||||
Method protectedPrintMethod = MethodsInterfaceImplementation.class.getDeclaredMethod("protectedPrint");
|
||||
assertThat(ClassUtils.getMostSpecificMethod(protectedPrintMethod, MethodsInterfaceImplementation.class)).isEqualTo(protectedPrintMethod);
|
||||
assertThat(ClassUtils.getMostSpecificMethod(protectedPrintMethod, SubMethodsInterfaceImplementation.class))
|
||||
.isEqualTo(SubMethodsInterfaceImplementation.class.getDeclaredMethod("protectedPrint"));
|
||||
|
||||
|
||||
Method packageAccessiblePrintMethod = MethodsInterfaceImplementation.class.getDeclaredMethod("packageAccessiblePrint");
|
||||
assertThat(ClassUtils.getMostSpecificMethod(packageAccessiblePrintMethod, MethodsInterfaceImplementation.class))
|
||||
.isEqualTo(packageAccessiblePrintMethod);
|
||||
assertThat(ClassUtils.getMostSpecificMethod(packageAccessiblePrintMethod, SubMethodsInterfaceImplementation.class))
|
||||
.isEqualTo(ClassUtils.getMethod(SubMethodsInterfaceImplementation.class, "packageAccessiblePrint"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@WrapperTypes
|
||||
void isPrimitiveWrapper(Class<?> type) {
|
||||
|
@ -558,4 +584,44 @@ class ClassUtilsTests {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private interface MethodsInterface{
|
||||
|
||||
default void defaultPrint(){
|
||||
|
||||
}
|
||||
void print(String messages);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private class MethodsInterfaceImplementation implements MethodsInterface{
|
||||
|
||||
@Override
|
||||
public void print(String message) {
|
||||
|
||||
}
|
||||
|
||||
protected void protectedPrint(){
|
||||
|
||||
}
|
||||
|
||||
void packageAccessiblePrint(){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private class SubMethodsInterfaceImplementation extends MethodsInterfaceImplementation{
|
||||
|
||||
@Override
|
||||
protected void protectedPrint(){
|
||||
|
||||
}
|
||||
|
||||
public void packageAccessiblePrint(){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue