Search in sources :

Example 1 with StaticWebServletApplicationContext

use of cn.taketoday.web.context.support.StaticWebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class ContentNegotiatingViewResolverTests method nestedViewResolverIsNotSpringBean.

@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
    StaticWebServletApplicationContext webAppContext = new StaticWebServletApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();
    InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
    nestedResolver.setApplicationContext(webAppContext);
    nestedResolver.setViewClass(InternalResourceView.class);
    viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));
    FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
    viewResolver.afterPropertiesSet();
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    View result = viewResolver.resolveViewName(viewName, locale);
    assertThat(result).as("Invalid view").isNotNull();
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) FixedContentNegotiationStrategy(cn.taketoday.web.accept.FixedContentNegotiationStrategy) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) MockServletContext(cn.taketoday.web.mock.MockServletContext) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 2 with StaticWebServletApplicationContext

use of cn.taketoday.web.context.support.StaticWebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class ControllerTests method doTestServletForwardingController.

private void doTestServletForwardingController(ServletForwardingController sfc, boolean include) throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletContext context = mock(ServletContext.class);
    RequestDispatcher dispatcher = mock(RequestDispatcher.class);
    given(request.getMethod()).willReturn("GET");
    given(context.getNamedDispatcher("action")).willReturn(dispatcher);
    if (include) {
        given(request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)).willReturn("somePath");
    } else {
        given(request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)).willReturn(null);
    }
    StaticWebServletApplicationContext sac = new StaticWebServletApplicationContext();
    sac.setServletContext(context);
    sfc.setApplicationContext(sac);
    sfc.setServletContext(context);
    ServletRequestContext servletRequestContext = new ServletRequestContext(sac, request, response);
    assertThat(sfc.handleRequest(servletRequestContext)).isNull();
    if (include) {
        verify(dispatcher).include(request, response);
    } else {
        verify(dispatcher).forward(request, response);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletContext(jakarta.servlet.ServletContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 3 with StaticWebServletApplicationContext

use of cn.taketoday.web.context.support.StaticWebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class EnvironmentSystemIntegrationTests method staticWebApplicationContext.

@Test
void staticWebApplicationContext() {
    StaticWebServletApplicationContext ctx = new StaticWebServletApplicationContext();
    assertHasStandardServletEnvironment(ctx);
    registerEnvironmentBeanDefinition(ctx);
    ctx.setEnvironment(prodWebEnv);
    ctx.refresh();
    assertHasEnvironment(ctx, prodWebEnv);
    assertEnvironmentBeanRegistered(ctx);
    assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}
Also used : StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) Test(org.junit.jupiter.api.Test)

Example 4 with StaticWebServletApplicationContext

use of cn.taketoday.web.context.support.StaticWebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class ScriptTemplateViewTests method setup.

@BeforeEach
public void setup() {
    this.configurer = new ScriptTemplateConfigurer();
    this.wac = new StaticWebServletApplicationContext();
    this.wac.getBeanFactory().registerSingleton("scriptTemplateConfigurer", this.configurer);
    this.view = new ScriptTemplateView();
}
Also used : StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with StaticWebServletApplicationContext

use of cn.taketoday.web.context.support.StaticWebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class ServletContextSupportTests method testServletContextAttributeFactoryBean.

@Test
@SuppressWarnings("resource")
public void testServletContextAttributeFactoryBean() {
    MockServletContext sc = new MockServletContext();
    sc.setAttribute("myAttr", "myValue");
    StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
    wac.setServletContext(sc);
    PropertyValues pvs = new PropertyValues();
    pvs.add("attributeName", "myAttr");
    wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
    wac.refresh();
    Object value = wac.getBean("importedAttr");
    assertThat(value).isEqualTo("myValue");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) MockServletContext(cn.taketoday.web.mock.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)22 Test (org.junit.jupiter.api.Test)18 PropertyValues (cn.taketoday.beans.PropertyValues)8 MockServletContext (cn.taketoday.web.mock.MockServletContext)7 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)7 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)6 InternalResourceViewResolver (cn.taketoday.web.servlet.view.InternalResourceViewResolver)4 Locale (java.util.Locale)4 MockPropertySource (cn.taketoday.mock.env.MockPropertySource)2 MockServletConfig (cn.taketoday.mock.web.MockServletConfig)2 MockServletContext (cn.taketoday.mock.web.MockServletContext)2 ContentNegotiationManager (cn.taketoday.web.accept.ContentNegotiationManager)2 FixedContentNegotiationStrategy (cn.taketoday.web.accept.FixedContentNegotiationStrategy)2 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)2 AbstractView (cn.taketoday.web.view.AbstractView)2 RedirectView (cn.taketoday.web.view.RedirectView)2 View (cn.taketoday.web.view.View)2 RequestDispatcher (jakarta.servlet.RequestDispatcher)2 ServletContext (jakarta.servlet.ServletContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2