1   /*
2    * Copyright 2001-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   
17  package org.apache.commons.betwixt.dotbetwixt;
18  
19  import java.io.StringWriter;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  
24  import org.apache.commons.betwixt.io.BeanWriter;
25  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
26  import org.apache.commons.betwixt.xmlunit.XmlTestCase;
27  
28  
29  /*** 
30    * Provides xml test utilities. 
31    * Hopefully, these might be moved into [xmlunit] sometime.
32    *
33    * @author Robert Burrell Donkin
34    */
35  public class TestBeanToXml extends XmlTestCase {
36  
37  //--------------------------------- Test Suite
38      
39      public static Test suite() {
40          return new TestSuite(TestBeanToXml.class);
41      }
42      
43  //--------------------------------- Constructor
44          
45      public TestBeanToXml(String testName) {
46          super(testName);
47      }
48  
49  //---------------------------------- Tests
50      
51      public void testOne() throws Exception {
52          // THIS TEST FAILS IN MAVEN
53          xmlAssertIsomorphicContent(	
54              parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"), 
55              parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"));
56      }
57      
58      public void testSimpleBean() throws Exception {
59          StringWriter out = new StringWriter();
60          out.write("<?xml version='1.0' encoding='UTF-8'?>");
61  //        SimpleLog log = new SimpleLog("[testSimpleBean:XMLIntrospector]");
62  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
63          BeanWriter writer = new BeanWriter(out);
64          writer.setWriteEmptyElements( true );
65  //        writer.getXMLIntrospector().setLog(log);
66          
67  //        log = new SimpleLog("[testSimpleBean:XMLIntrospectorHelper]");
68  //        XMLIntrospectorHelper.setLog(log);
69      
70          writer.setWriteIDs(false);
71  	SimpleTestBean bean = new SimpleTestBean("alpha-value","beta-value","gamma-value");
72          writer.write(bean);
73          out.flush();
74          String xml = out.toString();
75          
76          xmlAssertIsomorphicContent(
77                      parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/simpletestone.xml"),
78                      parseString(xml));
79  
80      }
81      
82      public void testWriteRecursiveBean() throws Exception {
83          /*
84          StringWriter out = new StringWriter();
85          out.write("<?xml version='1.0' encoding='UTF-8'?>");
86          BeanWriter writer = new BeanWriter(out);
87          RecursiveBean bean 
88              = new RecursiveBean(
89                  "alpha", 
90                  new RecursiveBean(
91                      "beta", 
92                      new RecursiveBean("gamma")));
93          writer.setWriteIDs(false);
94          writer.write(bean);
95          out.flush();
96          String xml = out.toString();
97          
98          if (debug) {
99              System.out.println(xml);
100         }
101         
102         
103         xmlAssertIsomorphicContent(
104                     parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"),
105                     parseString(xml));
106         */
107     }
108     
109     /*** 
110      * This tests that only well formed names for elements and attributes are allowed by .betwixt files
111      */
112     public void testBadDotBetwixtNames() throws Exception {
113         // this will work by testing that the output is well formed
114         
115         StringWriter out = new StringWriter();
116         out.write("<?xml version='1.0' encoding='UTF-8'?>");
117         BeanWriter writer = new BeanWriter(out);
118         writer.setWriteEmptyElements( true );
119         writer.write(new BadDotBetwixtNamesBean("one", "two"));
120         
121 //        System.out.println(out.toString());
122         
123         // this should fail if the output is not well formed
124         parseString(out.toString());
125     }
126     
127     /*** Test output of bean with mixed content */
128     public void testMixedContent() throws Exception {
129         StringWriter out = new StringWriter();
130         out.write("<?xml version='1.0' encoding='UTF-8'?>");
131         BeanWriter writer = new BeanWriter( out );
132         writer.write( new MixedContentBean("First", "Last", "Always") );
133         
134         String xml = "<?xml version='1.0' encoding='UTF-8'?><foo version='1.0'>"
135             + "<bar version='First'>Fiddle sticks<baa>Last</baa>Always</bar></foo>";
136         
137         xmlAssertIsomorphicContent(
138                     parseString(xml),
139                     parseString(out.toString()));
140         }
141         
142     /*** Test output of bean with mixed content */
143     public void testSimpleMixedContent() throws Exception {
144         StringWriter out = new StringWriter();
145         out.write("<?xml version='1.0' encoding='UTF-8'?>");
146         BeanWriter writer = new BeanWriter( out );
147         writer.write( new MixedContentOne("Life,", "The Universe And Everything", 42) );
148         
149         String xml = "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life,' gamma='42'>"
150             + "The Universe And Everything</deep-thought>";
151         
152         xmlAssertIsomorphicContent(
153                     parseString(xml),
154                     parseString(out.toString()));
155         }
156     
157     /*** Tests basic use of an implementation for an interface */
158     public void testBasicInterfaceImpl() throws Exception {
159         ExampleBean bean = new ExampleBean("Alice");
160         bean.addExample(new ExampleImpl(1, "Mad Hatter"));
161         bean.addExample(new ExampleImpl(2, "March Hare"));
162         bean.addExample(new ExampleImpl(3, "Dormouse"));
163         
164         StringWriter out = new StringWriter();
165         out.write("<?xml version='1.0' encoding='UTF-8'?>");
166         
167         BeanWriter writer = new BeanWriter( out );
168         writer.getXMLIntrospector().setElementNameMapper(new HyphenatedNameMapper());
169         writer.getXMLIntrospector().setWrapCollectionsInElement(false);
170         
171         writer.write( bean );
172         
173         String xml = "<?xml version='1.0' encoding='UTF-8'?>"
174             + "<example-bean><name>Alice</name>"
175             + "<example><id>1</id><name>Mad Hatter</name></example>"
176             + "<example><id>2</id><name>March Hare</name></example>"
177             + "<example><id>3</id><name>Dormouse</name></example>"
178             + "</example-bean>";
179         
180         xmlAssertIsomorphicContent(
181                     parseString(xml),
182                     parseString(out.toString()),
183                     true);
184     }        
185 }
186