Search in sources :

Example 91 with StaticWebApplicationContext

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

the class RequestAndSessionScopedBeanTests method testPutBeanInRequest.

@Test
@SuppressWarnings("resource")
public void testPutBeanInRequest() {
    String targetBeanName = "target";
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setScope(WebApplicationContext.SCOPE_REQUEST);
    bd.getPropertyValues().add("name", "abc");
    wac.registerBeanDefinition(targetBeanName, bd);
    wac.refresh();
    HttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    TestBean target = (TestBean) wac.getBean(targetBeanName);
    assertThat(target.getName()).isEqualTo("abc");
    assertThat(request.getAttribute(targetBeanName)).isSameAs(target);
    TestBean target2 = (TestBean) wac.getBean(targetBeanName);
    assertThat(target2.getName()).isEqualTo("abc");
    assertThat(target).isSameAs(target2);
    assertThat(request.getAttribute(targetBeanName)).isSameAs(target2);
    request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    TestBean target3 = (TestBean) wac.getBean(targetBeanName);
    assertThat(target3.getName()).isEqualTo("abc");
    assertThat(request.getAttribute(targetBeanName)).isSameAs(target3);
    assertThat(target).isNotSameAs(target3);
    RequestContextHolder.setRequestAttributes(null);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> wac.getBean(targetBeanName));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) BeanCreationException(org.springframework.beans.factory.BeanCreationException) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 92 with StaticWebApplicationContext

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

the class WebSocketUpgradeHandlerPredicateTests method simpleUrlHandlerMapping.

@Test
void simpleUrlHandlerMapping() {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setUrlMap(Collections.singletonMap("/path", this.webSocketHandler));
    mapping.setApplicationContext(new StaticWebApplicationContext());
    Object actual = mapping.getHandler(httpGetExchange).block();
    assertThat(actual).as("Should match HTTP GET by URL path").isSameAs(this.webSocketHandler);
    mapping.setHandlerPredicate(new WebSocketUpgradeHandlerPredicate());
    actual = mapping.getHandler(this.httpGetExchange).block();
    assertThat(actual).as("Should not match if not a WebSocket upgrade").isNull();
    actual = mapping.getHandler(this.httpPostExchange).block();
    assertThat(actual).as("Should not match if not a WebSocket upgrade").isNull();
    actual = mapping.getHandler(this.webSocketExchange).block();
    assertThat(actual).as("Should match WebSocket upgrade").isSameAs(this.webSocketHandler);
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 93 with StaticWebApplicationContext

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

the class HandlerMappingIntrospectorTests method detectHandlerMappings.

@Test
void detectHandlerMappings() {
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    context.registerSingleton("A", SimpleUrlHandlerMapping.class);
    context.registerSingleton("B", SimpleUrlHandlerMapping.class);
    context.registerSingleton("C", SimpleUrlHandlerMapping.class);
    context.refresh();
    List<?> expected = Arrays.asList(context.getBean("A"), context.getBean("B"), context.getBean("C"));
    List<HandlerMapping> actual = initIntrospector(context).getHandlerMappings();
    assertThat(actual).isEqualTo(expected);
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 94 with StaticWebApplicationContext

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

the class ViewResolverTests method xmlViewResolverDefaultLocation.

@Test
@SuppressWarnings("deprecation")
public void xmlViewResolverDefaultLocation() {
    StaticWebApplicationContext wac = new StaticWebApplicationContext() {

        @Override
        protected Resource getResourceByPath(String path) {
            assertThat(org.springframework.web.servlet.view.XmlViewResolver.DEFAULT_LOCATION.equals(path)).as("Correct default location").isTrue();
            return super.getResourceByPath(path);
        }
    };
    wac.setServletContext(this.sc);
    wac.refresh();
    org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
    vr.setApplicationContext(wac);
    assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(vr::afterPropertiesSet);
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 95 with StaticWebApplicationContext

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

the class FreeMarkerViewTests method freeMarkerViewResolver.

@Test
public void freeMarkerViewResolver() throws Exception {
    MockServletContext sc = new MockServletContext();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new TestConfiguration());
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.getBeanFactory().registerSingleton("configurer", configurer);
    wac.refresh();
    FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix");
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("test", Locale.CANADA);
    assertThat(view.getClass()).as("Correct view class").isEqualTo(FreeMarkerView.class);
    assertThat(((FreeMarkerView) view).getUrl()).as("Correct URL").isEqualTo("prefix_test_suffix");
    view = vr.resolveViewName("non-existing", Locale.CANADA);
    assertThat(view).isNull();
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
    assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) AbstractView(org.springframework.web.servlet.view.AbstractView) InternalResourceView(org.springframework.web.servlet.view.InternalResourceView) RedirectView(org.springframework.web.servlet.view.RedirectView) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.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