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.ElementDescriptor;
025: import org.apache.commons.betwixt.XMLBeanInfo;
026: import org.apache.commons.betwixt.XMLIntrospector;
027: import org.apache.commons.betwixt.io.BeanReader;
028: import org.apache.commons.betwixt.io.BeanWriter;
029: import org.apache.commons.betwixt.strategy.MappingDerivationStrategy;
030: import org.xml.sax.InputSource;
031:
032: /**
033: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
034: */
035: public class TestBindTimeTypeMapping extends AbstractTestCase {
036:
037: public TestBindTimeTypeMapping(String testName) {
038: super (testName);
039: }
040:
041: //todo: note to self need to consider how the design of the introspection will work when you have strategy which takes
042: // singular property which is not know until after the digestion
043:
044: public void testDefaultMappingDerivationStrategy() throws Exception {
045: XMLIntrospector xmlIntrospector = new XMLIntrospector();
046: XMLBeanInfo xmlBeanInfo = xmlIntrospector
047: .introspect(Animals.class);
048: ElementDescriptor animalsDescriptor = xmlBeanInfo
049: .getElementDescriptor();
050: assertEquals("Use bind time type", true, animalsDescriptor
051: .isUseBindTimeTypeForMapping());
052: ElementDescriptor animalDescriptor = animalsDescriptor
053: .getElementDescriptors()[0];
054: assertEquals("Use bind time type", true, animalDescriptor
055: .isUseBindTimeTypeForMapping());
056: }
057:
058: public void testIntrospectionTimeMappingDerivationStrategy()
059: throws Exception {
060: XMLIntrospector xmlIntrospector = new XMLIntrospector();
061: xmlIntrospector
062: .getConfiguration()
063: .setMappingDerivationStrategy(
064: MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
065: XMLBeanInfo xmlBeanInfo = xmlIntrospector
066: .introspect(Animals.class);
067: ElementDescriptor animalsDescriptor = xmlBeanInfo
068: .getElementDescriptor();
069: assertEquals("Use introspection time type", false,
070: animalsDescriptor.isUseBindTimeTypeForMapping());
071: ElementDescriptor animalDescriptor = animalsDescriptor
072: .getElementDescriptors()[0];
073: assertEquals("Use introspection time type", false,
074: animalDescriptor.isUseBindTimeTypeForMapping());
075: }
076:
077: public void testBindTypeMappingDerivationStrategy()
078: throws Exception {
079: XMLIntrospector xmlIntrospector = new XMLIntrospector();
080: xmlIntrospector.getConfiguration()
081: .setMappingDerivationStrategy(
082: MappingDerivationStrategy.USE_BIND_TIME_TYPE);
083: XMLBeanInfo xmlBeanInfo = xmlIntrospector
084: .introspect(Animals.class);
085: ElementDescriptor animalsDescriptor = xmlBeanInfo
086: .getElementDescriptor();
087: assertEquals("Use bind time type", true, animalsDescriptor
088: .isUseBindTimeTypeForMapping());
089: ElementDescriptor animalDescriptor = animalsDescriptor
090: .getElementDescriptors()[0];
091: assertEquals("Use bind time type", true, animalDescriptor
092: .isUseBindTimeTypeForMapping());
093: }
094:
095: public void testBindTypeMappingDerivationDotBetwixt()
096: throws Exception {
097: String mappingDocument = "<?xml version='1.0'?><info>"
098: + "<element name='pet-record'>"
099: + " <element name='pet' property='pet' mappingDerivation='bind'/>"
100: + "</element>" + "</info>";
101: XMLIntrospector xmlIntrospector = new XMLIntrospector();
102: xmlIntrospector
103: .getConfiguration()
104: .setMappingDerivationStrategy(
105: MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
106: XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(
107: PetBean.class, new InputSource(new StringReader(
108: mappingDocument)));
109: ElementDescriptor petDescriptor = xmlBeanInfo
110: .getElementDescriptor();
111: assertEquals("Use type from strategy", true, petDescriptor
112: .isUseBindTimeTypeForMapping());
113: ElementDescriptor animalDescriptor = petDescriptor
114: .getElementDescriptors()[0];
115: assertEquals("Use type from document", true, animalDescriptor
116: .isUseBindTimeTypeForMapping());
117: }
118:
119: public void testIntrospectionTypeMappingDerivationDotBetwixt()
120: throws Exception {
121: String mappingDocument = "<?xml version='1.0'?><info>"
122: + "<element name='pet-record'>"
123: + " <element name='pet' property='pet' mappingDerivation='introspection'/>"
124: + "</element>" + "</info>";
125: XMLIntrospector xmlIntrospector = new XMLIntrospector();
126: xmlIntrospector.getConfiguration()
127: .setMappingDerivationStrategy(
128: MappingDerivationStrategy.USE_BIND_TIME_TYPE);
129: XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(
130: PetBean.class, new InputSource(new StringReader(
131: mappingDocument)));
132: ElementDescriptor petDescriptor = xmlBeanInfo
133: .getElementDescriptor();
134: assertEquals("Use type from strategy", true, petDescriptor
135: .isUseBindTimeTypeForMapping());
136: ElementDescriptor animalDescriptor = petDescriptor
137: .getElementDescriptors()[0];
138: assertEquals("Use type from document", false, animalDescriptor
139: .isUseBindTimeTypeForMapping());
140: }
141:
142: public void testMappingDerivationDotBetwixtAddDefaults()
143: throws Exception {
144: String mappingDocument = "<?xml version='1.0'?><info>"
145: + "<element name='pet-record'>"
146: + " <element name='pet' property='pet' mappingDerivation='introspection'/>"
147: + " <addDefaults/>" + "</element>" + "</info>";
148: XMLIntrospector xmlIntrospector = new XMLIntrospector();
149: xmlIntrospector.getConfiguration()
150: .setMappingDerivationStrategy(
151: MappingDerivationStrategy.USE_BIND_TIME_TYPE);
152: XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(
153: PetBean.class, new InputSource(new StringReader(
154: mappingDocument)));
155: ElementDescriptor petDescriptor = xmlBeanInfo
156: .getElementDescriptor();
157: assertEquals("Use type from strategy", true, petDescriptor
158: .isUseBindTimeTypeForMapping());
159: ElementDescriptor animalDescriptor = petDescriptor
160: .getElementDescriptors()[0];
161: assertEquals("Use type from document", false, animalDescriptor
162: .isUseBindTimeTypeForMapping());
163: ElementDescriptor personDescriptor = petDescriptor
164: .getElementDescriptors()[1];
165: assertEquals("Use type from document", true, personDescriptor
166: .isUseBindTimeTypeForMapping());
167: }
168:
169: public void testBindTimeTypeWrite() throws Exception {
170: StringWriter out = new StringWriter();
171: out.write("<?xml version='1.0'?>");
172:
173: Animals animals = new Animals();
174: animals.addAnimal(new FerretBean("albino", "Lector"));
175: animals.addAnimal(new CatBean("Sam", "black"));
176: animals.addAnimal(new DogBean("Bobby"));
177:
178: BeanWriter writer = new BeanWriter(out);
179: writer.getXMLIntrospector().getConfiguration()
180: .setMappingDerivationStrategy(
181: MappingDerivationStrategy.USE_BIND_TIME_TYPE);
182: writer.getXMLIntrospector().getConfiguration()
183: .setWrapCollectionsInElement(false);
184: writer.getBindingConfiguration().setMapIDs(false);
185: writer.write(animals);
186:
187: String expected = "<?xml version='1.0'?>"
188: + "<Animals>"
189: + " <animal>"
190: + " <call>Dook</call><colour>albino</colour>"
191: + " <latinName>Mustela putoris furo</latinName><name>Lector</name>"
192: + " </animal>"
193: + " <animal>"
194: + " <call>Meow</call><colour>black</colour>"
195: + " <latinName>Felis catus</latinName><name>Sam</name>"
196: + " </animal>"
197: + " <animal>"
198: + " <breed>mongrol</breed><call>Woof</call><latinName>Canis familiaris</latinName>"
199: + " <name>Bobby</name><pedigree>false</pedigree>"
200: + " </animal>" + "</Animals>";
201:
202: xmlAssertIsomorphicContent(parseString(expected),
203: parseString(out), true);
204: }
205:
206: public void testBindTimeTypeRead() throws Exception {
207: String xml = "<?xml version='1.0'?>"
208: + "<Animals>"
209: + " <animal className='org.apache.commons.betwixt.io.read.FerretBean'>"
210: + " <call>Dook</call><colour>albino</colour>"
211: + " <latinName>Mustela putoris furo</latinName><name>Lector</name>"
212: + " </animal>"
213: + " <animal className='org.apache.commons.betwixt.io.read.CatBean'>"
214: + " <call>Meow</call><colour>black</colour>"
215: + " <latinName>Felis catus</latinName><name>Sam</name>"
216: + " </animal>"
217: + " <animal className='org.apache.commons.betwixt.io.read.DogBean'>"
218: + " <breed>mongrol</breed><call>Woof</call><latinName>Canis familiaris</latinName>"
219: + " <name>Bobby</name><pedigree>false</pedigree>"
220: + " </animal>" + "</Animals>";
221:
222: BeanReader reader = new BeanReader();
223: reader.getXMLIntrospector().getConfiguration()
224: .setMappingDerivationStrategy(
225: MappingDerivationStrategy.USE_BIND_TIME_TYPE);
226: reader.getXMLIntrospector().getConfiguration()
227: .setWrapCollectionsInElement(false);
228: reader.getBindingConfiguration().setMapIDs(false);
229:
230: reader.registerBeanClass(Animals.class);
231: Animals animals = (Animals) reader.parse(new StringReader(xml));
232: assertEquals("Expexted three animals", 3, animals.size());
233: Iterator it = animals.getAnimals();
234: Animal animalOne = (Animal) it.next();
235: assertTrue("Expected ferret", animalOne instanceof FerretBean);
236: FerretBean ferretBean = (FerretBean) animalOne;
237: assertEquals("Latin name property mapped",
238: "Mustela putoris furo", ferretBean.getLatinName());
239: assertEquals("Call property mapped", "Dook", ferretBean
240: .getCall());
241: assertEquals("Colour property mapped", "albino", ferretBean
242: .getColour());
243: assertEquals("Name property mapped", "Lector", ferretBean
244: .getName());
245: Animal animalTwo = (Animal) it.next();
246: assertTrue("Expected cat", animalTwo instanceof CatBean);
247: CatBean catBean = (CatBean) animalTwo;
248: assertEquals("Latin name property mapped", "Felis catus",
249: catBean.getLatinName());
250: assertEquals("Call property mapped", "Meow", catBean.getCall());
251: assertEquals("Colour property mapped", "black", catBean
252: .getColour());
253: assertEquals("Name property mapped", "Sam", catBean.getName());
254: Animal animalThree = (Animal) it.next();
255: assertTrue("Expected dog", animalThree instanceof DogBean);
256: DogBean dogBean = (DogBean) animalThree;
257: assertEquals("Latin name property mapped", "Canis familiaris",
258: dogBean.getLatinName());
259: assertEquals("Call property mapped", "Woof", dogBean.getCall());
260: assertEquals("Breed property mapped", "mongrol", dogBean
261: .getBreed());
262: assertEquals("Name property mapped", "Bobby", dogBean.getName());
263: }
264:
265: public void testIntrospectionTimeTypeWrite() throws Exception {
266: StringWriter out = new StringWriter();
267: out.write("<?xml version='1.0'?>");
268:
269: Animals animals = new Animals();
270: animals.addAnimal(new FerretBean("albino", "Lector"));
271: animals.addAnimal(new CatBean("Sam", "black"));
272: animals.addAnimal(new DogBean("Bobby"));
273:
274: BeanWriter writer = new BeanWriter(out);
275: writer
276: .getXMLIntrospector()
277: .getConfiguration()
278: .setMappingDerivationStrategy(
279: MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
280: writer.getXMLIntrospector().getConfiguration()
281: .setWrapCollectionsInElement(false);
282: writer.getBindingConfiguration().setMapIDs(false);
283: writer.write(animals);
284:
285: String expected = "<?xml version='1.0'?><Animals>"
286: + " <animal><call>Dook</call><latinName>Mustela putoris furo</latinName></animal>"
287: + " <animal><call>Meow</call><latinName>Felis catus</latinName></animal>"
288: + " <animal><call>Woof</call><latinName>Canis familiaris</latinName></animal>"
289: + "</Animals>";
290:
291: xmlAssertIsomorphicContent(parseString(expected),
292: parseString(out), true);
293: }
294:
295: public void testIntrospectionTimeTypeRead() throws Exception {
296: String xml = "<?xml version='1.0'?>"
297: + "<Animals>"
298: + " <animal className='org.apache.commons.betwixt.io.read.FerretBean'>"
299: + " <call>Dook</call><colour>albino</colour>"
300: + " <latinName>Mustela putoris furo</latinName><name>Lector</name>"
301: + " </animal>"
302: + " <animal className='org.apache.commons.betwixt.io.read.CatBean'>"
303: + " <call>Meow</call><colour>black</colour>"
304: + " <latinName>Felis catus</latinName><name>Sam</name>"
305: + " </animal>"
306: + " <animal className='org.apache.commons.betwixt.io.read.DogBean'>"
307: + " <breed>mongrol</breed><call>Woof</call><latinName>Canis familiaris</latinName>"
308: + " <name>Bobby</name><pedigree>false</pedigree>"
309: + " </animal>" + "</Animals>";
310:
311: BeanReader reader = new BeanReader();
312: reader
313: .getXMLIntrospector()
314: .getConfiguration()
315: .setMappingDerivationStrategy(
316: MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE);
317: reader.getXMLIntrospector().getConfiguration()
318: .setWrapCollectionsInElement(false);
319: reader.getBindingConfiguration().setMapIDs(false);
320:
321: reader.registerBeanClass(Animals.class);
322: Animals animals = (Animals) reader.parse(new StringReader(xml));
323: assertEquals("Expexted three animals", 3, animals.size());
324: Iterator it = animals.getAnimals();
325: Animal animalOne = (Animal) it.next();
326: assertTrue("Expected ferret", animalOne instanceof FerretBean);
327: FerretBean ferretBean = (FerretBean) animalOne;
328: assertEquals("Latin name property mapped",
329: "Mustela putoris furo", ferretBean.getLatinName());
330: assertEquals("Call property mapped", "Dook", ferretBean
331: .getCall());
332: assertNull("Colour property not mapped", ferretBean.getColour());
333: assertNull("Name property not mapped", ferretBean.getName());
334: Animal animalTwo = (Animal) it.next();
335: assertTrue("Expected cat", animalTwo instanceof CatBean);
336: CatBean catBean = (CatBean) animalTwo;
337: assertEquals("Latin name property mapped", "Felis catus",
338: catBean.getLatinName());
339: assertEquals("Call property mapped", "Meow", catBean.getCall());
340: assertNull("Colour property not mapped", catBean.getColour());
341: assertNull("Name property not mapped", catBean.getName());
342: Animal animalThree = (Animal) it.next();
343: assertTrue("Expected dog", animalThree instanceof DogBean);
344: DogBean dogBean = (DogBean) animalThree;
345: assertEquals("Latin name property mapped", "Canis familiaris",
346: dogBean.getLatinName());
347: assertEquals("Call property mapped", "Woof", dogBean.getCall());
348: assertNull("Breed property not mapped", dogBean.getBreed());
349: assertNull("Name property not mapped", dogBean.getName());
350: }
351: }
|