|
| 1 | +package com.dianpoint.summer.test.xml; |
| 2 | + |
| 3 | +import com.dianpoint.summer.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; |
| 4 | +import com.dianpoint.summer.beans.factory.config.BeanFactoryPostProcessor; |
| 5 | +import com.dianpoint.summer.context.ClassPathXmlApplicationContext; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * @author wangyi |
| 16 | + * @date 2023/3/31 |
| 17 | + */ |
| 18 | +public class ClassPathXmlApplicationContextTest { |
| 19 | + static ClassPathXmlApplicationContext classPathXmlApplicationContext; |
| 20 | + static String beanNames = "registerBeanStr"; |
| 21 | + static String beanObject = "beanObject"; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() { |
| 25 | + classPathXmlApplicationContext = new ClassPathXmlApplicationContext("xml/beanAutowire.xml",false); |
| 26 | + classPathXmlApplicationContext.registerBean(beanNames, beanObject); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testRegisterBean() { |
| 31 | + final String[] names = classPathXmlApplicationContext.getBeanFactory().getSingletonNames(); |
| 32 | + assertThat(names).contains(beanNames); |
| 33 | + assertThat(classPathXmlApplicationContext.getBeanFactory().containsBean(beanNames)).isTrue(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testGetSingleton() { |
| 38 | + final Object result = classPathXmlApplicationContext.getSingleton(beanNames); |
| 39 | + assertThat(result).isEqualTo(beanObject); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testAddBeanPostProcessor() { |
| 44 | + classPathXmlApplicationContext.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor()); |
| 45 | + final Integer count = classPathXmlApplicationContext.getBeanPostProcessorCount(); |
| 46 | + assertThat(count).isEqualTo(1); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testGetBeanDefinitionCount() { |
| 51 | + final Integer result = classPathXmlApplicationContext.getBeanDefinitionCount(); |
| 52 | + assertThat(result).isEqualTo(2); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testAddBeanFactoryPostProcessor() { |
| 58 | + classPathXmlApplicationContext.addBeanFactoryPostProcessor(beanFactory -> { |
| 59 | + |
| 60 | + }); |
| 61 | + final List<BeanFactoryPostProcessor> list = classPathXmlApplicationContext.getBeanFactoryPostProcessors(); |
| 62 | + assertThat(!list.isEmpty()).isTrue(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testIsActive() { |
| 67 | + final boolean result = classPathXmlApplicationContext.isActive(); |
| 68 | + assertThat(result).isTrue(); |
| 69 | + } |
| 70 | +} |
0 commit comments