Merge branch '5.3.x'

This commit is contained in:
Sam Brannen
2022-01-24 20:33:51 +01:00
2 changed files with 42 additions and 18 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.tests.sample.objects.TestObject;
import org.springframework.util.ReflectionUtils.MethodFilter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -184,27 +185,46 @@ class ReflectionUtilsTests {
}
@Test
void doWithProtectedMethods() {
void doWithMethodsUsingProtectedFilter() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestObject.class, mc, method -> Modifier.isProtected(method.getModifiers()));
assertThat(mc.getMethodNames().isEmpty()).isFalse();
assertThat(mc.getMethodNames().contains("clone")).as("Must find protected method on Object").isTrue();
assertThat(mc.getMethodNames().contains("finalize")).as("Must find protected method on Object").isTrue();
assertThat(mc.getMethodNames().contains("hashCode")).as("Public, not protected").isFalse();
assertThat(mc.getMethodNames().contains("absquatulate")).as("Public, not protected").isFalse();
assertThat(mc.getMethodNames())
.hasSizeGreaterThanOrEqualTo(2)
.as("Must find protected methods on Object").contains("clone", "finalize")
.as("Public, not protected").doesNotContain("hashCode", "absquatulate");
}
@Test
void duplicatesFound() {
void doWithMethodsUsingUserDeclaredMethodsFilterStartingWithObject() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(Object.class, mc, ReflectionUtils.USER_DECLARED_METHODS);
assertThat(mc.getMethodNames()).isEmpty();
}
@Test
void doWithMethodsUsingUserDeclaredMethodsFilterStartingWithTestObject() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestObject.class, mc, ReflectionUtils.USER_DECLARED_METHODS);
assertThat(mc.getMethodNames())
.as("user declared methods").contains("absquatulate", "compareTo", "getName", "setName", "getAge", "setAge", "getSpouse", "setSpouse")
.as("methods on Object").doesNotContain("equals", "hashCode", "toString", "clone", "finalize", "getClass", "notify", "notifyAll", "wait");
}
@Test
void doWithMethodsUsingUserDeclaredMethodsComposedFilter() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
// "q" because both absquatulate() and equals() contain "q"
MethodFilter isSetterMethodOrNameContainsQ = m -> m.getName().startsWith("set") || m.getName().contains("q");
MethodFilter methodFilter = ReflectionUtils.USER_DECLARED_METHODS.and(isSetterMethodOrNameContainsQ);
ReflectionUtils.doWithMethods(TestObject.class, mc, methodFilter);
assertThat(mc.getMethodNames()).containsExactlyInAnyOrder("setName", "setAge", "setSpouse", "absquatulate");
}
@Test
void doWithMethodsFindsDuplicatesInClassHierarchy() {
ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestObjectSubclass.class, mc);
int absquatulateCount = 0;
for (String name : mc.getMethodNames()) {
if (name.equals("absquatulate")) {
++absquatulateCount;
}
}
assertThat(absquatulateCount).as("Found 2 absquatulates").isEqualTo(2);
assertThat(mc.getMethodNames().stream()).filteredOn("absquatulate"::equals).as("Found 2 absquatulates").hasSize(2);
}
@Test