1 16 17 18 package org.apache.commons.betwixt.io.read; 19 20 import java.io.StringReader ; 21 import java.io.StringWriter ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.apache.commons.betwixt.AbstractTestCase; 26 import org.apache.commons.betwixt.io.BeanReader; 27 import org.apache.commons.betwixt.io.BeanWriter; 28 29 33 public class TestMaps extends AbstractTestCase { 34 35 public TestMaps(String testName) { 36 super(testName); 37 } 38 39 public void testWriteConcreateMapImplementation() throws Exception { 40 StringWriter out = new StringWriter (); 41 out.write("<?xml version='1.0'?>"); 42 BeanWriter writer = new BeanWriter(out); 43 writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false); 44 writer.getBindingConfiguration().setMapIDs(false); 45 BeanWithConcreteMap bean = new BeanWithConcreteMap(); 46 bean.addSomeThingy("Aethelred", "The Unready"); 47 bean.addSomeThingy("Swein", "Forkbeard"); 48 bean.addSomeThingy("Thorkell", "The Tall"); 49 writer.write(bean); 50 String xml = out.getBuffer().toString(); 51 String expected = "<?xml version='1.0'?><BeanWithConcreteMap>" + 52 "<entry>" + 53 "<key>Swein</key>" + 54 "<value>Forkbeard</value>" + 55 "</entry>" + 56 "<entry>" + 57 "<key>Thorkell</key>" + 58 "<value>The Tall</value>" + 59 "</entry>" + 60 "<entry>" + 61 "<key>Aethelred</key>" + 62 "<value>The Unready</value>" + 63 "</entry>" + 64 "</BeanWithConcreteMap>"; 65 xmlAssertIsomorphicContent(parseString(expected), parseString(xml)); 66 } 67 68 69 public void testReadConcreateMapImplementation() throws Exception { 70 StringReader in = new StringReader ("<?xml version='1.0'?><BeanWithConcreteMap>" + 71 "<entry>" + 72 "<key>Swein</key>" + 73 "<value>Forkbeard</value>" + 74 "</entry>" + 75 "<entry>" + 76 "<key>Thorkell</key>" + 77 "<value>The Tall</value>" + 78 "</entry>" + 79 "<entry>" + 80 "<key>Aethelred</key>" + 81 "<value>The Unready</value>" + 82 "</entry>" + 83 "</BeanWithConcreteMap>"); 84 85 BeanReader reader = new BeanReader(); 86 reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false); 87 reader.getBindingConfiguration().setMapIDs(false); 88 reader.registerBeanClass(BeanWithConcreteMap.class); 89 90 91 BeanWithConcreteMap bean = (BeanWithConcreteMap) reader.parse(in); 92 assertNotNull("Parse failed", bean); 93 94 Map map = bean.getSomeThingies(); 95 96 Set keyset = map.keySet(); 97 assertEquals("Three entries", 3, keyset.size()); 98 assertEquals("Aethelred The Unready", "The Unready", map.get("Aethelred")); 99 assertEquals("Swein Forkbeardy", "Forkbeard", map.get("Swein")); 100 assertEquals("Thorkell The Tall", "The Tall", map.get("Thorkell")); 101 102 } 103 104 public void testMapWithArray() throws Exception { 105 106 AddressBook addressBook = new AddressBook(); 107 AddressBean[] johnsAddresses = new AddressBean[2]; 108 johnsAddresses[0] = new AddressBean("12 here", "Chicago", "USA", "1234"); 109 johnsAddresses[1] = 110 new AddressBean("333 there", "Los Angeles", "USA", "99999"); 111 String name = "John"; 112 addressBook.addAddressBookItem(name, johnsAddresses); 113 StringWriter outputWriter = new StringWriter (); 114 outputWriter.write("<?xml version='1.0' ?>\n"); 115 BeanWriter beanWriter = new BeanWriter(outputWriter); 116 beanWriter.enablePrettyPrint(); 117 beanWriter.write(addressBook); 118 119 String xml = 120 "<?xml version='1.0' ?>\n" 121 + " <AddressBook id=\"1\">\n" 122 + " <addressBookItems>\n" 123 + " <entry id=\"2\">\n" 124 + " <key>John</key>\n" 125 + " <value id=\"3\">\n" 126 + " <AddressBean id=\"4\">\n" 127 + " <city>Chicago</city>\n" 128 + " <code>1234</code>\n" 129 + " <country>USA</country>\n" 130 + " <street>12 here</street>\n" 131 + " </AddressBean>\n" 132 + " <AddressBean id=\"5\">\n" 133 + " <city>Los Angeles</city>\n" 134 + " <code>99999</code>\n" 135 + " <country>USA</country>\n" 136 + " <street>333 there</street>\n" 137 + " </AddressBean>\n" 138 + " </value>\n" 139 + " </entry>\n" 140 + " </addressBookItems>\n" 141 + " </AddressBook>\n"; 142 143 assertEquals(xml, outputWriter.toString()); 144 BeanReader reader = new BeanReader(); 145 reader.registerBeanClass(AddressBook.class); 146 StringReader xmlReader = new StringReader (outputWriter.toString()); 147 AddressBook result = (AddressBook) reader.parse(xmlReader); 148 assertNotNull("Expected to get an AddressBook!", result); 149 assertNotNull( 150 "Expected AddressBook to have some address entryitems!", 151 result.getAddressBookItems()); 152 AddressBean[] resultAddresses = 153 (AddressBean[]) result.getAddressBookItems().get(name); 154 assertNotNull( 155 "Expected to have some addresses for " + name, 156 resultAddresses); 157 assertEquals( 158 "Got wrong city in first address for " + name, 159 johnsAddresses[0].getCity(), 160 resultAddresses[0].getCity()); 161 assertEquals( 162 "Got wrong city in second address for " + name, 163 johnsAddresses[1].getCity(), 164 resultAddresses[1].getCity()); 165 } 166 } 167 | Popular Tags |