Search in sources :

Example 1 with MockPageContext

use of org.springframework.web.testfixture.servlet.MockPageContext in project spring-framework by spring-projects.

the class ErrorsTagTests method exposeBindingResult.

@Override
protected void exposeBindingResult(Errors errors) {
    // wrap errors in a Model
    Map model = new HashMap();
    model.put(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, errors);
    // replace the request context with one containing the errors
    MockPageContext pageContext = getPageContext();
    RequestContext context = new RequestContext((HttpServletRequest) pageContext.getRequest(), model);
    pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, context);
}
Also used : HashMap(java.util.HashMap) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) RequestContext(org.springframework.web.servlet.support.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MockPageContext

use of org.springframework.web.testfixture.servlet.MockPageContext in project spring-framework by spring-projects.

the class AbstractTagTests method createPageContext.

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);
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) ThemeResolver(org.springframework.web.servlet.ThemeResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 3 with MockPageContext

use of org.springframework.web.testfixture.servlet.MockPageContext in project spring-framework by spring-projects.

the class AbstractHtmlElementTagTests method createAndPopulatePageContext.

protected MockPageContext createAndPopulatePageContext() throws JspException {
    MockPageContext pageContext = createPageContext();
    MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
    ((StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request)).registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
    extendRequest(request);
    extendPageContext(pageContext);
    RequestContext requestContext = new JspAwareRequestContext(pageContext);
    pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext);
    return pageContext;
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) JspAwareRequestContext(org.springframework.web.servlet.support.JspAwareRequestContext) JspAwareRequestContext(org.springframework.web.servlet.support.JspAwareRequestContext) RequestContext(org.springframework.web.servlet.support.RequestContext)

Example 4 with MockPageContext

use of org.springframework.web.testfixture.servlet.MockPageContext in project spring-framework by spring-projects.

the class TagIdGeneratorTests method assertNextId.

private void assertNextId() {
    PageContext pageContext = new MockPageContext();
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo1");
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo2");
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo3");
    assertThat(TagIdGenerator.nextId("foo", pageContext)).isEqualTo("foo4");
    assertThat(TagIdGenerator.nextId("bar", pageContext)).isEqualTo("bar1");
}
Also used : MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) PageContext(jakarta.servlet.jsp.PageContext) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext)

Example 5 with MockPageContext

use of org.springframework.web.testfixture.servlet.MockPageContext in project spring-framework by spring-projects.

the class OptionsTagTests method exposeBindingResult.

@Override
protected void exposeBindingResult(Errors errors) {
    // wrap errors in a Model
    Map model = new HashMap();
    model.put(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, errors);
    // replace the request context with one containing the errors
    MockPageContext pageContext = getPageContext();
    RequestContext context = new RequestContext((HttpServletRequest) pageContext.getRequest(), model);
    pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, context);
}
Also used : HashMap(java.util.HashMap) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) RequestContext(org.springframework.web.servlet.support.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MockPageContext (org.springframework.web.testfixture.servlet.MockPageContext)6 RequestContext (org.springframework.web.servlet.support.RequestContext)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JspAwareRequestContext (org.springframework.web.servlet.support.JspAwareRequestContext)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 PageContext (jakarta.servlet.jsp.PageContext)1 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)1 LocaleResolver (org.springframework.web.servlet.LocaleResolver)1 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)1 ThemeResolver (org.springframework.web.servlet.ThemeResolver)1 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)1 FixedThemeResolver (org.springframework.web.servlet.theme.FixedThemeResolver)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)1