Merge branch '5.3.x'

This commit is contained in:
Sam Brannen 2022-01-25 10:03:51 +01:00
commit 72fc706c22
1 changed files with 12 additions and 30 deletions

View File

@ -24,7 +24,6 @@ import java.rmi.RemoteException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.tests.sample.objects.TestObject; import org.springframework.tests.sample.objects.TestObject;
@ -228,16 +227,15 @@ class ReflectionUtilsTests {
} }
@Test @Test
void findMethod() throws Exception { void findMethod() {
assertThat(ReflectionUtils.findMethod(B.class, "bar", String.class)).isNotNull(); assertThat(ReflectionUtils.findMethod(B.class, "bar", String.class)).isNotNull();
assertThat(ReflectionUtils.findMethod(B.class, "foo", Integer.class)).isNotNull(); assertThat(ReflectionUtils.findMethod(B.class, "foo", Integer.class)).isNotNull();
assertThat(ReflectionUtils.findMethod(B.class, "getClass")).isNotNull(); assertThat(ReflectionUtils.findMethod(B.class, "getClass")).isNotNull();
} }
@Disabled("[SPR-8644] findMethod() does not currently support var-args")
@Test @Test
void findMethodWithVarArgs() throws Exception { void findMethodWithVarArgs() {
assertThat(ReflectionUtils.findMethod(B.class, "add", int.class, int.class, int.class)).isNotNull(); assertThat(ReflectionUtils.findMethod(B.class, "add", int[].class)).isNotNull();
} }
@Test @Test
@ -279,37 +277,27 @@ class ReflectionUtilsTests {
} }
@Test @Test
void getAllDeclaredMethods() throws Exception { void getAllDeclaredMethods() {
class Foo { class Foo {
@Override @Override
public String toString() { public String toString() {
return super.toString(); return super.toString();
} }
} }
int toStringMethodCount = 0; Method[] allDeclaredMethods = ReflectionUtils.getAllDeclaredMethods(Foo.class);
for (Method method : ReflectionUtils.getAllDeclaredMethods(Foo.class)) { assertThat(allDeclaredMethods).extracting(Method::getName).filteredOn("toString"::equals).hasSize(2);
if (method.getName().equals("toString")) {
toStringMethodCount++;
}
}
assertThat(toStringMethodCount).isEqualTo(2);
} }
@Test @Test
void getUniqueDeclaredMethods() throws Exception { void getUniqueDeclaredMethods() {
class Foo { class Foo {
@Override @Override
public String toString() { public String toString() {
return super.toString(); return super.toString();
} }
} }
int toStringMethodCount = 0; Method[] uniqueDeclaredMethods = ReflectionUtils.getUniqueDeclaredMethods(Foo.class);
for (Method method : ReflectionUtils.getUniqueDeclaredMethods(Foo.class)) { assertThat(uniqueDeclaredMethods).extracting(Method::getName).filteredOn("toString"::equals).hasSize(1);
if (method.getName().equals("toString")) {
toStringMethodCount++;
}
}
assertThat(toStringMethodCount).isEqualTo(1);
} }
@Test @Test
@ -326,16 +314,10 @@ class ReflectionUtilsTests {
return Integer.valueOf(42); return Integer.valueOf(42);
} }
} }
int m1MethodCount = 0;
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(Leaf.class); Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(Leaf.class);
for (Method method : methods) { assertThat(methods).extracting(Method::getName).filteredOn("m1"::equals).hasSize(1);
if (method.getName().equals("m1")) { assertThat(methods).contains(Leaf.class.getMethod("m1"));
m1MethodCount++; assertThat(methods).doesNotContain(Parent.class.getMethod("m1"));
}
}
assertThat(m1MethodCount).isEqualTo(1);
assertThat(ObjectUtils.containsElement(methods, Leaf.class.getMethod("m1"))).isTrue();
assertThat(ObjectUtils.containsElement(methods, Parent.class.getMethod("m1"))).isFalse();
} }
@Test @Test