use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNameRedirectView.
@Test
public void resolveViewNameRedirectView() throws Exception {
request.addHeader("Accept", "application/json");
request.setRequestURI("/test");
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
webAppContext.setServletContext(new MockServletContext());
webAppContext.refresh();
UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
urlViewResolver.setApplicationContext(webAppContext);
ViewResolver xmlViewResolver = mock(ViewResolver.class);
viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));
View xmlView = mock(View.class, "application_xml");
View jsonView = mock(View.class, "application_json");
viewResolver.setDefaultViews(Arrays.asList(jsonView));
viewResolver.afterPropertiesSet();
String viewName = "redirect:anotherTest";
Locale locale = Locale.ENGLISH;
given(xmlViewResolver.resolveViewName(viewName, locale)).willReturn(xmlView);
given(jsonView.getContentType()).willReturn("application/json");
View actualView = viewResolver.resolveViewName(viewName, locale);
assertEquals("Invalid view", RedirectView.class, actualView.getClass());
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-security by spring-projects.
the class AuthorizeTagTests method setUp.
// ~ Methods
// ========================================================================================================
@Before
public void setUp() throws Exception {
SecurityContextHolder.getContext().setAuthentication(currentUser);
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
BeanDefinitionBuilder webExpressionHandler = BeanDefinitionBuilder.rootBeanDefinition(DefaultWebSecurityExpressionHandler.class);
webExpressionHandler.addPropertyValue("permissionEvaluator", permissionEvaluator);
ctx.registerBeanDefinition("expressionHandler", webExpressionHandler.getBeanDefinition());
ctx.registerSingleton("wipe", MockWebInvocationPrivilegeEvaluator.class);
MockServletContext servletCtx = new MockServletContext();
servletCtx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
authorizeTag = new JspAuthorizeTag();
authorizeTag.setPageContext(new MockPageContext(servletCtx, request, new MockHttpServletResponse()));
}
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() throws Exception {
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);
assertEquals("abc", target.getName());
assertSame(target, request.getAttribute(targetBeanName));
TestBean target2 = (TestBean) wac.getBean(targetBeanName);
assertEquals("abc", target2.getName());
assertSame(target2, target);
assertSame(target2, request.getAttribute(targetBeanName));
request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
TestBean target3 = (TestBean) wac.getBean(targetBeanName);
assertEquals("abc", target3.getName());
assertSame(target3, request.getAttribute(targetBeanName));
assertNotSame(target3, target);
RequestContextHolder.setRequestAttributes(null);
try {
wac.getBean(targetBeanName);
fail("Should have thrown BeanCreationException");
} catch (BeanCreationException ex) {
// expected
}
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class CommonsMultipartResolverTests method withServletContextAndFilterWithCustomBeanName.
@Test
public void withServletContextAndFilterWithCustomBeanName() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.refresh();
wac.registerSingleton("myMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
filterConfig.addInitParameter("multipartResolverBeanName", "myMultipartResolver");
final List<MultipartFile> files = new ArrayList<>();
FilterChain filterChain = new FilterChain() {
@Override
public void doFilter(ServletRequest originalRequest, ServletResponse response) {
if (originalRequest instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest request = (MultipartHttpServletRequest) originalRequest;
files.addAll(request.getFileMap().values());
}
}
};
MultipartFilter filter = new MultipartFilter() {
private boolean invoked = false;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
super.doFilterInternal(request, response, filterChain);
super.doFilterInternal(request, response, filterChain);
if (invoked) {
throw new ServletException("Should not have been invoked twice");
}
invoked = true;
}
};
filter.init(filterConfig);
MockHttpServletRequest originalRequest = new MockHttpServletRequest();
originalRequest.setMethod("POST");
originalRequest.setContentType("multipart/form-data");
originalRequest.addHeader("Content-type", "multipart/form-data");
HttpServletResponse response = new MockHttpServletResponse();
filter.doFilter(originalRequest, response, filterChain);
CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
assertTrue(((MockFileItem) file1.getFileItem()).deleted);
assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class CommonsMultipartResolverTests method withServletContextAndFilter.
@Test
public void withServletContextAndFilter() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
wac.refresh();
wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
filterConfig.addInitParameter("class", "notWritable");
filterConfig.addInitParameter("unknownParam", "someValue");
final MultipartFilter filter = new MultipartFilter();
filter.init(filterConfig);
final List<MultipartFile> files = new ArrayList<>();
final FilterChain filterChain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
files.addAll(request.getFileMap().values());
}
};
FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);
MockHttpServletRequest originalRequest = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
originalRequest.setMethod("POST");
originalRequest.setContentType("multipart/form-data");
originalRequest.addHeader("Content-type", "multipart/form-data");
filter.doFilter(originalRequest, response, filterChain2);
CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
assertTrue(((MockFileItem) file1.getFileItem()).deleted);
assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
Aggregations