1   /*
2    * Copyright 2005 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.io;
17  
18  import java.io.StringWriter;
19  
20  import org.apache.commons.betwixt.AbstractTestCase;
21  import org.apache.commons.betwixt.LoopBean;
22  
23  /***
24   */
25  public class TestIgnoreEmptyElements extends AbstractTestCase {
26  
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 superhero = new SuperheroBean(sidekick);
39          writer.write(superhero);
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      
49      /*** Test nested case for writing empty elements */
50      public void testNestedWriteEmptyElements() throws Exception{
51          
52          // write same bean both times
53          LoopBean root = new LoopBean("base");
54          LoopBean middle = new LoopBean(null);
55          root.setFriend(middle);
56          middle.setFriend(new LoopBean(null));
57  
58          // test output when writing empty elements
59          StringWriter out = new StringWriter();
60          out.write("<?xml version='1.0'?>");
61          BeanWriter writer = new BeanWriter(out);
62          writer.setWriteEmptyElements(true);
63          writer.getBindingConfiguration().setMapIDs(false);
64          writer.write(root);
65          String xml = "<?xml version='1.0'?><LoopBean><name>base</name><friend><name/><friend><name/></friend>"
66                      + "</friend></LoopBean>";
67          xmlAssertIsomorphicContent(parseString(out.getBuffer().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().toString()),parseString(xml), true);
78          
79      }
80  }