KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > io > read > TestMaps


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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
18 package org.apache.commons.betwixt.io.read;
19
20 import java.io.StringReader JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
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 /**
30  * @author <a HREF='http://jakarta.apache.org/'>Jakarta Commons Team</a>
31  * @version $Revision: 1.2 $
32  */

33 public class TestMaps extends AbstractTestCase {
34
35     public TestMaps(String JavaDoc testName) {
36         super(testName);
37     }
38     
39     public void testWriteConcreateMapImplementation() throws Exception JavaDoc {
40         StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc xml = out.getBuffer().toString();
51         String JavaDoc 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 JavaDoc {
70         StringReader JavaDoc in = new StringReader JavaDoc("<?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 JavaDoc map = bean.getSomeThingies();
95         
96         Set JavaDoc 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 JavaDoc {
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 JavaDoc name = "John";
112         addressBook.addAddressBookItem(name, johnsAddresses);
113         StringWriter JavaDoc outputWriter = new StringWriter JavaDoc();
114         outputWriter.write("<?xml version='1.0' ?>\n");
115         BeanWriter beanWriter = new BeanWriter(outputWriter);
116         beanWriter.enablePrettyPrint();
117         beanWriter.write(addressBook);
118     
119         String JavaDoc 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 JavaDoc xmlReader = new StringReader JavaDoc(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