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