Search in sources :

Example 31 with StaticWebApplicationContext

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

the class LinksEnhancerTests method getLinksEnhancer.

private LinksEnhancer getLinksEnhancer(List<MvcEndpoint> endpoints) throws Exception {
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    for (MvcEndpoint endpoint : endpoints) {
        context.getDefaultListableBeanFactory().registerSingleton(endpoint.toString(), endpoint);
    }
    MvcEndpoints mvcEndpoints = new MvcEndpoints();
    mvcEndpoints.setApplicationContext(context);
    mvcEndpoints.afterPropertiesSet();
    return new LinksEnhancer("", mvcEndpoints);
}
Also used : AbstractMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.AbstractMvcEndpoint) MvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MvcEndpoints(org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints)

Example 32 with StaticWebApplicationContext

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

the class MustacheViewResolverTests method init.

@Before
public void init() {
    this.resolver.setApplicationContext(new StaticWebApplicationContext());
    this.resolver.setServletContext(new MockServletContext());
    this.resolver.setPrefix("classpath:/mustache-templates/");
    this.resolver.setSuffix(".html");
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Before(org.junit.Before)

Example 33 with StaticWebApplicationContext

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

the class MustacheViewResolverTests method templateResourceInputStreamIsClosed.

@Test
public void templateResourceInputStreamIsClosed() throws Exception {
    final Resource resource = mock(Resource.class);
    given(resource.exists()).willReturn(true);
    InputStream inputStream = new ByteArrayInputStream(new byte[0]);
    InputStream spyInputStream = spy(inputStream);
    given(resource.getInputStream()).willReturn(spyInputStream);
    this.resolver = new MustacheViewResolver();
    this.resolver.setApplicationContext(new StaticWebApplicationContext() {

        @Override
        public Resource getResource(String location) {
            return resource;
        }
    });
    this.resolver.loadView("foo", null);
    verify(spyInputStream).close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 34 with StaticWebApplicationContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithFrameworkServletContext.

@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    proxyConfig.addInitParameter("targetBeanName", "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) Test(org.junit.Test)

Example 35 with StaticWebApplicationContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetBeanName.

@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
    filterProxy.init(new MockFilterConfig(sc));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) 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