001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.io.read;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.commons.betwixt.AbstractTestCase;
027: import org.apache.commons.betwixt.io.BeanReader;
028: import org.apache.commons.betwixt.io.BeanWriter;
029:
030: /**
031: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
032: * @version $Revision: 438373 $
033: */
034: public class TestMaps extends AbstractTestCase {
035:
036: public TestMaps(String testName) {
037: super (testName);
038: }
039:
040: public void testWriteConcreateMapImplementation() throws Exception {
041: StringWriter out = new StringWriter();
042: out.write("<?xml version='1.0'?>");
043: BeanWriter writer = new BeanWriter(out);
044: writer.getXMLIntrospector().getConfiguration()
045: .setWrapCollectionsInElement(false);
046: writer.getBindingConfiguration().setMapIDs(false);
047: BeanWithConcreteMap bean = new BeanWithConcreteMap();
048: bean.addSomeThingy("Aethelred", "The Unready");
049: bean.addSomeThingy("Swein", "Forkbeard");
050: bean.addSomeThingy("Thorkell", "The Tall");
051: writer.write(bean);
052: String xml = out.getBuffer().toString();
053:
054: StringBuffer buffer = new StringBuffer(
055: "<?xml version='1.0'?><BeanWithConcreteMap>");
056: for (Iterator it = bean.getSomeThingies().keySet().iterator(); it
057: .hasNext();) {
058: String key = (String) it.next();
059: if ("Aethelred".equals(key)) {
060: buffer.append("<entry>" + "<key>Aethelred</key>"
061: + "<value>The Unready</value>" + "</entry>");
062:
063: } else if ("Swein".equals(key)) {
064: buffer.append("<entry>" + "<key>Swein</key>"
065: + "<value>Forkbeard</value>" + "</entry>");
066:
067: } else if ("Thorkell".equals(key)) {
068: buffer.append("<entry>" + "<key>Thorkell</key>"
069: + "<value>The Tall</value>" + "</entry>");
070:
071: }
072: }
073: buffer.append("</BeanWithConcreteMap>");
074:
075: String expected = buffer.toString();
076:
077: xmlAssertIsomorphicContent(parseString(expected),
078: parseString(xml), true);
079: }
080:
081: public void testReadConcreateMapImplementation() throws Exception {
082: StringReader in = new StringReader(
083: "<?xml version='1.0'?><BeanWithConcreteMap>"
084: + "<entry>" + "<key>Swein</key>"
085: + "<value>Forkbeard</value>" + "</entry>"
086: + "<entry>" + "<key>Thorkell</key>"
087: + "<value>The Tall</value>" + "</entry>"
088: + "<entry>" + "<key>Aethelred</key>"
089: + "<value>The Unready</value>" + "</entry>"
090: + "</BeanWithConcreteMap>");
091:
092: BeanReader reader = new BeanReader();
093: reader.getXMLIntrospector().getConfiguration()
094: .setWrapCollectionsInElement(false);
095: reader.getBindingConfiguration().setMapIDs(false);
096: reader.registerBeanClass(BeanWithConcreteMap.class);
097:
098: BeanWithConcreteMap bean = (BeanWithConcreteMap) reader
099: .parse(in);
100: assertNotNull("Parse failed", bean);
101:
102: Map map = bean.getSomeThingies();
103:
104: Set keyset = map.keySet();
105: assertEquals("Three entries", 3, keyset.size());
106: assertEquals("Aethelred The Unready", "The Unready", map
107: .get("Aethelred"));
108: assertEquals("Swein Forkbeardy", "Forkbeard", map.get("Swein"));
109: assertEquals("Thorkell The Tall", "The Tall", map
110: .get("Thorkell"));
111:
112: }
113:
114: public void testMapWithArray() throws Exception {
115:
116: AddressBook addressBook = new AddressBook();
117: AddressBean[] johnsAddresses = new AddressBean[2];
118: johnsAddresses[0] = new AddressBean("12 here", "Chicago",
119: "USA", "1234");
120: johnsAddresses[1] = new AddressBean("333 there", "Los Angeles",
121: "USA", "99999");
122: String name = "John";
123: addressBook.addAddressBookItem(name, johnsAddresses);
124: StringWriter outputWriter = new StringWriter();
125: outputWriter.write("<?xml version='1.0' ?>\n");
126: BeanWriter beanWriter = new BeanWriter(outputWriter);
127: beanWriter.setEndOfLine("\n");
128: beanWriter.enablePrettyPrint();
129: beanWriter.write(addressBook);
130:
131: String xml = "<?xml version='1.0' ?>\n"
132: + " <AddressBook id=\"1\">\n"
133: + " <addressBookItems>\n"
134: + " <entry id=\"2\">\n"
135: + " <key>John</key>\n"
136: + " <value id=\"3\">\n"
137: + " <AddressBean id=\"4\">\n"
138: + " <city>Chicago</city>\n"
139: + " <code>1234</code>\n"
140: + " <country>USA</country>\n"
141: + " <street>12 here</street>\n"
142: + " </AddressBean>\n"
143: + " <AddressBean id=\"5\">\n"
144: + " <city>Los Angeles</city>\n"
145: + " <code>99999</code>\n"
146: + " <country>USA</country>\n"
147: + " <street>333 there</street>\n"
148: + " </AddressBean>\n" + " </value>\n"
149: + " </entry>\n" + " </addressBookItems>\n"
150: + " </AddressBook>\n";
151:
152: xmlAssertIsomorphicContent(parseString(xml),
153: parseString(outputWriter.toString()), true);
154: BeanReader reader = new BeanReader();
155: reader.registerBeanClass(AddressBook.class);
156: StringReader xmlReader = new StringReader(outputWriter
157: .toString());
158: AddressBook result = (AddressBook) reader.parse(xmlReader);
159: assertNotNull("Expected to get an AddressBook!", result);
160: assertNotNull(
161: "Expected AddressBook to have some address entryitems!",
162: result.getAddressBookItems());
163: AddressBean[] resultAddresses = (AddressBean[]) result
164: .getAddressBookItems().get(name);
165: assertNotNull("Expected to have some addresses for " + name,
166: resultAddresses);
167: assertEquals("Got wrong city in first address for " + name,
168: johnsAddresses[0].getCity(), resultAddresses[0]
169: .getCity());
170: assertEquals("Got wrong city in second address for " + name,
171: johnsAddresses[1].getCity(), resultAddresses[1]
172: .getCity());
173: }
174: }
|