spring AbstractServletHandlerMethodTests 源码
spring AbstractServletHandlerMethodTests 代码
文件路径:/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/AbstractServletHandlerMethodTests.java
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.mvc.method.annotation;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.AfterEach;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.lang.Nullable;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import org.springframework.web.testfixture.servlet.MockServletConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Base class for tests using on the DispatcherServlet and HandlerMethod infrastructure classes:
* <ul>
* <li>RequestMappingHandlerMapping
* <li>RequestMappingHandlerAdapter
* <li>ExceptionHandlerExceptionResolver
* </ul>
*
* @author Rossen Stoyanchev
*/
public abstract class AbstractServletHandlerMethodTests {
@Nullable
private DispatcherServlet servlet;
protected DispatcherServlet getServlet() {
assertThat(servlet).as("DispatcherServlet not initialized").isNotNull();
return servlet;
}
@AfterEach
public void tearDown() {
this.servlet = null;
}
/**
* Initialize a DispatcherServlet instance registering zero or more controller classes.
*/
protected WebApplicationContext initDispatcherServlet(
Class<?> controllerClass, boolean usePathPatterns) throws ServletException {
return initDispatcherServlet(controllerClass, usePathPatterns, null);
}
@SuppressWarnings("serial")
WebApplicationContext initDispatcherServlet(
@Nullable Class<?> controllerClass, boolean usePathPatterns,
@Nullable ApplicationContextInitializer<GenericWebApplicationContext> initializer)
throws ServletException {
final GenericWebApplicationContext wac = new GenericWebApplicationContext();
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
if (controllerClass != null) {
wac.registerBeanDefinition(
controllerClass.getSimpleName(), new RootBeanDefinition(controllerClass));
}
if (initializer != null) {
initializer.initialize(wac);
}
if (!wac.containsBeanDefinition("handlerMapping")) {
BeanDefinition def = register("handlerMapping", RequestMappingHandlerMapping.class, wac);
def.getPropertyValues().add("removeSemicolonContent", "false");
}
BeanDefinition mappingDef = wac.getBeanDefinition("handlerMapping");
if (!usePathPatterns) {
mappingDef.getPropertyValues().add("patternParser", null);
}
register("handlerAdapter", RequestMappingHandlerAdapter.class, wac);
register("requestMappingResolver", ExceptionHandlerExceptionResolver.class, wac);
register("responseStatusResolver", ResponseStatusExceptionResolver.class, wac);
register("defaultResolver", DefaultHandlerExceptionResolver.class, wac);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
return wac;
}
private BeanDefinition register(String beanName, Class<?> beanType, GenericWebApplicationContext wac) {
if (wac.containsBeanDefinition(beanName)) {
return wac.getBeanDefinition(beanName);
}
RootBeanDefinition beanDef = new RootBeanDefinition(beanType);
wac.registerBeanDefinition(beanName, beanDef);
return beanDef;
}
}
相关信息
相关文章
spring AbstractRequestAttributesArgumentResolverTests 源码
spring DeferredResultReturnValueHandlerTests 源码
spring ExceptionHandlerExceptionResolverTests 源码
spring ExtendedServletRequestDataBinderTests 源码
spring HandlerMethodAnnotationDetectionTests 源码
spring HttpEntityMethodProcessorMockTests 源码
spring HttpEntityMethodProcessorTests 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦