use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class CrossOriginTests method pathPatternsArguments.
@SuppressWarnings("unused")
static Stream<TestRequestMappingInfoHandlerMapping> pathPatternsArguments() {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
Properties props = new Properties();
props.setProperty("myOrigin", "https://example.com");
props.setProperty("myDomainPattern", "http://*.example.com");
wac.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props));
wac.registerSingleton("ppc", PropertySourcesPlaceholderConfigurer.class);
wac.refresh();
TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
mapping1.setPatternParser(new PathPatternParser());
wac.getAutowireCapableBeanFactory().initializeBean(mapping1, "mapping1");
TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
wac.getAutowireCapableBeanFactory().initializeBean(mapping2, "mapping2");
wac.close();
return Stream.of(mapping1, mapping2);
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class ControllerTests method doTestServletForwardingController.
private void doTestServletForwardingController(ServletForwardingController sfc, boolean include) throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
ServletContext context = mock(ServletContext.class);
RequestDispatcher dispatcher = mock(RequestDispatcher.class);
given(request.getMethod()).willReturn("GET");
given(context.getNamedDispatcher("action")).willReturn(dispatcher);
if (include) {
given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somePath");
} else {
given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
}
StaticWebApplicationContext sac = new StaticWebApplicationContext();
sac.setServletContext(context);
sfc.setApplicationContext(sac);
assertThat(sfc.handleRequest(request, response)).isNull();
if (include) {
verify(dispatcher).include(request, response);
} else {
verify(dispatcher).forward(request, response);
}
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method createViewResolver.
@BeforeEach
public void createViewResolver() {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.refresh();
viewResolver = new ContentNegotiatingViewResolver();
viewResolver.setApplicationContext(wac);
request = new MockHttpServletRequest("GET", "/test");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method nestedViewResolverIsNotSpringBean.
@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
webAppContext.setServletContext(new MockServletContext());
webAppContext.refresh();
InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
nestedResolver.setApplicationContext(webAppContext);
nestedResolver.setViewClass(InternalResourceView.class);
viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));
FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
viewResolver.afterPropertiesSet();
String viewName = "view";
Locale locale = Locale.ENGLISH;
View result = viewResolver.resolveViewName(viewName, locale);
assertThat(result).as("Invalid view").isNotNull();
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewFilter.
@Test
public void testOpenEntityManagerInViewFilter() throws Exception {
given(manager.isOpen()).willReturn(true);
final EntityManagerFactory factory2 = mock(EntityManagerFactory.class);
final EntityManager manager2 = mock(EntityManager.class);
given(factory2.createEntityManager()).willReturn(manager2);
given(manager2.isOpen()).willReturn(true);
MockServletContext sc = new MockServletContext();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
wac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", factory);
wac.getDefaultListableBeanFactory().registerSingleton("myEntityManagerFactory", factory2);
wac.refresh();
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
filterConfig2.addInitParameter("entityManagerFactoryBeanName", "myEntityManagerFactory");
final OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter();
filter.init(filterConfig);
final OpenEntityManagerInViewFilter filter2 = new OpenEntityManagerInViewFilter();
filter2.init(filterConfig2);
final FilterChain filterChain = (servletRequest, servletResponse) -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
servletRequest.setAttribute("invoked", Boolean.TRUE);
};
final FilterChain filterChain2 = (servletRequest, servletResponse) -> {
assertThat(TransactionSynchronizationManager.hasResource(factory2)).isTrue();
filter.doFilter(servletRequest, servletResponse, filterChain);
};
FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2);
assertThat(TransactionSynchronizationManager.hasResource(factory)).isFalse();
assertThat(TransactionSynchronizationManager.hasResource(factory2)).isFalse();
filter2.doFilter(request, response, filterChain3);
assertThat(TransactionSynchronizationManager.hasResource(factory)).isFalse();
assertThat(TransactionSynchronizationManager.hasResource(factory2)).isFalse();
assertThat(request.getAttribute("invoked")).isNotNull();
verify(manager).close();
verify(manager2).close();
wac.close();
}
Aggregations