Search in sources :

Example 36 with TestBean

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

the class ServletRequestDataBinderTests method testFieldDefaultNonBoolean.

@Test
public void testFieldDefaultNonBoolean() throws Exception {
    TestBean target = new TestBean();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("!name", "anonymous");
    request.addParameter("name", "Scott");
    binder.bind(request);
    assertThat(target.getName()).isEqualTo("Scott");
    request.removeParameter("name");
    binder.bind(request);
    assertThat(target.getName()).isEqualTo("anonymous");
}
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 37 with TestBean

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

the class EscapedErrorsTests method testEscapedErrors.

@Test
public void testEscapedErrors() {
    TestBean tb = new TestBean();
    tb.setName("empty &");
    Errors errors = new EscapedErrors(new BindException(tb, "tb"));
    errors.rejectValue("name", "NAME_EMPTY &", null, "message: &");
    errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>");
    errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>");
    errors.reject("GENERAL_ERROR \" '", null, "message: \" '");
    assertThat(errors.hasErrors()).as("Correct errors flag").isTrue();
    assertThat(errors.getErrorCount() == 4).as("Correct number of errors").isTrue();
    assertThat("tb".equals(errors.getObjectName())).as("Correct object name").isTrue();
    assertThat(errors.hasGlobalErrors()).as("Correct global errors flag").isTrue();
    assertThat(errors.getGlobalErrorCount() == 1).as("Correct number of global errors").isTrue();
    ObjectError globalError = errors.getGlobalError();
    String defaultMessage = globalError.getDefaultMessage();
    assertThat("message: &quot; &#39;".equals(defaultMessage)).as("Global error message escaped").isTrue();
    assertThat("GENERAL_ERROR \" '".equals(globalError.getCode())).as("Global error code not escaped").isTrue();
    ObjectError globalErrorInList = errors.getGlobalErrors().get(0);
    assertThat(defaultMessage.equals(globalErrorInList.getDefaultMessage())).as("Same global error in list").isTrue();
    ObjectError globalErrorInAllList = errors.getAllErrors().get(3);
    assertThat(defaultMessage.equals(globalErrorInAllList.getDefaultMessage())).as("Same global error in list").isTrue();
    assertThat(errors.hasFieldErrors()).as("Correct field errors flag").isTrue();
    assertThat(errors.getFieldErrorCount() == 3).as("Correct number of field errors").isTrue();
    assertThat(errors.getFieldErrors().size() == 3).as("Correct number of field errors in list").isTrue();
    FieldError fieldError = errors.getFieldError();
    assertThat("NAME_EMPTY &".equals(fieldError.getCode())).as("Field error code not escaped").isTrue();
    assertThat("empty &amp;".equals(errors.getFieldValue("name"))).as("Field value escaped").isTrue();
    FieldError fieldErrorInList = errors.getFieldErrors().get(0);
    assertThat(fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())).as("Same field error in list").isTrue();
    assertThat(errors.hasFieldErrors("name")).as("Correct name errors flag").isTrue();
    assertThat(errors.getFieldErrorCount("name") == 1).as("Correct number of name errors").isTrue();
    assertThat(errors.getFieldErrors("name").size() == 1).as("Correct number of name errors in list").isTrue();
    FieldError nameError = errors.getFieldError("name");
    assertThat("message: &amp;".equals(nameError.getDefaultMessage())).as("Name error message escaped").isTrue();
    assertThat("NAME_EMPTY &".equals(nameError.getCode())).as("Name error code not escaped").isTrue();
    assertThat("empty &amp;".equals(errors.getFieldValue("name"))).as("Name value escaped").isTrue();
    FieldError nameErrorInList = errors.getFieldErrors("name").get(0);
    assertThat(nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage())).as("Same name error in list").isTrue();
    assertThat(errors.hasFieldErrors("age")).as("Correct age errors flag").isTrue();
    assertThat(errors.getFieldErrorCount("age") == 2).as("Correct number of age errors").isTrue();
    assertThat(errors.getFieldErrors("age").size() == 2).as("Correct number of age errors in list").isTrue();
    FieldError ageError = errors.getFieldError("age");
    assertThat("message: &lt;tag&gt;".equals(ageError.getDefaultMessage())).as("Age error message escaped").isTrue();
    assertThat("AGE_NOT_SET <tag>".equals(ageError.getCode())).as("Age error code not escaped").isTrue();
    assertThat((Integer.valueOf(0)).equals(errors.getFieldValue("age"))).as("Age value not escaped").isTrue();
    FieldError ageErrorInList = errors.getFieldErrors("age").get(0);
    assertThat(ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage())).as("Same name error in list").isTrue();
    FieldError ageError2 = errors.getFieldErrors("age").get(1);
    assertThat("message: &lt;tag&gt;".equals(ageError2.getDefaultMessage())).as("Age error 2 message escaped").isTrue();
    assertThat("AGE_NOT_32 <tag>".equals(ageError2.getCode())).as("Age error 2 code not escaped").isTrue();
}
Also used : Errors(org.springframework.validation.Errors) ObjectError(org.springframework.validation.ObjectError) TestBean(org.springframework.beans.testfixture.beans.TestBean) BindException(org.springframework.validation.BindException) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 38 with TestBean

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

the class RequestScopedProxyTests method testGetFromScopeThroughDynamicProxy.

@Test
public void testGetFromScopeThroughDynamicProxy() {
    String name = "requestScopedProxy";
    ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
    // assertTrue(AopUtils.isJdkDynamicProxy(bean));
    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 : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) 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 39 with TestBean

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

the class RequestScopedProxyTests method testGetInnerBeanFromScope.

@Test
public void testGetInnerBeanFromScope() {
    TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
    assertThat(AopUtils.isAopProxy(bean)).isFalse();
    assertThat(AopUtils.isCglibProxy(bean.getSpouse())).isTrue();
    String name = "scopedInnerBean";
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
        assertThat(request.getAttribute("scopedTarget." + name)).isNull();
        assertThat(bean.getSpouse().getName()).isEqualTo("scoped");
        assertThat(request.getAttribute("scopedTarget." + name)).isNotNull();
        assertThat(request.getAttribute("scopedTarget." + name).getClass()).isEqualTo(TestBean.class);
        assertThat(((TestBean) request.getAttribute("scopedTarget." + name)).getName()).isEqualTo("scoped");
    } 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 40 with TestBean

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

the class RequestAndSessionScopedBeanTests method testPutBeanInSession.

@Test
@SuppressWarnings("resource")
public void testPutBeanInSession() {
    String targetBeanName = "target";
    HttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setScope(WebApplicationContext.SCOPE_SESSION);
    bd.getPropertyValues().add("name", "abc");
    wac.registerBeanDefinition(targetBeanName, bd);
    wac.refresh();
    TestBean target = (TestBean) wac.getBean(targetBeanName);
    assertThat(target.getName()).isEqualTo("abc");
    assertThat(request.getSession().getAttribute(targetBeanName)).isSameAs(target);
    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)

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