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.xml.sax.InputSource;
031:
032: public class TestPolyListHolder extends AbstractTestCase {
033:
034: public TestPolyListHolder(String testName) {
035: super (testName);
036: }
037:
038: private static final String XML = "<AlphaListHolder>"
039: + " <AlphaList>" + " <AlphaOneImpl>"
040: + " <one>1</one>" + " </AlphaOneImpl>"
041: + " <AlphaTwoImpl>" + " <two>2</two>"
042: + " </AlphaTwoImpl>" + " </AlphaList>"
043: + "</AlphaListHolder>";
044:
045: private static final String MAPPING = "<?xml version='1.0'?>"
046: + "<betwixt-config>"
047: + " <class name='org.apache.commons.betwixt.poly.AlphaListHolder'>"
048: + " <element name='AlphaListHolder'>"
049: + " <element name='AlphaList' property='alphaList' updater='setAlphaList'/>"
050: + " </element>"
051: + " </class>"
052: + " <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
053: + " <element name='AlphaOneImpl'>"
054: + " <element name='one' property='one'/>"
055: + " </element>"
056: + " </class>"
057: + " <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
058: + " <element name='AlphaTwoImpl'>"
059: + " <element name='two' property='two'/>"
060: + " </element>" + " </class>" + "</betwixt-config>";
061:
062: public void testWrite() throws Exception {
063: AlphaList list = new AlphaList();
064: AlphaOneImpl one = new AlphaOneImpl("1");
065: list.add(one);
066: AlphaTwoImpl two = new AlphaTwoImpl("2");
067: list.add(two);
068:
069: AlphaListHolder bean = new AlphaListHolder();
070: bean.setAlphaList(list);
071:
072: StringWriter out = new StringWriter();
073: BeanWriter writer = new BeanWriter(out);
074: StringReader mapping = new StringReader(MAPPING);
075: writer.getXMLIntrospector().register(new InputSource(mapping));
076: configure(writer.getBindingConfiguration());
077: writer.write(bean);
078:
079: String written = out.getBuffer().toString();
080:
081: xmlAssertIsomorphicContent(parseString(XML),
082: parseString(written), true);
083: }
084:
085: public void testRead() throws Exception {
086:
087: StringReader in = new StringReader(XML);
088: BeanReader reader = new BeanReader();
089:
090: StringReader mapping = new StringReader(MAPPING);
091: reader.registerMultiMapping(new InputSource(mapping));
092: reader.registerBeanClass(AlphaList.class);
093: configure(reader.getBindingConfiguration());
094: Object bean = reader.parse(in);
095:
096: assertTrue(bean instanceof AlphaListHolder);
097: AlphaListHolder holder = (AlphaListHolder) bean;
098:
099: AlphaList list = holder.getAlphaList();
100: assertNotNull(list);
101: assertEquals(2, list.size());
102:
103: assertTrue(list.get(0) instanceof AlphaOneImpl);
104: AlphaOneImpl one = (AlphaOneImpl) list.get(0);
105: assertEquals("1", one.alpha());
106:
107: assertTrue(list.get(1) instanceof AlphaTwoImpl);
108: AlphaTwoImpl two = (AlphaTwoImpl) list.get(1);
109: assertEquals("2", two.alpha());
110: }
111:
112: private void configure(BindingConfiguration configuration) {
113: configuration.setMapIDs(false);
114: }
115:
116: public void testIntrospection() throws Exception {
117: XMLIntrospector introspector = new XMLIntrospector();
118:
119: StringReader mapping = new StringReader(MAPPING);
120: introspector.register(new InputSource(mapping));
121:
122: XMLBeanInfo beanInfo = introspector
123: .introspect(AlphaListHolder.class);
124: ElementDescriptor descriptor = beanInfo.getElementDescriptor();
125: assertNotNull(descriptor);
126: ElementDescriptor[] descriptors = descriptor
127: .getElementDescriptors();
128: assertNotNull(descriptors);
129: assertEquals("Only one descriptor", 1, descriptors.length);
130: assertNotNull("Expected updater", descriptors[0].getUpdater());
131: }
132: }
|