Search in sources :

Example 1 with MockRequestDispatcher

use of org.springframework.mock.web.test.MockRequestDispatcher in project spring-framework by spring-projects.

the class InternalResourceViewTests method includeOnAttribute.

@Test
public void includeOnAttribute() throws Exception {
    given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
    given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somepath");
    given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
    view.setUrl(url);
    // Can now try multiple tests
    view.render(model, request, response);
    assertEquals(url, response.getIncludedUrl());
    model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
}
Also used : MockRequestDispatcher(org.springframework.mock.web.test.MockRequestDispatcher) Test(org.junit.Test)

Example 2 with MockRequestDispatcher

use of org.springframework.mock.web.test.MockRequestDispatcher in project spring-framework by spring-projects.

the class InternalResourceViewTests method includeOnCommitted.

@Test
public void includeOnCommitted() throws Exception {
    given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
    given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
    response.setCommitted(true);
    view.setUrl(url);
    // Can now try multiple tests
    view.render(model, request, response);
    assertEquals(url, response.getIncludedUrl());
    model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
}
Also used : MockRequestDispatcher(org.springframework.mock.web.test.MockRequestDispatcher) Test(org.junit.Test)

Example 3 with MockRequestDispatcher

use of org.springframework.mock.web.test.MockRequestDispatcher 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 4 with MockRequestDispatcher

use of org.springframework.mock.web.test.MockRequestDispatcher 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 5 with MockRequestDispatcher

use of org.springframework.mock.web.test.MockRequestDispatcher in project spring-framework by spring-projects.

the class InternalResourceViewTests method alwaysInclude.

@Test
public void alwaysInclude() throws Exception {
    given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
    given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
    view.setUrl(url);
    view.setAlwaysInclude(true);
    // Can now try multiple tests
    view.render(model, request, response);
    assertEquals(url, response.getIncludedUrl());
    model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
}
Also used : MockRequestDispatcher(org.springframework.mock.web.test.MockRequestDispatcher) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 MockRequestDispatcher (org.springframework.mock.web.test.MockRequestDispatcher)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 MockServletContext (org.springframework.mock.web.test.MockServletContext)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 View (org.springframework.web.servlet.View)2 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)2