Search in sources :

Example 6 with AcceptHeaderLocaleResolver

use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver in project spring-framework by spring-projects.

the class ViewResolverTests method doTestUrlBasedViewResolverWithoutPrefixes.

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());
}
Also used : 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) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) TestBean(org.springframework.tests.sample.beans.TestBean) RequestContext(org.springframework.web.servlet.support.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 7 with AcceptHeaderLocaleResolver

use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithAttributes.

@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 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 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 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"));
}
Also used : 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) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestBean(org.springframework.tests.sample.beans.TestBean) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with AcceptHeaderLocaleResolver

use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver 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 9 with AcceptHeaderLocaleResolver

use of org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver 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)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)9 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)9 MockServletContext (org.springframework.mock.web.test.MockServletContext)9 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)9 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)8 HashMap (java.util.HashMap)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Test (org.junit.Test)6 Map (java.util.Map)5 View (org.springframework.web.servlet.View)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 FixedThemeResolver (org.springframework.web.servlet.theme.FixedThemeResolver)4 Properties (java.util.Properties)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 MockRequestDispatcher (org.springframework.mock.web.test.MockRequestDispatcher)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 ServletContext (javax.servlet.ServletContext)1