01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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://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'?>" + "<Hashtable>"
42: + " <empty>false</empty>" + " <entry>"
43: + " <key>two</key>" + " <value>deux</value>"
44: + " </entry>" + " <entry>"
45: + " <key>one</key>" + " <value>un</value>"
46: + " </entry>" + " <entry>"
47: + " <key>three</key>"
48: + " <value>trois</value>" + " </entry>"
49: + " </Hashtable>";
50:
51: StringWriter out = new StringWriter();
52:
53: BeanWriter beanWriter = new BeanWriter(out);
54: beanWriter.setEndOfLine("\n");
55: beanWriter.enablePrettyPrint();
56: beanWriter.setWriteEmptyElements(false);
57: beanWriter.getBindingConfiguration().setMapIDs(false);
58: beanWriter.setXMLIntrospector(new XMLIntrospector());
59: beanWriter.write(hash);
60:
61: xmlAssertIsomorphic(parseString(expected), parseString(out));
62: }
63:
64: public void testHashMapWriteNotEmpty() throws Exception {
65:
66: Map hash = new Hashtable();
67: hash.put("one", "un");
68: hash.put("two", "deux");
69: hash.put("three", "trois");
70:
71: String expected = "<?xml version='1.0'?>" + "<Hashtable>"
72: + " <empty>false</empty>" + " <entry>"
73: + " <key>two</key>" + " <value>deux</value>"
74: + " </entry>" + " <entry>"
75: + " <key>one</key>" + " <value>un</value>"
76: + " </entry>" + " <entry>"
77: + " <key>three</key>"
78: + " <value>trois</value>" + " </entry>"
79: + " </Hashtable>";
80:
81: StringWriter out = new StringWriter();
82:
83: BeanWriter beanWriter = new BeanWriter(out);
84: beanWriter.setEndOfLine("\n");
85: beanWriter.enablePrettyPrint();
86: beanWriter.setWriteEmptyElements(true);
87: beanWriter.getBindingConfiguration().setMapIDs(false);
88: beanWriter.setXMLIntrospector(new XMLIntrospector());
89: beanWriter.write(hash);
90:
91: xmlAssertIsomorphic(parseString(expected), parseString(out));
92: }
93:
94: }
|