use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class RequestAndSessionScopedBeanTests method testPutBeanInRequest.
@Test
@SuppressWarnings("resource")
public void testPutBeanInRequest() {
String targetBeanName = "target";
StaticWebApplicationContext wac = new StaticWebApplicationContext();
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setScope(WebApplicationContext.SCOPE_REQUEST);
bd.getPropertyValues().add("name", "abc");
wac.registerBeanDefinition(targetBeanName, bd);
wac.refresh();
HttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
TestBean target = (TestBean) wac.getBean(targetBeanName);
assertThat(target.getName()).isEqualTo("abc");
assertThat(request.getAttribute(targetBeanName)).isSameAs(target);
TestBean target2 = (TestBean) wac.getBean(targetBeanName);
assertThat(target2.getName()).isEqualTo("abc");
assertThat(target).isSameAs(target2);
assertThat(request.getAttribute(targetBeanName)).isSameAs(target2);
request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
TestBean target3 = (TestBean) wac.getBean(targetBeanName);
assertThat(target3.getName()).isEqualTo("abc");
assertThat(request.getAttribute(targetBeanName)).isSameAs(target3);
assertThat(target).isNotSameAs(target3);
RequestContextHolder.setRequestAttributes(null);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> wac.getBean(targetBeanName));
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class WebSocketUpgradeHandlerPredicateTests method simpleUrlHandlerMapping.
@Test
void simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(Collections.singletonMap("/path", this.webSocketHandler));
mapping.setApplicationContext(new StaticWebApplicationContext());
Object actual = mapping.getHandler(httpGetExchange).block();
assertThat(actual).as("Should match HTTP GET by URL path").isSameAs(this.webSocketHandler);
mapping.setHandlerPredicate(new WebSocketUpgradeHandlerPredicate());
actual = mapping.getHandler(this.httpGetExchange).block();
assertThat(actual).as("Should not match if not a WebSocket upgrade").isNull();
actual = mapping.getHandler(this.httpPostExchange).block();
assertThat(actual).as("Should not match if not a WebSocket upgrade").isNull();
actual = mapping.getHandler(this.webSocketExchange).block();
assertThat(actual).as("Should match WebSocket upgrade").isSameAs(this.webSocketHandler);
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class HandlerMappingIntrospectorTests method detectHandlerMappings.
@Test
void detectHandlerMappings() {
StaticWebApplicationContext context = new StaticWebApplicationContext();
context.registerSingleton("A", SimpleUrlHandlerMapping.class);
context.registerSingleton("B", SimpleUrlHandlerMapping.class);
context.registerSingleton("C", SimpleUrlHandlerMapping.class);
context.refresh();
List<?> expected = Arrays.asList(context.getBean("A"), context.getBean("B"), context.getBean("C"));
List<HandlerMapping> actual = initIntrospector(context).getHandlerMappings();
assertThat(actual).isEqualTo(expected);
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class ViewResolverTests method xmlViewResolverDefaultLocation.
@Test
@SuppressWarnings("deprecation")
public void xmlViewResolverDefaultLocation() {
StaticWebApplicationContext wac = new StaticWebApplicationContext() {
@Override
protected Resource getResourceByPath(String path) {
assertThat(org.springframework.web.servlet.view.XmlViewResolver.DEFAULT_LOCATION.equals(path)).as("Correct default location").isTrue();
return super.getResourceByPath(path);
}
};
wac.setServletContext(this.sc);
wac.refresh();
org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
vr.setApplicationContext(wac);
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(vr::afterPropertiesSet);
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class FreeMarkerViewTests method freeMarkerViewResolver.
@Test
public void freeMarkerViewResolver() throws Exception {
MockServletContext sc = new MockServletContext();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
wac.getBeanFactory().registerSingleton("configurer", configurer);
wac.refresh();
FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix");
vr.setApplicationContext(wac);
View view = vr.resolveViewName("test", Locale.CANADA);
assertThat(view.getClass()).as("Correct view class").isEqualTo(FreeMarkerView.class);
assertThat(((FreeMarkerView) view).getUrl()).as("Correct URL").isEqualTo("prefix_test_suffix");
view = vr.resolveViewName("non-existing", Locale.CANADA);
assertThat(view).isNull();
view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Aggregations