1 16 17 18 package org.apache.commons.betwixt; 19 20 import java.io.StringReader ; 21 import java.io.StringWriter ; 22 import java.util.Iterator ; 23 24 import org.apache.commons.betwixt.io.BeanReader; 25 import org.apache.commons.betwixt.io.BeanWriter; 26 import org.apache.commons.betwixt.strategy.CapitalizeNameMapper; 27 28 32 public class TestCollectives extends AbstractTestCase{ 33 34 private IntrospectionConfiguration categoriesIntrospectionConfiguration = new IntrospectionConfiguration(); 35 private BindingConfiguration noIDsBindingConfiguration = new BindingConfiguration(); 36 37 public TestCollectives(String testName) { 38 super(testName); 39 40 CapitalizeNameMapper capitalizeNameMapper = new CapitalizeNameMapper(); 41 categoriesIntrospectionConfiguration.setAttributesForPrimitives(false); 42 categoriesIntrospectionConfiguration.setElementNameMapper(capitalizeNameMapper); 43 categoriesIntrospectionConfiguration.setAttributeNameMapper(capitalizeNameMapper); 44 categoriesIntrospectionConfiguration.setWrapCollectionsInElement(false); 45 46 noIDsBindingConfiguration.setMapIDs(false); 47 } 48 49 50 public void testWriteCategories() throws Exception { 51 StringWriter out = new StringWriter (); 52 out.write("<?xml version='1.0'?>"); 53 BeanWriter writer = new BeanWriter(out); 54 writer.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration); 55 writer.setBindingConfiguration(noIDsBindingConfiguration); 56 57 Categories categories = new Categories(); 58 categories.addCategory(new Category("Runs")); 59 categories.addCategory(new Category("Innings")); 60 categories.addCategory(new Category("Dismissals")); 61 categories.addCategory(new Category("High Score")); 62 categories.addCategory(new Category("Average")); 63 64 writer.write(categories); 65 66 String xml = out.getBuffer().toString(); 67 String expected = "<?xml version='1.0'?><Categories>" + 68 "<Category><Name>Runs</Name></Category>" + 69 "<Category><Name>Innings</Name></Category>" + 70 "<Category><Name>Dismissals</Name></Category>" + 71 "<Category><Name>High Score</Name></Category>" + 72 "<Category><Name>Average</Name></Category>" + 73 "</Categories>"; 74 75 xmlAssertIsomorphicContent(parseString(expected), parseString(xml)); 76 } 77 78 public void testReadCategories() throws Exception { 79 BeanReader beanReader = new BeanReader(); 80 beanReader.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration); 81 beanReader.setBindingConfiguration(noIDsBindingConfiguration); 82 beanReader.registerBeanClass(Categories.class); 83 84 String xml = "<?xml version='1.0'?><Categories>" + 85 "<Category><Name>Runs</Name></Category>" + 86 "<Category><Name>Innings</Name></Category>" + 87 "<Category><Name>Dismissals</Name></Category>" + 88 "<Category><Name>High Score</Name></Category>" + 89 "<Category><Name>Average</Name></Category>" + 90 "</Categories>"; 91 92 StringReader in = new StringReader (xml); 93 94 Categories bean = (Categories) beanReader.parse(in); 95 96 assertEquals("5 categories", 5, bean.size()); 97 98 Iterator it = bean.getCategories(); 99 assertEquals("Runs category", new Category("Runs"), it.next()); 100 assertEquals("Runs category", new Category("Innings"), it.next()); 101 assertEquals("Runs category", new Category("Dismissals"), it.next()); 102 assertEquals("Runs category", new Category("High Score"), it.next()); 103 assertEquals("Runs category", new Category("Average"), it.next()); 104 105 } 106 107 108 } 109 | Popular Tags |