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.poly;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022:
023: import org.apache.commons.betwixt.AbstractTestCase;
024: import org.apache.commons.betwixt.BindingConfiguration;
025: import org.apache.commons.betwixt.ElementDescriptor;
026: import org.apache.commons.betwixt.XMLBeanInfo;
027: import org.apache.commons.betwixt.XMLIntrospector;
028: import org.apache.commons.betwixt.io.BeanReader;
029: import org.apache.commons.betwixt.io.BeanWriter;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.xml.sax.InputSource;
033:
034: public class TestPolyList extends AbstractTestCase {
035: public static Log log = LogFactory.getLog(TestPolyList.class);
036:
037: public TestPolyList(String testName) {
038: super (testName);
039:
040: log.info("Mapping:\n" + MAPPING);
041: }
042:
043: private static final String XML = "<AlphaList><AlphaOneImpl><one>1</one></AlphaOneImpl><AlphaTwoImpl><two>2</two></AlphaTwoImpl></AlphaList>";
044:
045: private static final String MAPPING = "<?xml version='1.0'?>"
046: + "<betwixt-config>"
047: + " <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
048: + " <element name='AlphaOneImpl'>"
049: + " <element name='one' property='one'/>"
050: + " </element>"
051: + " </class>"
052: + " <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
053: + " <element name='AlphaTwoImpl'>"
054: + " <element name='two' property='two'/>"
055: + " </element>" + " </class>" + "</betwixt-config>";
056:
057: public void testWrite() throws Exception {
058: AlphaList bean = new AlphaList();
059: AlphaOneImpl one = new AlphaOneImpl("1");
060: bean.add(one);
061: AlphaTwoImpl two = new AlphaTwoImpl("2");
062: bean.add(two);
063:
064: StringWriter out = new StringWriter();
065: BeanWriter writer = new BeanWriter(out);
066: StringReader mapping = new StringReader(MAPPING);
067: writer.getXMLIntrospector().register(new InputSource(mapping));
068: configure(writer.getBindingConfiguration());
069: writer.write(bean);
070:
071: String written = out.getBuffer().toString();
072: log.info("Written:\n" + written);
073:
074: xmlAssertIsomorphicContent(parseString(XML),
075: parseString(written), true);
076: }
077:
078: public void testRead() throws Exception {
079: StringReader in = new StringReader(XML);
080: BeanReader reader = new BeanReader();
081: StringReader mapping = new StringReader(MAPPING);
082: reader.registerMultiMapping(new InputSource(mapping));
083: reader.registerBeanClass(AlphaList.class);
084: configure(reader.getBindingConfiguration());
085: Object bean = reader.parse(in);
086: assertTrue(bean instanceof AlphaList);
087: AlphaList list = (AlphaList) bean;
088: assertEquals(2, list.size());
089:
090: assertTrue(list.get(0) instanceof AlphaOneImpl);
091: AlphaOneImpl one = (AlphaOneImpl) list.get(0);
092: assertEquals("1", one.alpha());
093:
094: assertTrue(list.get(1) instanceof AlphaTwoImpl);
095: AlphaTwoImpl two = (AlphaTwoImpl) list.get(1);
096: assertEquals("2", two.alpha());
097: }
098:
099: public void testIntrospection() throws Exception {
100: XMLIntrospector introspector = new XMLIntrospector();
101: XMLBeanInfo beanInfo = introspector.introspect(AlphaList.class);
102: ElementDescriptor[] descriptors = beanInfo
103: .getElementDescriptor().getElementDescriptors();
104: assertEquals("One descriptor", 1, descriptors.length);
105: assertTrue(descriptors[0].isHollow());
106: assertNotNull(descriptors[0].getContextExpression());
107: assertNotNull(descriptors[0].getUpdater());
108: assertEquals("A list can contain any object", Object.class,
109: descriptors[0].getSingularPropertyType());
110: }
111:
112: private void configure(BindingConfiguration configuration) {
113: configuration.setMapIDs(false);
114: }
115: }
|