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.io.read;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021: import java.util.Iterator;
022:
023: import org.apache.commons.betwixt.AbstractTestCase;
024: import org.apache.commons.betwixt.io.BeanReader;
025: import org.apache.commons.betwixt.io.BeanWriter;
026: import org.xml.sax.InputSource;
027:
028: /**
029: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a> for <a href='http://www.apache.org'>The Apache Software Foundation</a>
030: */
031: public class TestPolymorphic extends AbstractTestCase {
032:
033: public TestPolymorphic(String arg0) {
034: super (arg0);
035: }
036:
037: private static final String MAPPING = "<?xml version='1.0'?>"
038: + "<betwixt-config>"
039: + " <class name='org.apache.commons.betwixt.io.read.Animals'>"
040: + " <element name='animals'>"
041: + " <element property='animals' updater='addAnimal'/>"
042: + " </element>"
043: + " </class>"
044: + " <class name='org.apache.commons.betwixt.io.read.FerretBean'>"
045: + " <element name='ferret'>"
046: + " <addDefaults/>"
047: + " </element>"
048: + " </class>"
049: + " <class name='org.apache.commons.betwixt.io.read.CatBean'>"
050: + " <element name='cat'>"
051: + " <addDefaults/>"
052: + " </element>"
053: + " </class>"
054: + " <class name='org.apache.commons.betwixt.io.read.DogBean'>"
055: + " <element name='dog'>" + " <addDefaults/>"
056: + " </element>" + " </class>" + "</betwixt-config>";
057:
058: private static final String XML = "<?xml version='1.0'?>"
059: + " <animals> " + " <ferret>"
060: + " <call>Dook</call>"
061: + " <colour>albino</colour>"
062: + " <latinName>Mustela putoris furo</latinName>"
063: + " <name>Lector</name>" + " </ferret>"
064: + " <cat>" + " <call>Meow</call>"
065: + " <colour>black</colour>"
066: + " <latinName>Felis catus</latinName>"
067: + " <name>Sam</name>" + " </cat>"
068: + " <dog>" + " <breed>mongrol</breed>"
069: + " <call>Woof</call>"
070: + " <latinName>Canis familiaris</latinName>"
071: + " <name>Bobby</name>"
072: + " <pedigree>false</pedigree>"
073: + " </dog>" + " </animals>";
074:
075: public void testWrite() throws Exception {
076: Animals animals = new Animals();
077: animals.addAnimal(new FerretBean("albino", "Lector"));
078: animals.addAnimal(new CatBean("Sam", "black"));
079: animals.addAnimal(new DogBean(false, "mongrol", "Bobby"));
080:
081: StringWriter out = new StringWriter();
082: out.write("<?xml version='1.0'?>");
083: BeanWriter writer = new BeanWriter(out);
084: writer.getBindingConfiguration().setMapIDs(false);
085: writer.getXMLIntrospector().register(
086: new InputSource(new StringReader(MAPPING)));
087: writer.write(animals);
088: xmlAssertIsomorphic(parseString(XML), parseString(out), true);
089: }
090:
091: public void testRead() throws Exception {
092: StringReader in = new StringReader(XML);
093: BeanReader reader = new BeanReader();
094: reader.getBindingConfiguration().setMapIDs(false);
095: reader.getXMLIntrospector().register(
096: new InputSource(new StringReader(MAPPING)));
097: reader.registerBeanClass(Animals.class);
098: Animals animals = (Animals) reader.parse(in);
099:
100: assertNotNull(animals);
101: assertEquals(3, animals.size());
102:
103: Iterator it = animals.getAnimals();
104: Object firstAnimal = it.next();
105: assertTrue("First animal is a ferret",
106: firstAnimal instanceof FerretBean);
107: FerretBean ferret = (FerretBean) firstAnimal;
108: assertEquals("Ferret name", "Lector", ferret.getName());
109: assertEquals("Ferret colour", "albino", ferret.getColour());
110:
111: Object secondAnimal = it.next();
112: assertTrue(secondAnimal instanceof CatBean);
113: CatBean cat = (CatBean) secondAnimal;
114: assertEquals("Cat name", "Sam", cat.getName());
115: assertEquals("Cat colour", "black", cat.getColour());
116:
117: Object thirdAnimal = it.next();
118: assertTrue(thirdAnimal instanceof DogBean);
119: DogBean dog = (DogBean) thirdAnimal;
120: assertEquals("Dog pedigree", false, dog.isPedigree());
121: assertEquals("Dog name", "Bobby", dog.getName());
122: }
123: }
|