Search in sources :

Example 1 with IndexedTestBean

use of org.springframework.beans.testfixture.beans.IndexedTestBean 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 2 with IndexedTestBean

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

the class BindTagTests method bindTagWithIndexedProperties.

@Test
void bindTagWithIndexedProperties() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    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("array[0]".equals(status.getExpression())).as("Correct expression").isTrue();
    boolean condition = status.getValue() instanceof TestBean;
    assertThat(condition).as("Value is TestBean").isTrue();
    assertThat("name0".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 3 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOptionalCollectionResourceInjection.

@Test
public void testOptionalCollectionResourceInjection() {
    RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class);
    rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", rbd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    IndexedTestBean itb = new IndexedTestBean();
    bf.registerSingleton("indexedTestBean", itb);
    NestedTestBean ntb1 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    NestedTestBean ntb2 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    // Two calls to verify that caching doesn't break re-creation.
    OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb);
    assertThat(bean.getTestBean3()).isSameAs(tb);
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().size()).isEqualTo(2);
    assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb1);
    assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(2);
    assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansSetter.get(1)).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField.size()).isEqualTo(2);
    assertThat(bean.nestedTestBeansField.get(0)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField.get(1)).isSameAs(ntb2);
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testAnnotationOrderedCollectionResourceInjection.

@Test
public void testAnnotationOrderedCollectionResourceInjection() {
    RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class);
    rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", rbd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    IndexedTestBean itb = new IndexedTestBean();
    bf.registerSingleton("indexedTestBean", itb);
    FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    // Two calls to verify that caching doesn't break re-creation.
    OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb);
    assertThat(bean.getTestBean3()).isSameAs(tb);
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().size()).isEqualTo(2);
    assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2);
    assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(2);
    assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansSetter.get(1)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField.size()).isEqualTo(2);
    assertThat(bean.nestedTestBeansField.get(0)).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField.get(1)).isSameAs(ntb1);
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOptionalResourceInjectionWithSingletonRemoval.

@Test
public void testOptionalResourceInjectionWithSingletonRemoval() {
    RootBeanDefinition rbd = new RootBeanDefinition(OptionalResourceInjectionBean.class);
    rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", rbd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    IndexedTestBean itb = new IndexedTestBean();
    bf.registerSingleton("indexedTestBean", itb);
    NestedTestBean ntb1 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    NestedTestBean ntb2 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb);
    assertThat(bean.getTestBean3()).isSameAs(tb);
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().length).isEqualTo(2);
    assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1);
    assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField.length).isEqualTo(2);
    assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2);
    bf.destroySingleton("testBean");
    bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isNull();
    assertThat(bean.getTestBean2()).isNull();
    assertThat(bean.getTestBean3()).isNull();
    assertThat(bean.getTestBean4()).isNull();
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().length).isEqualTo(2);
    assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1);
    assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField.length).isEqualTo(2);
    assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2);
    bf.registerSingleton("testBean", tb);
    bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb);
    assertThat(bean.getTestBean3()).isSameAs(tb);
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().length).isEqualTo(2);
    assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1);
    assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField.length).isEqualTo(2);
    assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2);
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)62 Test (org.junit.jupiter.api.Test)61 TestBean (org.springframework.beans.testfixture.beans.TestBean)39 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)31 PropertyEditorSupport (java.beans.PropertyEditorSupport)22 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)17 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)16 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)15 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)15 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)12 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)11 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)11 BeanWrapper (org.springframework.beans.BeanWrapper)10 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)10 ArrayList (java.util.ArrayList)7 BeanDefinitionBuilder.genericBeanDefinition (org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition)7 ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)7 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4