Search in sources :

Example 51 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithSpecificContextBeans.

@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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Properties(java.util.Properties) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockRequestDispatcher(org.springframework.mock.web.test.MockRequestDispatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 52 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithJstl.

@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.addMessage("code1", locale, "messageX");
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    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") == null);
    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
Also used : Locale(java.util.Locale) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) TestBean(org.springframework.tests.sample.beans.TestBean) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 53 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithContextBeans.

@Test
public void testInternalResourceViewResolverWithContextBeans() 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.setExposeContextBeansAsAttributes(true);
    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"));
                    assertSame(wac.getBean("myBean"), 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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Properties(java.util.Properties) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockRequestDispatcher(org.springframework.mock.web.test.MockRequestDispatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 54 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class TilesViewResolverTests method setUp.

@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);
}
Also used : Renderer(org.apache.tiles.request.render.Renderer) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Before(org.junit.Before)

Example 55 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class TilesViewTests method setUp.

@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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Renderer(org.apache.tiles.request.render.Renderer) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)101 Test (org.junit.jupiter.api.Test)54 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)20 MockServletContext (org.springframework.mock.web.test.MockServletContext)16 Test (org.junit.Test)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 ServletContext (jakarta.servlet.ServletContext)11 View (org.springframework.web.servlet.View)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)10 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Before (org.junit.Before)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Properties (java.util.Properties)5