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