KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.beans.BeansException;
26 import org.springframework.beans.TestBean;
27 import org.springframework.beans.factory.BeanFactory;
28 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
29
30 /**
31  * @author Rod Johnson
32  * @since 02.12.2002
33  */

34 public class BeanFactoryBootstrapTests extends TestCase {
35     
36     private Properties JavaDoc savedProps;
37
38     protected void setUp() {
39         // Save and restore System properties, which get destroyed for the tests.
40
this.savedProps = System.getProperties();
41     }
42
43     protected void tearDown() {
44         System.setProperties(this.savedProps);
45     }
46
47     /** How to test many singletons? */
48     public void testGetInstanceWithNullPropertiesFails() throws BeansException {
49         System.setProperties(null);
50         BeanFactoryBootstrap.reinitialize();
51         try {
52             BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance();
53             fail("Should have failed with no system properties");
54         }
55         catch (BootstrapException ex) {
56             // OK
57
}
58     }
59     
60     public void testGetInstanceWithUnknownBeanFactoryClassFails() throws BeansException {
61         System.setProperties(null);
62         Properties JavaDoc p = new Properties JavaDoc();
63         p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class",
64         "org.springframework.beans.factory.support.xxxxXmlBeanFactory");
65         
66         System.setProperties(p);
67         BeanFactoryBootstrap.reinitialize();
68         try {
69             BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance();
70             fail("Should have failed with invalid class");
71         }
72         catch (BootstrapException ex) {
73             // OK
74
}
75     }
76     
77     public void testGetInstanceWithMistypedBeanFactoryClassFails() throws BeansException {
78         System.setProperties(null);
79         Properties JavaDoc p = new Properties JavaDoc();
80         p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class",
81         "java.awt.Point");
82         
83         System.setProperties(p);
84         BeanFactoryBootstrap.reinitialize();
85         try {
86             BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance();
87             fail("Should have failed with mistyped class");
88         }
89         catch (BootstrapException ex) {
90             // OK
91
}
92         catch (Exception JavaDoc ex) {
93             ex.printStackTrace();
94         }
95     }
96     
97 // public void testXmlBeanFactory() throws Exception {
98
// Properties p = new Properties();
99
// p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class",
100
// "XmlBeanFactory");
101
// p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".url",
102
// "c:/checkouts/book/framework/src/org/springframework/beans/factory/support/bs.xml");
103
//
104
//
105
// System.setProperties(p);
106
// System.getProperties().list(System.out);
107
//
108
// BeanFactoryBootstrap.reinitialize();
109
//
110
// try {
111
// BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance();
112
//
113
// BeanFactory bf1 = BeanFactoryBootstrap.getInstance().getBeanFactory();
114
// BeanFactory bf2 = BeanFactoryBootstrap.getInstance().getBeanFactory();
115
// assertTrue("Two instances identical", bf1==bf2);
116
//
117
// System.out.println("Got bean factory");
118
// assertNotNull("Bsb instance is not null", bsb);
119
// TestBean tb = (TestBean) bsb.getBeanFactory().getBean("test");
120
// assertNotNull("Test bean is not null", tb);
121
// System.out.println(tb);
122
// assertTrue("Property set", tb.getFoo().equals("bar"));
123
// }
124
// catch (Exception ex) {
125
// ex.printStackTrace();
126
// throw ex;
127
// }
128
// }
129

130     public void testDummyBeanFactory() throws Exception JavaDoc {
131         Properties JavaDoc p = new Properties JavaDoc();
132         p.put(BeanFactoryBootstrap.BEAN_FACTORY_BEAN_NAME + ".class",
133         "org.springframework.beans.factory.access.BeanFactoryBootstrapTests$DummyBeanFactory");
134         
135         System.setProperties(p);
136
137         BeanFactoryBootstrap.reinitialize();
138
139         try {
140             BeanFactoryBootstrap bsb = BeanFactoryBootstrap.getInstance();
141             assertNotNull("Bsb instance is not null", bsb);
142             assertTrue("Is dummy", bsb.getBeanFactory() instanceof DummyBeanFactory);
143             TestBean tb = (TestBean) bsb.getBeanFactory().getBean("test");
144             assertNotNull("Test bean is not null", tb);
145             //assertTrue("Property set", tb.getFoo().equals("bar"));
146
}
147         catch (Exception JavaDoc ex) {
148             ex.printStackTrace();
149             throw ex;
150         }
151     }
152
153
154     public static class DummyBeanFactory implements BeanFactory {
155
156         public Map JavaDoc map = new HashMap JavaDoc();
157
158          {
159             this.map.put("test", new TestBean());
160             this.map.put("s", new String JavaDoc());
161         }
162
163         public Object JavaDoc getBean(String JavaDoc name) {
164             Object JavaDoc bean = this.map.get(name);
165             if (bean == null) {
166                 throw new NoSuchBeanDefinitionException(name, "no message");
167             }
168             return bean;
169         }
170
171         public Object JavaDoc getBean(String JavaDoc name, Class JavaDoc requiredType) {
172             return getBean(name);
173         }
174
175         public boolean containsBean(String JavaDoc name) {
176             return this.map.containsKey(name);
177         }
178
179         public boolean isSingleton(String JavaDoc name) {
180             return true;
181         }
182
183         public Class JavaDoc getType(String JavaDoc name) {
184             return null;
185         }
186
187         public String JavaDoc[] getAliases(String JavaDoc name) {
188             throw new UnsupportedOperationException JavaDoc("getAliases");
189         }
190     }
191
192 }
193
Popular Tags