1 16 17 package org.apache.commons.betwixt.nowrap; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.StringWriter ; 22 import java.util.List ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 import org.apache.commons.betwixt.AbstractTestCase; 28 import org.apache.commons.betwixt.XMLIntrospector; 29 import org.apache.commons.betwixt.io.BeanReader; 30 import org.apache.commons.betwixt.io.BeanWriter; 31 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; 32 import org.apache.commons.betwixt.strategy.DefaultPluralStemmer; 33 34 40 public class TestNoWrap 41 extends AbstractTestCase 42 { 43 private POTest po; 44 45 48 public static Test suite() 49 { 50 return new TestSuite(TestNoWrap.class); 51 } 52 53 58 public TestNoWrap(String testName) 59 { 60 super(testName); 61 } 62 63 66 public void testRoundTrip() 67 throws Exception 68 { 69 load(); 70 write(); 71 } 72 73 76 public void load() 77 throws Exception 78 { 79 String xmlLocation = getTestFile("src/test/org/apache/commons/betwixt/nowrap/po_add_test.xml"); 80 81 FileInputStream in = new FileInputStream (new File (xmlLocation)); 82 83 BeanReader reader = createBeanReader(POTest.class); 85 po = (POTest) reader.parse(in); 86 assertEquals("PO Printing No", "555008805581", po.getPrintingNumber()); 87 List componentTests = po.getComponenttests(); 88 89 assertEquals("#Component tests", 3, componentTests.size()); 90 Componenttest testOne = (Componenttest) componentTests.get(0); 91 assertEquals("Component Test One", "Text", testOne.getCompDescription()); 92 Componenttest testTwo = (Componenttest) componentTests.get(1); 93 assertEquals("Component Test Two", "Binding", testTwo.getCompDescription()); 94 Componenttest testThree = (Componenttest) componentTests.get(2); 95 assertEquals("Component Test Three", "Paper Cover", testThree.getCompDescription()); 96 } 97 98 101 public void write() 102 throws Exception 103 { 104 StringWriter out = new StringWriter (); 106 out.write("<?xml version='1.0'?>"); 107 BeanWriter beanWriter = new BeanWriter(out); 108 beanWriter.setXMLIntrospector(createXMLIntrospector()); 109 beanWriter.getBindingConfiguration().setMapIDs(false); 110 beanWriter.enablePrettyPrint(); 111 112 beanWriter.write(po); 113 String xml = "<?xml version='1.0'?><content><printingno>555008805581</printingno>" 114 + "<componenttest><compdescription>Text</compdescription></componenttest>" 115 + "<componenttest><compdescription>Binding</compdescription></componenttest>" 116 + "<componenttest><compdescription>Paper Cover</compdescription>" 117 + "</componenttest></content>"; 118 119 xmlAssertIsomorphicContent( 120 parseString(xml), 121 parseString(out.getBuffer().toString()), 122 true); 123 } 124 125 128 131 protected BeanReader createBeanReader(Class beanClass) 132 throws Exception 133 { 134 BeanReader reader = new BeanReader(); 135 reader.setXMLIntrospector(createXMLIntrospector()); 136 reader.registerBeanClass(beanClass); 137 return reader; 138 } 139 140 145 protected XMLIntrospector createXMLIntrospector() 146 { 147 XMLIntrospector introspector = new XMLIntrospector(); 148 149 introspector.getConfiguration().setAttributesForPrimitives(false); 151 152 introspector.getConfiguration().setWrapCollectionsInElement(false); 154 155 introspector.getConfiguration().setElementNameMapper( new DecapitalizeNameMapper() ); 157 158 introspector.getConfiguration().setPluralStemmer( new DefaultPluralStemmer() ); 160 161 return introspector; 162 } 163 } 164 165 | Popular Tags |