Search in sources :

Example 6 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-infrastructure by TAKETODAY.

the class ConfigurationClassProcessingTests method configWithFactoryBeanReturnType.

@Test
public void configWithFactoryBeanReturnType() {
    BeanFactory factory = initBeanFactory(ConfigWithNonSpecificReturnTypes.class);
    assertThat(factory.getType("factoryBean")).isEqualTo(List.class);
    assertThat(factory.isTypeMatch("factoryBean", List.class)).isTrue();
    assertThat(factory.getType("&factoryBean")).isEqualTo(FactoryBean.class);
    assertThat(factory.isTypeMatch("&factoryBean", FactoryBean.class)).isTrue();
    assertThat(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class)).isFalse();
    assertThat(factory.isTypeMatch("&factoryBean", ListFactoryBean.class)).isFalse();
    boolean condition = factory.getBean("factoryBean") instanceof List;
    assertThat(condition).isTrue();
    Set<String> beanNames = factory.getBeanNamesForType(FactoryBean.class);
    assertThat(beanNames.size()).isEqualTo(1);
    assertThat(beanNames.iterator().next()).isEqualTo("&factoryBean");
    beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
    assertThat(beanNames.size()).isEqualTo(1);
    assertThat(beanNames.iterator().next()).isEqualTo("&factoryBean");
    beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
    assertThat(beanNames.size()).isEqualTo(1);
    assertThat(beanNames.iterator().next()).isEqualTo("&factoryBean");
    beanNames = factory.getBeanNamesForType(List.class);
    assertThat(beanNames.iterator().next()).isEqualTo("factoryBean");
}
Also used : BeanClassLoaderAware(cn.taketoday.beans.factory.BeanClassLoaderAware) BeanFactory(cn.taketoday.beans.factory.BeanFactory) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) List(java.util.List) ListFactoryBean(cn.taketoday.beans.factory.config.ListFactoryBean) Test(org.junit.jupiter.api.Test)

Example 7 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-infrastructure by TAKETODAY.

the class SelectivePrototypeTargetSourceCreator method testCommonInterceptorAndAdvisor.

/**
 * Check that we can provide a common interceptor that will
 * appear in the chain before "specific" interceptors,
 * which are sourced from matching advisors
 */
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
    BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
    ITestBean test1 = (ITestBean) bf.getBean("test1");
    assertThat(AopUtils.isAopProxy(test1)).isTrue();
    Lockable lockable1 = (Lockable) test1;
    NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
    NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();
    ITestBean test2 = (ITestBean) bf.getBean("test2");
    Lockable lockable2 = (Lockable) test2;
    // Locking should be independent; nop is shared
    assertThat(lockable1.locked()).isFalse();
    assertThat(lockable2.locked()).isFalse();
    // equals 2 calls on shared nop, because it's first and sees calls
    // against the Lockable interface introduced by the specific advisor
    assertThat(nop1.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(0);
    lockable1.lock();
    assertThat(lockable1.locked()).isTrue();
    assertThat(lockable2.locked()).isFalse();
    assertThat(nop1.getCount()).isEqualTo(5);
    assertThat(nop2.getCount()).isEqualTo(0);
    PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
    assertThat(nop1.getCount()).isEqualTo(5);
    assertThat(nop2.getCount()).isEqualTo(0);
    packageVisibleMethod.doSomething();
    assertThat(nop1.getCount()).isEqualTo(6);
    assertThat(nop2.getCount()).isEqualTo(1);
    boolean condition = packageVisibleMethod instanceof Lockable;
    assertThat(condition).isTrue();
    Lockable lockable3 = (Lockable) packageVisibleMethod;
    lockable3.lock();
    assertThat(lockable3.locked()).isTrue();
    lockable3.unlock();
    assertThat(lockable3.locked()).isFalse();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) BeanFactory(cn.taketoday.beans.factory.BeanFactory) Lockable(test.aop.Lockable) Test(org.junit.jupiter.api.Test)

Example 8 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-infrastructure by TAKETODAY.

the class ConfigurationClassProcessingTests method configurationWithPrototypeScopedBeans.

@Test
public void configurationWithPrototypeScopedBeans() {
    BeanFactory factory = initBeanFactory(ConfigWithPrototypeBean.class);
    TestBean foo = factory.getBean("foo", TestBean.class);
    ITestBean bar = factory.getBean("bar", ITestBean.class);
    ITestBean baz = factory.getBean("baz", ITestBean.class);
    assertThat(bar).isSameAs(foo.getSpouse());
    assertThat(baz).isNotSameAs(bar.getSpouse());
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BeanFactory(cn.taketoday.beans.factory.BeanFactory) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 9 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-infrastructure by TAKETODAY.

the class ConfigurationClassProcessingTests method aliasesAreRespected.

private void aliasesAreRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
    TestBean testBean = testBeanSupplier.get();
    BeanFactory factory = initBeanFactory(testClass);
    assertThat(factory.getBean(beanName)).isSameAs(testBean);
    Arrays.stream(factory.getAliases(beanName)).map(factory::getBean).forEach(alias -> assertThat(alias).isSameAs(testBean));
    // method name should not be registered
    assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> factory.getBean("methodName"));
}
Also used : NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BeanFactory(cn.taketoday.beans.factory.BeanFactory) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException)

Example 10 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-infrastructure by TAKETODAY.

the class ConfigurationClassProcessingTests method simplestPossibleConfig.

@Test
public void simplestPossibleConfig() {
    BeanFactory factory = initBeanFactory(SimplestPossibleConfig.class);
    String stringBean = factory.getBean("stringBean", String.class);
    assertThat(stringBean).isEqualTo("foo");
}
Also used : BeanFactory(cn.taketoday.beans.factory.BeanFactory) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Aggregations

BeanFactory (cn.taketoday.beans.factory.BeanFactory)80 Test (org.junit.jupiter.api.Test)60 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)24 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)16 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)16 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)10 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)8 CallCountingTransactionManager (cn.taketoday.testfixture.transaction.CallCountingTransactionManager)8 TransactionManager (cn.taketoday.transaction.TransactionManager)8 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)6 List (java.util.List)6 DataSource (javax.sql.DataSource)6 Advised (cn.taketoday.aop.framework.Advised)4 BeansException (cn.taketoday.beans.BeansException)4 FatalBeanException (cn.taketoday.beans.FatalBeanException)4 NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)4 Nullable (cn.taketoday.lang.Nullable)4 Collection (java.util.Collection)4 Set (java.util.Set)4 ServletException (jakarta.servlet.ServletException)3