1 16 17 package org.apache.commons.betwixt.strategy.alt; 18 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 import java.io.Writer ; 22 23 import junit.framework.TestCase; 24 25 import org.apache.commons.betwixt.XMLIntrospector; 26 import org.apache.commons.betwixt.io.BeanReader; 27 import org.apache.commons.betwixt.io.BeanWriter; 28 import org.apache.commons.betwixt.strategy.CapitalizeNameMapper; 29 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; 30 import org.apache.commons.betwixt.strategy.DefaultNameMapper; 31 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; 32 import org.apache.commons.betwixt.strategy.NameMapper; 33 34 35 43 public class TestElementsAlt extends TestCase { 44 45 47 public TestElementsAlt(String name) { 48 super(name); 49 } 52 53 public void testCapitalizeNameMapper() throws Exception { 54 doTest(new CapitalizeNameMapper(), "capitalize name mapper"); 56 } 57 58 public void testDecapitalizeNameMapper() throws Exception { 59 doTest(new DecapitalizeNameMapper(), "decapitalize name mapper"); 61 } 62 63 public void testDefaultElementMapper() throws Exception { 64 doTest(new DefaultNameMapper(), "default name mapper"); 66 } 67 68 public void testHyphenatedNameMapper() throws Exception { 69 doTest(new HyphenatedNameMapper(), "hyphenated name mapper"); 71 } 72 73 private void doTest(NameMapper mapper, String testName) throws Exception { 74 Elements elements = new Elements(); 75 elements.addElement(new Element("a")); 76 elements.addElement(new Element("b")); 77 elements.addElement(new Element("c")); 78 79 StringWriter out = new StringWriter (); 80 BeanWriter writer = newBeanWriter(out, mapper); 81 writer.setWriteEmptyElements( true ); 82 writer.write(elements); 83 writer.flush(); 84 85 String xmlOut = out.toString(); 86 87 89 StringReader in = new StringReader (xmlOut); 90 91 94 BeanReader reader = new BeanReader(); 95 97 101 reader.setXMLIntrospector(newXMLIntrospector(mapper)); 102 reader.registerBeanClass(Elements.class); 103 Elements result = (Elements) reader.parse(in); 104 105 assertNotNull("Element 'a' is null (" + testName + ")", result.getElement("a")); 106 assertNotNull("Element 'b' is null (" + testName + ")", result.getElement("b")); 107 assertNotNull("Element 'c' is null (" + testName + ")", result.getElement("c")); 108 } 109 110 private BeanWriter newBeanWriter(Writer writer, NameMapper mapper) { 111 114 BeanWriter result = new BeanWriter(writer); 115 result.setWriteEmptyElements( true ); 116 118 122 result.setXMLIntrospector(newXMLIntrospector(mapper)); 123 result.enablePrettyPrint(); 124 result.getBindingConfiguration().setMapIDs(false); 125 return result; 126 } 127 128 private XMLIntrospector newXMLIntrospector(NameMapper mapper) { 129 XMLIntrospector introspector = new XMLIntrospector(); 130 introspector.getConfiguration().setAttributesForPrimitives(true); 131 introspector.getConfiguration().setWrapCollectionsInElement(false); 132 introspector.getConfiguration().setElementNameMapper(mapper); 133 return introspector; 134 } 135 } 136 137 | Popular Tags |