Search in sources :

Example 1 with INestedTestBean

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

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

the class DelegatingIntroductionInterceptorTests method testDelegateReturnsThisIsMassagedToReturnProxy.

@Test
public void testDelegateReturnsThisIsMassagedToReturnProxy() {
    NestedTestBean target = new NestedTestBean();
    String company = "Interface21";
    target.setCompany(company);
    TestBean delegate = new TestBean() {

        @Override
        public ITestBean getSpouse() {
            return this;
        }
    };
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new DelegatingIntroductionInterceptor(delegate));
    INestedTestBean proxy = (INestedTestBean) pf.getProxy();
    assertThat(proxy.getCompany()).isEqualTo(company);
    ITestBean introduction = (ITestBean) proxy;
    assertThat(introduction.getSpouse()).as("Introduced method returning delegate returns proxy").isSameAs(introduction);
    assertThat(AopUtils.isAopProxy(introduction.getSpouse())).as("Introduced method returning delegate returns proxy").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)2 INestedTestBean (org.springframework.beans.testfixture.beans.INestedTestBean)2 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)2 Properties (java.util.Properties)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1