Search in sources :

Example 31 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class WebRequestDataBinderTests method testWithCommaSeparatedStringArray.

@Test
public void testWithCommaSeparatedStringArray() throws Exception {
    TestBean target = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("stringArray", "bar");
    request.addParameter("stringArray", "abc");
    request.addParameter("stringArray", "123,def");
    binder.bind(new ServletWebRequest(request));
    assertThat(target.getStringArray().length).as("Expected all three items to be bound").isEqualTo(3);
    request.removeParameter("stringArray");
    request.addParameter("stringArray", "123,def");
    binder.bind(new ServletWebRequest(request));
    assertThat(target.getStringArray().length).as("Expected only 1 item to be bound").isEqualTo(1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 32 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class RequestScopedProxyTests method testGetFromScope.

@Test
public void testGetFromScope() {
    String name = "requestScopedObject";
    TestBean bean = (TestBean) this.beanFactory.getBean(name);
    assertThat(AopUtils.isCglibProxy(bean)).isTrue();
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
        assertThat(request.getAttribute("scopedTarget." + name)).isNull();
        assertThat(bean.getName()).isEqualTo("scoped");
        assertThat(request.getAttribute("scopedTarget." + name)).isNotNull();
        TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
        assertThat(target.getClass()).isEqualTo(TestBean.class);
        assertThat(target.getName()).isEqualTo("scoped");
        assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
        assertThat(target.toString()).isEqualTo(bean.toString());
    } finally {
        RequestContextHolder.setRequestAttributes(null);
    }
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) CountingTestBean(org.springframework.beans.testfixture.beans.CountingTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 33 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class ServletRequestDataBinderTests method testBindingWithNestedObjectCreation.

@Test
public void testBindingWithNestedObjectCreation() throws Exception {
    TestBean tb = new TestBean();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
    binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean());
        }
    });
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("spouse", "someValue");
    request.addParameter("spouse.name", "test");
    binder.bind(request);
    assertThat(tb.getSpouse()).isNotNull();
    assertThat(tb.getSpouse().getName()).isEqualTo("test");
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 34 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class ServletRequestDataBinderTests method testFieldDefaultPreemptsFieldMarker.

@Test
public void testFieldDefaultPreemptsFieldMarker() throws Exception {
    TestBean target = new TestBean();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("!postProcessed", "on");
    request.addParameter("_postProcessed", "visible");
    request.addParameter("postProcessed", "on");
    binder.bind(request);
    assertThat(target.isPostProcessed()).isTrue();
    request.removeParameter("postProcessed");
    binder.bind(request);
    assertThat(target.isPostProcessed()).isTrue();
    request.removeParameter("!postProcessed");
    binder.bind(request);
    assertThat(target.isPostProcessed()).isFalse();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 35 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class ServletRequestDataBinderTests method testFieldPrefixCausesFieldResetWithIgnoreUnknownFields.

@Test
public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields() throws Exception {
    TestBean target = new TestBean();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
    binder.setIgnoreUnknownFields(false);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("_postProcessed", "visible");
    request.addParameter("postProcessed", "on");
    binder.bind(request);
    assertThat(target.isPostProcessed()).isTrue();
    request.removeParameter("postProcessed");
    binder.bind(request);
    assertThat(target.isPostProcessed()).isFalse();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28