Search in sources :

Example 46 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 47 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 48 with StaticWebApplicationContext

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

the class ViewResolverTests method testInternalResourceViewResolverWithAttributes.

@Test
public void testInternalResourceViewResolverWithAttributes() throws Exception {
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    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.setApplicationContext(wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
    Map attributes = ((InternalResourceView) view).getStaticAttributes();
    assertEquals("value1", attributes.get("key1"));
    assertEquals(new Integer(2), attributes.get("key2"));
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
    attributes = ((InternalResourceView) view).getStaticAttributes();
    assertEquals("value1", attributes.get("key1"));
    assertEquals(new Integer(2), attributes.get("key2"));
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);
    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
    assertEquals("value1", request.getAttribute("key1"));
    assertEquals(new Integer(2), request.getAttribute("key2"));
}
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) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Properties(java.util.Properties) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestBean(org.springframework.tests.sample.beans.TestBean) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 49 with StaticWebApplicationContext

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

the class ViewResolverTests method testXmlViewResolverWithoutCache.

@Test
public void testXmlViewResolverWithoutCache() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext() {

        @Override
        protected Resource getResourceByPath(String path) {
            assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
            return super.getResourceByPath(path);
        }
    };
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    XmlViewResolver vr = new XmlViewResolver();
    vr.setCache(false);
    try {
        vr.setApplicationContext(wac);
    } catch (ApplicationContextException ex) {
        fail("Should not have thrown ApplicationContextException: " + ex.getMessage());
    }
    try {
        vr.resolveViewName("example1", Locale.getDefault());
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
    // expected
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) ApplicationContextException(org.springframework.context.ApplicationContextException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 50 with StaticWebApplicationContext

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

the class ViewResolverTests method testXmlViewResolverDefaultLocation.

@Test
public void testXmlViewResolverDefaultLocation() {
    StaticWebApplicationContext wac = new StaticWebApplicationContext() {

        @Override
        protected Resource getResourceByPath(String path) {
            assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
            return super.getResourceByPath(path);
        }
    };
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    XmlViewResolver vr = new XmlViewResolver();
    try {
        vr.setApplicationContext(wac);
        vr.afterPropertiesSet();
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
    // expected
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)101 Test (org.junit.jupiter.api.Test)54 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)20 MockServletContext (org.springframework.mock.web.test.MockServletContext)16 Test (org.junit.Test)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 ServletContext (jakarta.servlet.ServletContext)11 View (org.springframework.web.servlet.View)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)10 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Before (org.junit.Before)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Properties (java.util.Properties)5