Use Named arguments in parameterized tests

This commit is contained in:
Sam Brannen
2022-03-16 14:45:47 +01:00
parent 8a0c4caff6
commit c462fe30ed
28 changed files with 416 additions and 366 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
@@ -16,9 +16,7 @@
package org.springframework.web.servlet.handler;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
@@ -32,6 +30,7 @@ import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.mock;
/**
@@ -44,19 +43,12 @@ class HandlerMappingTests {
@SuppressWarnings("unused")
private static Stream<Arguments> pathPatternsArguments() {
List<Function<String, MockHttpServletRequest>> factories =
PathPatternsTestUtils.requestArguments().collect(Collectors.toList());
return Stream.of(
Arguments.arguments(new TestHandlerMapping(), factories.get(0)),
Arguments.arguments(new TestHandlerMapping(), factories.get(1))
);
return PathPatternsTestUtils.requestArguments().map(function -> arguments(function, new TestHandlerMapping()));
}
@PathPatternsParameterizedTest
void orderedInterceptors(
TestHandlerMapping mapping, Function<String, MockHttpServletRequest> requestFactory)
throws Exception {
void orderedInterceptors(Function<String, MockHttpServletRequest> requestFactory, TestHandlerMapping mapping) throws Exception {
MappedInterceptor i1 = new MappedInterceptor(new String[] {"/**"}, mock(HandlerInterceptor.class));
HandlerInterceptor i2 = mock(HandlerInterceptor.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
@@ -24,6 +24,7 @@ import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.springframework.util.PathMatcher;
@@ -47,7 +48,7 @@ class MappedInterceptorTests {
@SuppressWarnings("unused")
private static Stream<Function<String, MockHttpServletRequest>> pathPatternsArguments() {
private static Stream<Named<Function<String, MockHttpServletRequest>>> pathPatternsArguments() {
return PathPatternsTestUtils.requestArguments();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
@@ -21,6 +21,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Annotation for tests parameterized to use either
* {@link org.springframework.web.util.pattern.PathPatternParser} or
@@ -33,7 +36,7 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD)
// Do not auto-close arguments since ConfigurableWebApplicationContext implements
// AutoCloseable and is shared between parameterized test invocations.
@org.junit.jupiter.params.ParameterizedTest(autoCloseArguments = false)
@org.junit.jupiter.params.provider.MethodSource("pathPatternsArguments")
@ParameterizedTest(name = "[{index}] {0}", autoCloseArguments = false)
@MethodSource("pathPatternsArguments")
public @interface PathPatternsParameterizedTest {
}

View File

@@ -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.
@@ -20,11 +20,15 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Named;
import org.springframework.lang.Nullable;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.UrlPathHelper;
import static org.junit.jupiter.api.Named.named;
/**
* Utility methods to help with parameterized tests for URL pattern matching
* via pre-parsed {@code PathPattern}s or String pattern matching with
@@ -35,22 +39,22 @@ import org.springframework.web.util.UrlPathHelper;
*/
public abstract class PathPatternsTestUtils {
public static Stream<Function<String, MockHttpServletRequest>> requestArguments() {
public static Stream<Named<Function<String, MockHttpServletRequest>>> requestArguments() {
return requestArguments(null);
}
public static Stream<Function<String, MockHttpServletRequest>> requestArguments(@Nullable String contextPath) {
public static Stream<Named<Function<String, MockHttpServletRequest>>> requestArguments(@Nullable String contextPath) {
return Stream.of(
path -> {
named("ServletRequestPathUtils", path -> {
MockHttpServletRequest request = createRequest("GET", contextPath, path);
ServletRequestPathUtils.parseAndCache(request);
return request;
},
path -> {
}),
named("UrlPathHelper", path -> {
MockHttpServletRequest request = createRequest("GET", contextPath, path);
UrlPathHelper.defaultInstance.resolveAndCacheLookupPath(request);
return request;
}
})
);
}

View File

@@ -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.
@@ -19,6 +19,7 @@ package org.springframework.web.servlet.mvc;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.springframework.ui.ModelMap;
@@ -42,7 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class UrlFilenameViewControllerTests {
@SuppressWarnings("unused")
private static Stream<Function<String, MockHttpServletRequest>> pathPatternsArguments() {
private static Stream<Named<Function<String, MockHttpServletRequest>>> pathPatternsArguments() {
return PathPatternsTestUtils.requestArguments();
}

View File

@@ -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.
@@ -20,6 +20,7 @@ import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.springframework.web.servlet.handler.PathPatternsParameterizedTest;
@@ -46,7 +47,7 @@ class WebContentInterceptorTests {
@SuppressWarnings("unused")
private static Stream<Function<String, MockHttpServletRequest>> pathPatternsArguments() {
private static Stream<Named<Function<String, MockHttpServletRequest>>> pathPatternsArguments() {
return PathPatternsTestUtils.requestArguments();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@@ -19,8 +19,10 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
@@ -49,6 +51,8 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.params.provider.Arguments.arguments;
/**
* Test various scenarios for detecting method-level and method parameter annotations depending
@@ -60,34 +64,39 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class HandlerMethodAnnotationDetectionTests {
static Object[][] handlerTypes() {
return new Object[][] {
{ SimpleController.class, true }, // CGLIB proxy
{ SimpleController.class, false },
static Stream<Arguments> handlerTypes() {
return Stream.of(
args(SimpleController.class, true), // CGLIB proxy
args(SimpleController.class, false),
{ AbstractClassController.class, true }, // CGLIB proxy
{ AbstractClassController.class, false },
args(AbstractClassController.class, true), // CGLIB proxy
args(AbstractClassController.class, false),
{ ParameterizedAbstractClassController.class, true }, // CGLIB proxy
{ ParameterizedAbstractClassController.class, false },
args(ParameterizedAbstractClassController.class, true), // CGLIB proxy
args(ParameterizedAbstractClassController.class, false),
{ ParameterizedSubclassOverridesDefaultMappings.class, true }, // CGLIB proxy
{ ParameterizedSubclassOverridesDefaultMappings.class, false },
args(ParameterizedSubclassOverridesDefaultMappings.class, true), // CGLIB proxy
args(ParameterizedSubclassOverridesDefaultMappings.class, false),
// TODO [SPR-9517] Enable ParameterizedSubclassDoesNotOverrideConcreteImplementationsFromGenericAbstractSuperclass test cases
// { ParameterizedSubclassDoesNotOverrideConcreteImplementationsFromGenericAbstractSuperclass.class, true }, // CGLIB proxy
// { ParameterizedSubclassDoesNotOverrideConcreteImplementationsFromGenericAbstractSuperclass.class, false },
// args(ParameterizedSubclassDoesNotOverrideConcreteImplementationsFromGenericAbstractSuperclass.class, true), // CGLIB proxy
// args(ParameterizedSubclassDoesNotOverrideConcreteImplementationsFromGenericAbstractSuperclass.class, false),
{ InterfaceController.class, true }, // JDK dynamic proxy
{ InterfaceController.class, false },
args(InterfaceController.class, true), // JDK dynamic proxy
args(InterfaceController.class, false),
{ ParameterizedInterfaceController.class, false }, // no AOP
args(ParameterizedInterfaceController.class, false), // no AOP
{ SupportClassController.class, true }, // CGLIB proxy
{ SupportClassController.class, false }
};
args(SupportClassController.class, true), // CGLIB proxy
args(SupportClassController.class, false)
);
}
private static Arguments args(Class<?> controllerType, boolean useAutoProxy) {
return arguments(named(controllerType.getSimpleName(), controllerType), useAutoProxy);
}
private RequestMappingHandlerMapping handlerMapping;
private RequestMappingHandlerAdapter handlerAdapter;
@@ -116,9 +125,9 @@ class HandlerMethodAnnotationDetectionTests {
}
@ParameterizedTest(name = "[{index}] controller [{0}], auto-proxy [{1}]")
@ParameterizedTest(name = "[{index}] controller = {0}, auto-proxy = {1}")
@MethodSource("handlerTypes")
void testRequestMappingMethod(Class<?> controllerType, boolean useAutoProxy) throws Exception {
void requestMappingMethod(Class<?> controllerType, boolean useAutoProxy) throws Exception {
setUp(controllerType, useAutoProxy);
String datePattern = "MM:dd:yyyy";

View File

@@ -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.
@@ -19,6 +19,8 @@ package org.springframework.web.servlet.view;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Named;
import org.springframework.web.servlet.handler.PathPatternsParameterizedTest;
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
@@ -38,59 +40,59 @@ public class DefaultRequestToViewNameTranslatorTests {
@SuppressWarnings("unused")
private static Stream<Function<String, MockHttpServletRequest>> pathPatternsArguments() {
private static Stream<Named<Function<String, MockHttpServletRequest>>> pathPatternsArguments() {
return PathPatternsTestUtils.requestArguments("/sundays");
}
@PathPatternsParameterizedTest
void testGetViewNameLeavesLeadingSlashIfSoConfigured(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameLeavesLeadingSlashIfSoConfigured(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME + "/");
this.translator.setStripLeadingSlash(false);
assertViewName(request, "/" + VIEW_NAME);
}
@PathPatternsParameterizedTest
void testGetViewNameLeavesTrailingSlashIfSoConfigured(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameLeavesTrailingSlashIfSoConfigured(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME + "/");
this.translator.setStripTrailingSlash(false);
assertViewName(request, VIEW_NAME + "/");
}
@PathPatternsParameterizedTest
void testGetViewNameLeavesExtensionIfSoConfigured(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameLeavesExtensionIfSoConfigured(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME + EXTENSION);
this.translator.setStripExtension(false);
assertViewName(request, VIEW_NAME + EXTENSION);
}
@PathPatternsParameterizedTest
void testGetViewNameWithDefaultConfiguration(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithDefaultConfiguration(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME + EXTENSION);
assertViewName(request, VIEW_NAME);
}
@PathPatternsParameterizedTest
void testGetViewNameWithCustomSeparator(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithCustomSeparator(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME + "/fiona" + EXTENSION);
this.translator.setSeparator("_");
assertViewName(request, VIEW_NAME + "_fiona");
}
@PathPatternsParameterizedTest
void testGetViewNameWithNoExtension(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithNoExtension(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME);
assertViewName(request, VIEW_NAME);
}
@PathPatternsParameterizedTest
void testGetViewNameWithSemicolonContent(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithSemicolonContent(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME + ";a=A;b=B");
assertViewName(request, VIEW_NAME);
}
@PathPatternsParameterizedTest
void testGetViewNameWithPrefix(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithPrefix(Function<String, MockHttpServletRequest> requestFactory) {
final String prefix = "fiona_";
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME);
this.translator.setPrefix(prefix);
@@ -98,14 +100,14 @@ public class DefaultRequestToViewNameTranslatorTests {
}
@PathPatternsParameterizedTest
void testGetViewNameWithNullPrefix(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithNullPrefix(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME);
this.translator.setPrefix(null);
assertViewName(request, VIEW_NAME);
}
@PathPatternsParameterizedTest
void testGetViewNameWithSuffix(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithSuffix(Function<String, MockHttpServletRequest> requestFactory) {
final String suffix = ".fiona";
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME);
this.translator.setSuffix(suffix);
@@ -113,7 +115,7 @@ public class DefaultRequestToViewNameTranslatorTests {
}
@PathPatternsParameterizedTest
void testGetViewNameWithNullSuffix(Function<String, MockHttpServletRequest> requestFactory) {
void getViewNameWithNullSuffix(Function<String, MockHttpServletRequest> requestFactory) {
MockHttpServletRequest request = requestFactory.apply(VIEW_NAME);
this.translator.setSuffix(null);
assertViewName(request, VIEW_NAME);