Search in sources :

Example 1 with StaticWebApplicationContext

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

the class CommonsMultipartResolverTests method doTestWithApplicationContext.

private void doTestWithApplicationContext(boolean lazy) throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();
    MockCommonsMultipartResolver resolver = new MockCommonsMultipartResolver();
    resolver.setMaxUploadSize(1000);
    resolver.setMaxInMemorySize(100);
    resolver.setDefaultEncoding("enc");
    if (lazy) {
        resolver.setResolveLazily(false);
    }
    resolver.setServletContext(wac.getServletContext());
    assertEquals(1000, resolver.getFileUpload().getSizeMax());
    assertEquals(100, resolver.getFileItemFactory().getSizeThreshold());
    assertEquals("enc", resolver.getFileUpload().getHeaderEncoding());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    originalRequest.addParameter("getField", "getValue");
    assertTrue(resolver.isMultipart(originalRequest));
    MultipartHttpServletRequest request = resolver.resolveMultipart(originalRequest);
    doTestParameters(request);
    doTestFiles(request);
    doTestBinding(resolver, originalRequest, request);
    wac.close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockServletContext(org.springframework.mock.web.test.MockServletContext) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 2 with StaticWebApplicationContext

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

the class ViewResolverRegistryTests method setUp.

@Before
public void setUp() {
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
    context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
    context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
    context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
    this.registry = new ViewResolverRegistry();
    this.registry.setApplicationContext(context);
    this.registry.setContentNegotiationManager(new ContentNegotiationManager());
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Before(org.junit.Before)

Example 3 with StaticWebApplicationContext

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

the class HandlerMappingTests method setup.

@Before
public void setup() {
    this.context = new StaticWebApplicationContext();
    this.handlerMapping = new TestHandlerMapping();
    this.request = new MockHttpServletRequest();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Before(org.junit.Before)

Example 4 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 5 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)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