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.io;
18:
19: import java.io.StringWriter;
20:
21: import org.apache.commons.betwixt.AbstractTestCase;
22: import org.apache.commons.betwixt.LoopBean;
23:
24: /**
25: */
26: public class TestIgnoreEmptyElements extends AbstractTestCase {
27:
28: public TestIgnoreEmptyElements(String testName) {
29: super (testName);
30: }
31:
32: public void testWritePersonBean() throws Exception {
33: StringWriter out = new StringWriter();
34: out.write("<?xml version='1.0'?>");
35: BeanWriter writer = new BeanWriter(out);
36: writer.setWriteEmptyElements(false);
37: SidekickBean sidekick = new SidekickBean("Robin");
38: SuperheroBean super hero = new SuperheroBean(sidekick);
39: writer.write(super hero);
40: String expected = "<?xml version='1.0'?>"
41: + "<SuperheroBean id='1'>"
42: + " <sidekick id='2'><nickname>Robin</nickname></sidekick>"
43: + "</SuperheroBean>";
44: String xml = out.toString();
45: xmlAssertIsomorphic(parseString(expected), parseString(xml));
46: }
47:
48: /** Test nested case for writing empty elements */
49: public void testNestedWriteEmptyElements() throws Exception {
50:
51: // write same bean both times
52: LoopBean root = new LoopBean("base");
53: LoopBean middle = new LoopBean(null);
54: root.setFriend(middle);
55: middle.setFriend(new LoopBean(null));
56:
57: // test output when writing empty elements
58: StringWriter out = new StringWriter();
59: out.write("<?xml version='1.0'?>");
60: BeanWriter writer = new BeanWriter(out);
61: writer.setWriteEmptyElements(true);
62: writer.getBindingConfiguration().setMapIDs(false);
63: writer.write(root);
64: String xml = "<?xml version='1.0'?><LoopBean><name>base</name><friend><name/><friend><name/></friend>"
65: + "</friend></LoopBean>";
66: xmlAssertIsomorphicContent(parseString(out.getBuffer()
67: .toString()), parseString(xml), true);
68:
69: // test output when not writing empty elements
70: out = new StringWriter();
71: out.write("<?xml version='1.0'?>");
72: writer = new BeanWriter(out);
73: writer.setWriteEmptyElements(false);
74: writer.getBindingConfiguration().setMapIDs(false);
75: writer.write(root);
76: xml = "<?xml version='1.0'?><LoopBean><name>base</name></LoopBean>";
77: xmlAssertIsomorphicContent(parseString(out.getBuffer()
78: .toString()), parseString(xml), true);
79:
80: }
81: }
|