From commons-dev-return-59524-apmail-jakarta-commons-dev-archive=jakarta.apache.org@jakarta.apache.org Thu Sep 23 21:49:06 2004 Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 8265 invoked from network); 23 Sep 2004 21:49:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 23 Sep 2004 21:49:05 -0000 Received: (qmail 15356 invoked by uid 500); 23 Sep 2004 21:50:03 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 14976 invoked by uid 500); 23 Sep 2004 21:49:59 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 14851 invoked by uid 500); 23 Sep 2004 21:49:57 -0000 Received: (qmail 14795 invoked by uid 99); 23 Sep 2004 21:49:56 -0000 X-ASF-Spam-Status: No, hits=-10.0 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Thu, 23 Sep 2004 14:49:15 -0700 Received: (qmail 7021 invoked by uid 1289); 23 Sep 2004 21:46:44 -0000 Date: 23 Sep 2004 21:46:44 -0000 Message-ID: <20040923214644.7020.qmail@minotaur.apache.org> From: rdonkin@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/io/read TestBindTimeTypeMapping.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N rdonkin 2004/09/23 14:46:44 Added: betwixt/src/test/org/apache/commons/betwixt/io/read TestBindTimeTypeMapping.java Log: Improved support for derived beans. Revision Changes Path 1.1 jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/io/read/TestBindTimeTypeMapping.java Index: TestBindTimeTypeMapping.java =================================================================== /* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.betwixt.io.read; import java.io.StringReader; import java.io.StringWriter; import java.util.Iterator; import org.apache.commons.betwixt.AbstractTestCase; import org.apache.commons.betwixt.ElementDescriptor; import org.apache.commons.betwixt.XMLBeanInfo; import org.apache.commons.betwixt.XMLIntrospector; import org.apache.commons.betwixt.io.BeanReader; import org.apache.commons.betwixt.io.BeanWriter; import org.apache.commons.betwixt.strategy.MappingDerivationStrategy; import org.xml.sax.InputSource; /** * @author Jakarta Commons Team, Apache Software Foundation */ public class TestBindTimeTypeMapping extends AbstractTestCase { public TestBindTimeTypeMapping(String testName) { super(testName); } //todo: note to self need to consider how the design of the introspection will work when you have strategy which takes // singular property which is not know until after the digestion public void testDefaultMappingDerivationStrategy() throws Exception { XMLIntrospector xmlIntrospector = new XMLIntrospector(); XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(Animals.class); ElementDescriptor animalsDescriptor = xmlBeanInfo.getElementDescriptor(); assertEquals("Use bind time type", true, animalsDescriptor.isUseBindTimeTypeForMapping()); ElementDescriptor animalDescriptor = animalsDescriptor.getElementDescriptors()[0]; assertEquals("Use bind time type", true, animalDescriptor.isUseBindTimeTypeForMapping()); } public void testIntrospectionTimeMappingDerivationStrategy() throws Exception { XMLIntrospector xmlIntrospector = new XMLIntrospector(); xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE); XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(Animals.class); ElementDescriptor animalsDescriptor = xmlBeanInfo.getElementDescriptor(); assertEquals("Use introspection time type", false, animalsDescriptor.isUseBindTimeTypeForMapping()); ElementDescriptor animalDescriptor = animalsDescriptor.getElementDescriptors()[0]; assertEquals("Use introspection time type", false, animalDescriptor.isUseBindTimeTypeForMapping()); } public void testBindTypeMappingDerivationStrategy() throws Exception { XMLIntrospector xmlIntrospector = new XMLIntrospector(); xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE); XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(Animals.class); ElementDescriptor animalsDescriptor = xmlBeanInfo.getElementDescriptor(); assertEquals("Use bind time type", true, animalsDescriptor.isUseBindTimeTypeForMapping()); ElementDescriptor animalDescriptor = animalsDescriptor.getElementDescriptors()[0]; assertEquals("Use bind time type", true, animalDescriptor.isUseBindTimeTypeForMapping()); } public void testBindTypeMappingDerivationDotBetwixt() throws Exception { String mappingDocument="" + "" + " " + "" + ""; XMLIntrospector xmlIntrospector = new XMLIntrospector(); xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE); XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(PetBean.class, new InputSource(new StringReader(mappingDocument))); ElementDescriptor petDescriptor = xmlBeanInfo.getElementDescriptor(); assertEquals("Use type from strategy", true, petDescriptor.isUseBindTimeTypeForMapping()); ElementDescriptor animalDescriptor = petDescriptor.getElementDescriptors()[0]; assertEquals("Use type from document", true, animalDescriptor.isUseBindTimeTypeForMapping()); } public void testIntrospectionTypeMappingDerivationDotBetwixt() throws Exception { String mappingDocument="" + "" + " " + "" + ""; XMLIntrospector xmlIntrospector = new XMLIntrospector(); xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE); XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(PetBean.class, new InputSource(new StringReader(mappingDocument))); ElementDescriptor petDescriptor = xmlBeanInfo.getElementDescriptor(); assertEquals("Use type from strategy", true, petDescriptor.isUseBindTimeTypeForMapping()); ElementDescriptor animalDescriptor = petDescriptor.getElementDescriptors()[0]; assertEquals("Use type from document", false, animalDescriptor.isUseBindTimeTypeForMapping()); } public void testMappingDerivationDotBetwixtAddDefaults() throws Exception { String mappingDocument="" + "" + " " + " " + "" + ""; XMLIntrospector xmlIntrospector = new XMLIntrospector(); xmlIntrospector.getConfiguration().setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE); XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(PetBean.class, new InputSource(new StringReader(mappingDocument))); ElementDescriptor petDescriptor = xmlBeanInfo.getElementDescriptor(); assertEquals("Use type from strategy", true, petDescriptor.isUseBindTimeTypeForMapping()); ElementDescriptor animalDescriptor = petDescriptor.getElementDescriptors()[0]; assertEquals("Use type from document", false, animalDescriptor.isUseBindTimeTypeForMapping()); ElementDescriptor personDescriptor = petDescriptor.getElementDescriptors()[1]; assertEquals("Use type from document", true, personDescriptor.isUseBindTimeTypeForMapping()); } public void testBindTimeTypeWrite() throws Exception { StringWriter out = new StringWriter(); out.write(""); Animals animals = new Animals(); animals.addAnimal(new FerretBean("albino", "Lector")); animals.addAnimal(new CatBean("Sam", "black")); animals.addAnimal(new DogBean("Bobby")); BeanWriter writer = new BeanWriter(out); writer.getXMLIntrospector().getConfiguration() .setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE); writer.getXMLIntrospector().getConfiguration() .setWrapCollectionsInElement(false); writer.getBindingConfiguration().setMapIDs(false); writer.write(animals); String expected = "" + "" + " " + " Dookalbino" + " Mustela putoris furoLector" + " " + " " + " Meowblack" + " Felis catusSam" + " " + " " + " mongrolWoofCanis familiaris" + " Bobbyfalse" + " " + ""; xmlAssertIsomorphicContent(parseString(expected), parseString(out)); } public void testBindTimeTypeRead() throws Exception { String xml = "" + "" + " " + " Dookalbino" + " Mustela putoris furoLector" + " " + " " + " Meowblack" + " Felis catusSam" + " " + " " + " mongrolWoofCanis familiaris" + " Bobbyfalse" + " " + ""; BeanReader reader = new BeanReader(); reader.getXMLIntrospector().getConfiguration() .setMappingDerivationStrategy(MappingDerivationStrategy.USE_BIND_TIME_TYPE); reader.getXMLIntrospector().getConfiguration() .setWrapCollectionsInElement(false); reader.getBindingConfiguration().setMapIDs(false); reader.registerBeanClass(Animals.class); Animals animals = (Animals) reader.parse(new StringReader(xml)); assertEquals("Expexted three animals", 3, animals.size()); Iterator it=animals.getAnimals(); Animal animalOne = (Animal) it.next(); assertTrue("Expected ferret", animalOne instanceof FerretBean); FerretBean ferretBean = (FerretBean) animalOne; assertEquals("Latin name property mapped", "Mustela putoris furo" , ferretBean.getLatinName()); assertEquals("Call property mapped", "Dook" , ferretBean.getCall()); assertEquals("Colour property mapped", "albino", ferretBean.getColour()); assertEquals("Name property mapped", "Lector", ferretBean.getName()); Animal animalTwo = (Animal) it.next(); assertTrue("Expected cat", animalTwo instanceof CatBean); CatBean catBean = (CatBean) animalTwo; assertEquals("Latin name property mapped", "Felis catus" , catBean.getLatinName()); assertEquals("Call property mapped", "Meow" , catBean.getCall()); assertEquals("Colour property mapped", "black", catBean.getColour()); assertEquals("Name property mapped", "Sam", catBean.getName()); Animal animalThree = (Animal) it.next(); assertTrue("Expected dog", animalThree instanceof DogBean); DogBean dogBean = (DogBean) animalThree; assertEquals("Latin name property mapped", "Canis familiaris" , dogBean.getLatinName()); assertEquals("Call property mapped", "Woof" , dogBean.getCall()); assertEquals("Breed property mapped", "mongrol", dogBean.getBreed()); assertEquals("Name property mapped", "Bobby", dogBean.getName()); } public void testIntrospectionTimeTypeWrite() throws Exception { StringWriter out = new StringWriter(); out.write(""); Animals animals = new Animals(); animals.addAnimal(new FerretBean("albino", "Lector")); animals.addAnimal(new CatBean("Sam", "black")); animals.addAnimal(new DogBean("Bobby")); BeanWriter writer = new BeanWriter(out); writer.getXMLIntrospector().getConfiguration() .setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE); writer.getXMLIntrospector().getConfiguration() .setWrapCollectionsInElement(false); writer.getBindingConfiguration().setMapIDs(false); writer.write(animals); String expected = "" + " DookMustela putoris furo" + " MeowFelis catus" + " WoofCanis familiaris" + ""; xmlAssertIsomorphicContent(parseString(expected), parseString(out)); } public void testIntrospectionTimeTypeRead() throws Exception { String xml = "" + "" + " " + " Dookalbino" + " Mustela putoris furoLector" + " " + " " + " Meowblack" + " Felis catusSam" + " " + " " + " mongrolWoofCanis familiaris" + " Bobbyfalse" + " " + ""; BeanReader reader = new BeanReader(); reader.getXMLIntrospector().getConfiguration() .setMappingDerivationStrategy(MappingDerivationStrategy.USE_INTROSPECTION_TIME_TYPE); reader.getXMLIntrospector().getConfiguration() .setWrapCollectionsInElement(false); reader.getBindingConfiguration().setMapIDs(false); reader.registerBeanClass(Animals.class); Animals animals = (Animals) reader.parse(new StringReader(xml)); assertEquals("Expexted three animals", 3, animals.size()); Iterator it=animals.getAnimals(); Animal animalOne = (Animal) it.next(); assertTrue("Expected ferret", animalOne instanceof FerretBean); FerretBean ferretBean = (FerretBean) animalOne; assertEquals("Latin name property mapped", "Mustela putoris furo" , ferretBean.getLatinName()); assertEquals("Call property mapped", "Dook" , ferretBean.getCall()); assertNull("Colour property not mapped", ferretBean.getColour()); assertNull("Name property not mapped", ferretBean.getName()); Animal animalTwo = (Animal) it.next(); assertTrue("Expected cat", animalTwo instanceof CatBean); CatBean catBean = (CatBean) animalTwo; assertEquals("Latin name property mapped", "Felis catus" , catBean.getLatinName()); assertEquals("Call property mapped", "Meow" , catBean.getCall()); assertNull("Colour property not mapped", catBean.getColour()); assertNull("Name property not mapped", catBean.getName()); Animal animalThree = (Animal) it.next(); assertTrue("Expected dog", animalThree instanceof DogBean); DogBean dogBean = (DogBean) animalThree; assertEquals("Latin name property mapped", "Canis familiaris" , dogBean.getLatinName()); assertEquals("Call property mapped", "Woof" , dogBean.getCall()); assertNull("Breed property not mapped", dogBean.getBreed()); assertNull("Name property not mapped", dogBean.getName()); } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org