1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  package org.apache.commons.betwixt.io.read;
17  
18  import java.io.StringReader;
19  import java.io.StringWriter;
20  import java.util.Iterator;
21  
22  import org.apache.commons.betwixt.AbstractTestCase;
23  import org.apache.commons.betwixt.ElementDescriptor;
24  import org.apache.commons.betwixt.XMLBeanInfo;
25  import org.apache.commons.betwixt.XMLIntrospector;
26  import org.apache.commons.betwixt.io.BeanReader;
27  import org.apache.commons.betwixt.io.BeanWriter;
28  import org.apache.commons.betwixt.strategy.MappingDerivationStrategy;
29  import org.xml.sax.InputSource;
30  
31  /***
32   * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
33   */
34  public class TestBindTimeTypeMapping extends AbstractTestCase {
35      
36      
37      
38      public TestBindTimeTypeMapping(String testName) {
39          super(testName);
40      }
41  
42      //todo: note to self need to consider how the design of the introspection will work when you have strategy which takes 
43      // singular property which is not know until after the digestion
44      
45      public void testDefaultMappingDerivationStrategy() throws Exception {
46          XMLIntrospector xmlIntrospector = new XMLIntrospector();
47          XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(Animals.class);
48          ElementDescriptor animalsDescriptor = xmlBeanInfo.getElementDescriptor();
49          assertEquals("Use bind time type", true, animalsDescriptor.isUseBindTimeTypeForMapping());
50          ElementDescriptor animalDescriptor = animalsDescriptor.getElementDescriptors()[0];
51          assertEquals("Use bind time type", true, animalDescriptor.isUseBindTimeTypeForMapping());
52      }
53      
54      public void testIntrospectionTimeMappingDerivationStrategy() throws Exception {
55          XMLIntrospector xmlIntrospector = new XMLIntrospector();
56          xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
57          XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(Animals.class);
58          ElementDescriptor animalsDescriptor = xmlBeanInfo.getElementDescriptor();
59          assertEquals("Use introspection time type", false, animalsDescriptor.isUseBindTimeTypeForMapping());
60          ElementDescriptor animalDescriptor = animalsDescriptor.getElementDescriptors()[0];
61          assertEquals("Use introspection time type", false, animalDescriptor.isUseBindTimeTypeForMapping());
62      }
63  
64      public void testBindTypeMappingDerivationStrategy() throws Exception {
65          XMLIntrospector xmlIntrospector = new XMLIntrospector();
66          xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE);
67          XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(Animals.class);
68          ElementDescriptor animalsDescriptor = xmlBeanInfo.getElementDescriptor();
69          assertEquals("Use bind time type", true, animalsDescriptor.isUseBindTimeTypeForMapping());
70          ElementDescriptor animalDescriptor = animalsDescriptor.getElementDescriptors()[0];
71          assertEquals("Use bind time type", true, animalDescriptor.isUseBindTimeTypeForMapping());
72      }
73      
74      public void testBindTypeMappingDerivationDotBetwixt() throws Exception {
75          String mappingDocument="<?xml version='1.0'?><info>" +
76          		"<element name='pet-record'>" +
77          		"	<element name='pet' property='pet' mappingDerivation='bind'/>" +
78          		"</element>" +
79          		"</info>";
80          XMLIntrospector xmlIntrospector = new XMLIntrospector();
81          xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
82          XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(PetBean.class, new InputSource(new StringReader(mappingDocument)));
83          ElementDescriptor petDescriptor = xmlBeanInfo.getElementDescriptor();
84          assertEquals("Use type from strategy", true, petDescriptor.isUseBindTimeTypeForMapping());
85          ElementDescriptor animalDescriptor = petDescriptor.getElementDescriptors()[0];
86          assertEquals("Use type from document", true, animalDescriptor.isUseBindTimeTypeForMapping());
87      }
88      
89      public void testIntrospectionTypeMappingDerivationDotBetwixt() throws Exception {
90          String mappingDocument="<?xml version='1.0'?><info>" +
91          		"<element name='pet-record'>" +
92          		"	<element name='pet' property='pet' mappingDerivation='introspection'/>" +
93          		"</element>" +
94          		"</info>";
95          XMLIntrospector xmlIntrospector = new XMLIntrospector();
96          xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE);
97          XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(PetBean.class, new InputSource(new StringReader(mappingDocument)));
98          ElementDescriptor petDescriptor = xmlBeanInfo.getElementDescriptor();
99          assertEquals("Use type from strategy", true, petDescriptor.isUseBindTimeTypeForMapping());
100         ElementDescriptor animalDescriptor = petDescriptor.getElementDescriptors()[0];
101         assertEquals("Use type from document", false, animalDescriptor.isUseBindTimeTypeForMapping());
102     }
103     
104     public void testMappingDerivationDotBetwixtAddDefaults() throws Exception {
105         String mappingDocument="<?xml version='1.0'?><info>" +
106         		"<element name='pet-record'>" +
107         		"	<element name='pet' property='pet' mappingDerivation='introspection'/>" +
108         		"   <addDefaults/>" +
109         		"</element>" +
110         		"</info>";
111         XMLIntrospector xmlIntrospector = new XMLIntrospector();
112         xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE);
113         XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(PetBean.class, new InputSource(new StringReader(mappingDocument)));
114         ElementDescriptor petDescriptor = xmlBeanInfo.getElementDescriptor();
115         assertEquals("Use type from strategy", true, petDescriptor.isUseBindTimeTypeForMapping());
116         ElementDescriptor animalDescriptor = petDescriptor.getElementDescriptors()[0];
117         assertEquals("Use type from document", false, animalDescriptor.isUseBindTimeTypeForMapping());
118         ElementDescriptor personDescriptor = petDescriptor.getElementDescriptors()[1];
119         assertEquals("Use type from document", true, personDescriptor.isUseBindTimeTypeForMapping());
120     }
121     
122     public void testBindTimeTypeWrite() throws Exception {
123         StringWriter out = new StringWriter();
124         out.write("<?xml version='1.0'?>");
125         
126         Animals animals = new Animals();
127         animals.addAnimal(new FerretBean("albino", "Lector"));
128         animals.addAnimal(new CatBean("Sam", "black"));
129         animals.addAnimal(new DogBean("Bobby"));
130        
131         BeanWriter writer = new BeanWriter(out);
132         writer.getXMLIntrospector().getConfiguration()
133         		.setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE);
134         writer.getXMLIntrospector().getConfiguration()
135         		.setWrapCollectionsInElement(false);
136         writer.getBindingConfiguration().setMapIDs(false);
137         writer.write(animals);
138         
139         String expected = "<?xml version='1.0'?>" +
140         		"<Animals>" +
141         		"	<animal>" +
142         		"		<call>Dook</call><colour>albino</colour>" +
143         		"		<latinName>Mustela putoris furo</latinName><name>Lector</name>" +
144         		"	</animal>" +
145         		"	<animal>" +
146         		"		<call>Meow</call><colour>black</colour>" +
147         		"		<latinName>Felis catus</latinName><name>Sam</name>" +
148         		"	</animal>" +
149         		"	<animal>" +
150         		"		<breed>mongrol</breed><call>Woof</call><latinName>Canis familiaris</latinName>" +
151         		"		<name>Bobby</name><pedigree>false</pedigree>" +
152         		"	</animal>" +
153         		"</Animals>";
154         
155         xmlAssertIsomorphicContent(parseString(expected), parseString(out), true);
156     }
157     
158     public void testBindTimeTypeRead() throws Exception {
159         String xml = "<?xml version='1.0'?>" +
160 		"<Animals>" +
161 		"	<animal className='org.apache.commons.betwixt.io.read.FerretBean'>" +
162 		"		<call>Dook</call><colour>albino</colour>" +
163 		"		<latinName>Mustela putoris furo</latinName><name>Lector</name>" +
164 		"	</animal>" +
165 		"	<animal className='org.apache.commons.betwixt.io.read.CatBean'>" +
166 		"		<call>Meow</call><colour>black</colour>" +
167 		"		<latinName>Felis catus</latinName><name>Sam</name>" +
168 		"	</animal>" +
169 		"	<animal className='org.apache.commons.betwixt.io.read.DogBean'>" +
170 		"		<breed>mongrol</breed><call>Woof</call><latinName>Canis familiaris</latinName>" +
171 		"		<name>Bobby</name><pedigree>false</pedigree>" +
172 		"	</animal>" +
173 		"</Animals>";
174         
175         BeanReader reader = new BeanReader();
176         reader.getXMLIntrospector().getConfiguration()
177         		.setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE);
178         reader.getXMLIntrospector().getConfiguration()
179 			.setWrapCollectionsInElement(false);
180         reader.getBindingConfiguration().setMapIDs(false);
181         
182         reader.registerBeanClass(Animals.class);
183         Animals animals = (Animals) reader.parse(new StringReader(xml));
184         assertEquals("Expexted three animals", 3, animals.size()); 
185         Iterator it=animals.getAnimals(); 
186         Animal animalOne = (Animal) it.next();
187         assertTrue("Expected ferret", animalOne instanceof FerretBean);
188         FerretBean ferretBean = (FerretBean) animalOne;
189         assertEquals("Latin name property mapped", "Mustela putoris furo" , ferretBean.getLatinName());
190         assertEquals("Call property mapped", "Dook" , ferretBean.getCall());
191         assertEquals("Colour property mapped", "albino", ferretBean.getColour());
192         assertEquals("Name property mapped", "Lector", ferretBean.getName());
193         Animal animalTwo = (Animal) it.next();
194         assertTrue("Expected cat", animalTwo instanceof CatBean);
195         CatBean catBean = (CatBean) animalTwo;
196         assertEquals("Latin name property mapped", "Felis catus" , catBean.getLatinName());
197         assertEquals("Call property mapped", "Meow" , catBean.getCall());
198         assertEquals("Colour property mapped", "black", catBean.getColour());
199         assertEquals("Name property mapped", "Sam", catBean.getName());
200         Animal animalThree = (Animal) it.next();
201         assertTrue("Expected dog", animalThree instanceof DogBean);
202         DogBean dogBean = (DogBean) animalThree;
203         assertEquals("Latin name property mapped", "Canis familiaris" , dogBean.getLatinName());
204         assertEquals("Call property mapped", "Woof" , dogBean.getCall());
205         assertEquals("Breed property mapped", "mongrol", dogBean.getBreed());
206         assertEquals("Name property mapped", "Bobby", dogBean.getName());
207     }
208     
209     public void testIntrospectionTimeTypeWrite() throws Exception {
210         StringWriter out = new StringWriter();
211         out.write("<?xml version='1.0'?>");
212         
213         Animals animals = new Animals();
214         animals.addAnimal(new FerretBean("albino", "Lector"));
215         animals.addAnimal(new CatBean("Sam", "black"));
216         animals.addAnimal(new DogBean("Bobby"));
217        
218         BeanWriter writer = new BeanWriter(out);
219         writer.getXMLIntrospector().getConfiguration()
220         		.setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
221         writer.getXMLIntrospector().getConfiguration()
222         		.setWrapCollectionsInElement(false);
223         writer.getBindingConfiguration().setMapIDs(false);
224         writer.write(animals);
225         
226         String expected = "<?xml version='1.0'?><Animals>" +
227         		"	<animal><call>Dook</call><latinName>Mustela putoris furo</latinName></animal>" +
228         		"	<animal><call>Meow</call><latinName>Felis catus</latinName></animal>" +
229         		"	<animal><call>Woof</call><latinName>Canis familiaris</latinName></animal>" +
230         		"</Animals>";
231         
232         xmlAssertIsomorphicContent(parseString(expected), parseString(out), true);        
233     }
234     
235     public void testIntrospectionTimeTypeRead() throws Exception {
236         String xml = "<?xml version='1.0'?>" +
237 		"<Animals>" +
238 		"	<animal className='org.apache.commons.betwixt.io.read.FerretBean'>" +
239 		"		<call>Dook</call><colour>albino</colour>" +
240 		"		<latinName>Mustela putoris furo</latinName><name>Lector</name>" +
241 		"	</animal>" +
242 		"	<animal className='org.apache.commons.betwixt.io.read.CatBean'>" +
243 		"		<call>Meow</call><colour>black</colour>" +
244 		"		<latinName>Felis catus</latinName><name>Sam</name>" +
245 		"	</animal>" +
246 		"	<animal className='org.apache.commons.betwixt.io.read.DogBean'>" +
247 		"		<breed>mongrol</breed><call>Woof</call><latinName>Canis familiaris</latinName>" +
248 		"		<name>Bobby</name><pedigree>false</pedigree>" +
249 		"	</animal>" +
250 		"</Animals>";
251         
252         BeanReader reader = new BeanReader();
253         reader.getXMLIntrospector().getConfiguration()
254         		.setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
255         reader.getXMLIntrospector().getConfiguration()
256 			.setWrapCollectionsInElement(false);
257         reader.getBindingConfiguration().setMapIDs(false);
258         
259         reader.registerBeanClass(Animals.class);
260         Animals animals = (Animals) reader.parse(new StringReader(xml));
261         assertEquals("Expexted three animals", 3, animals.size()); 
262         Iterator it=animals.getAnimals(); 
263         Animal animalOne = (Animal) it.next();
264         assertTrue("Expected ferret", animalOne instanceof FerretBean);
265         FerretBean ferretBean = (FerretBean) animalOne;
266         assertEquals("Latin name property mapped", "Mustela putoris furo" , ferretBean.getLatinName());
267         assertEquals("Call property mapped", "Dook" , ferretBean.getCall());
268         assertNull("Colour property not mapped", ferretBean.getColour());
269         assertNull("Name property not mapped", ferretBean.getName());
270         Animal animalTwo = (Animal) it.next();
271         assertTrue("Expected cat", animalTwo instanceof CatBean);
272         CatBean catBean = (CatBean) animalTwo;
273         assertEquals("Latin name property mapped", "Felis catus" , catBean.getLatinName());
274         assertEquals("Call property mapped", "Meow" , catBean.getCall());
275         assertNull("Colour property not mapped", catBean.getColour());
276         assertNull("Name property not mapped", catBean.getName());
277         Animal animalThree = (Animal) it.next();
278         assertTrue("Expected dog", animalThree instanceof DogBean);
279         DogBean dogBean = (DogBean) animalThree;
280         assertEquals("Latin name property mapped", "Canis familiaris" , dogBean.getLatinName());
281         assertEquals("Call property mapped", "Woof" , dogBean.getCall());
282         assertNull("Breed property not mapped", dogBean.getBreed());
283         assertNull("Name property not mapped", dogBean.getName());
284     }
285 }