From 7303f2f090dbbbe9013dcc43b493ccc89e4d15cc Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Mon, 6 Jan 2025 19:13:22 +0800 Subject: [PATCH] Polish #57 : BeanFactoryUtilsTest --- .../beans/factory/BeanFactoryUtilsTest.java | 72 ++++++++++++++++--- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/microsphere-spring-context/src/test/java/io/microsphere/spring/beans/factory/BeanFactoryUtilsTest.java b/microsphere-spring-context/src/test/java/io/microsphere/spring/beans/factory/BeanFactoryUtilsTest.java index b3633e86..21da5a0b 100644 --- a/microsphere-spring-context/src/test/java/io/microsphere/spring/beans/factory/BeanFactoryUtilsTest.java +++ b/microsphere-spring-context/src/test/java/io/microsphere/spring/beans/factory/BeanFactoryUtilsTest.java @@ -19,17 +19,38 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; import java.util.List; +import static io.microsphere.collection.SetUtils.ofSet; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asAutowireCapableBeanFactory; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asBeanDefinitionRegistry; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asConfigurableBeanFactory; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asConfigurableListableBeanFactory; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asDefaultListableBeanFactory; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asHierarchicalBeanFactory; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asListableBeanFactory; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getBeanPostProcessors; import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getBeans; import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getOptionalBean; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getResolvableDependencyTypes; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.isBeanDefinitionRegistry; +import static io.microsphere.spring.beans.factory.BeanFactoryUtils.isDefaultListableBeanFactory; +import static io.microsphere.spring.context.ApplicationContextUtils.APPLICATION_CONTEXT_AWARE_PROCESSOR_CLASS_NAME; import static io.microsphere.util.ArrayUtils.of; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -41,22 +62,25 @@ public class BeanFactoryUtilsTest { private AnnotationConfigApplicationContext applicationContext; + private ConfigurableListableBeanFactory beanFactory; + @BeforeEach public void init() { - applicationContext = new AnnotationConfigApplicationContext(); + this.applicationContext = new AnnotationConfigApplicationContext(); + this.beanFactory = this.applicationContext.getBeanFactory(); } @AfterEach public void afterTest() { - applicationContext.close(); + this.applicationContext.close(); } @Test public void testGetOptionalBean() { - applicationContext.register(BaseTestBean.class); + this.applicationContext.register(BaseTestBean.class); - applicationContext.refresh(); + this.applicationContext.refresh(); BaseTestBean testBean = getOptionalBean(applicationContext, "baseTestBean", BaseTestBean.class); @@ -69,7 +93,7 @@ public void testGetOptionalBean() { @Test public void testGetOptionalBeanIfAbsent() { - applicationContext.refresh(); + this.applicationContext.refresh(); BaseTestBean testBean = getOptionalBean(applicationContext, "baseTestBean", BaseTestBean.class); @@ -87,9 +111,9 @@ public void testGetOptionalBeanIfAbsent() { @Test public void testGetBeans() { - applicationContext.register(BaseTestBean.class, BaseTestBean2.class); + this.applicationContext.register(BaseTestBean.class, BaseTestBean2.class); - applicationContext.refresh(); + this.applicationContext.refresh(); List testBeans = getBeans(applicationContext, new String[]{"baseTestBean"}, BaseTestBean.class); @@ -113,7 +137,7 @@ public void testGetBeans() { @Test public void testGetBeansIfAbsent() { - applicationContext.refresh(); + this.applicationContext.refresh(); List testBeans = getBeans(applicationContext, new String[]{"baseTestBean"}, BaseTestBean.class); @@ -121,6 +145,38 @@ public void testGetBeansIfAbsent() { } + @Test + public void testIsMethods() { + assertTrue(isDefaultListableBeanFactory(this.beanFactory)); + assertTrue(isBeanDefinitionRegistry(this.beanFactory)); + } + + @Test + public void testAsMethods() { + assertSame(this.beanFactory, asBeanDefinitionRegistry(this.beanFactory)); + assertSame(this.beanFactory, asListableBeanFactory(this.beanFactory)); + assertSame(this.beanFactory, asHierarchicalBeanFactory(this.beanFactory)); + assertSame(this.beanFactory, asConfigurableBeanFactory(this.beanFactory)); + assertSame(this.beanFactory, asAutowireCapableBeanFactory(this.beanFactory)); + assertSame(this.beanFactory, asConfigurableListableBeanFactory(this.beanFactory)); + assertSame(this.beanFactory, asDefaultListableBeanFactory(this.beanFactory)); + } + + @Test + public void testGetResolvableDependencyTypes() { + this.applicationContext.refresh(); + assertEquals(ofSet(BeanFactory.class, ResourceLoader.class, ApplicationEventPublisher.class, ApplicationContext.class), + getResolvableDependencyTypes(this.beanFactory)); + } + + @Test + public void testGetBeanPostProcessors() { + this.applicationContext.refresh(); + List beanPostProcessors = getBeanPostProcessors(this.beanFactory); + assertFalse(beanPostProcessors.isEmpty()); + assertEquals(APPLICATION_CONTEXT_AWARE_PROCESSOR_CLASS_NAME, beanPostProcessors.get(0).getClass().getName()); + } + @Component("baseTestBean2") private static class BaseTestBean2 extends BaseTestBean {