1 16 17 package org.springframework.beans.factory.xml; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.TreeMap ; 28 import java.util.TreeSet ; 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 45 public class XmlBeanCollectionTests extends TestCase { 46 47 public void testRefSubelement() throws Exception { 48 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass())); 49 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 { 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 { 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 { 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 { 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 List friends = (List ) 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 } 89 90 public void testRefSubelementsBuildCollectionWithPrototypes() throws Exception { 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 friends = (List ) 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 friends2 = (List ) 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 { 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 { 123 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 l = (List ) 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 { 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 { 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 { 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 (10))); 158 TestBean jenny = (TestBean) xbf.getBean("jenny"); 159 assertTrue(hasMap.getMap().get("jenny") == jenny); 160 assertTrue(hasMap.getMap().get(new Integer (5)).equals("david")); 161 } 162 163 public void testMapWithLiteralsAndPrototypeReferences() throws Exception { 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 { 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 List l = (List ) 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 Map m = (Map ) 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 l = (List ) 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 m = (Map ) 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 { 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 { 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 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 { 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 { 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 { 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 { 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 .class)); 266 assertTrue(hasMap.getClassArray()[1].equals(Exception .class)); 267 } 268 269 public void testIntegerArray() throws Exception { 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 { 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 { 293 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass())); 294 List list = (List ) xbf.getBean("listFactory"); 295 assertTrue(list instanceof LinkedList ); 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 { 302 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass())); 303 List list = (List ) xbf.getBean("pListFactory"); 304 assertTrue(list instanceof LinkedList ); 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 { 311 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass())); 312 Set set = (Set ) xbf.getBean("setFactory"); 313 assertTrue(set instanceof TreeSet ); 314 assertTrue(set.size() == 2); 315 assertTrue(set.contains("bar")); 316 assertTrue(set.contains("jenny")); 317 } 318 319 public void testPrototypeSetFactory() throws Exception { 320 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass())); 321 Set set = (Set ) xbf.getBean("pSetFactory"); 322 assertTrue(set instanceof TreeSet ); 323 assertTrue(set.size() == 2); 324 assertTrue(set.contains("bar")); 325 assertTrue(set.contains("jenny")); 326 } 327 328 public void testMapFactory() throws Exception { 329 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass())); 330 Map map = (Map ) xbf.getBean("mapFactory"); 331 assertTrue(map instanceof TreeMap ); 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 { 338 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass())); 339 Map map = (Map ) xbf.getBean("pMapFactory"); 340 assertTrue(map instanceof TreeMap ); 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 { 347 ListFactoryBean listFactory = new ListFactoryBean(); 348 listFactory.setSourceList(new LinkedList ()); 349 listFactory.afterPropertiesSet(); 350 assertTrue(listFactory.getObject() instanceof ArrayList ); 351 352 SetFactoryBean setFactory = new SetFactoryBean(); 353 setFactory.setSourceSet(new TreeSet ()); 354 setFactory.afterPropertiesSet(); 355 assertTrue(setFactory.getObject() instanceof HashSet ); 356 357 MapFactoryBean mapFactory = new MapFactoryBean(); 358 mapFactory.setSourceMap(new TreeMap ()); 359 mapFactory.afterPropertiesSet(); 360 assertTrue(mapFactory.getObject() instanceof HashMap ); 361 } 362 363 } 364 | Popular Tags |