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  package org.apache.commons.betwixt;
17  
18  import java.io.StringWriter;
19  import java.util.Map;
20  
21  import org.apache.commons.betwixt.io.BeanWriter;
22  
23  import java.util.Hashtable;
24  
25  /***
26   * @author <a href='http://jakarta.apache.org/commons'>Jakarta 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.enablePrettyPrint();
62          beanWriter.setWriteEmptyElements(false);
63          beanWriter.getBindingConfiguration().setMapIDs(false);
64          beanWriter.setXMLIntrospector(new XMLIntrospector());
65          beanWriter.write(hash);
66  
67          xmlAssertIsomorphic(parseString(expected), parseString(out));
68      }
69  
70      public void testHashMapWriteNotEmpty() throws Exception {
71          
72          Map hash = new Hashtable();
73          hash.put("one", "un");
74          hash.put("two", "deux");
75          hash.put("three", "trois");
76  
77          String expected = "<?xml version='1.0'?>" +
78          		"<Hashtable>" +
79          		"	<empty>false</empty>" +
80          		"    <entry>" +
81          		"      <key>two</key>" +
82          		"      <value>deux</value>" +
83          		"    </entry>" +
84          		"   <entry>" +
85          		"      <key>one</key>" +
86          		"      <value>un</value>" +
87          		"    </entry>" +
88          		"    <entry>" +
89          		"      <key>three</key>" +
90          		"      <value>trois</value>" +
91          		"    </entry>" +
92          		"  </Hashtable>";
93          
94          StringWriter out = new StringWriter();
95          
96          BeanWriter beanWriter = new BeanWriter(out);
97          beanWriter.enablePrettyPrint();
98          beanWriter.setWriteEmptyElements(true);
99          beanWriter.getBindingConfiguration().setMapIDs(false);
100         beanWriter.setXMLIntrospector(new XMLIntrospector());
101         beanWriter.write(hash);
102 
103         xmlAssertIsomorphic(parseString(expected), parseString(out));
104     }
105     
106 }