Search in sources :

Example 26 with NestedTestBean

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

the class CommonAnnotationBeanPostProcessorTests method testExtendedResourceInjection.

@Test
public void testExtendedResourceInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerResolvableDependency(BeanFactory.class, bf);
    @SuppressWarnings("deprecation") org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ppc = new org.springframework.beans.factory.config.PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("tb", "testBean3");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(bf);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class));
    bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
    bf.registerBeanDefinition("annotatedBean3", new RootBeanDefinition(ConvertedResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    TestBean tb3 = new TestBean();
    bf.registerSingleton("testBean3", tb3);
    TestBean tb4 = new TestBean();
    bf.registerSingleton("testBean4", tb4);
    NestedTestBean tb6 = new NestedTestBean();
    bf.registerSingleton("value", "5");
    bf.registerSingleton("xy", tb6);
    bf.registerAlias("xy", "testBean9");
    ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.initCalled).isTrue();
    assertThat(bean.init2Called).isTrue();
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb2);
    assertThat(bean.getTestBean3()).isSameAs(tb4);
    assertThat(bean.getTestBean4()).isSameAs(tb3);
    assertThat(bean.testBean5).isSameAs(tb6);
    assertThat(bean.testBean6).isSameAs(tb6);
    assertThat(bean.beanFactory).isSameAs(bf);
    NamedResourceInjectionBean bean2 = (NamedResourceInjectionBean) bf.getBean("annotatedBean2");
    assertThat(bean2.testBean).isSameAs(tb6);
    ConvertedResourceInjectionBean bean3 = (ConvertedResourceInjectionBean) bf.getBean("annotatedBean3");
    assertThat(bean3.value).isSameAs(5);
    bf.destroySingletons();
    assertThat(bean.destroyCalled).isTrue();
    assertThat(bean.destroy2Called).isTrue();
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 27 with NestedTestBean

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

the class CommonAnnotationBeanPostProcessorTests method testExtendedResourceInjectionWithOverriding.

@Test
public void testExtendedResourceInjectionWithOverriding() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerResolvableDependency(BeanFactory.class, bf);
    @SuppressWarnings("deprecation") org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ppc = new org.springframework.beans.factory.config.PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("tb", "testBean3");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(bf);
    RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
    TestBean tb5 = new TestBean();
    annotatedBd.getPropertyValues().add("testBean2", tb5);
    bf.registerBeanDefinition("annotatedBean", annotatedBd);
    bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    TestBean tb3 = new TestBean();
    bf.registerSingleton("testBean3", tb3);
    TestBean tb4 = new TestBean();
    bf.registerSingleton("testBean4", tb4);
    NestedTestBean tb6 = new NestedTestBean();
    bf.registerSingleton("xy", tb6);
    ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.initCalled).isTrue();
    assertThat(bean.init2Called).isTrue();
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb5);
    assertThat(bean.getTestBean3()).isSameAs(tb4);
    assertThat(bean.getTestBean4()).isSameAs(tb3);
    assertThat(bean.testBean5).isSameAs(tb6);
    assertThat(bean.testBean6).isSameAs(tb6);
    assertThat(bean.beanFactory).isSameAs(bf);
    try {
        bf.getBean("annotatedBean2");
    } catch (BeanCreationException ex) {
        boolean condition = ex.getRootCause() instanceof NoSuchBeanDefinitionException;
        assertThat(condition).isTrue();
        NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
        assertThat(innerEx.getBeanName()).isEqualTo("testBean9");
    }
    bf.destroySingletons();
    assertThat(bean.destroyCalled).isTrue();
    assertThat(bean.destroy2Called).isTrue();
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.jupiter.api.Test)

Example 28 with NestedTestBean

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

the class CommonAnnotationBeanPostProcessorTests method testExtendedEjbInjection.

@Test
public void testExtendedEjbInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerResolvableDependency(BeanFactory.class, bf);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedEjbInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    TestBean tb3 = new TestBean();
    bf.registerSingleton("testBean3", tb3);
    TestBean tb4 = new TestBean();
    bf.registerSingleton("testBean4", tb4);
    NestedTestBean tb6 = new NestedTestBean();
    bf.registerSingleton("xy", tb6);
    bf.registerAlias("xy", "testBean9");
    ExtendedEjbInjectionBean bean = (ExtendedEjbInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.initCalled).isTrue();
    assertThat(bean.init2Called).isTrue();
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb2);
    assertThat(bean.getTestBean3()).isSameAs(tb4);
    assertThat(bean.getTestBean4()).isSameAs(tb3);
    assertThat(bean.testBean5).isSameAs(tb6);
    assertThat(bean.testBean6).isSameAs(tb6);
    assertThat(bean.beanFactory).isSameAs(bf);
    bf.destroySingletons();
    assertThat(bean.destroyCalled).isTrue();
    assertThat(bean.destroy2Called).isTrue();
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 29 with NestedTestBean

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

the class BindTagTests method bindTagWithToStringAndHtmlEscaping.

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

Aggregations

Test (org.junit.jupiter.api.Test)29 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)29 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)27 TestBean (org.springframework.beans.testfixture.beans.TestBean)27 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)26 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)22 INestedTestBean (org.springframework.beans.testfixture.beans.INestedTestBean)6 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 Properties (java.util.Properties)3 PageContext (jakarta.servlet.jsp.PageContext)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 BindStatus (org.springframework.web.servlet.support.BindStatus)1