|
| 1 | +package com.dianpoint.summer.test.beans; |
| 2 | + |
| 3 | +import com.dianpoint.summer.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; |
| 4 | +import com.dianpoint.summer.beans.factory.config.BeanDefinition; |
| 5 | +import com.dianpoint.summer.beans.factory.support.DefaultListableBeanFactory; |
| 6 | +import com.dianpoint.summer.test.xml.beans.AutowiredService; |
| 7 | +import com.dianpoint.summer.test.xml.beans.BaseService; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * @author wangyi |
| 19 | + * @date 2023/3/28 |
| 20 | + */ |
| 21 | +public class DefaultListableBeanFactoryTest { |
| 22 | + |
| 23 | + static DefaultListableBeanFactory defaultListableBeanFactory; |
| 24 | + |
| 25 | + @BeforeClass |
| 26 | + public static void init(){ |
| 27 | + // 默认设置值 |
| 28 | + defaultListableBeanFactory = new DefaultListableBeanFactory(); |
| 29 | + // 注册若干 beanDefinition |
| 30 | + final BeanDefinition beanDefinition1 = new BeanDefinition("baseService1", BaseService.class.getName()); |
| 31 | + final BeanDefinition beanDefinition2 = new BeanDefinition("baseService2", BaseService.class.getName()); |
| 32 | + final BeanDefinition beanDefinition3 = new BeanDefinition("baseService3", BaseService.class.getName()); |
| 33 | + final BeanDefinition beanDefinition4 = new BeanDefinition("baseService4", BaseService.class.getName()); |
| 34 | + defaultListableBeanFactory.registerBeanDefinition("baseService1",beanDefinition1); |
| 35 | + defaultListableBeanFactory.registerBeanDefinition("baseService2",beanDefinition2); |
| 36 | + defaultListableBeanFactory.registerBeanDefinition("baseService3",beanDefinition3); |
| 37 | + defaultListableBeanFactory.registerBeanDefinition("baseService4",beanDefinition4); |
| 38 | + // 注册beans |
| 39 | + defaultListableBeanFactory.registerBean("baseService1",beanDefinition1); |
| 40 | + defaultListableBeanFactory.registerBean("baseService2",beanDefinition2); |
| 41 | + defaultListableBeanFactory.registerBean("baseService3",beanDefinition3); |
| 42 | + defaultListableBeanFactory.registerBean("baseService4",beanDefinition4); |
| 43 | + // 覆盖一条 |
| 44 | + defaultListableBeanFactory.registerBean("baseService4",beanDefinition4); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetBeanDefinitionCount() throws Exception { |
| 50 | + final int result = defaultListableBeanFactory.getBeanDefinitionCount(); |
| 51 | + Assert.assertEquals(4, result); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testGetBeanDefinitionNames() throws Exception { |
| 56 | + final String[] result = defaultListableBeanFactory.getBeanDefinitionNames(); |
| 57 | + Assert.assertArrayEquals(new String[]{"baseService1","baseService2","baseService3","baseService4"}, result); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testGetBeanNamesForType() throws Exception { |
| 62 | + final String[] result = defaultListableBeanFactory.getBeanNamesForType(BaseService.class); |
| 63 | + assertThat(result).isEmpty(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGetBeansOfType() throws Exception { |
| 68 | + final Map<String, BaseService> result = defaultListableBeanFactory.getBeansOfType(BaseService.class); |
| 69 | + for (final Map.Entry<String, BaseService> entry : result.entrySet()) { |
| 70 | + final BaseService baseService = (BaseService)defaultListableBeanFactory.getBean(entry.getKey()); |
| 71 | + System.out.println(entry.getValue()); |
| 72 | + System.out.println(baseService); |
| 73 | + assertThat(baseService).isEqualTo(entry.getValue()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testAddBeanPostProcessor() { |
| 79 | + final AutowiredAnnotationBeanPostProcessor r = new AutowiredAnnotationBeanPostProcessor(); |
| 80 | + defaultListableBeanFactory.addBeanPostProcessor(r); |
| 81 | + assertThat(defaultListableBeanFactory.getBeanPostProcessors().contains(r)).isTrue(); |
| 82 | + final int result = defaultListableBeanFactory.getBeanPostProcessorCount(); |
| 83 | + Assert.assertEquals(1, result); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testApplyBeanPostProcessorsBeforeInitialization() throws Exception { |
| 88 | + defaultListableBeanFactory.registerBean("autowiredService",new AutowiredService()); |
| 89 | + final BaseService base = new BaseService(); |
| 90 | + final Object result = defaultListableBeanFactory.applyBeanPostProcessorsBeforeInitialization(base, "baseService10"); |
| 91 | + Assert.assertEquals(base, result); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testApplyBeanPostProcessorsAfterInitialization() throws Exception { |
| 96 | + // applyBeanPostProcessorsAfterInitialization目前是空实现,但是由于不存在BeanPostProcessor,所以会返回入参 |
| 97 | + final BaseService baseService = new BaseService(); |
| 98 | + final Object result = defaultListableBeanFactory.applyBeanPostProcessorsAfterInitialization(baseService, "baseService10"); |
| 99 | + assertThat(result).isEqualTo(baseService); |
| 100 | + } |
| 101 | + |
| 102 | +// @Test |
| 103 | +// public void testRegisterDependentBean() throws Exception { |
| 104 | + |
| 105 | +// } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testGetDependentBeans() throws Exception { |
| 109 | + final String[] result = defaultListableBeanFactory.getDependentBeans("beanName"); |
| 110 | + Assert.assertArrayEquals(new String[]{}, result); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testGetDependenciesForBean() throws Exception { |
| 115 | + final String[] result = defaultListableBeanFactory.getDependenciesForBean("beanName"); |
| 116 | + Assert.assertArrayEquals(new String[]{}, result); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testGetBean() throws Exception { |
| 122 | + final Object result = defaultListableBeanFactory.getBean("baseService1"); |
| 123 | + assertThat(result).isNotNull(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testContainsBean() throws Exception { |
| 128 | + final boolean result = defaultListableBeanFactory.containsBean("baseService1"); |
| 129 | + Assert.assertTrue(result); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testIsSingleton() throws Exception { |
| 134 | + final boolean result = defaultListableBeanFactory.isSingleton("baseService1"); |
| 135 | + Assert.assertTrue(result); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testIsPrototype() throws Exception { |
| 140 | + final boolean result = defaultListableBeanFactory.isPrototype("baseService1"); |
| 141 | + Assert.assertFalse(result); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments