Search in sources :

Example 11 with NestedTestBean

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

the class InjectAnnotationBeanPostProcessorTests method testConstructorResourceInjectionWithMultipleCandidatesAsCollection.

@Test
public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() {
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsCollectionResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    NestedTestBean ntb1 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    NestedTestBean ntb2 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean3()).isNull();
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getNestedTestBeans().size()).isEqualTo(2);
    assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb1);
    assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb2);
}
Also used : 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 12 with NestedTestBean

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

the class InjectAnnotationBeanPostProcessorTests method testExtendedResourceInjection.

@Test
public void testExtendedResourceInjection() {
    RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", bd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    NestedTestBean ntb = new NestedTestBean();
    bf.registerSingleton("nestedTestBean", ntb);
    TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) 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.getNestedTestBean()).isSameAs(ntb);
    assertThat(bean.getBeanFactory()).isSameAs(bf);
    bean = (TypedExtendedResourceInjectionBean) 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.getNestedTestBean()).isSameAs(ntb);
    assertThat(bean.getBeanFactory()).isSameAs(bf);
}
Also used : 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 13 with NestedTestBean

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

the class CommonAnnotationBeanPostProcessorTests method testResourceInjectionWithResolvableDependencyType.

@Test
public void testResourceInjectionWithResolvableDependencyType() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition abd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
    abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", abd);
    RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
    tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("testBean4", tbd);
    bf.registerResolvableDependency(BeanFactory.class, bf);
    bf.registerResolvableDependency(INestedTestBean.class, (ObjectFactory<Object>) () -> new NestedTestBean());
    @SuppressWarnings("deprecation") org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ppc = new org.springframework.beans.factory.config.PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("tb", "testBean4");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(bf);
    ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
    INestedTestBean tb = bean.getTestBean6();
    assertThat(tb).isNotNull();
    ExtendedResourceInjectionBean anotherBean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean).isNotSameAs(anotherBean);
    assertThat(tb).isNotSameAs(anotherBean.getTestBean6());
    String[] depBeans = bf.getDependenciesForBean("annotatedBean");
    assertThat(depBeans.length).isEqualTo(1);
    assertThat(depBeans[0]).isEqualTo("testBean4");
}
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) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 14 with NestedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testExtendedResourceInjectionWithDestruction.

@Test
public void testExtendedResourceInjectionWithDestruction() {
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(TypedExtendedResourceInjectionBean.class));
    bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
    NestedTestBean ntb = new NestedTestBean();
    bf.registerSingleton("nestedTestBean", ntb);
    TestBean tb = bf.getBean("testBean", TestBean.class);
    TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) 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.getNestedTestBean()).isSameAs(ntb);
    assertThat(bean.getBeanFactory()).isSameAs(bf);
    assertThat(bf.getDependenciesForBean("annotatedBean")).isEqualTo(new String[] { "testBean", "nestedTestBean" });
    bf.destroySingleton("testBean");
    assertThat(bf.containsSingleton("testBean")).isFalse();
    assertThat(bf.containsSingleton("annotatedBean")).isFalse();
    assertThat(bean.destroyed).isTrue();
    assertThat(bf.getDependenciesForBean("annotatedBean").length).isSameAs(0);
}
Also used : 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 15 with NestedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOptionalResourceInjection.

@Test
public void testOptionalResourceInjection() {
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
    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);
}
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

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