KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.betwixt.io.BeanReader;
27 import org.apache.commons.betwixt.io.BeanWriter;
28 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
29 import org.apache.commons.betwixt.xmlunit.XmlTestCase;
30
31 /**
32   * Test customization of xml to bean mapping using .betwixt files.
33   *
34   * @author Robert Burrell Donkin
35   */

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

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

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

52     public void _testCustomUpdaters() throws Exception JavaDoc {
53         // might as well check writer whilst we're at it
54
MixedUpdatersBean bean = new MixedUpdatersBean("Lov");
55         bean.badNameSetter("Hate");
56         bean.addItem("White");
57         bean.badItemAdder("Black");
58         bean.addItem("Life");
59         bean.badItemAdder("Death");
60         
61 // SimpleLog log = new SimpleLog("[testCustomUpdaters:XMLIntrospector]");
62
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
63

64         StringWriter JavaDoc out = new StringWriter JavaDoc();
65         out.write("<?xml version='1.0'?>");
66         BeanWriter writer = new BeanWriter(out);
67 // writer.getXMLIntrospector().setLog(log);
68

69 // log = new SimpleLog("[testCustomUpdaters:XMLIntrospectorHelper]");
70
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
71
// XMLIntrospectorHelper.setLog(log);
72

73         writer.getBindingConfiguration().setMapIDs(false);
74         writer.write(bean);
75
76         String JavaDoc xml = "<?xml version='1.0'?><mixed><name>Lov</name><bad-name>Hate</bad-name>"
77           + "<items><item>White</item><item>Life</item></items>"
78           + "<bad-items><bad-item>Black</bad-item><bad-item>Death</bad-item></bad-items></mixed>";
79         
80         xmlAssertIsomorphicContent(
81                     parseString(xml),
82                     parseString(out.toString()),
83                     true);
84         
85 // SimpleLog log = new SimpleLog("[testCustomUpdaters:XMLIntrospectorHelper]");
86
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
87
// XMLIntrospectorHelper.setLog(log);
88

89 // log = new SimpleLog("[testCustomUpdaters:BeanRuleSet]");
90
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
91
// BeanRuleSet.setLog(log);
92

93 // log = new SimpleLog("[testCustomUpdaters:ElementRule]");
94
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
95
// ElementRule.setLog(log);
96

97         // now we'll test reading via round tripping
98
BeanReader reader = new BeanReader();
99         reader.getBindingConfiguration().setMapIDs(false);
100         reader.registerBeanClass("mixed", MixedUpdatersBean.class);
101         bean = (MixedUpdatersBean) reader.parse(new StringReader JavaDoc(xml));
102         
103         assertEquals("Name incorrect", "Lov", bean.getName());
104         assertEquals("BadName incorrect", "Hate", bean.getBadName());
105         List JavaDoc items = bean.getItems();
106         assertEquals("Wrong number of items", 2, items.size());
107         assertEquals("Item one wrong", "White", items.get(0));
108         assertEquals("Item two wrong", "Life", items.get(1));
109         List JavaDoc badItems = bean.getBadItems();
110         assertEquals("Wrong number of bad items", 2, badItems.size());
111         assertEquals("Bad item one wrong", "Black", badItems.get(0));
112         assertEquals("Bad item two wrong", "Death", badItems.get(1));
113         
114     }
115
116     
117     /** Test output of bean with mixed content */
118     public void testMixedContent() throws Exception JavaDoc {
119         
120         StringReader JavaDoc xml = new StringReader JavaDoc(
121             "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life' gamma='42'>"
122             + "The Universe And Everything</deep-thought>");
123
124         //SimpleLog log = new SimpleLog("[testMixedContent:BeanRuleSet]");
125
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
126
//BeanRuleSet.setLog(log);
127
//log = new SimpleLog("[testMixedContent:BeanReader]");
128
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
129

130         BeanReader reader = new BeanReader();
131         //reader.setLog(log);
132
reader.registerBeanClass(MixedContentOne.class);
133         Object JavaDoc resultObject = reader.parse(xml);
134         assertEquals("Object is MixedContentOne", true, resultObject instanceof MixedContentOne);
135         MixedContentOne result = (MixedContentOne) resultObject;
136         assertEquals("Property Alpha matches", "Life", result.getAlpha());
137         assertEquals("Property Beta matches", "The Universe And Everything", result.getBeta());
138         assertEquals("Property Gamma matches", 42, result.getGamma());
139     }
140     
141     
142     /** Tests basic use of an implementation for an interface */
143     public void _testBasicInterfaceImpl() throws Exception JavaDoc {
144         //SimpleLog log = new SimpleLog("[testBasicInterfaceImpl:BeanRuleSet]");
145
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
146
//BeanRuleSet.setLog(log);
147
//log = new SimpleLog("[testBasicInterfaceImpl:BeanReader]");
148
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
149

150         ExampleBean bean = new ExampleBean("Alice");
151         bean.addExample(new ExampleImpl(1, "Mad Hatter"));
152         bean.addExample(new ExampleImpl(2, "March Hare"));
153         bean.addExample(new ExampleImpl(3, "Dormouse"));
154         
155         String JavaDoc xml = "<?xml version='1.0' encoding='UTF-8'?>"
156             + "<example-bean><name>Alice</name>"
157             + "<example><id>1</id><name>Mad Hatter</name></example>"
158             + "<example><id>2</id><name>March Hare</name></example>"
159             + "<example><id>3</id><name>Dormouse</name></example>"
160             + "</example-bean>";
161         
162         
163         BeanReader reader = new BeanReader();
164         //reader.setLog(log);
165
reader.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
166         reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
167         reader.registerBeanClass( ExampleBean.class );
168         
169         StringReader JavaDoc in = new StringReader JavaDoc( xml );
170         ExampleBean out = (ExampleBean) reader.parse( in );
171         assertEquals("Interface read failed", bean, out);
172         
173     }
174 }
175
176
Popular Tags