KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > xml > XmlBeanCollectionTests


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.xml;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeMap JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.springframework.beans.TestBean;
33 import org.springframework.beans.factory.HasMap;
34 import org.springframework.beans.factory.config.ListFactoryBean;
35 import org.springframework.beans.factory.config.MapFactoryBean;
36 import org.springframework.beans.factory.config.SetFactoryBean;
37 import org.springframework.core.io.ClassPathResource;
38
39 /**
40  * Tests for collections in XML bean definitions.
41  *
42  * @author Juergen Hoeller
43  * @since 19.12.2004
44  */

45 public class XmlBeanCollectionTests extends TestCase {
46
47     public void testRefSubelement() throws Exception JavaDoc {
48         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
49         //assertTrue("5 beans in reftypes, not " + xbf.getBeanDefinitionCount(), xbf.getBeanDefinitionCount() == 5);
50
TestBean jen = (TestBean) xbf.getBean("jenny");
51         TestBean dave = (TestBean) xbf.getBean("david");
52         assertTrue(jen.getSpouse() == dave);
53     }
54
55     public void testPropertyWithLiteralValueSubelement() throws Exception JavaDoc {
56         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
57         TestBean verbose = (TestBean) xbf.getBean("verbose");
58         assertTrue(verbose.getName().equals("verbose"));
59     }
60
61     public void testPropertyWithIdRefLocalAttrSubelement() throws Exception JavaDoc {
62         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
63         TestBean verbose = (TestBean) xbf.getBean("verbose2");
64         assertTrue(verbose.getName().equals("verbose"));
65     }
66
67     public void testPropertyWithIdRefBeanAttrSubelement() throws Exception JavaDoc {
68         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
69         TestBean verbose = (TestBean) xbf.getBean("verbose3");
70         assertTrue(verbose.getName().equals("verbose"));
71     }
72
73     public void testRefSubelementsBuildCollection() throws Exception JavaDoc {
74         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
75         TestBean jen = (TestBean) xbf.getBean("jenny");
76         TestBean dave = (TestBean) xbf.getBean("david");
77         TestBean rod = (TestBean) xbf.getBean("rod");
78
79         // Must be a list to support ordering
80
// Our bean doesn't modify the collection:
81
// of course it could be a different copy in a real object.
82
List JavaDoc friends = (List JavaDoc) rod.getFriends();
83         assertTrue(friends.size() == 2);
84
85         assertTrue("First friend must be jen, not " + friends.get(0), friends.get(0) == jen);
86         assertTrue(friends.get(1) == dave);
87         // Should be ordered
88
}
89
90     public void testRefSubelementsBuildCollectionWithPrototypes() throws Exception JavaDoc {
91         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
92
93         TestBean jen = (TestBean) xbf.getBean("pJenny");
94         TestBean dave = (TestBean) xbf.getBean("pDavid");
95         TestBean rod = (TestBean) xbf.getBean("pRod");
96         List JavaDoc friends = (List JavaDoc) rod.getFriends();
97         assertTrue(friends.size() == 2);
98         assertTrue("First friend must be jen, not " + friends.get(0),
99                 friends.get(0).toString().equals(jen.toString()));
100         assertTrue("Jen not same instance", friends.get(0) != jen);
101         assertTrue(friends.get(1).toString().equals(dave.toString()));
102         assertTrue("Dave not same instance", friends.get(1) != dave);
103
104         TestBean rod2 = (TestBean) xbf.getBean("pRod");
105         List JavaDoc friends2 = (List JavaDoc) rod2.getFriends();
106         assertTrue(friends2.size() == 2);
107         assertTrue("First friend must be jen, not " + friends2.get(0),
108                 friends2.get(0).toString().equals(jen.toString()));
109         assertTrue("Jen not same instance", friends2.get(0) != friends.get(0));
110         assertTrue(friends2.get(1).toString().equals(dave.toString()));
111         assertTrue("Dave not same instance", friends2.get(1) != friends.get(1));
112     }
113
114     public void testRefSubelementsBuildCollectionFromSingleElement() throws Exception JavaDoc {
115         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
116         TestBean loner = (TestBean) xbf.getBean("loner");
117         TestBean dave = (TestBean) xbf.getBean("david");
118         assertTrue(loner.getFriends().size() == 1);
119         assertTrue(loner.getFriends().contains(dave));
120     }
121
122     public void testBuildCollectionFromMixtureOfReferencesAndValues() throws Exception JavaDoc {
123         // Ensure that a test runner like Eclipse, that keeps the same JVM up,
124
// will get fresh static values.
125
MixedCollectionBean.resetStaticState();
126         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
127         MixedCollectionBean jumble = (MixedCollectionBean) xbf.getBean("jumble");
128         assertEquals(1, MixedCollectionBean.nrOfInstances);
129         assertTrue("Expected 3 elements, not " + jumble.getJumble().size(),
130                 jumble.getJumble().size() == 4);
131         List JavaDoc l = (List JavaDoc) jumble.getJumble();
132         assertTrue(l.get(0).equals(xbf.getBean("david")));
133         assertTrue(l.get(1).equals("literal"));
134         assertTrue(l.get(2).equals(xbf.getBean("jenny")));
135         assertTrue(l.get(3).equals("rod"));
136     }
137
138     public void testEmptyMap() throws Exception JavaDoc {
139         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
140         HasMap hasMap = (HasMap) xbf.getBean("emptyMap");
141         assertTrue(hasMap.getMap().size() == 0);
142     }
143
144     public void testMapWithLiteralsOnly() throws Exception JavaDoc {
145         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
146         HasMap hasMap = (HasMap) xbf.getBean("literalMap");
147         assertTrue(hasMap.getMap().size() == 3);
148         assertTrue(hasMap.getMap().get("foo").equals("bar"));
149         assertTrue(hasMap.getMap().get("fi").equals("fum"));
150         assertTrue(hasMap.getMap().get("fa") == null);
151     }
152
153     public void testMapWithLiteralsAndReferences() throws Exception JavaDoc {
154         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
155         HasMap hasMap = (HasMap) xbf.getBean("mixedMap");
156         assertTrue(hasMap.getMap().size() == 3);
157         assertTrue(hasMap.getMap().get("foo").equals(new Integer JavaDoc(10)));
158         TestBean jenny = (TestBean) xbf.getBean("jenny");
159         assertTrue(hasMap.getMap().get("jenny") == jenny);
160         assertTrue(hasMap.getMap().get(new Integer JavaDoc(5)).equals("david"));
161     }
162
163     public void testMapWithLiteralsAndPrototypeReferences() throws Exception JavaDoc {
164         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
165
166         TestBean jenny = (TestBean) xbf.getBean("pJenny");
167         HasMap hasMap = (HasMap) xbf.getBean("pMixedMap");
168         assertTrue(hasMap.getMap().size() == 2);
169         assertTrue(hasMap.getMap().get("foo").equals("bar"));
170         assertTrue(hasMap.getMap().get("jenny").toString().equals(jenny.toString()));
171         assertTrue("Not same instance", hasMap.getMap().get("jenny") != jenny);
172
173         HasMap hasMap2 = (HasMap) xbf.getBean("pMixedMap");
174         assertTrue(hasMap2.getMap().size() == 2);
175         assertTrue(hasMap2.getMap().get("foo").equals("bar"));
176         assertTrue(hasMap2.getMap().get("jenny").toString().equals(jenny.toString()));
177         assertTrue("Not same instance", hasMap2.getMap().get("jenny") != hasMap.getMap().get("jenny"));
178     }
179
180     public void testMapWithLiteralsReferencesAndList() throws Exception JavaDoc {
181         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
182         HasMap hasMap = (HasMap) xbf.getBean("mixedMapWithList");
183         assertTrue(hasMap.getMap().size() == 4);
184         assertTrue(hasMap.getMap().get(null).equals("bar"));
185         TestBean jenny = (TestBean) xbf.getBean("jenny");
186         assertTrue(hasMap.getMap().get("jenny").equals(jenny));
187
188         // Check list
189
List JavaDoc l = (List JavaDoc) hasMap.getMap().get("list");
190         assertNotNull(l);
191         assertTrue(l.size() == 4);
192         assertTrue(l.get(0).equals("zero"));
193         assertTrue(l.get(3) == null);
194
195         // Check nested map in list
196
Map JavaDoc m = (Map JavaDoc) l.get(1);
197         assertNotNull(m);
198         assertTrue(m.size() == 2);
199         assertTrue(m.get("fo").equals("bar"));
200         assertTrue("Map element 'jenny' should be equal to jenny bean, not " + m.get("jen"),
201                 m.get("jen").equals(jenny));
202
203         // Check nested list in list
204
l = (List JavaDoc) l.get(2);
205         assertNotNull(l);
206         assertTrue(l.size() == 2);
207         assertTrue(l.get(0).equals(jenny));
208         assertTrue(l.get(1).equals("ba"));
209
210         // Check nested map
211
m = (Map JavaDoc) hasMap.getMap().get("map");
212         assertNotNull(m);
213         assertTrue(m.size() == 2);
214         assertTrue(m.get("foo").equals("bar"));
215         assertTrue("Map element 'jenny' should be equal to jenny bean, not " + m.get("jenny"),
216                 m.get("jenny").equals(jenny));
217     }
218
219     public void testEmptySet() throws Exception JavaDoc {
220         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
221         HasMap hasMap = (HasMap) xbf.getBean("emptySet");
222         assertTrue(hasMap.getSet().size() == 0);
223     }
224
225     public void testPopulatedSet() throws Exception JavaDoc {
226         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
227         HasMap hasMap = (HasMap) xbf.getBean("set");
228         assertTrue(hasMap.getSet().size() == 3);
229         assertTrue(hasMap.getSet().contains("bar"));
230         TestBean jenny = (TestBean) xbf.getBean("jenny");
231         assertTrue(hasMap.getSet().contains(jenny));
232         assertTrue(hasMap.getSet().contains(null));
233         Iterator JavaDoc it = hasMap.getSet().iterator();
234         assertEquals("bar", it.next());
235         assertEquals(jenny, it.next());
236         assertEquals(null, it.next());
237     }
238
239     public void testEmptyProps() throws Exception JavaDoc {
240         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
241         HasMap hasMap = (HasMap) xbf.getBean("emptyProps");
242         assertTrue(hasMap.getProps().size() == 0);
243     }
244
245     public void testPopulatedProps() throws Exception JavaDoc {
246         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
247         HasMap hasMap = (HasMap) xbf.getBean("props");
248         assertTrue(hasMap.getProps().size() == 2);
249         assertTrue(hasMap.getProps().get("foo").equals("bar"));
250         assertTrue(hasMap.getProps().get("2").equals("TWO"));
251     }
252
253     public void testObjectArray() throws Exception JavaDoc {
254         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
255         HasMap hasMap = (HasMap) xbf.getBean("objectArray");
256         assertTrue(hasMap.getObjectArray().length == 2);
257         assertTrue(hasMap.getObjectArray()[0].equals("one"));
258         assertTrue(hasMap.getObjectArray()[1].equals(xbf.getBean("jenny")));
259     }
260
261     public void testClassArray() throws Exception JavaDoc {
262         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
263         HasMap hasMap = (HasMap) xbf.getBean("classArray");
264         assertTrue(hasMap.getClassArray().length == 2);
265         assertTrue(hasMap.getClassArray()[0].equals(String JavaDoc.class));
266         assertTrue(hasMap.getClassArray()[1].equals(Exception JavaDoc.class));
267     }
268
269     public void testIntegerArray() throws Exception JavaDoc {
270         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
271         HasMap hasMap = (HasMap) xbf.getBean("integerArray");
272         assertTrue(hasMap.getIntegerArray().length == 3);
273         assertTrue(hasMap.getIntegerArray()[0].intValue() == 0);
274         assertTrue(hasMap.getIntegerArray()[1].intValue() == 1);
275         assertTrue(hasMap.getIntegerArray()[2].intValue() == 2);
276     }
277
278     public void testProps() throws Exception JavaDoc {
279         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
280
281         HasMap hasMap = (HasMap) xbf.getBean("props");
282         assertEquals(2, hasMap.getProps().size());
283         assertEquals("bar", hasMap.getProps().getProperty("foo"));
284         assertEquals("TWO", hasMap.getProps().getProperty("2"));
285
286         HasMap hasMap2 = (HasMap) xbf.getBean("propsViaMap");
287         assertEquals(2, hasMap2.getProps().size());
288         assertEquals("bar", hasMap2.getProps().getProperty("foo"));
289         assertEquals("TWO", hasMap2.getProps().getProperty("2"));
290     }
291
292     public void testListFactory() throws Exception JavaDoc {
293         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
294         List JavaDoc list = (List JavaDoc) xbf.getBean("listFactory");
295         assertTrue(list instanceof LinkedList JavaDoc);
296         assertTrue(list.size() == 2);
297         assertEquals("bar", list.get(0));
298         assertEquals("jenny", list.get(1));
299     }
300
301     public void testPrototypeListFactory() throws Exception JavaDoc {
302         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
303         List JavaDoc list = (List JavaDoc) xbf.getBean("pListFactory");
304         assertTrue(list instanceof LinkedList JavaDoc);
305         assertTrue(list.size() == 2);
306         assertEquals("bar", list.get(0));
307         assertEquals("jenny", list.get(1));
308     }
309
310     public void testSetFactory() throws Exception JavaDoc {
311         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
312         Set JavaDoc set = (Set JavaDoc) xbf.getBean("setFactory");
313         assertTrue(set instanceof TreeSet JavaDoc);
314         assertTrue(set.size() == 2);
315         assertTrue(set.contains("bar"));
316         assertTrue(set.contains("jenny"));
317     }
318
319     public void testPrototypeSetFactory() throws Exception JavaDoc {
320         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
321         Set JavaDoc set = (Set JavaDoc) xbf.getBean("pSetFactory");
322         assertTrue(set instanceof TreeSet JavaDoc);
323         assertTrue(set.size() == 2);
324         assertTrue(set.contains("bar"));
325         assertTrue(set.contains("jenny"));
326     }
327
328     public void testMapFactory() throws Exception JavaDoc {
329         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
330         Map JavaDoc map = (Map JavaDoc) xbf.getBean("mapFactory");
331         assertTrue(map instanceof TreeMap JavaDoc);
332         assertTrue(map.size() == 2);
333         assertEquals("bar", map.get("foo"));
334         assertEquals("jenny", map.get("jen"));
335     }
336
337     public void testPrototypeMapFactory() throws Exception JavaDoc {
338         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
339         Map JavaDoc map = (Map JavaDoc) xbf.getBean("pMapFactory");
340         assertTrue(map instanceof TreeMap JavaDoc);
341         assertTrue(map.size() == 2);
342         assertEquals("bar", map.get("foo"));
343         assertEquals("jenny", map.get("jen"));
344     }
345
346     public void testCollectionFactoryDefaults() throws Exception JavaDoc {
347         ListFactoryBean listFactory = new ListFactoryBean();
348         listFactory.setSourceList(new LinkedList JavaDoc());
349         listFactory.afterPropertiesSet();
350         assertTrue(listFactory.getObject() instanceof ArrayList JavaDoc);
351
352         SetFactoryBean setFactory = new SetFactoryBean();
353         setFactory.setSourceSet(new TreeSet JavaDoc());
354         setFactory.afterPropertiesSet();
355         assertTrue(setFactory.getObject() instanceof HashSet JavaDoc);
356
357         MapFactoryBean mapFactory = new MapFactoryBean();
358         mapFactory.setSourceMap(new TreeMap JavaDoc());
359         mapFactory.afterPropertiesSet();
360         assertTrue(mapFactory.getObject() instanceof HashMap JavaDoc);
361     }
362
363 }
364
Popular Tags