1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt.io.read;
18  
19  import java.io.StringReader;
20  import java.io.StringWriter;
21  import java.util.Iterator;
22  
23  import org.apache.commons.betwixt.AbstractTestCase;
24  import org.apache.commons.betwixt.io.BeanReader;
25  import org.apache.commons.betwixt.io.BeanWriter;
26  import org.xml.sax.InputSource;
27  
28  /**
29   * @author <a href='http://commons.apache.org'>Apache Commons Team</a> for <a href='http://www.apache.org'>The Apache Software Foundation</a>
30   */
31  public class TestPolymorphic extends AbstractTestCase {
32  
33      public TestPolymorphic(String arg0) {
34          super(arg0);
35      }
36  
37      private static final String MAPPING = "<?xml version='1.0'?>" +
38              "<betwixt-config>" +
39              "  <class name='org.apache.commons.betwixt.io.read.Animals'>" +
40              "    <element name='animals'>" +
41              "      <element property='animals' updater='addAnimal'/>" +
42              "    </element>" +
43              "  </class>" +
44              "  <class name='org.apache.commons.betwixt.io.read.FerretBean'>" +
45              "    <element name='ferret'>" +
46              "      <addDefaults/>" +
47              "    </element>" +
48              "  </class>" +
49              "  <class name='org.apache.commons.betwixt.io.read.CatBean'>" +
50              "    <element name='cat'>" +
51              "      <addDefaults/>" +
52              "    </element>" +
53              "  </class>" +
54              "  <class name='org.apache.commons.betwixt.io.read.DogBean'>" +
55              "    <element name='dog'>" +
56              "      <addDefaults/>" +
57              "    </element>" +
58              "  </class>" +
59              "</betwixt-config>";
60      
61      private static final String XML = "<?xml version='1.0'?>" +
62              "    <animals> " +
63              "        <ferret>" +
64              "            <call>Dook</call>" +
65              "            <colour>albino</colour>" +
66              "            <latinName>Mustela putoris furo</latinName>" +
67              "            <name>Lector</name>" +
68              "        </ferret>" +
69              "        <cat>" +
70              "            <call>Meow</call>" +
71              "            <colour>black</colour>" +
72              "            <latinName>Felis catus</latinName>" +
73              "            <name>Sam</name>" +
74              "        </cat>" +
75              "        <dog>" +
76              "            <breed>mongrol</breed>" +
77              "            <call>Woof</call>" +
78              "            <latinName>Canis familiaris</latinName>" +
79              "            <name>Bobby</name>" +
80              "            <pedigree>false</pedigree>" +
81              "        </dog>" +
82              "    </animals>";
83      
84      public void testWrite() throws Exception
85      {
86          Animals animals = new Animals();
87          animals.addAnimal(new FerretBean("albino", "Lector"));
88          animals.addAnimal(new CatBean("Sam", "black"));
89          animals.addAnimal(new DogBean(false, "mongrol", "Bobby"));
90          
91          StringWriter out = new StringWriter();
92          out.write("<?xml version='1.0'?>");
93          BeanWriter writer = new BeanWriter(out);
94          writer.getBindingConfiguration().setMapIDs(false);
95          writer.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
96          writer.write(animals);
97          xmlAssertIsomorphic(parseString(XML), parseString(out), true);
98      }
99      
100     
101     public void testRead() throws Exception
102     {
103         StringReader in = new StringReader(XML);
104         BeanReader reader = new BeanReader();
105         reader.getBindingConfiguration().setMapIDs(false);
106         reader.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
107         reader.registerBeanClass(Animals.class);
108         Animals animals = (Animals) reader.parse(in);
109         
110         assertNotNull(animals);
111         assertEquals(3, animals.size());
112         
113         Iterator it = animals.getAnimals();
114         Object firstAnimal = it.next();
115         assertTrue("First animal is a ferret", firstAnimal instanceof FerretBean);
116         FerretBean ferret = (FerretBean) firstAnimal;
117         assertEquals("Ferret name", "Lector", ferret.getName());
118         assertEquals("Ferret colour", "albino", ferret.getColour());
119         
120         Object secondAnimal = it.next();
121         assertTrue(secondAnimal instanceof CatBean);
122         CatBean cat = (CatBean) secondAnimal;
123         assertEquals("Cat name", "Sam", cat.getName());
124         assertEquals("Cat colour", "black", cat.getColour());
125         
126         Object thirdAnimal = it.next();
127         assertTrue(thirdAnimal instanceof DogBean);
128         DogBean dog = (DogBean) thirdAnimal;
129         assertEquals("Dog pedigree", false, dog.isPedigree());
130         assertEquals("Dog name", "Bobby", dog.getName());
131     }
132 }