KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > dotbetwixt > TestBeanToXml


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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  
18 package org.apache.commons.betwixt.dotbetwixt;
19
20 import java.io.StringWriter JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.betwixt.io.BeanWriter;
26 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
27 import org.apache.commons.betwixt.xmlunit.XmlTestCase;
28
29
30 /**
31   * Provides xml test utilities.
32   * Hopefully, these might be moved into [xmlunit] sometime.
33   *
34   * @author Robert Burrell Donkin
35   */

36 public class TestBeanToXml extends XmlTestCase {
37
38 //--------------------------------- Test Suite
39

40     public static Test suite() {
41         return new TestSuite(TestBeanToXml.class);
42     }
43     
44 //--------------------------------- Constructor
45

46     public TestBeanToXml(String JavaDoc testName) {
47         super(testName);
48     }
49
50 //---------------------------------- Tests
51

52     public void testOne() throws Exception JavaDoc {
53         // THIS TEST FAILS IN MAVEN
54
xmlAssertIsomorphicContent(
55             parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"),
56             parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"));
57     }
58     
59     public void testSimpleBean() throws Exception JavaDoc {
60         StringWriter JavaDoc out = new StringWriter JavaDoc();
61         out.write("<?xml version='1.0' encoding='UTF-8'?>");
62 // SimpleLog log = new SimpleLog("[testSimpleBean:XMLIntrospector]");
63
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
64
BeanWriter writer = new BeanWriter(out);
65         writer.setWriteEmptyElements( true );
66 // writer.getXMLIntrospector().setLog(log);
67

68 // log = new SimpleLog("[testSimpleBean:XMLIntrospectorHelper]");
69
// XMLIntrospectorHelper.setLog(log);
70

71         writer.getBindingConfiguration().setMapIDs(false);
72     SimpleTestBean bean = new SimpleTestBean("alpha-value","beta-value","gamma-value");
73         writer.write(bean);
74         out.flush();
75         String JavaDoc xml = out.toString();
76         
77         xmlAssertIsomorphicContent(
78                     parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/simpletestone.xml"),
79                     parseString(xml));
80
81     }
82     
83     public void testWriteRecursiveBean() throws Exception JavaDoc {
84         /*
85         StringWriter out = new StringWriter();
86         out.write("<?xml version='1.0' encoding='UTF-8'?>");
87         BeanWriter writer = new BeanWriter(out);
88         RecursiveBean bean
89             = new RecursiveBean(
90                 "alpha",
91                 new RecursiveBean(
92                     "beta",
93                     new RecursiveBean("gamma")));
94         writer.setWriteIDs(false);
95         writer.write(bean);
96         out.flush();
97         String xml = out.toString();
98         
99         if (debug) {
100             System.out.println(xml);
101         }
102         
103         
104         xmlAssertIsomorphicContent(
105                     parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"),
106                     parseString(xml));
107         */

108     }
109     
110     /**
111      * This tests that only well formed names for elements and attributes are allowed by .betwixt files
112      */

113     public void testBadDotBetwixtNames() throws Exception JavaDoc {
114         // this will work by testing that the output is well formed
115

116         StringWriter JavaDoc out = new StringWriter JavaDoc();
117         out.write("<?xml version='1.0' encoding='UTF-8'?>");
118         BeanWriter writer = new BeanWriter(out);
119         writer.setWriteEmptyElements( true );
120         writer.write(new BadDotBetwixtNamesBean("one", "two"));
121         
122 // System.out.println(out.toString());
123

124         // this should fail if the output is not well formed
125
parseString(out.toString());
126     }
127     
128     /** Test output of bean with mixed content */
129     public void testMixedContent() throws Exception JavaDoc {
130         StringWriter JavaDoc out = new StringWriter JavaDoc();
131         out.write("<?xml version='1.0' encoding='UTF-8'?>");
132         BeanWriter writer = new BeanWriter( out );
133         writer.getBindingConfiguration().setMapIDs(false);
134         writer.write( new MixedContentBean("First", "Last", "Always") );
135         
136         String JavaDoc xml = "<?xml version='1.0' encoding='UTF-8'?><foo version='1.0'>"
137             + "<bar version='First'>Fiddle sticks<baa>Last</baa>Always</bar></foo>";
138         
139         xmlAssertIsomorphicContent(
140                     parseString(xml),
141                     parseString(out.toString()));
142         }
143         
144     /** Test output of bean with mixed content */
145     public void testSimpleMixedContent() throws Exception JavaDoc {
146         StringWriter JavaDoc out = new StringWriter JavaDoc();
147         out.write("<?xml version='1.0' encoding='UTF-8'?>");
148         BeanWriter writer = new BeanWriter( out );
149         writer.getBindingConfiguration().setMapIDs(false);
150         writer.write( new MixedContentOne("Life,", "The Universe And Everything", 42) );
151         
152         String JavaDoc xml = "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life,' gamma='42'>"
153             + "The Universe And Everything</deep-thought>";
154         
155         xmlAssertIsomorphicContent(
156                     parseString(xml),
157                     parseString(out.toString()));
158         }
159     
160     /** Tests basic use of an implementation for an interface */
161     public void testBasicInterfaceImpl() throws Exception JavaDoc {
162         ExampleBean bean = new ExampleBean("Alice");
163         bean.addExample(new ExampleImpl(1, "Mad Hatter"));
164         bean.addExample(new ExampleImpl(2, "March Hare"));
165         bean.addExample(new ExampleImpl(3, "Dormouse"));
166         
167         StringWriter JavaDoc out = new StringWriter JavaDoc();
168         out.write("<?xml version='1.0' encoding='UTF-8'?>");
169         
170         BeanWriter writer = new BeanWriter( out );
171         writer.getBindingConfiguration().setMapIDs(false);
172         writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
173         writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
174         
175         writer.write( bean );
176         
177         String JavaDoc xml = "<?xml version='1.0' encoding='UTF-8'?>"
178             + "<example-bean><name>Alice</name>"
179             + "<example><id>1</id><name>Mad Hatter</name></example>"
180             + "<example><id>2</id><name>March Hare</name></example>"
181             + "<example><id>3</id><name>Dormouse</name></example>"
182             + "</example-bean>";
183         
184         xmlAssertIsomorphicContent(
185                     parseString(xml),
186                     parseString(out.toString()),
187                     true);
188     }
189 }
190
191
Popular Tags