1 16 17 package org.springframework.beans.factory.access; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.beans.factory.BeanFactory; 22 import org.springframework.context.support.ClassPathXmlApplicationContext; 23 import org.springframework.util.ClassUtils; 24 25 28 public class SingletonBeanFactoryLocatorTests extends TestCase { 29 30 public void testBaseBeanFactoryDefs() { 31 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 34 "/org/springframework/beans/factory/access/beans*.xml"); 35 } 36 37 public void testBasicFunctionality() { 38 SingletonBeanFactoryLocator facLoc = new SingletonBeanFactoryLocator( 39 "classpath*:" + ClassUtils.addResourcePathToPackagePath(getClass(), "ref1.xml")); 40 41 basicFunctionalityTest(facLoc); 42 } 43 44 47 protected void basicFunctionalityTest(SingletonBeanFactoryLocator facLoc) { 48 49 BeanFactoryReference bfr = facLoc.useBeanFactory("a.qualified.name.of.some.sort"); 50 BeanFactory fac = bfr.getFactory(); 51 BeanFactoryReference bfr2 = facLoc.useBeanFactory("another.qualified.name"); 52 fac = bfr2.getFactory(); 53 TestBean tb = (TestBean) fac.getBean("beans1.bean1"); 55 assertTrue(tb.getName().equals("beans1.bean1")); 56 tb.setName("was beans1.bean1"); 57 BeanFactoryReference bfr3 = facLoc.useBeanFactory("another.qualified.name"); 58 fac = bfr3.getFactory(); 59 tb = (TestBean) fac.getBean("beans1.bean1"); 60 assertTrue(tb.getName().equals("was beans1.bean1")); 61 BeanFactoryReference bfr4 = facLoc.useBeanFactory("a.qualified.name.which.is.an.alias"); 62 fac = bfr4.getFactory(); 63 tb = (TestBean) fac.getBean("beans1.bean1"); 64 assertTrue(tb.getName().equals("was beans1.bean1")); 65 bfr2.release(); 68 bfr3.release(); 69 bfr.release(); 70 bfr4.release(); 71 } 72 73 78 public void testGetInstance() { 79 BeanFactoryLocator facLoc = SingletonBeanFactoryLocator.getInstance( 81 ClassUtils.addResourcePathToPackagePath(getClass(), "ref1.xml")); 82 getInstanceTest1(facLoc); 83 84 facLoc = SingletonBeanFactoryLocator.getInstance( 85 "classpath*:/" + ClassUtils.addResourcePathToPackagePath(getClass(), "ref1.xml")); 86 getInstanceTest2(facLoc); 87 88 facLoc = SingletonBeanFactoryLocator.getInstance( 90 "classpath:" + ClassUtils.addResourcePathToPackagePath(getClass(), "ref1.xml")); 91 getInstanceTest3(facLoc); 92 93 } 94 95 98 protected void getInstanceTest1(BeanFactoryLocator facLoc) { 99 100 BeanFactoryReference bfr = facLoc.useBeanFactory("a.qualified.name.of.some.sort"); 101 BeanFactory fac = bfr.getFactory(); 102 BeanFactoryReference bfr2 = facLoc.useBeanFactory("another.qualified.name"); 103 fac = bfr2.getFactory(); 104 TestBean tb = (TestBean) fac.getBean("beans1.bean1"); 106 assertTrue(tb.getName().equals("beans1.bean1")); 107 tb.setName("was beans1.bean1"); 108 BeanFactoryReference bfr3 = facLoc.useBeanFactory("another.qualified.name"); 109 fac = bfr3.getFactory(); 110 tb = (TestBean) fac.getBean("beans1.bean1"); 111 assertTrue(tb.getName().equals("was beans1.bean1")); 112 113 BeanFactoryReference bfr4 = facLoc.useBeanFactory("a.qualified.name.which.is.an.alias"); 114 fac = bfr4.getFactory(); 115 tb = (TestBean) fac.getBean("beans1.bean1"); 116 assertTrue(tb.getName().equals("was beans1.bean1")); 117 118 bfr.release(); 119 bfr3.release(); 120 bfr2.release(); 121 bfr4.release(); 122 } 123 124 127 protected void getInstanceTest2(BeanFactoryLocator facLoc) { 128 BeanFactoryReference bfr; 129 BeanFactory fac; 130 BeanFactoryReference bfr2; 131 TestBean tb; 132 BeanFactoryReference bfr3; 133 BeanFactoryReference bfr4; 134 bfr = facLoc.useBeanFactory("a.qualified.name.of.some.sort"); 135 fac = bfr.getFactory(); 136 bfr2 = facLoc.useBeanFactory("another.qualified.name"); 137 fac = bfr2.getFactory(); 138 tb = (TestBean) fac.getBean("beans1.bean1"); 140 assertTrue(tb.getName().equals("beans1.bean1")); 141 tb.setName("was beans1.bean1"); 142 bfr3 = facLoc.useBeanFactory("another.qualified.name"); 143 fac = bfr3.getFactory(); 144 tb = (TestBean) fac.getBean("beans1.bean1"); 145 assertTrue(tb.getName().equals("was beans1.bean1")); 146 bfr4 = facLoc.useBeanFactory("a.qualified.name.which.is.an.alias"); 147 fac = bfr4.getFactory(); 148 tb = (TestBean) fac.getBean("beans1.bean1"); 149 assertTrue(tb.getName().equals("was beans1.bean1")); 150 bfr.release(); 151 bfr2.release(); 152 bfr4.release(); 153 bfr3.release(); 154 } 155 156 159 protected void getInstanceTest3(BeanFactoryLocator facLoc) { 160 BeanFactoryReference bfr; 161 BeanFactory fac; 162 BeanFactoryReference bfr2; 163 TestBean tb; 164 BeanFactoryReference bfr3; 165 BeanFactoryReference bfr4; 166 bfr = facLoc.useBeanFactory("a.qualified.name.of.some.sort"); 167 fac = bfr.getFactory(); 168 bfr2 = facLoc.useBeanFactory("another.qualified.name"); 169 fac = bfr2.getFactory(); 170 tb = (TestBean) fac.getBean("beans1.bean1"); 172 assertTrue(tb.getName().equals("beans1.bean1")); 173 tb.setName("was beans1.bean1"); 174 bfr3 = facLoc.useBeanFactory("another.qualified.name"); 175 fac = bfr3.getFactory(); 176 tb = (TestBean) fac.getBean("beans1.bean1"); 177 assertTrue(tb.getName().equals("was beans1.bean1")); 178 bfr4 = facLoc.useBeanFactory("a.qualified.name.which.is.an.alias"); 179 fac = bfr4.getFactory(); 180 tb = (TestBean) fac.getBean("beans1.bean1"); 181 assertTrue(tb.getName().equals("was beans1.bean1")); 182 bfr4.release(); 183 bfr3.release(); 184 bfr2.release(); 185 bfr.release(); 186 } 187 } 188 | Popular Tags |