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  package org.apache.commons.betwixt;
18  
19  import java.io.StringWriter;
20  import java.util.Hashtable;
21  import java.util.Map;
22  
23  import org.apache.commons.betwixt.io.BeanWriter;
24  
25  /**
26   * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
27   */
28  public class TestMaps extends AbstractTestCase {
29  
30      public TestMaps(String testName) {
31          super(testName);
32      }
33  
34      public void testHashMapWriteEmpty() throws Exception {
35          
36          Map hash = new Hashtable();
37          hash.put("one", "un");
38          hash.put("two", "deux");
39          hash.put("three", "trois");
40  
41          String expected = "<?xml version='1.0'?>" +
42          		"<Hashtable>" +
43          		"	<empty>false</empty>" +
44          		"    <entry>" +
45          		"      <key>two</key>" +
46          		"      <value>deux</value>" +
47          		"    </entry>" +
48          		"   <entry>" +
49          		"      <key>one</key>" +
50          		"      <value>un</value>" +
51          		"    </entry>" +
52          		"    <entry>" +
53          		"      <key>three</key>" +
54          		"      <value>trois</value>" +
55          		"    </entry>" +
56          		"  </Hashtable>";
57          
58          StringWriter out = new StringWriter();
59          
60          BeanWriter beanWriter = new BeanWriter(out);
61          beanWriter.setEndOfLine("\n");
62          beanWriter.enablePrettyPrint();
63          beanWriter.setWriteEmptyElements(false);
64          beanWriter.getBindingConfiguration().setMapIDs(false);
65          beanWriter.setXMLIntrospector(new XMLIntrospector());
66          beanWriter.write(hash);
67  
68          xmlAssertIsomorphic(parseString(expected), parseString(out));
69      }
70  
71      public void testHashMapWriteNotEmpty() throws Exception {
72          
73          Map hash = new Hashtable();
74          hash.put("one", "un");
75          hash.put("two", "deux");
76          hash.put("three", "trois");
77  
78          String expected = "<?xml version='1.0'?>" +
79          		"<Hashtable>" +
80          		"	<empty>false</empty>" +
81          		"    <entry>" +
82          		"      <key>two</key>" +
83          		"      <value>deux</value>" +
84          		"    </entry>" +
85          		"   <entry>" +
86          		"      <key>one</key>" +
87          		"      <value>un</value>" +
88          		"    </entry>" +
89          		"    <entry>" +
90          		"      <key>three</key>" +
91          		"      <value>trois</value>" +
92          		"    </entry>" +
93          		"  </Hashtable>";
94          
95          StringWriter out = new StringWriter();
96          
97          BeanWriter beanWriter = new BeanWriter(out);
98          beanWriter.setEndOfLine("\n");
99          beanWriter.enablePrettyPrint();
100         beanWriter.setWriteEmptyElements(true);
101         beanWriter.getBindingConfiguration().setMapIDs(false);
102         beanWriter.setXMLIntrospector(new XMLIntrospector());
103         beanWriter.write(hash);
104 
105         xmlAssertIsomorphic(parseString(expected), parseString(out));
106     }
107     
108 }