1 16 package org.apache.commons.betwixt; 17 18 import java.io.StringWriter ; 19 20 import junit.framework.Test; 21 import junit.framework.TestSuite; 22 import junit.textui.TestRunner; 23 24 import org.apache.commons.beanutils.BasicDynaClass; 25 import org.apache.commons.beanutils.DynaBean; 26 import org.apache.commons.beanutils.DynaClass; 27 import org.apache.commons.beanutils.DynaProperty; 28 import org.apache.commons.betwixt.io.BeanWriter; 29 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; 30 31 36 public class TestDynaBeanSupport extends AbstractTestCase { 37 38 public static void main( String [] args ) { 39 TestRunner.run( suite() ); 40 } 41 42 public static Test suite() { 43 return new TestSuite(TestDynaBeanSupport.class); 44 } 45 46 public TestDynaBeanSupport(String testName) { 47 super(testName); 48 } 49 50 public void testIntrospectDynaBean() throws Exception 51 { 52 XMLIntrospector introspector = new XMLIntrospector(); 53 introspector.getConfiguration().setAttributesForPrimitives(false); 54 XMLBeanInfo beanInfo = introspector.introspect(createDynasaurClass()); 55 ElementDescriptor baseElement = beanInfo.getElementDescriptor(); 56 assertEquals("Correct number of attributes", 0, baseElement.getAttributeDescriptors().length); 58 ElementDescriptor[] descriptors = baseElement.getElementDescriptors(); 59 assertEquals("Correct number of elements", 3, descriptors.length); 60 61 boolean matchedSpecies = false; 62 boolean matchedIsRaptor = false; 63 boolean matchedPeriod = false; 64 65 for (int i=0, size = descriptors.length; i< size; i++) { 66 if ("Species".equals(descriptors[i].getPropertyName())) { 67 matchedSpecies = true; 68 } 69 70 if ("isRaptor".equals(descriptors[i].getPropertyName())) { 71 matchedIsRaptor = true; 72 } 73 74 if ("Period".equals(descriptors[i].getPropertyName())) { 75 matchedPeriod = true; 76 } 77 } 78 79 assertTrue("Species descriptor not found", matchedSpecies); 80 assertTrue("isRaptor descriptor not found", matchedIsRaptor); 81 assertTrue("Period descriptor not found", matchedPeriod); 82 } 83 84 public void testWriteDynaBean() throws Exception { 85 DynaBean dynasaur = createDynasaurClass().newInstance(); 86 dynasaur.set("Species", "Allosaurus"); 87 dynasaur.set("isRaptor", Boolean.TRUE); 88 dynasaur.set("Period", "Jurassic"); 89 90 StringWriter out = new StringWriter (); 91 out.write("<?xml version='1.0'?>"); 92 BeanWriter writer = new BeanWriter(out); 93 writer.getBindingConfiguration().setMapIDs(false); 94 writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new DecapitalizeNameMapper()); 95 writer.write(dynasaur); 96 97 String xml = "<?xml version='1.0'?><dynasaur><species>Allosaurus</species>" 98 + "<isRaptor>true</isRaptor><period>Jurassic</period></dynasaur>"; 99 100 xmlAssertIsomorphicContent( 101 "Test write dyna beans", 102 parseString(xml), 103 parseString(out.getBuffer().toString()), 104 true); 105 } 106 107 public void testOverrideWithDotBetwixt() throws Exception { 108 DynaWithDotBetwixt bean = new DynaWithDotBetwixt("Tweedledum","Tweedledee"); 109 StringWriter out = new StringWriter (); 110 out.write("<?xml version='1.0'?>"); 111 BeanWriter writer = new BeanWriter(out); 112 writer.getBindingConfiguration().setMapIDs(false); 113 writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new DecapitalizeNameMapper()); 114 writer.write("bean", bean); 115 116 String xml = "<?xml version='1.0'?><bean><ndp>Tweedledum</ndp></bean>"; 117 xmlAssertIsomorphicContent( 118 "Test write dyna beans with dt betwixt", 119 parseString(xml), 120 parseString(out.getBuffer().toString()), 121 true); 122 123 } 124 125 private DynaClass createDynasaurClass() throws Exception { 126 DynaClass dynaClass = new BasicDynaClass 127 ("Dynasaur", null, 128 new DynaProperty[]{ 129 new DynaProperty("Species", String .class), 130 new DynaProperty("isRaptor", Boolean.TYPE), 131 new DynaProperty("Period", String .class), 132 }); 133 return (dynaClass); 134 } 135 } 136 137 138 | Popular Tags |