Java Code Examples for org.springframework.web.context.support.StaticWebApplicationContext#getServletContext()
The following examples show how to use
org.springframework.web.context.support.StaticWebApplicationContext#getServletContext() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViewResolverTests.java From spring-analysis-note with MIT License | 5 votes |
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); vr.setApplicationContext(wac); vr.setContentType("myContentType"); vr.setRequestContextAttribute("rc"); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext()); HttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); Map<String, Object> model = new HashMap<>(); TestBean tb = new TestBean(); model.put("tb", tb); view.render(model, request, response); assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb"))); assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
Example 2
Source File: CommonsMultipartResolverTests.java From spring-analysis-note with MIT License | 5 votes |
@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); }
Example 3
Source File: ViewResolverTests.java From java-technology-stack with MIT License | 5 votes |
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); vr.setApplicationContext(wac); vr.setContentType("myContentType"); vr.setRequestContextAttribute("rc"); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext()); HttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); Map<String, Object> model = new HashMap<>(); TestBean tb = new TestBean(); model.put("tb", tb); view.render(model, request, response); assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb"))); assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
Example 4
Source File: CommonsMultipartResolverTests.java From java-technology-stack with MIT License | 5 votes |
@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); }
Example 5
Source File: ViewResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); vr.setApplicationContext(wac); vr.setContentType("myContentType"); vr.setRequestContextAttribute("rc"); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext()); HttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); Map model = new HashMap(); TestBean tb = new TestBean(); model.put("tb", tb); view.render(model, request, response); assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb"))); assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
Example 6
Source File: CommonsMultipartResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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<MultipartFile>(); 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); }
Example 7
Source File: OpenSessionInViewTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test public void testOpenSessionInViewFilterAsyncScenario() throws Exception { final SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); // Initial request during which concurrent handling starts.. given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); final AtomicInteger count = new AtomicInteger(0); final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); filter.init(filterConfig); final FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { assertTrue(TransactionSynchronizationManager.hasResource(sf)); count.incrementAndGet(); } }; AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response); WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request); asyncManager.setTaskExecutor(new SyncTaskExecutor()); asyncManager.setAsyncWebRequest(asyncWebRequest); asyncManager.startCallableProcessing(new Callable<String>() { @Override public String call() throws Exception { return "anything"; } }); assertFalse(TransactionSynchronizationManager.hasResource(sf)); filter.doFilter(this.request, this.response, filterChain); assertFalse(TransactionSynchronizationManager.hasResource(sf)); assertEquals(1, count.get()); verify(session, never()).close(); // Async dispatch after concurrent handling produces result ... this.request.setAsyncStarted(false); assertFalse(TransactionSynchronizationManager.hasResource(sf)); filter.doFilter(this.request, this.response, filterChain); assertFalse(TransactionSynchronizationManager.hasResource(sf)); assertEquals(2, count.get()); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); wac.close(); }
Example 8
Source File: OpenSessionInViewTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test public void testOpenSessionInViewFilterWithSingleSessionAndPreBoundSession() throws Exception { final SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory"); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf); interceptor.preHandle(this.webRequest); final OpenSessionInViewFilter filter = new OpenSessionInViewFilter(); filter.init(filterConfig); final FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { assertTrue(TransactionSynchronizationManager.hasResource(sf)); servletRequest.setAttribute("invoked", Boolean.TRUE); } }; assertTrue(TransactionSynchronizationManager.hasResource(sf)); filter.doFilter(this.request, this.response, filterChain); assertTrue(TransactionSynchronizationManager.hasResource(sf)); assertNotNull(this.request.getAttribute("invoked")); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); wac.close(); }