Search in sources :

Example 66 with TestBean

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

the class BindTagTests method bindTagWithSetValueAndHtmlEscaping.

@Test
void bindTagWithSetValueAndHtmlEscaping() throws JspException {
    PageContext pc = createPageContext();
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.someSet");
    tag.setHtmlEscape(true);
    pc.getRequest().setAttribute("tb", new TestBean("juergen&eva", 99));
    tag.doStartTag();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getExpression()).isEqualTo("someSet");
    boolean condition = status.getValue() instanceof Set;
    assertThat(condition).isTrue();
}
Also used : Set(java.util.Set) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Example 67 with TestBean

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

the class BindTagTests method bindTagWithBeanButWithoutErrorsInstance.

@Test
void bindTagWithBeanButWithoutErrorsInstance() throws JspException {
    PageContext pc = createPageContext();
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    pc.getRequest().setAttribute("tb", new TestBean("juergen", 99));
    tag.doStartTag();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getExpression()).isNull();
    assertThat(status.getValue()).isNull();
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Example 68 with TestBean

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

the class BindTagTests method nestedPathWithBindTag.

@Test
void nestedPathWithBindTag() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    NestedPathTag nestedPathTag = new NestedPathTag();
    nestedPathTag.setPath("tb");
    nestedPathTag.setPageContext(pc);
    nestedPathTag.doStartTag();
    BindTag bindTag = new BindTag();
    bindTag.setPageContext(pc);
    bindTag.setPath("name");
    assertThat(bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat(status.getPath()).isEqualTo("tb.name");
    assertThat(status.getDisplayValue()).as("Correct field value").isEqualTo("");
    BindTag bindTag2 = new BindTag();
    bindTag2.setPageContext(pc);
    bindTag2.setPath("age");
    assertThat(bindTag2.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status2 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status2 != null).as("Has status variable").isTrue();
    assertThat(status2.getPath()).isEqualTo("tb.age");
    assertThat(status2.getDisplayValue()).as("Correct field value").isEqualTo("0");
    bindTag2.doEndTag();
    BindStatus status3 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status3).as("Status matches previous status").isSameAs(status);
    assertThat(status.getPath()).isEqualTo("tb.name");
    assertThat(status.getDisplayValue()).as("Correct field value").isEqualTo("");
    bindTag.doEndTag();
    nestedPathTag.doEndTag();
}
Also used : Errors(org.springframework.validation.Errors) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Example 69 with TestBean

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

the class BindTagTests method bindTagWithMappedProperties.

@Test
void bindTagWithMappedProperties() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("map[key1]", "code1", "message1");
    errors.rejectValue("map[key1]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.map[key1]");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat("map[key1]".equals(status.getExpression())).as("Correct expression").isTrue();
    boolean condition = status.getValue() instanceof TestBean;
    assertThat(condition).as("Value is TestBean").isTrue();
    assertThat("name4".equals(((TestBean) status.getValue()).getName())).as("Correct value").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 2).as("Correct errorCodes").isTrue();
    assertThat(status.getErrorMessages().length == 2).as("Correct errorMessages").isTrue();
    assertThat("code1".equals(status.getErrorCodes()[0])).as("Correct errorCode").isTrue();
    assertThat("code2".equals(status.getErrorCodes()[1])).as("Correct errorCode").isTrue();
    assertThat("message1".equals(status.getErrorMessages()[0])).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessages()[1])).as("Correct errorMessage").isTrue();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Example 70 with TestBean

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

the class BindTagTests method bindTagWithoutErrors.

@Test
void bindTagWithoutErrors() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat(status.getExpression() == null).as("Correct expression").isTrue();
    assertThat(status.getValue() == null).as("Correct value").isTrue();
    assertThat("".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    boolean condition = !status.isError();
    assertThat(condition).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 0).as("Correct errorCodes").isTrue();
    assertThat(status.getErrorMessages().length == 0).as("Correct errorMessages").isTrue();
    assertThat("".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("".equals(status.getErrorMessagesAsString(","))).as("Correct errorMessagesAsString").isTrue();
}
Also used : Errors(org.springframework.validation.Errors) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) 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