Search in sources :

Example 16 with StaticWebApplicationContext

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

the class RequestMappingInfoHandlerMappingTests method getHandlerMappedInterceptors.

@Test
public void getHandlerMappedInterceptors() throws Exception {
    String path = "/foo";
    HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {
    };
    MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { path }, interceptor);
    TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
    mapping.registerHandler(new TestController());
    mapping.setInterceptors(new Object[] { mappedInterceptor });
    mapping.setApplicationContext(new StaticWebApplicationContext());
    HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
    assertNotNull(chain);
    assertNotNull(chain.getInterceptors());
    assertSame(interceptor, chain.getInterceptors()[0]);
    chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
    assertNull(chain);
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 17 with StaticWebApplicationContext

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

the class RequestMappingHandlerAdapterTests method setup.

@Before
public void setup() throws Exception {
    this.webAppContext = new StaticWebApplicationContext();
    this.handlerAdapter = new RequestMappingHandlerAdapter();
    this.handlerAdapter.setApplicationContext(this.webAppContext);
    this.request = new MockHttpServletRequest("GET", "/");
    this.response = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 18 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext 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 wac = (StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request);
    wac.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.mock.web.test.MockHttpServletRequest) MockPageContext(org.springframework.mock.web.test.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 19 with StaticWebApplicationContext

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

the class ViewResolverTests method testCacheRemoval.

@Test
public void testCacheRemoval() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    View cached = vr.resolveViewName("example1", Locale.getDefault());
    if (view != cached) {
        fail("Caching doesn't work");
    }
    vr.removeFromCache("example1", Locale.getDefault());
    cached = vr.resolveViewName("example1", Locale.getDefault());
    if (view == cached) {
        // the chance of having the same reference (hashCode) twice if negligible).
        fail("View wasn't removed from cache");
    }
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 20 with StaticWebApplicationContext

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

the class ViewResolverTests method testBeanNameViewResolver.

@Test
public void testBeanNameViewResolver() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    MutablePropertyValues pvs1 = new MutablePropertyValues();
    pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
    wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    MutablePropertyValues pvs2 = new MutablePropertyValues();
    pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
    wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(wac);
    wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", InternalResourceView.class, view.getClass());
    assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl());
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValue(org.springframework.beans.PropertyValue) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)75 Test (org.junit.Test)52 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)42 MockServletContext (org.springframework.mock.web.test.MockServletContext)38 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)30 Before (org.junit.Before)14 View (org.springframework.web.servlet.View)13 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)12 ServletContext (javax.servlet.ServletContext)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 TestBean (org.springframework.tests.sample.beans.TestBean)9 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ServletRequest (javax.servlet.ServletRequest)6 ServletResponse (javax.servlet.ServletResponse)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Locale (java.util.Locale)4 Properties (java.util.Properties)4