use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceTests method testServletContextResource.
@Test
public void testServletContextResource() throws IOException {
MockServletContext sc = new MockServletContext();
Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class");
doTestResource(resource);
assertThat(new ServletContextResource(sc, "org/springframework/core/../core/io/./Resource.class")).isEqualTo(resource);
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class SpringBeanAutowiringSupportTests method testProcessInjectionBasedOnServletContext.
@Test
public void testProcessInjectionBasedOnServletContext() {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "tb");
wac.registerSingleton("testBean", TestBean.class, pvs);
MockServletContext sc = new MockServletContext();
wac.setServletContext(sc);
wac.refresh();
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
InjectionTarget target = new InjectionTarget();
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(target, sc);
boolean condition = target.testBean instanceof TestBean;
assertThat(condition).isTrue();
assertThat(target.name).isEqualTo("tb");
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.
@PathPatternsParameterizedTest
void parameterDispatchingController(boolean usePathPatterns) throws Exception {
final MockServletContext servletContext = new MockServletContext();
final MockServletConfig servletConfig = new MockServletConfig(servletContext);
WebApplicationContext webAppContext = initDispatcherServlet(MyParameterDispatchingController.class, usePathPatterns, wac -> {
wac.setServletContext(servletContext);
AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
});
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
HttpSession session = request.getSession();
assertThat(session).isNotNull();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView");
assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
assertThat(request.getAttribute("locale")).isSameAs(request.getLocale());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
response = new MockHttpServletResponse();
session = request.getSession();
assertThat(session).isNotNull();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView");
assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "other");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myOtherView");
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myLangView");
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("surprise", "!");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("mySurpriseView");
MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
assertThat(deserialized.request).isNotNull();
assertThat(deserialized.session).isNotNull();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceUrlProviderTests method initializeOnce.
// SPR-12592
@Test
@SuppressWarnings("resource")
void initializeOnce() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(HandlerMappingConfiguration.class);
context.refresh();
assertThat(context.getBean(ResourceUrlProvider.class).getHandlerMap()).hasKeySatisfying(pathPatternStringOf("/resources/**"));
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceUrlProviderTests method initializeOnCurrentContext.
@Test
void initializeOnCurrentContext() {
AnnotationConfigWebApplicationContext parentContext = new AnnotationConfigWebApplicationContext();
parentContext.setServletContext(new MockServletContext());
parentContext.register(ParentHandlerMappingConfiguration.class);
AnnotationConfigWebApplicationContext childContext = new AnnotationConfigWebApplicationContext();
childContext.setParent(parentContext);
childContext.setServletContext(new MockServletContext());
childContext.register(HandlerMappingConfiguration.class);
parentContext.refresh();
childContext.refresh();
ResourceUrlProvider parentUrlProvider = parentContext.getBean(ResourceUrlProvider.class);
assertThat(parentUrlProvider.getHandlerMap()).isEmpty();
ResourceUrlProvider childUrlProvider = childContext.getBean(ResourceUrlProvider.class);
assertThat(childUrlProvider.getHandlerMap()).hasKeySatisfying(pathPatternStringOf("/resources/**"));
childContext.close();
parentContext.close();
}
Aggregations