Search in sources :

Example 31 with MockServletContext

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

the class ThemeResolverTests method internalTest.

private void internalTest(ThemeResolver themeResolver, boolean shouldSet, String defaultName) {
    // create mocks
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    MockHttpServletResponse response = new MockHttpServletResponse();
    // check original theme
    String themeName = themeResolver.resolveThemeName(request);
    assertEquals(themeName, defaultName);
    // set new theme name
    try {
        themeResolver.setThemeName(request, response, TEST_THEME_NAME);
        if (!shouldSet)
            fail("should not be able to set Theme name");
        // check new theme namelocale
        themeName = themeResolver.resolveThemeName(request);
        assertEquals(TEST_THEME_NAME, themeName);
        themeResolver.setThemeName(request, response, null);
        themeName = themeResolver.resolveThemeName(request);
        assertEquals(themeName, defaultName);
    } catch (UnsupportedOperationException ex) {
        if (shouldSet)
            fail("should be able to set Theme name");
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 32 with MockServletContext

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

the class BaseViewTests method pathVarsOverrideStaticAttributes.

@Test
public void pathVarsOverrideStaticAttributes() throws Exception {
    WebApplicationContext wac = mock(WebApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    Properties p = new Properties();
    p.setProperty("one", "bar");
    p.setProperty("something", "else");
    tv.setAttributes(p);
    Map<String, Object> pathVars = new HashMap<>();
    pathVars.put("one", new HashMap<>());
    pathVars.put("two", new Object());
    request.setAttribute(View.PATH_VARIABLES, pathVars);
    tv.render(new HashMap<>(), request, response);
    checkContainsAll(pathVars, tv.model);
    assertEquals(3, tv.model.size());
    assertEquals("else", tv.model.get("something"));
    assertTrue(tv.initialized);
}
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) Properties(java.util.Properties) MockServletContext(org.springframework.mock.web.test.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 33 with MockServletContext

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

the class BaseViewTests method dynamicModelOverridesPathVariables.

@Test
public void dynamicModelOverridesPathVariables() throws Exception {
    WebApplicationContext wac = mock(WebApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> pathVars = new HashMap<>();
    pathVars.put("one", "bar");
    pathVars.put("something", "else");
    request.setAttribute(View.PATH_VARIABLES, pathVars);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    tv.render(model, request, response);
    checkContainsAll(model, tv.model);
    assertEquals(3, tv.model.size());
    assertEquals("else", tv.model.get("something"));
    assertTrue(tv.initialized);
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 34 with MockServletContext

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

the class BaseViewTests method renderWithStaticAttributesNoCollision.

/**
	 * Test attribute passing, NOT CSV parsing.
	 */
@Test
public void renderWithStaticAttributesNoCollision() throws Exception {
    WebApplicationContext wac = mock(WebApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    Properties p = new Properties();
    p.setProperty("foo", "bar");
    p.setProperty("something", "else");
    tv.setAttributes(p);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    tv.render(model, request, response);
    checkContainsAll(model, tv.model);
    checkContainsAll(p, tv.model);
    assertTrue(tv.initialized);
}
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) Properties(java.util.Properties) MockServletContext(org.springframework.mock.web.test.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 35 with MockServletContext

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

the class BaseViewTests method renderWithoutStaticAttributes.

@Test
public void renderWithoutStaticAttributes() throws Exception {
    WebApplicationContext wac = mock(WebApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    // Check superclass handles duplicate init
    tv.setApplicationContext(wac);
    tv.setApplicationContext(wac);
    Map<String, Object> model = new HashMap<>();
    model.put("foo", "bar");
    model.put("something", new Object());
    tv.render(model, request, response);
    checkContainsAll(model, tv.model);
    assertTrue(tv.initialized);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9