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: package org.apache.commons.betwixt.dotbetwixt;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021:
022: import org.apache.commons.betwixt.AbstractTestCase;
023: import org.apache.commons.betwixt.ElementDescriptor;
024: import org.apache.commons.betwixt.XMLBeanInfo;
025: import org.apache.commons.betwixt.XMLIntrospector;
026: import org.apache.commons.betwixt.io.BeanReader;
027: import org.apache.commons.betwixt.io.BeanWriter;
028: import org.xml.sax.InputSource;
029:
030: /**
031: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
032: */
033: public class TestCustomDotBetwixt extends AbstractTestCase {
034:
035: public TestCustomDotBetwixt(String testName) {
036: super (testName);
037: }
038:
039: public void testIntrospectWithCustomDotBetwixt() throws Exception {
040: StringReader reader = new StringReader("<?xml version='1.0' ?>"
041: + "<info>" + " <element name='jelly'>"
042: + " <element name='wibble' property='alpha'/>"
043: + " <element name='wobble' property='beta'/>"
044: + " </element>" + "</info>");
045: XMLIntrospector introspector = new XMLIntrospector();
046: XMLBeanInfo xmlBeanInfo = introspector.introspect(
047: SimpleTestBean.class, new InputSource(reader));
048:
049: ElementDescriptor elementDescriptor = xmlBeanInfo
050: .getElementDescriptor();
051: assertEquals("Root is jelly", "jelly", elementDescriptor
052: .getLocalName());
053: ElementDescriptor[] childDescriptors = elementDescriptor
054: .getElementDescriptors();
055: assertEquals("Expected two child elements", 2,
056: childDescriptors.length);
057: assertEquals("Wibble comes first", "wibble",
058: childDescriptors[0].getLocalName());
059: assertEquals("Wobble comes last", "wobble", childDescriptors[1]
060: .getLocalName());
061:
062: reader = new StringReader(
063: "<?xml version='1.0' ?>"
064: + "<info>"
065: + " <element name='not-jelly'>"
066: + " <element name='no-wibble' property='alpha'/>"
067: + " <element name='no-wobble' property='beta'/>"
068: + " </element>" + "</info>");
069:
070: xmlBeanInfo = introspector.introspect(SimpleTestBean.class,
071: new InputSource(reader));
072:
073: elementDescriptor = xmlBeanInfo.getElementDescriptor();
074: assertEquals("Root is not-jelly", "not-jelly",
075: elementDescriptor.getLocalName());
076: childDescriptors = elementDescriptor.getElementDescriptors();
077: assertEquals("Expected two child elements", 2,
078: childDescriptors.length);
079: assertEquals("No wibble comes first", "no-wibble",
080: childDescriptors[0].getLocalName());
081: assertEquals("No wobble comes last", "no-wobble",
082: childDescriptors[1].getLocalName());
083: }
084:
085: public void testRegisterCustomDotBetwixt() throws Exception {
086: StringReader reader = new StringReader("<?xml version='1.0' ?>"
087: + "<info>" + " <element name='jelly'>"
088: + " <element name='wibble' property='alpha'/>"
089: + " <element name='wobble' property='beta'/>"
090: + " </element>" + "</info>");
091: XMLIntrospector introspector = new XMLIntrospector();
092: introspector.register(SimpleTestBean.class, new InputSource(
093: reader));
094: XMLBeanInfo xmlBeanInfo = introspector
095: .introspect(SimpleTestBean.class);
096:
097: ElementDescriptor elementDescriptor = xmlBeanInfo
098: .getElementDescriptor();
099: assertEquals("Root is jelly", "jelly", elementDescriptor
100: .getLocalName());
101: ElementDescriptor[] childDescriptors = elementDescriptor
102: .getElementDescriptors();
103: assertEquals("Expected two child elements", 2,
104: childDescriptors.length);
105: assertEquals("Wibble comes first", "wibble",
106: childDescriptors[0].getLocalName());
107: assertEquals("Wobble comes last", "wobble", childDescriptors[1]
108: .getLocalName());
109: }
110:
111: public void testWriteCustomDotBetwixt() throws Exception {
112: StringReader reader = new StringReader("<?xml version='1.0' ?>"
113: + "<info>" + " <element name='jelly'>"
114: + " <element name='wibble' property='alpha'/>"
115: + " <element name='wobble' property='beta'/>"
116: + " </element>" + "</info>");
117:
118: StringWriter out = new StringWriter();
119: out.write("<?xml version='1.0'?>");
120: SimpleTestBean bean = new SimpleTestBean("one", "two", "three");
121:
122: BeanWriter writer = new BeanWriter(out);
123: writer.getBindingConfiguration().setMapIDs(false);
124: writer.write(bean, new InputSource(reader));
125:
126: String expected = "<?xml version='1.0'?>"
127: + "<jelly><wibble>one</wibble><wobble>two</wobble></jelly>";
128: xmlAssertIsomorphic(parseString(expected), parseString(out));
129: }
130:
131: public void testReadCustomDotBetwixt() throws Exception {
132: String xml = "<?xml version='1.0'?>"
133: + "<jelly><wibble>one</wibble><wobble>two</wobble></jelly>";
134: StringReader in = new StringReader(xml);
135:
136: StringReader dotBetwixt = new StringReader(
137: "<?xml version='1.0' ?>"
138: + "<info>"
139: + " <element name='jelly'>"
140: + " <element name='wibble' property='alpha'/>"
141: + " <element name='wobble' property='beta'/>"
142: + " </element>" + "</info>");
143:
144: BeanReader reader = new BeanReader();
145: reader.getBindingConfiguration().setMapIDs(false);
146: reader.registerBeanClass(new InputSource(dotBetwixt),
147: SimpleTestBean.class);
148: SimpleTestBean bean = (SimpleTestBean) reader.parse(in);
149: assertNotNull("Bean not mapped", bean);
150: assertEquals("Property alpha mapping", "one", bean.getAlpha());
151: assertEquals("Property beta mapping", "two", bean.getBeta());
152: }
153: }
|