1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  
19  package org.apache.commons.betwixt.io.read;
20  
21  import java.io.StringReader;
22  import java.io.StringWriter;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.commons.betwixt.AbstractTestCase;
28  import org.apache.commons.betwixt.io.BeanReader;
29  import org.apache.commons.betwixt.io.BeanWriter;
30  
31  /**
32   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
33   * @version $Revision: 561314 $
34   */
35  public class TestMaps extends AbstractTestCase {
36  
37      public TestMaps(String testName) {
38          super(testName);
39      }
40      
41      public void testWriteConcreateMapImplementation() throws Exception {
42          StringWriter out = new StringWriter();
43          out.write("<?xml version='1.0'?>");
44          BeanWriter writer = new BeanWriter(out);
45          writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
46          writer.getBindingConfiguration().setMapIDs(false);
47          BeanWithConcreteMap bean = new BeanWithConcreteMap();
48          bean.addSomeThingy("Aethelred", "The Unready");
49          bean.addSomeThingy("Swein", "Forkbeard");
50          bean.addSomeThingy("Thorkell", "The Tall");
51          writer.write(bean);
52          String xml = out.getBuffer().toString();
53          
54          StringBuffer buffer = new StringBuffer("<?xml version='1.0'?><BeanWithConcreteMap>");
55          for (Iterator it=bean.getSomeThingies().keySet().iterator(); it.hasNext();)
56          {
57              String key = (String) it.next();
58              if ("Aethelred".equals(key))
59              {
60                  buffer.append(
61                          "<entry>" +
62                          "<key>Aethelred</key>" +
63                          "<value>The Unready</value>" +
64                          "</entry>");
65                  
66              }
67              else if ("Swein".equals(key))
68              {
69                  buffer.append(
70                          "<entry>" +
71                          "<key>Swein</key>" +
72                          "<value>Forkbeard</value>" +
73                          "</entry>");
74                  
75              }
76              else if ("Thorkell".equals(key))
77              {
78                  buffer.append(
79                          "<entry>" +
80                          "<key>Thorkell</key>" +
81                          "<value>The Tall</value>" +
82                          "</entry>");
83                  
84              }
85          }
86          buffer.append("</BeanWithConcreteMap>");
87          
88          String expected = buffer.toString();
89          
90          xmlAssertIsomorphicContent(parseString(expected), parseString(xml), true);
91      }
92  
93      
94      public void testReadConcreateMapImplementation() throws Exception {
95          StringReader in =  new StringReader("<?xml version='1.0'?><BeanWithConcreteMap>" +
96              "<entry>" +
97              "<key>Swein</key>" +
98              "<value>Forkbeard</value>" +
99              "</entry>" +
100             "<entry>" +
101             "<key>Thorkell</key>" +
102             "<value>The Tall</value>" +
103             "</entry>" +
104             "<entry>" +
105             "<key>Aethelred</key>" +
106             "<value>The Unready</value>" +
107             "</entry>" +
108             "</BeanWithConcreteMap>");
109 
110         BeanReader reader = new BeanReader();
111         reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
112         reader.getBindingConfiguration().setMapIDs(false);
113         reader.registerBeanClass(BeanWithConcreteMap.class);
114         
115         
116         BeanWithConcreteMap bean = (BeanWithConcreteMap) reader.parse(in);
117         assertNotNull("Parse failed", bean);
118         
119         Map map = bean.getSomeThingies();
120         
121         Set keyset = map.keySet();
122         assertEquals("Three entries", 3, keyset.size());
123         assertEquals("Aethelred The Unready", "The Unready", map.get("Aethelred"));
124         assertEquals("Swein Forkbeardy", "Forkbeard", map.get("Swein"));
125         assertEquals("Thorkell The Tall", "The Tall", map.get("Thorkell"));
126  
127     }
128 
129     public void testMapWithArray() throws Exception {
130 
131         AddressBook addressBook = new AddressBook();
132         AddressBean[] johnsAddresses = new AddressBean[2];
133         johnsAddresses[0] = new AddressBean("12 here", "Chicago", "USA", "1234");
134         johnsAddresses[1] =
135             new AddressBean("333 there", "Los Angeles", "USA", "99999");
136         String name = "John";
137         addressBook.addAddressBookItem(name, johnsAddresses);
138         StringWriter outputWriter = new StringWriter();
139         outputWriter.write("<?xml version='1.0' ?>\n");
140         BeanWriter beanWriter = new BeanWriter(outputWriter);
141         beanWriter.setEndOfLine("\n");
142         beanWriter.enablePrettyPrint();
143         beanWriter.write(addressBook);
144     
145         String xml =
146             "<?xml version='1.0' ?>\n"
147                 + "  <AddressBook id=\"1\">\n"
148                 + "    <addressBookItems>\n"
149                 + "      <entry id=\"2\">\n"
150                 + "        <key>John</key>\n"
151                 + "        <value id=\"3\">\n"
152                 + "          <AddressBean id=\"4\">\n"
153                 + "            <city>Chicago</city>\n"
154                 + "            <code>1234</code>\n"
155                 + "            <country>USA</country>\n"
156                 + "            <street>12 here</street>\n"
157                 + "          </AddressBean>\n"
158                 + "          <AddressBean id=\"5\">\n"
159                 + "            <city>Los Angeles</city>\n"
160                 + "            <code>99999</code>\n"
161                 + "            <country>USA</country>\n"
162                 + "            <street>333 there</street>\n"
163                 + "          </AddressBean>\n"
164                 + "        </value>\n"
165                 + "      </entry>\n"
166                 + "    </addressBookItems>\n"
167                 + "  </AddressBook>\n";
168     
169         xmlAssertIsomorphicContent(parseString(xml), parseString(outputWriter.toString()), true);
170         BeanReader reader = new BeanReader();
171         reader.registerBeanClass(AddressBook.class);
172         StringReader xmlReader = new StringReader(outputWriter.toString());
173         AddressBook result = (AddressBook) reader.parse(xmlReader);
174         assertNotNull("Expected to get an AddressBook!", result);
175         assertNotNull(
176             "Expected AddressBook to have some address entryitems!",
177             result.getAddressBookItems());
178         AddressBean[] resultAddresses =
179             (AddressBean[]) result.getAddressBookItems().get(name);
180         assertNotNull(
181             "Expected to have some addresses for " + name,
182             resultAddresses);
183         assertEquals(
184             "Got wrong city in first address for " + name,
185             johnsAddresses[0].getCity(),
186             resultAddresses[0].getCity());
187         assertEquals(
188             "Got wrong city in second address for " + name,
189             johnsAddresses[1].getCity(),
190             resultAddresses[1].getCity());
191     }
192 }