Merge branch '6.2.x'

This commit is contained in:
Sam Brannen
2025-08-19 12:34:42 +02:00
5 changed files with 209 additions and 8 deletions
@@ -35,32 +35,46 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link HandlerMethod}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
class HandlerMethodTests {
@Test
void shouldValidateArgsWithConstraintsDirectlyOnClass() {
void shouldValidateArgsWithConstraintsDirectlyInClass() {
Object target = new MyClass();
testValidateArgs(target, List.of("addIntValue", "addPersonAndIntValue", "addPersons", "addPeople", "addNames"), true);
testValidateArgs(target, List.of("addPerson", "getPerson", "getIntValue", "addPersonNotValidated"), false);
}
@Test
void shouldValidateArgsWithConstraintsOnInterface() {
void shouldValidateArgsWithConstraintsInInterface() {
Object target = new MyInterfaceImpl();
testValidateArgs(target, List.of("addIntValue", "addPersonAndIntValue", "addPersons", "addPeople"), true);
testValidateArgs(target, List.of("addPerson", "addPersonNotValidated", "getPerson", "getIntValue"), false);
}
@Test
void shouldValidateReturnValueWithConstraintsDirectlyOnClass() {
void shouldValidateArgsWithConstraintsInGenericAbstractSuperclass() {
Object target = new GenericInterfaceImpl();
shouldValidateArguments(getHandlerMethod(target, "processTwo", String.class), true);
}
@Test
void shouldValidateArgsWithConstraintsInGenericInterface() {
Object target = new GenericInterfaceImpl();
shouldValidateArguments(getHandlerMethod(target, "processOne", Long.class), false);
shouldValidateArguments(getHandlerMethod(target, "processOneAndTwo", Long.class, Object.class), true);
}
@Test
void shouldValidateReturnValueWithConstraintsDirectlyInClass() {
Object target = new MyClass();
testValidateReturnValue(target, List.of("getPerson", "getIntValue"), true);
testValidateReturnValue(target, List.of("addPerson", "addIntValue", "addPersonNotValidated"), false);
}
@Test
void shouldValidateReturnValueWithConstraintsOnInterface() {
void shouldValidateReturnValueWithConstraintsInInterface() {
Object target = new MyInterfaceImpl();
testValidateReturnValue(target, List.of("getPerson", "getIntValue"), true);
testValidateReturnValue(target, List.of("addPerson", "addIntValue", "addPersonNotValidated"), false);
@@ -97,9 +111,19 @@ class HandlerMethodTests {
assertThat(hm3.getResolvedFromHandlerMethod()).isSameAs(hm1);
}
private static void shouldValidateArguments(HandlerMethod handlerMethod, boolean expected) {
if (expected) {
assertThat(handlerMethod.shouldValidateArguments()).as(handlerMethod.getMethod().getName()).isTrue();
}
else {
assertThat(handlerMethod.shouldValidateArguments()).as(handlerMethod.getMethod().getName()).isFalse();
}
}
private static void testValidateArgs(Object target, List<String> methodNames, boolean expected) {
for (String methodName : methodNames) {
assertThat(getHandlerMethod(target, methodName).shouldValidateArguments()).isEqualTo(expected);
shouldValidateArguments(getHandlerMethod(target, methodName), expected);
}
}
@@ -110,7 +134,11 @@ class HandlerMethodTests {
}
private static HandlerMethod getHandlerMethod(Object target, String methodName) {
Method method = ClassUtils.getMethod(target.getClass(), methodName, (Class<?>[]) null);
return getHandlerMethod(target, methodName, (Class<?>[]) null);
}
private static HandlerMethod getHandlerMethod(Object target, String methodName, Class<?>... parameterTypes) {
Method method = ClassUtils.getMethod(target.getClass(), methodName, parameterTypes);
return new HandlerMethod(target, method).createWithValidateFlags();
}
@@ -236,4 +264,32 @@ class HandlerMethodTests {
}
}
interface GenericInterface<A, B> {
void processOne(@Valid A value1);
void processOneAndTwo(A value1, @Max(42) B value2);
}
abstract static class GenericAbstractSuperclass<C> implements GenericInterface<Long, C> {
@Override
public void processOne(Long value1) {
}
@Override
public void processOneAndTwo(Long value1, C value2) {
}
public abstract void processTwo(@Max(42) C value);
}
static class GenericInterfaceImpl extends GenericAbstractSuperclass<String> {
@Override
public void processTwo(String value) {
}
}
}