1 16 17 18 package org.apache.commons.betwixt.schema; 19 20 import java.io.StringWriter ; 21 22 import org.apache.commons.betwixt.AbstractTestCase; 23 import org.apache.commons.betwixt.io.BeanWriter; 24 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; 25 26 31 public class TestSchemaGeneration extends AbstractTestCase { 32 33 public TestSchemaGeneration(String name) { 34 super(name); 35 } 36 37 public void testSimplestBeanWithAttributes() throws Exception { 38 SchemaTranscriber transcriber = new SchemaTranscriber(); 39 transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); 40 Schema schema = transcriber.generate(SimplestBean.class); 41 42 StringWriter out = new StringWriter (); 43 out.write("<?xml version='1.0'?>"); 44 BeanWriter writer = new BeanWriter(out); 45 writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration()); 46 writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration()); 47 writer.write(schema); 48 49 String xsd = out.getBuffer().toString(); 50 51 String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + 52 "<xsd:element name='SimplestBean' type='org.apache.commons.betwixt.schema.SimplestBean'/>" + 53 "<xsd:complexType name='org.apache.commons.betwixt.schema.SimplestBean'>" + 54 "<xsd:sequence/>" + 55 "<xsd:attribute name='name' type='xsd:string'/>" + 56 "</xsd:complexType>" + 57 "</xsd:schema>"; 58 59 xmlAssertIsomorphicContent(parseString(expected), parseString(xsd)); 60 } 61 62 63 public void testSimplestBeanWithElement() throws Exception { 64 SchemaTranscriber transcriber = new SchemaTranscriber(); 65 transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); 66 Schema schema = transcriber.generate(SimplestElementBean.class); 67 68 StringWriter out = new StringWriter (); 69 out.write("<?xml version='1.0'?>"); 70 BeanWriter writer = new BeanWriter(out); 71 writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration()); 72 writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration()); 73 writer.write(schema); 74 75 String xsd = out.getBuffer().toString(); 76 77 String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + 78 "<xsd:element name='SimplestBean' type='org.apache.commons.betwixt.schema.SimplestElementBean'/>" + 79 "<xsd:complexType name='org.apache.commons.betwixt.schema.SimplestElementBean'>" + 80 "<xsd:sequence>" + 81 "<xsd:element name='name' type='xsd:string' minOccurs='0' maxOccurs='1'/>" + 82 "</xsd:sequence>" + 83 "</xsd:complexType>" + 84 "</xsd:schema>"; 85 86 xmlAssertIsomorphicContent(parseString(expected), parseString(xsd)); 87 } 88 89 public void testSimpleBean() throws Exception { 90 SchemaTranscriber transcriber = new SchemaTranscriber(); 91 Schema schema = transcriber.generate(SimpleBean.class); 92 93 StringWriter out = new StringWriter (); 94 out.write("<?xml version='1.0'?>"); 95 BeanWriter writer = new BeanWriter(out); 96 writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration()); 97 writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration()); 98 writer.write(schema); 99 100 String xsd = out.getBuffer().toString(); 101 102 String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + 103 "<xsd:element name='simple' type='org.apache.commons.betwixt.schema.SimpleBean'/>" + 104 "<xsd:complexType name='org.apache.commons.betwixt.schema.SimpleBean'>" + 105 "<xsd:sequence>" + 106 "<xsd:element name='three' type='xsd:string' minOccurs='0' maxOccurs='1'/>" + 107 "<xsd:element name='four' type='xsd:string' minOccurs='0' maxOccurs='1'/>" + 108 "</xsd:sequence>" + 109 "<xsd:attribute name='one' type='xsd:string'/>" + 110 "<xsd:attribute name='two' type='xsd:string'/>" + 111 "</xsd:complexType>" + 112 "</xsd:schema>"; 113 114 xmlAssertIsomorphicContent(parseString(expected), parseString(xsd)); 115 } 116 117 118 public void testOrderLineBean() throws Exception { 119 SchemaTranscriber transcriber = new SchemaTranscriber(); 120 transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); 121 transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper()); 122 Schema schema = transcriber.generate(OrderLineBean.class); 123 124 StringWriter out = new StringWriter (); 125 out.write("<?xml version='1.0'?>"); 126 BeanWriter writer = new BeanWriter(out); 127 writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration()); 128 writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration()); 129 writer.write(schema); 130 131 String xsd = out.getBuffer().toString(); 132 133 String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + 134 "<xsd:element name='OrderLineBean' type='org.apache.commons.betwixt.schema.OrderLineBean'/>" + 135 "<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>" + 136 "<xsd:sequence/>" + 137 "<xsd:attribute name='barcode' type='xsd:string'/>" + 138 "<xsd:attribute name='code' type='xsd:string'/>" + 139 "<xsd:attribute name='display-name' type='xsd:string'/>" + 140 "<xsd:attribute name='name' type='xsd:string'/>" + 141 "</xsd:complexType>" + 142 "<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>" + 143 "<xsd:sequence>" + 144 "<xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>" + 145 "</xsd:sequence>" + 146 "<xsd:attribute name='quantity' type='xsd:string'/>" + 147 "</xsd:complexType>" + 148 "</xsd:schema>"; 149 150 xmlAssertIsomorphicContent(parseString(expected), parseString(xsd), true); 151 } 152 153 public void testOrder() throws Exception { 154 SchemaTranscriber transcriber = new SchemaTranscriber(); 155 transcriber.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper()); 156 transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper()); 157 transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); 158 transcriber.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false); 159 Schema schema = transcriber.generate(OrderBean.class); 160 161 StringWriter out = new StringWriter (); 162 out.write("<?xml version='1.0'?>"); 163 BeanWriter writer = new BeanWriter(out); 164 writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration()); 165 writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration()); 166 writer.write(schema); 167 168 String xsd = out.getBuffer().toString(); 169 170 String expected = "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + 171 " <xsd:element name='order-bean' type='org.apache.commons.betwixt.schema.OrderBean'/>" + 172 " <xsd:complexType name='org.apache.commons.betwixt.schema.CustomerBean'>" + 173 " <xsd:sequence/>" + 174 " <xsd:attribute name='code' type='xsd:string'/>" + 175 " <xsd:attribute name='country' type='xsd:string'/>" + 176 " <xsd:attribute name='name' type='xsd:string'/>" + 177 " <xsd:attribute name='postcode' type='xsd:string'/>" + 178 " <xsd:attribute name='street' type='xsd:string'/>" + 179 " <xsd:attribute name='town' type='xsd:string'/>" + 180 " </xsd:complexType>" + 181 " <xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>" + 182 " <xsd:sequence/>" + 183 " <xsd:attribute name='barcode' type='xsd:string'/>" + 184 " <xsd:attribute name='code' type='xsd:string'/>" + 185 " <xsd:attribute name='display-name' type='xsd:string'/>" + 186 " <xsd:attribute name='name' type='xsd:string'/>" + 187 " </xsd:complexType>" + 188 " <xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>" + 189 " <xsd:sequence>" + 190 " <xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>" + 191 " </xsd:sequence>" + 192 " <xsd:attribute name='quantity' type='xsd:string'/>" + 193 " </xsd:complexType>" + 194 " <xsd:complexType name='org.apache.commons.betwixt.schema.OrderBean'>" + 195 " <xsd:sequence>" + 196 " <xsd:element name='customer' type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' maxOccurs='1'/>" + 197 " <xsd:element name='line' type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' maxOccurs='unbounded'/>" + 198 " </xsd:sequence>" + 199 " <xsd:attribute name='code' type='xsd:string'/>" + 200 " </xsd:complexType>" + 201 " </xsd:schema>"; 202 203 xmlAssertIsomorphicContent(parseString(xsd), parseString(expected)); 204 } 205 206 } 207 | Popular Tags |