Java Code Examples for org.springframework.web.context.support.StaticWebApplicationContext#setServletContext()
The following examples show how to use
org.springframework.web.context.support.StaticWebApplicationContext#setServletContext() .
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: DelegatingFilterProxyTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException { ServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.registerSingleton("targetFilter", MockFilter.class); wac.refresh(); sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac); MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); MockFilterConfig proxyConfig = new MockFilterConfig(sc); proxyConfig.addInitParameter("targetBeanName", "targetFilter"); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(); filterProxy.init(proxyConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
Example 2
Source File: ViewResolverTests.java From java-technology-stack with MIT License | 6 votes |
private void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); vr.setPrefix("/WEB-INF/"); vr.setSuffix(".jsp"); vr.setApplicationContext(wac); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "/WEB-INF/example1.jsp", ((InternalResourceView) view).getUrl()); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "/WEB-INF/example2.jsp", ((InternalResourceView) view).getUrl()); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
Example 3
Source File: ViewResolverTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testBeanNameViewResolver() throws ServletException { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); MutablePropertyValues pvs1 = new MutablePropertyValues(); pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp")); wac.registerSingleton("example1", InternalResourceView.class, pvs1); MutablePropertyValues pvs2 = new MutablePropertyValues(); pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp")); wac.registerSingleton("example2", JstlView.class, pvs2); BeanNameViewResolver vr = new BeanNameViewResolver(); vr.setApplicationContext(wac); wac.refresh(); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl()); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl()); }
Example 4
Source File: ViewResolverTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testCacheRemoval() throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); InternalResourceViewResolver vr = new InternalResourceViewResolver(); vr.setViewClass(JstlView.class); vr.setApplicationContext(wac); View view = vr.resolveViewName("example1", Locale.getDefault()); View cached = vr.resolveViewName("example1", Locale.getDefault()); if (view != cached) { fail("Caching doesn't work"); } vr.removeFromCache("example1", Locale.getDefault()); cached = vr.resolveViewName("example1", Locale.getDefault()); if (view == cached) { // the chance of having the same reference (hashCode) twice if negligible). fail("View wasn't removed from cache"); } }
Example 5
Source File: ContentNegotiatingViewResolverTests.java From spring-analysis-note with MIT License | 6 votes |
@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); assertNotNull("Invalid view", result); }
Example 6
Source File: DefaultMockMvcBuilderTests.java From spring-analysis-note with MIT License | 6 votes |
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributeNotPreviouslySetWithContextHierarchy() { StaticApplicationContext ear = new StaticApplicationContext(); StaticWebApplicationContext root = new StaticWebApplicationContext(); root.setParent(ear); root.setServletContext(this.servletContext); StaticWebApplicationContext dispatcher = new StaticWebApplicationContext(); dispatcher.setParent(root); dispatcher.setServletContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(dispatcher); WebApplicationContext wac = builder.initWebAppContext(); assertSame(dispatcher, wac); assertSame(root, wac.getParent()); assertSame(ear, wac.getParent().getParent()); assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
Example 7
Source File: DelegatingFilterProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException { ServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.registerSingleton("targetFilter", MockFilter.class); wac.refresh(); sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac); MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); MockFilterConfig proxyConfig = new MockFilterConfig(sc); proxyConfig.addInitParameter("targetBeanName", "targetFilter"); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(); filterProxy.init(proxyConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
Example 8
Source File: DelegatingFilterProxyTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext() throws ServletException, IOException { MockServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.registerSingleton("targetFilter", MockFilter.class); // wac.refresh(); // note that the context is not set as the ROOT attribute in the ServletContext! DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", wac); filterProxy.init(new MockFilterConfig(sc)); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
Example 9
Source File: ViewResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception { MockServletContext sc = new MockServletContext(); final StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.registerSingleton("myBean", TestBean.class); wac.registerSingleton("myBean2", TestBean.class); wac.setServletContext(sc); wac.refresh(); InternalResourceViewResolver vr = new InternalResourceViewResolver(); Properties props = new Properties(); props.setProperty("key1", "value1"); vr.setAttributes(props); Map map = new HashMap(); map.put("key2", new Integer(2)); vr.setAttributesMap(map); vr.setExposedContextBeanNames(new String[] {"myBean2"}); vr.setApplicationContext(wac); MockHttpServletRequest request = new MockHttpServletRequest(sc) { @Override public RequestDispatcher getRequestDispatcher(String path) { return new MockRequestDispatcher(path) { @Override public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) { assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null); assertEquals("value1", forwardRequest.getAttribute("key1")); assertEquals(new Integer(2), forwardRequest.getAttribute("key2")); assertNull(forwardRequest.getAttribute("myBean")); assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2")); } }; } }; HttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); View view = vr.resolveViewName("example1", Locale.getDefault()); view.render(new HashMap(), request, response); }
Example 10
Source File: DelegatingFilterProxyTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testDelegatingFilterProxyNotInjectedWithRootPreferred() throws ServletException, IOException { ServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.refresh(); sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac); sc.setAttribute("another", wac); StaticWebApplicationContext wacToUse = new StaticWebApplicationContext(); wacToUse.setServletContext(sc); String beanName = "targetFilter"; wacToUse.registerSingleton(beanName, MockFilter.class); wacToUse.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wacToUse); MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName); filterProxy.setServletContext(sc); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
Example 11
Source File: ContentNegotiatingViewResolverTests.java From spring-analysis-note with MIT License | 5 votes |
@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()); }
Example 12
Source File: ContentNegotiatingViewResolverTests.java From spring-analysis-note with MIT License | 5 votes |
@Before 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)); }
Example 13
Source File: DefaultMockMvcBuilderTests.java From java-technology-stack with MIT License | 5 votes |
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributePreviouslySetWithContextHierarchy() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, root); StaticWebApplicationContext child = new StaticWebApplicationContext(); child.setParent(root); child.setServletContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(child); assertSame(builder.initWebAppContext().getParent(), WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
Example 14
Source File: ContentNegotiatingViewResolverTests.java From java-technology-stack with MIT License | 5 votes |
@Before 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)); }
Example 15
Source File: ViewResolverTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception { MockServletContext sc = new MockServletContext(); final StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.registerSingleton("myBean", TestBean.class); wac.registerSingleton("myBean2", TestBean.class); wac.setServletContext(sc); wac.refresh(); InternalResourceViewResolver vr = new InternalResourceViewResolver(); Properties props = new Properties(); props.setProperty("key1", "value1"); vr.setAttributes(props); Map<String, Object> map = new HashMap<>(); map.put("key2", new Integer(2)); vr.setAttributesMap(map); vr.setExposedContextBeanNames(new String[] {"myBean2"}); vr.setApplicationContext(wac); MockHttpServletRequest request = new MockHttpServletRequest(sc) { @Override public RequestDispatcher getRequestDispatcher(String path) { return new MockRequestDispatcher(path) { @Override public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) { assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null); assertEquals("value1", forwardRequest.getAttribute("key1")); assertEquals(new Integer(2), forwardRequest.getAttribute("key2")); assertNull(forwardRequest.getAttribute("myBean")); assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2")); } }; } }; HttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); View view = vr.resolveViewName("example1", Locale.getDefault()); view.render(new HashMap<String, Object>(), request, response); }
Example 16
Source File: DelegatingFilterProxyTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testDelegatingFilterProxy() throws ServletException, IOException { ServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.registerSingleton("targetFilter", MockFilter.class); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); MockFilterConfig proxyConfig = new MockFilterConfig(sc); proxyConfig.addInitParameter("targetBeanName", "targetFilter"); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(); filterProxy.init(proxyConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); filterProxy.doFilter(request, response, null); assertNull(targetFilter.filterConfig); assertEquals(Boolean.TRUE, request.getAttribute("called")); filterProxy.destroy(); assertNull(targetFilter.filterConfig); }
Example 17
Source File: DispatcherServletTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void detectHandlerMappingFromParent() throws ServletException, IOException { // create a parent context that includes a mapping StaticWebApplicationContext parent = new StaticWebApplicationContext(); parent.setServletContext(getServletContext()); parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues()); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler")); parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs); parent.refresh(); DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); // will have parent complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); ServletConfig config = new MockServletConfig(getServletContext(), "complex"); config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent); complexDispatcherServlet.init(config); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(), response.getStatus() == HttpServletResponse.SC_NOT_FOUND); }
Example 18
Source File: ViewResolverTests.java From spring-analysis-note with MIT License | 4 votes |
@Test public void testInternalResourceViewResolverWithAttributes() throws Exception { MockServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.refresh(); InternalResourceViewResolver vr = new InternalResourceViewResolver(); Properties props = new Properties(); props.setProperty("key1", "value1"); vr.setAttributes(props); Map<String, Object> map = new HashMap<>(); map.put("key2", new Integer(2)); vr.setAttributesMap(map); vr.setApplicationContext(wac); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl()); Map<String, Object> attributes = ((InternalResourceView) view).getStaticAttributes(); assertEquals("value1", attributes.get("key1")); assertEquals(new Integer(2), attributes.get("key2")); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl()); attributes = ((InternalResourceView) view).getStaticAttributes(); assertEquals("value1", attributes.get("key1")); assertEquals(new Integer(2), attributes.get("key2")); MockHttpServletRequest request = new MockHttpServletRequest(sc); HttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); 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") == null); assertEquals("value1", request.getAttribute("key1")); assertEquals(new Integer(2), request.getAttribute("key2")); }
Example 19
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 20
Source File: ViewResolverTests.java From java-technology-stack with MIT License | 4 votes |
@Test public void testInternalResourceViewResolverWithAttributes() throws Exception { MockServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.refresh(); InternalResourceViewResolver vr = new InternalResourceViewResolver(); Properties props = new Properties(); props.setProperty("key1", "value1"); vr.setAttributes(props); Map<String, Object> map = new HashMap<>(); map.put("key2", new Integer(2)); vr.setAttributesMap(map); vr.setApplicationContext(wac); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl()); Map<String, Object> attributes = ((InternalResourceView) view).getStaticAttributes(); assertEquals("value1", attributes.get("key1")); assertEquals(new Integer(2), attributes.get("key2")); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl()); attributes = ((InternalResourceView) view).getStaticAttributes(); assertEquals("value1", attributes.get("key1")); assertEquals(new Integer(2), attributes.get("key2")); MockHttpServletRequest request = new MockHttpServletRequest(sc); HttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); 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") == null); assertEquals("value1", request.getAttribute("key1")); assertEquals(new Integer(2), request.getAttribute("key2")); }