Browse Source

StandaloneMockMvcBuilder allows custom HandlerMapping

Issue: SPR-15472
pull/1407/head
Rossen Stoyanchev 9 years ago
parent
commit
191bbc20dc
  1. 42
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java

42
spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.BeanInitializationException;
@ -85,7 +86,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
*/ */
public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneMockMvcBuilder> { public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneMockMvcBuilder> {
private final Object[] controllers; private final List<Object> controllers;
private List<Object> controllerAdvice; private List<Object> controllerAdvice;
@ -121,6 +122,8 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
private Map<String, String> placeholderValues = new HashMap<>(); private Map<String, String> placeholderValues = new HashMap<>();
private Supplier<RequestMappingHandlerMapping> handlerMappingFactory = RequestMappingHandlerMapping::new;
/** /**
* Protected constructor. Not intended for direct instantiation. * Protected constructor. Not intended for direct instantiation.
@ -128,7 +131,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
*/ */
protected StandaloneMockMvcBuilder(Object... controllers) { protected StandaloneMockMvcBuilder(Object... controllers) {
Assert.isTrue(!ObjectUtils.isEmpty(controllers), "At least one controller is required"); Assert.isTrue(!ObjectUtils.isEmpty(controllers), "At least one controller is required");
this.controllers = controllers; this.controllers = Arrays.asList(controllers);
} }
/** /**
@ -324,6 +327,17 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
return this; return this;
} }
/**
* Configure factory to create a custom {@link RequestMappingHandlerMapping}.
* @param factory the factory
* @since 5.0
*/
public StandaloneMockMvcBuilder setCustomHandlerMapping(Supplier<RequestMappingHandlerMapping> factory) {
Assert.notNull(factory, "RequestMappingHandlerMapping supplier is required.");
this.handlerMappingFactory = factory;
return this;
}
@Override @Override
protected WebApplicationContext initWebAppContext() { protected WebApplicationContext initWebAppContext() {
@ -338,13 +352,13 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
StandaloneConfiguration config = new StandaloneConfiguration(); StandaloneConfiguration config = new StandaloneConfiguration();
config.setApplicationContext(wac); config.setApplicationContext(wac);
wac.addBeans(this.controllers);
wac.addBeans(this.controllerAdvice); wac.addBeans(this.controllerAdvice);
StaticRequestMappingHandlerMapping hm = config.getHandlerMapping(); RequestMappingHandlerMapping hm = config.getHandlerMapping();
hm.setServletContext(wac.getServletContext()); hm.setServletContext(wac.getServletContext());
hm.setApplicationContext(wac); hm.setApplicationContext(wac);
hm.afterPropertiesSet(); hm.afterPropertiesSet();
hm.registerHandlers(this.controllers);
wac.addBean("requestMappingHandlerMapping", hm); wac.addBean("requestMappingHandlerMapping", hm);
RequestMappingHandlerAdapter handlerAdapter = config.requestMappingHandlerAdapter(); RequestMappingHandlerAdapter handlerAdapter = config.requestMappingHandlerAdapter();
@ -379,8 +393,8 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
/** Using the MVC Java configuration as the starting point for the "standalone" setup */ /** Using the MVC Java configuration as the starting point for the "standalone" setup */
private class StandaloneConfiguration extends WebMvcConfigurationSupport { private class StandaloneConfiguration extends WebMvcConfigurationSupport {
public StaticRequestMappingHandlerMapping getHandlerMapping() { public RequestMappingHandlerMapping getHandlerMapping() {
StaticRequestMappingHandlerMapping handlerMapping = new StaticRequestMappingHandlerMapping(); RequestMappingHandlerMapping handlerMapping = handlerMappingFactory.get();
handlerMapping.setEmbeddedValueResolver(new StaticStringValueResolver(placeholderValues)); handlerMapping.setEmbeddedValueResolver(new StaticStringValueResolver(placeholderValues));
handlerMapping.setUseSuffixPatternMatch(useSuffixPatternMatch); handlerMapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
handlerMapping.setUseTrailingSlashMatch(useTrailingSlashPatternMatch); handlerMapping.setUseTrailingSlashMatch(useTrailingSlashPatternMatch);
@ -470,20 +484,6 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
} }
} }
/**
* A {@code RequestMappingHandlerMapping} that allows registration of controllers.
*/
private static class StaticRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
public void registerHandlers(Object...handlers) {
for (Object handler : handlers) {
detectHandlerMethods(handler);
}
}
}
/** /**
* A static resolver placeholder for values embedded in request mappings. * A static resolver placeholder for values embedded in request mappings.
*/ */

Loading…
Cancel
Save