@Test @SuppressWarnings("resource") public void testWebApplicationObjectSupport() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); File tempDir = new File(""); wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, tempDir); wac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class)); wac.refresh(); WebApplicationObjectSupport wao = (WebApplicationObjectSupport) wac.getBean("test"); assertEquals(wao.getServletContext(), wac.getServletContext()); assertEquals(wao.getTempDir(), tempDir); }
@Before public void setUp() throws Exception { rb.setBasename(PROPS_FILE); rb.setCache(getCache()); rb.setDefaultParentView("testParent"); wac.setServletContext(new MockServletContext()); wac.refresh(); // This will be propagated to views, so we need it. rb.setApplicationContext(wac); }
@Before public void setUp() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); this.renderer = mock(Renderer.class); this.viewResolver = new TilesViewResolver(); this.viewResolver.setRenderer(this.renderer); this.viewResolver.setApplicationContext(wac); }
@Test @SuppressWarnings("resource") public void testServletContextParameterFactoryBeanWithAttributeNotFound() { MockServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("initParamName", "myParam"); wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs); try { wac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getCause().getMessage().contains("myParam")); } }
@Test @SuppressWarnings("resource") public void testServletContextAttributeFactoryBeanWithAttributeNotFound() { MockServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("attributeName", "myAttr"); wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs); try { wac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getCause().getMessage().contains("myAttr")); } }
@Test public void testXmlViewResolverDefaultLocation() { StaticWebApplicationContext wac = new StaticWebApplicationContext() { @Override protected Resource getResourceByPath(String path) { assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path)); return super.getResourceByPath(path); } }; wac.setServletContext(new MockServletContext()); wac.refresh(); XmlViewResolver vr = new XmlViewResolver(); try { vr.setApplicationContext(wac); vr.afterPropertiesSet(); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected } }
@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); }
@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)); }
/** * 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)); }
/** * 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)); }
@Test @SuppressWarnings("resource") public void testServletContextAttributeFactoryBean() { MockServletContext sc = new MockServletContext(); sc.setAttribute("myAttr", "myValue"); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("attributeName", "myAttr"); wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs); wac.refresh(); Object value = wac.getBean("importedAttr"); assertEquals("myValue", value); }
@Before public void setUp() { this.context = new StaticWebApplicationContext(); this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader())); this.context.registerSingleton("controller", TestController.class); this.context.registerSingleton("userController", UserController.class); this.config = new TestWebMvcConfigurationSupport(); this.config.setApplicationContext(this.context); this.config.setServletContext(this.context.getServletContext()); }
@Test @SuppressWarnings("resource") public void testServletContextParameterFactoryBean() { MockServletContext sc = new MockServletContext(); sc.addInitParameter("myParam", "myValue"); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("initParamName", "myParam"); wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs); wac.refresh(); Object value = wac.getBean("importedParam"); assertEquals("myValue", value); }
@Test public void missingTemplate() throws Exception { MockServletContext servletContext = new MockServletContext(); this.wac.setServletContext(servletContext); this.wac.refresh(); this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/"); this.view.setUrl("missing.txt"); this.view.setEngine(mock(InvocableScriptEngine.class)); this.configurer.setRenderFunction("render"); this.view.setApplicationContext(this.wac); assertFalse(this.view.checkResource(Locale.ENGLISH)); }
@Before public void setUp() throws Exception { ServletContext sc = new MockServletContext(); wac = new StaticWebApplicationContext(); wac.setServletContext(sc); // final Template expectedTemplate = new Template(); fc = new FreeMarkerConfigurer(); fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir")); fc.setServletContext(sc); fc.afterPropertiesSet(); wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc); wac.refresh(); request = new MockHttpServletRequest(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); response = new MockHttpServletResponse(); }
@Before public void setUp() throws Exception { MockServletContext servletContext = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(servletContext); wac.refresh(); request = new MockHttpServletRequest(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); response = new MockHttpServletResponse(); renderer = mock(Renderer.class); view = new TilesView(); view.setServletContext(servletContext); view.setRenderer(renderer); view.setUrl(VIEW_PATH); view.afterPropertiesSet(); }
@Test public void testProcessInjectionBasedOnServletContext() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(wac); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "tb"); wac.registerSingleton("testBean", TestBean.class, pvs); MockServletContext sc = new MockServletContext(); wac.setServletContext(sc); wac.refresh(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); InjectionTarget target = new InjectionTarget(); SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(target, sc); assertTrue(target.testBean instanceof TestBean); assertEquals("tb", target.name); }
@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); }
@Test public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException { MockServletContext 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"); DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter"); filterProxy.init(new MockFilterConfig(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); }
@Test public void updateTargetUrl() throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class); wac.setServletContext(new MockServletContext()); wac.refresh(); RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class); wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor); RedirectView rv = new RedirectView(); rv.setApplicationContext(wac); // Init RedirectView with WebAppCxt rv.setUrl("/path"); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123"); rv.render(new ModelMap(), request, response); verify(mockProcessor).processUrl(request, "/path"); }