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");
}
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();
}
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());
}
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"));
}
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");
}
Aggregations