Fix parameterized test display names and polishing

This commit is contained in:
Sam Brannen 2024-09-13 15:26:56 +02:00
parent 412f5f677b
commit b388ff60dd
2 changed files with 36 additions and 25 deletions

View File

@ -45,9 +45,7 @@ class SimpleUrlHandlerMappingTests {
@Test @Test
void handlerMappingJavaConfig() { void handlerMappingJavaConfig() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(WebConfig.class);
wac.register(WebConfig.class);
wac.refresh();
HandlerMapping handlerMapping = (HandlerMapping) wac.getBean("handlerMapping"); HandlerMapping handlerMapping = (HandlerMapping) wac.getBean("handlerMapping");
Object mainController = wac.getBean("mainController"); Object mainController = wac.getBean("mainController");
@ -62,7 +60,6 @@ class SimpleUrlHandlerMappingTests {
@Test @Test
void handlerMappingXmlConfig() { void handlerMappingXmlConfig() {
ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext("map.xml", getClass()); ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext("map.xml", getClass());
wac.refresh();
HandlerMapping handlerMapping = wac.getBean("mapping", HandlerMapping.class); HandlerMapping handlerMapping = wac.getBean("mapping", HandlerMapping.class);
Object mainController = wac.getBean("mainController"); Object mainController = wac.getBean("mainController");

View File

@ -16,6 +16,11 @@
package org.springframework.web.servlet.handler; package org.springframework.web.servlet.handler;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -40,12 +45,15 @@ import org.springframework.web.util.WebUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE; import static org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE;
import static org.springframework.web.servlet.HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE; import static org.springframework.web.servlet.HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE;
import static org.springframework.web.servlet.HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; import static org.springframework.web.servlet.HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
/** /**
* Tests for {@link SimpleUrlHandlerMapping}. * Tests for {@link SimpleUrlHandlerMapping}.
*
* @author Brian Clozel * @author Brian Clozel
*/ */
class SimpleUrlHandlerMappingTests { class SimpleUrlHandlerMappingTests {
@ -73,8 +81,7 @@ class SimpleUrlHandlerMappingTests {
assertThat(hec.getHandler()).isSameAs(controller); assertThat(hec.getHandler()).isSameAs(controller);
} }
@ParameterizedTest @HandlerMappingsTest
@MethodSource("handlerMappings")
void resolveFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception { void resolveFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext(); StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("mainController", Object.class); applicationContext.registerSingleton("mainController", Object.class);
@ -91,8 +98,7 @@ class SimpleUrlHandlerMappingTests {
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController); assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController);
} }
@ParameterizedTest @HandlerMappingsTest
@MethodSource("handlerMappings")
void resolvePatternFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception { void resolvePatternFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext(); StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("mainController", Object.class); applicationContext.registerSingleton("mainController", Object.class);
@ -109,8 +115,7 @@ class SimpleUrlHandlerMappingTests {
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController); assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController);
} }
@ParameterizedTest @HandlerMappingsTest
@MethodSource("handlerMappings")
void resolvePathWithParamFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception { void resolvePathWithParamFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext(); StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("mainController", Object.class); applicationContext.registerSingleton("mainController", Object.class);
@ -127,8 +132,7 @@ class SimpleUrlHandlerMappingTests {
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController); assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController);
} }
@ParameterizedTest @HandlerMappingsTest
@MethodSource("handlerMappings")
void resolvePathWithContextFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception { void resolvePathWithContextFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext(); StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("mainController", Object.class); applicationContext.registerSingleton("mainController", Object.class);
@ -145,8 +149,7 @@ class SimpleUrlHandlerMappingTests {
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController); assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController);
} }
@ParameterizedTest @HandlerMappingsTest
@MethodSource("handlerMappings")
void resolvePathWithIncludeFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception { void resolvePathWithIncludeFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext(); StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("mainController", Object.class); applicationContext.registerSingleton("mainController", Object.class);
@ -164,8 +167,7 @@ class SimpleUrlHandlerMappingTests {
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController); assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController);
} }
@ParameterizedTest @HandlerMappingsTest
@MethodSource("handlerMappings")
void resolveDefaultPathFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception { void resolveDefaultPathFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext(); StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("mainController", Object.class); applicationContext.registerSingleton("mainController", Object.class);
@ -182,8 +184,7 @@ class SimpleUrlHandlerMappingTests {
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController); assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(mainController);
} }
@ParameterizedTest @HandlerMappingsTest
@MethodSource("handlerMappings")
void resolveParameterizedControllerFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception { void resolveParameterizedControllerFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
ParameterizableViewController viewController = new ParameterizableViewController(); ParameterizableViewController viewController = new ParameterizableViewController();
viewController.setView(new RedirectView("/after/{variable}")); viewController.setView(new RedirectView("/after/{variable}"));
@ -196,18 +197,12 @@ class SimpleUrlHandlerMappingTests {
HandlerExecutionChain chain = getHandler(handlerMapping, request); HandlerExecutionChain chain = getHandler(handlerMapping, request);
assertThat(chain.getHandler()).isSameAs(viewController); assertThat(chain.getHandler()).isSameAs(viewController);
@SuppressWarnings("unchecked")
Map<String, String> variables = (Map<String, String>) request.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE); Map<String, String> variables = (Map<String, String>) request.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE);
assertThat(variables).containsEntry("variable", "test"); assertThat(variables).containsEntry("variable", "test");
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(viewController); assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(viewController);
} }
static Stream<Arguments> handlerMappings() {
SimpleUrlHandlerMapping defaultConfig = new SimpleUrlHandlerMapping();
SimpleUrlHandlerMapping antPatternConfig = new SimpleUrlHandlerMapping();
antPatternConfig.setPatternParser(null);
return Stream.of(Arguments.of(defaultConfig, "with PathPattern"), Arguments.of(antPatternConfig, "with AntPathMatcher"));
}
private HandlerExecutionChain getHandler(HandlerMapping mapping, MockHttpServletRequest request) throws Exception { private HandlerExecutionChain getHandler(HandlerMapping mapping, MockHttpServletRequest request) throws Exception {
HandlerExecutionChain chain = mapping.getHandler(request); HandlerExecutionChain chain = mapping.getHandler(request);
Assert.notNull(chain, "No handler found for request: " + request.getRequestURI()); Assert.notNull(chain, "No handler found for request: " + request.getRequestURI());
@ -217,4 +212,23 @@ class SimpleUrlHandlerMappingTests {
return chain; return chain;
} }
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ParameterizedTest(name="[{index}] {0}")
@MethodSource("handlerMappings")
@interface HandlerMappingsTest {
}
static Stream<Arguments> handlerMappings() {
SimpleUrlHandlerMapping defaultConfig = new SimpleUrlHandlerMapping();
SimpleUrlHandlerMapping antPatternConfig = new SimpleUrlHandlerMapping();
antPatternConfig.setPatternParser(null);
return Stream.of(
arguments(named("with PathPattern", defaultConfig)),
arguments(named("with AntPathMatcher", antPatternConfig))
);
}
} }