KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > access > SingletonBeanFactoryLocatorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
26  * @author Colin Sampaleanu
27  */

28 public class SingletonBeanFactoryLocatorTests extends TestCase {
29
30     public void testBaseBeanFactoryDefs() {
31         // Just test the base BeanFactory/AppContext defs we are going to work with
32
// in other tests.
33
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     /**
45      * Worker method so subclass can use it too.
46      */

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         // verify that the same instance is returned
54
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         // now verify that we can call release in any order
66
// unfortunately this doesn't validate complete release after the last one
67
bfr2.release();
68         bfr3.release();
69         bfr.release();
70         bfr4.release();
71     }
72
73     /**
74      * This test can run multiple times, but due to static keyed lookup of the locators,
75      * 2nd and subsequent calls will actuall get back same locator instance. This is not
76      * an issue really, since the contained beanfactories will still be loaded and released.
77      */

78     public void testGetInstance() {
79         // try with and without 'classpath*:' prefix, and with 'classpath:' prefix
80
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         // this will actually get another locator instance, as the key is the resource name
89
facLoc = SingletonBeanFactoryLocator.getInstance(
90                 "classpath:" + ClassUtils.addResourcePathToPackagePath(getClass(), "ref1.xml"));
91         getInstanceTest3(facLoc);
92         
93     }
94
95     /**
96      * Worker method so subclass can use it too
97      */

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         // verify that the same instance is returned
105
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     /**
125      * Worker method so subclass can use it too
126      */

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         // verify that the same instance is returned
139
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     /**
157      * Worker method so subclass can use it too
158      */

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         // verify that the same instance is returned
171
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