Java Code Examples for org.springframework.mock.web.test.MockServletContext#setAttribute()
The following examples show how to use
org.springframework.mock.web.test.MockServletContext#setAttribute() .
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: AbstractTagTests.java From spring-analysis-note with MIT License | 6 votes |
protected MockPageContext createPageContext() { MockServletContext sc = new MockServletContext(); SimpleWebApplicationContext wac = new SimpleWebApplicationContext(); wac.setServletContext(sc); wac.setNamespace("test"); wac.refresh(); MockHttpServletRequest request = new MockHttpServletRequest(sc); MockHttpServletResponse response = new MockHttpServletResponse(); if (inDispatcherServlet()) { request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); LocaleResolver lr = new AcceptHeaderLocaleResolver(); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr); ThemeResolver tr = new FixedThemeResolver(); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr); request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac); } else { sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); } return new MockPageContext(sc, request, response); }
Example 2
Source File: ServletContextSupportTests.java From spring-analysis-note with MIT License | 6 votes |
@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); }
Example 3
Source File: SpringBeanAutowiringSupportTests.java From spring-analysis-note with MIT License | 6 votes |
@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); }
Example 4
Source File: DelegatingFilterProxyTests.java From spring-analysis-note with MIT License | 6 votes |
@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); }
Example 5
Source File: AbstractTagTests.java From java-technology-stack with MIT License | 6 votes |
protected MockPageContext createPageContext() { MockServletContext sc = new MockServletContext(); SimpleWebApplicationContext wac = new SimpleWebApplicationContext(); wac.setServletContext(sc); wac.setNamespace("test"); wac.refresh(); MockHttpServletRequest request = new MockHttpServletRequest(sc); MockHttpServletResponse response = new MockHttpServletResponse(); if (inDispatcherServlet()) { request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); LocaleResolver lr = new AcceptHeaderLocaleResolver(); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr); ThemeResolver tr = new FixedThemeResolver(); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr); request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac); } else { sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); } return new MockPageContext(sc, request, response); }
Example 6
Source File: ServletContextSupportTests.java From java-technology-stack with MIT License | 6 votes |
@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); }
Example 7
Source File: SpringBeanAutowiringSupportTests.java From java-technology-stack with MIT License | 6 votes |
@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); }
Example 8
Source File: DelegatingFilterProxyTests.java From java-technology-stack with MIT License | 6 votes |
@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); }
Example 9
Source File: AbstractTagTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
protected MockPageContext createPageContext() { MockServletContext sc = new MockServletContext(); SimpleWebApplicationContext wac = new SimpleWebApplicationContext(); wac.setServletContext(sc); wac.setNamespace("test"); wac.refresh(); MockHttpServletRequest request = new MockHttpServletRequest(sc); MockHttpServletResponse response = new MockHttpServletResponse(); if (inDispatcherServlet()) { request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); LocaleResolver lr = new AcceptHeaderLocaleResolver(); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr); ThemeResolver tr = new FixedThemeResolver(); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr); request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac); } else { sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); } return new MockPageContext(sc, request, response); }
Example 10
Source File: SpringBeanAutowiringSupportTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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); }
Example 11
Source File: DelegatingFilterProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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); }
Example 12
Source File: ServletContextSupportTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test 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); }
Example 13
Source File: VelocityViewTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * @param mergeTemplateFailureException may be null in which case mergeTemplate override will succeed. * If it's non null it will be checked */ private void testValidTemplateName(final Exception mergeTemplateFailureException) throws Exception { Map<String, Object> model = new HashMap<String, Object>(); model.put("foo", "bar"); final String templateName = "test.vm"; WebApplicationContext wac = mock(WebApplicationContext.class); MockServletContext sc = new MockServletContext(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); final Template expectedTemplate = new Template(); VelocityConfig vc = new VelocityConfig() { @Override public VelocityEngine getVelocityEngine() { return new TestVelocityEngine(templateName, expectedTemplate); } }; Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>(); configurers.put("velocityConfigurer", vc); given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers); given(wac.getServletContext()).willReturn(sc); given(wac.getBean("requestDataValueProcessor", RequestDataValueProcessor.class)).willReturn(null); HttpServletRequest request = new MockHttpServletRequest(); final HttpServletResponse expectedResponse = new MockHttpServletResponse(); VelocityView vv = new VelocityView() { @Override protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception { assertTrue(template == expectedTemplate); assertTrue(context.getKeys().length >= 1); assertTrue(context.get("foo").equals("bar")); assertTrue(response == expectedResponse); if (mergeTemplateFailureException != null) { throw mergeTemplateFailureException; } } }; vv.setUrl(templateName); vv.setApplicationContext(wac); try { vv.render(model, request, expectedResponse); if (mergeTemplateFailureException != null) { fail(); } } catch (Exception ex) { assertNotNull(mergeTemplateFailureException); assertEquals(ex, mergeTemplateFailureException); } }