Search in sources :

Example 1 with MockServletContext

use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.

the class ServletContextSupportTests method testServletContextAttributeExporter.

@Test
public void testServletContextAttributeExporter() {
    TestBean tb = new TestBean();
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("attr1", "value1");
    attributes.put("attr2", tb);
    MockServletContext sc = new MockServletContext();
    ServletContextAttributeExporter exporter = new ServletContextAttributeExporter();
    exporter.setAttributes(attributes);
    exporter.setServletContext(sc);
    assertThat(sc.getAttribute("attr1")).isEqualTo("value1");
    assertThat(sc.getAttribute("attr2")).isSameAs(tb);
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) HashMap(java.util.HashMap) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) ServletContextAttributeExporter(cn.taketoday.web.context.support.ServletContextAttributeExporter) Test(org.junit.jupiter.api.Test)

Example 2 with MockServletContext

use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework 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.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockServletContext

use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.

the class ServletContextSupportTests method testServletContextAttributeFactoryBeanWithAttributeNotFound.

@Test
@SuppressWarnings("resource")
public void testServletContextAttributeFactoryBeanWithAttributeNotFound() {
    MockServletContext sc = new MockServletContext();
    StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
    wac.setServletContext(sc);
    PropertyValues pvs = new PropertyValues();
    pvs.add("attributeName", "myAttr");
    wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(wac::refresh).havingCause().isInstanceOf(IllegalStateException.class).withMessageContaining("myAttr");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 4 with MockServletContext

use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.

the class ServletContextSupportTests method testServletContextParameterFactoryBean.

@Test
@SuppressWarnings("resource")
public void testServletContextParameterFactoryBean() {
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter("myParam", "myValue");
    StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
    wac.setServletContext(sc);
    PropertyValues pvs = new PropertyValues();
    pvs.add("initParamName", "myParam");
    wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
    wac.refresh();
    Object value = wac.getBean("importedParam");
    assertThat(value).isEqualTo("myValue");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 5 with MockServletContext

use of cn.taketoday.web.testfixture.servlet.MockServletContext in project today-framework by TAKETODAY.

the class ServletContextSupportTests method testServletContextParameterFactoryBeanWithAttributeNotFound.

@Test
@SuppressWarnings("resource")
public void testServletContextParameterFactoryBeanWithAttributeNotFound() {
    MockServletContext sc = new MockServletContext();
    StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
    wac.setServletContext(sc);
    PropertyValues pvs = new PropertyValues();
    pvs.add("initParamName", "myParam");
    wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(wac::refresh).havingCause().isInstanceOf(IllegalStateException.class).withMessageContaining("myParam");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)41 Test (org.junit.jupiter.api.Test)35 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)14 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)12 AnnotationConfigServletWebApplicationContext (cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebApplicationContext)10 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)10 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)10 HashMap (java.util.HashMap)10 Resource (cn.taketoday.core.io.Resource)6 RequestContext (cn.taketoday.web.RequestContext)6 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)6 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)6 ServletContextResource (cn.taketoday.web.context.support.ServletContextResource)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 PropertyValues (cn.taketoday.beans.PropertyValues)4 ServletContextResourcePatternLoader (cn.taketoday.web.context.support.ServletContextResourcePatternLoader)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HashSet (java.util.HashSet)4 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)3 Properties (java.util.Properties)3