1 16 17 package org.apache.commons.betwixt.schema; 18 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 import java.io.Writer ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 import org.apache.commons.betwixt.AbstractTestCase; 27 import org.apache.commons.betwixt.XMLIntrospector; 28 import org.apache.commons.betwixt.io.BeanReader; 29 import org.apache.commons.betwixt.io.BeanWriter; 30 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry; 31 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; 32 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; 33 34 37 44 public class TestSchema extends AbstractTestCase 45 { 46 47 public static Test suite() 48 { 49 return new TestSuite(TestSchema.class); 50 } 51 52 53 public TestSchema(String testName) 54 { 55 super(testName); 56 } 57 58 64 public void testCombinedRoundTrip() 65 throws Exception 66 { 67 71 74 BeanReader reader = createBeanReader(); 75 76 PhysicalSchema schema = (PhysicalSchema) reader.parse( 77 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml")); 78 StringWriter buffer = new StringWriter (); 79 write(schema, buffer, true); 80 81 83 StringReader in = new StringReader (buffer.getBuffer().toString()); 84 reader = createBeanReader(); 85 XMLIntrospector intro = createXMLIntrospector(); 86 DefaultXMLBeanInfoRegistry registry = new DefaultXMLBeanInfoRegistry(); 87 intro.setRegistry(registry); 88 intro.getConfiguration().setWrapCollectionsInElement(true); 92 registry.flush(); 95 reader.setXMLIntrospector(intro); 97 reader.deregisterBeanClass(PhysicalSchema.class); 98 reader.getRules().clear(); 99 reader.registerBeanClass(PhysicalSchema.class); 100 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in); 101 buffer.close(); 102 write(schema,buffer, true); 103 assertEquals(schema, schemaSecond); 104 } 105 111 public void testRoundTripWithoutCollectionElement() 112 throws Exception 113 { 114 BeanReader reader = createBeanReader(); 115 PhysicalSchema schema = (PhysicalSchema) reader.parse( 116 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml")); 117 StringWriter buffer = new StringWriter (); 118 write(schema, buffer, false); 119 StringReader in = new StringReader (buffer.getBuffer().toString()); 120 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in); 121 assertEquals(schemaSecond, schema); 122 } 123 124 127 protected BeanReader createBeanReader() 128 throws Exception 129 { 130 BeanReader reader = new BeanReader(); 131 reader.setXMLIntrospector(createXMLIntrospector()); 132 reader.registerBeanClass(PhysicalSchema.class); 135 return reader; 136 } 137 138 141 protected XMLIntrospector createXMLIntrospector() { 142 XMLIntrospector introspector = new XMLIntrospector(); 143 144 introspector.getConfiguration().setAttributesForPrimitives(true); 146 147 introspector.getConfiguration().setWrapCollectionsInElement(false); 151 152 introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper(true, "_")); 159 introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper()); 163 164 return introspector; 165 } 166 167 171 private void write(Object bean, Writer out, boolean wrapCollectionsInElement) 172 throws Exception 173 { 174 BeanWriter writer = new BeanWriter(out); 175 writer.setWriteEmptyElements( true ); 176 writer.setXMLIntrospector(createXMLIntrospector()); 177 writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(wrapCollectionsInElement); 179 writer.getBindingConfiguration().setMapIDs(false); 182 writer.setIndent(" "); 184 writer.setEndOfLine("\n"); 185 writer.write(bean); 186 } 187 } 188 189 | Popular Tags |