001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.dotbetwixt;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022: import java.util.List;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.commons.betwixt.ElementDescriptor;
028: import org.apache.commons.betwixt.XMLBeanInfo;
029: import org.apache.commons.betwixt.XMLIntrospector;
030: import org.apache.commons.betwixt.io.BeanReader;
031: import org.apache.commons.betwixt.io.BeanWriter;
032: import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
033: import org.apache.commons.betwixt.xmlunit.XmlTestCase;
034:
035: /**
036: * Test customization of xml to bean mapping using .betwixt files.
037: *
038: * @author Robert Burrell Donkin
039: */
040: public class TestXmlToBean extends XmlTestCase {
041:
042: //--------------------------------- Test Suite
043:
044: public static Test suite() {
045: return new TestSuite(TestXmlToBean.class);
046: }
047:
048: //--------------------------------- Constructor
049:
050: public TestXmlToBean(String testName) {
051: super (testName);
052: }
053:
054: //---------------------------------- Tests
055:
056: public void testForceAccessibleSuper() throws Exception {
057: XMLIntrospector xmlIntrospector = new XMLIntrospector();
058: XMLBeanInfo xmlBeanInfo = xmlIntrospector
059: .introspect(MixedUpdatersBean.class);
060: ElementDescriptor[] descriptors = xmlBeanInfo
061: .getElementDescriptor().getElementDescriptors();
062: boolean propertyFound = false;
063: for (int i = 0; i < descriptors.length; i++) {
064: ElementDescriptor descriptor = descriptors[i];
065: if ("private-super".equals(descriptor.getLocalName())) {
066: propertyFound = true;
067: assertNotNull("Updater found", descriptor.getUpdater());
068: assertNotNull("Expression found", descriptor
069: .getTextExpression());
070: }
071: }
072: assertTrue("Found inaccessible super methods", propertyFound);
073: }
074:
075: public void testCustomUpdaters() throws Exception {
076: // might as well check writer whilst we're at it
077: MixedUpdatersBean bean = new MixedUpdatersBean("Lov");
078: bean.badNameSetter("Hate");
079: bean.addItem("White");
080: bean.badItemAdder("Black");
081: bean.addItem("Life");
082: bean.badItemAdder("Death");
083: bean.privatePropertyWorkaroundSetter("Private");
084: bean.getPrivateItems().add("private item 1");
085: bean.privateField = 100;
086:
087: StringWriter out = new StringWriter();
088: out.write("<?xml version='1.0'?>");
089: BeanWriter writer = new BeanWriter(out);
090:
091: writer.getBindingConfiguration().setMapIDs(false);
092: writer.write(bean);
093:
094: String xml = "<?xml version='1.0'?><mixed><name>Lov</name><bad-name>Hate</bad-name>"
095: + "<items><item>White</item><item>Life</item></items>"
096: + "<bad-items><bad-item>Black</bad-item><bad-item>Death</bad-item></bad-items>"
097: + "<private-property>Private</private-property>"
098: + "<private-items><private-item>private item 1</private-item></private-items>"
099: + "<private-super>100</private-super>" + "</mixed>";
100:
101: xmlAssertIsomorphicContent(parseString(xml), parseString(out
102: .toString()), true);
103:
104: // now we'll test reading via round tripping
105: BeanReader reader = new BeanReader();
106: reader.getBindingConfiguration().setMapIDs(false);
107: reader.registerBeanClass("mixed", MixedUpdatersBean.class);
108: bean = (MixedUpdatersBean) reader.parse(new StringReader(xml));
109:
110: assertEquals("Name incorrect", "Lov", bean.getName());
111: assertEquals("BadName incorrect", "Hate", bean.getBadName());
112: List items = bean.getItems();
113: assertEquals("Wrong number of items", 2, items.size());
114: assertEquals("Item one wrong", "White", items.get(0));
115: assertEquals("Item two wrong", "Life", items.get(1));
116: List badItems = bean.getBadItems();
117: assertEquals("Wrong number of bad items", 2, badItems.size());
118: // awaiting implementation
119: //assertEquals("Bad item one wrong", "Black", badItems.get(0));
120: //assertEquals("Bad item two wrong", "Death", badItems.get(1));
121: assertEquals("Private property incorrect", "Private", bean
122: .getPrivateProperty());
123:
124: //this shows that a private adder can be utilized
125: List privateItems = bean.getPrivateItems();
126: assertEquals("Wrong number of private items", 1, privateItems
127: .size());
128: //TODO can't assert contents - gets the right number of items, but each is null (badItems, too)
129: assertEquals("Private property accessed on super", 100,
130: bean.privateField);
131: }
132:
133: /** Test output of bean with mixed content */
134: public void testMixedContent() throws Exception {
135:
136: StringReader xml = new StringReader(
137: "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life' gamma='42'>"
138: + "The Universe And Everything</deep-thought>");
139:
140: BeanReader reader = new BeanReader();
141: reader.registerBeanClass(MixedContentOne.class);
142: Object resultObject = reader.parse(xml);
143: assertEquals("Object is MixedContentOne", true,
144: resultObject instanceof MixedContentOne);
145: MixedContentOne result = (MixedContentOne) resultObject;
146: assertEquals("Property Alpha matches", "Life", result
147: .getAlpha());
148: assertEquals("Property Beta matches",
149: "The Universe And Everything", result.getBeta());
150: assertEquals("Property Gamma matches", 42, result.getGamma());
151: }
152:
153: /** Tests basic use of an implementation for an interface */
154: public void _testBasicInterfaceImpl() throws Exception {
155:
156: ExampleBean bean = new ExampleBean("Alice");
157: bean.addExample(new ExampleImpl(1, "Mad Hatter"));
158: bean.addExample(new ExampleImpl(2, "March Hare"));
159: bean.addExample(new ExampleImpl(3, "Dormouse"));
160:
161: String xml = "<?xml version='1.0' encoding='UTF-8'?>"
162: + "<example-bean><name>Alice</name>"
163: + "<example><id>1</id><name>Mad Hatter</name></example>"
164: + "<example><id>2</id><name>March Hare</name></example>"
165: + "<example><id>3</id><name>Dormouse</name></example>"
166: + "</example-bean>";
167:
168: BeanReader reader = new BeanReader();
169: reader.getXMLIntrospector().getConfiguration()
170: .setElementNameMapper(new HyphenatedNameMapper());
171: reader.getXMLIntrospector().getConfiguration()
172: .setWrapCollectionsInElement(false);
173: reader.registerBeanClass(ExampleBean.class);
174:
175: StringReader in = new StringReader(xml);
176: ExampleBean out = (ExampleBean) reader.parse(in);
177: assertEquals("Interface read failed", bean, out);
178:
179: }
180: }
|