1 16 17 package org.apache.commons.betwixt.dotbetwixt; 18 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 import java.util.List ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 import org.apache.commons.betwixt.io.BeanReader; 27 import org.apache.commons.betwixt.io.BeanWriter; 28 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; 29 import org.apache.commons.betwixt.xmlunit.XmlTestCase; 30 31 36 public class TestXmlToBean extends XmlTestCase { 37 38 40 public static Test suite() { 41 return new TestSuite(TestXmlToBean.class); 42 } 43 44 46 public TestXmlToBean(String testName) { 47 super(testName); 48 } 49 50 52 public void _testCustomUpdaters() throws Exception { 53 MixedUpdatersBean bean = new MixedUpdatersBean("Lov"); 55 bean.badNameSetter("Hate"); 56 bean.addItem("White"); 57 bean.badItemAdder("Black"); 58 bean.addItem("Life"); 59 bean.badItemAdder("Death"); 60 61 64 StringWriter out = new StringWriter (); 65 out.write("<?xml version='1.0'?>"); 66 BeanWriter writer = new BeanWriter(out); 67 69 73 writer.getBindingConfiguration().setMapIDs(false); 74 writer.write(bean); 75 76 String xml = "<?xml version='1.0'?><mixed><name>Lov</name><bad-name>Hate</bad-name>" 77 + "<items><item>White</item><item>Life</item></items>" 78 + "<bad-items><bad-item>Black</bad-item><bad-item>Death</bad-item></bad-items></mixed>"; 79 80 xmlAssertIsomorphicContent( 81 parseString(xml), 82 parseString(out.toString()), 83 true); 84 85 89 93 97 BeanReader reader = new BeanReader(); 99 reader.getBindingConfiguration().setMapIDs(false); 100 reader.registerBeanClass("mixed", MixedUpdatersBean.class); 101 bean = (MixedUpdatersBean) reader.parse(new StringReader (xml)); 102 103 assertEquals("Name incorrect", "Lov", bean.getName()); 104 assertEquals("BadName incorrect", "Hate", bean.getBadName()); 105 List items = bean.getItems(); 106 assertEquals("Wrong number of items", 2, items.size()); 107 assertEquals("Item one wrong", "White", items.get(0)); 108 assertEquals("Item two wrong", "Life", items.get(1)); 109 List badItems = bean.getBadItems(); 110 assertEquals("Wrong number of bad items", 2, badItems.size()); 111 assertEquals("Bad item one wrong", "Black", badItems.get(0)); 112 assertEquals("Bad item two wrong", "Death", badItems.get(1)); 113 114 } 115 116 117 118 public void testMixedContent() throws Exception { 119 120 StringReader xml = new StringReader ( 121 "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life' gamma='42'>" 122 + "The Universe And Everything</deep-thought>"); 123 124 130 BeanReader reader = new BeanReader(); 131 reader.registerBeanClass(MixedContentOne.class); 133 Object resultObject = reader.parse(xml); 134 assertEquals("Object is MixedContentOne", true, resultObject instanceof MixedContentOne); 135 MixedContentOne result = (MixedContentOne) resultObject; 136 assertEquals("Property Alpha matches", "Life", result.getAlpha()); 137 assertEquals("Property Beta matches", "The Universe And Everything", result.getBeta()); 138 assertEquals("Property Gamma matches", 42, result.getGamma()); 139 } 140 141 142 143 public void _testBasicInterfaceImpl() throws Exception { 144 150 ExampleBean bean = new ExampleBean("Alice"); 151 bean.addExample(new ExampleImpl(1, "Mad Hatter")); 152 bean.addExample(new ExampleImpl(2, "March Hare")); 153 bean.addExample(new ExampleImpl(3, "Dormouse")); 154 155 String xml = "<?xml version='1.0' encoding='UTF-8'?>" 156 + "<example-bean><name>Alice</name>" 157 + "<example><id>1</id><name>Mad Hatter</name></example>" 158 + "<example><id>2</id><name>March Hare</name></example>" 159 + "<example><id>3</id><name>Dormouse</name></example>" 160 + "</example-bean>"; 161 162 163 BeanReader reader = new BeanReader(); 164 reader.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper()); 166 reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false); 167 reader.registerBeanClass( ExampleBean.class ); 168 169 StringReader in = new StringReader ( xml ); 170 ExampleBean out = (ExampleBean) reader.parse( in ); 171 assertEquals("Interface read failed", bean, out); 172 173 } 174 } 175 176 | Popular Tags |