001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.schema;
019:
020: import java.beans.IntrospectionException;
021: import java.beans.Introspector;
022: import java.beans.PropertyDescriptor;
023: import java.io.StringWriter;
024:
025: import org.apache.commons.betwixt.AbstractTestCase;
026: import org.apache.commons.betwixt.io.BeanWriter;
027: import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
028:
029: /**
030: * Tests for the generation of schema from the object models.
031: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
032: * @version $Revision: 438373 $
033: */
034: public class TestSchemaGeneration extends AbstractTestCase {
035:
036: public TestSchemaGeneration(String name) {
037: super (name);
038: }
039:
040: public void testSimplestBeanWithAttributes() throws Exception {
041: SchemaTranscriber transcriber = new SchemaTranscriber();
042: transcriber.getXMLIntrospector().getConfiguration()
043: .setAttributesForPrimitives(true);
044: Schema schema = transcriber.generate(SimplestBean.class);
045:
046: StringWriter out = new StringWriter();
047: out.write("<?xml version='1.0'?>");
048: BeanWriter writer = new BeanWriter(out);
049: writer.setBindingConfiguration(transcriber
050: .createSchemaBindingConfiguration());
051: writer.getXMLIntrospector().setConfiguration(
052: transcriber.createSchemaIntrospectionConfiguration());
053: writer.write(schema);
054:
055: String xsd = out.getBuffer().toString();
056:
057: String expected = "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
058: + "<xsd:element name='SimplestBean' type='org.apache.commons.betwixt.schema.SimplestBean'/>"
059: + "<xsd:complexType name='org.apache.commons.betwixt.schema.SimplestBean'>"
060: + "<xsd:sequence/>"
061: + "<xsd:attribute name='name' type='xsd:string'/>"
062: + "</xsd:complexType>" + "</xsd:schema>";
063:
064: xmlAssertIsomorphicContent(parseString(expected),
065: parseString(xsd));
066: }
067:
068: public void testSimplestBeanWithElement() throws Exception {
069: SchemaTranscriber transcriber = new SchemaTranscriber();
070: transcriber.getXMLIntrospector().getConfiguration()
071: .setAttributesForPrimitives(true);
072: Schema schema = transcriber.generate(SimplestElementBean.class);
073:
074: StringWriter out = new StringWriter();
075: out.write("<?xml version='1.0'?>");
076: BeanWriter writer = new BeanWriter(out);
077: writer.setBindingConfiguration(transcriber
078: .createSchemaBindingConfiguration());
079: writer.getXMLIntrospector().setConfiguration(
080: transcriber.createSchemaIntrospectionConfiguration());
081: writer.write(schema);
082:
083: String xsd = out.getBuffer().toString();
084:
085: String expected = "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
086: + "<xsd:element name='SimplestBean' type='org.apache.commons.betwixt.schema.SimplestElementBean'/>"
087: + "<xsd:complexType name='org.apache.commons.betwixt.schema.SimplestElementBean'>"
088: + "<xsd:sequence>"
089: + "<xsd:element name='name' type='xsd:string' minOccurs='0' maxOccurs='1'/>"
090: + "</xsd:sequence>"
091: + "</xsd:complexType>"
092: + "</xsd:schema>";
093:
094: xmlAssertIsomorphicContent(parseString(expected),
095: parseString(xsd));
096: }
097:
098: public void testSimpleBean() throws Exception {
099: SchemaTranscriber transcriber = new SchemaTranscriber();
100: Schema schema = transcriber.generate(SimpleBean.class);
101:
102: StringWriter out = new StringWriter();
103: out.write("<?xml version='1.0'?>");
104: BeanWriter writer = new BeanWriter(out);
105: writer.setBindingConfiguration(transcriber
106: .createSchemaBindingConfiguration());
107: writer.getXMLIntrospector().setConfiguration(
108: transcriber.createSchemaIntrospectionConfiguration());
109: writer.write(schema);
110:
111: String xsd = out.getBuffer().toString();
112:
113: String expected = "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
114: + "<xsd:element name='simple' type='org.apache.commons.betwixt.schema.SimpleBean'/>"
115: + "<xsd:complexType name='org.apache.commons.betwixt.schema.SimpleBean'>"
116: + "<xsd:sequence>"
117: + "<xsd:element name='three' type='xsd:string' minOccurs='0' maxOccurs='1'/>"
118: + "<xsd:element name='four' type='xsd:string' minOccurs='0' maxOccurs='1'/>"
119: + "</xsd:sequence>"
120: + "<xsd:attribute name='one' type='xsd:string'/>"
121: + "<xsd:attribute name='two' type='xsd:string'/>"
122: + "</xsd:complexType>" + "</xsd:schema>";
123:
124: xmlAssertIsomorphicContent(parseString(expected),
125: parseString(xsd));
126: }
127:
128: public void testOrderLineBean() throws Exception {
129: SchemaTranscriber transcriber = new SchemaTranscriber();
130: transcriber.getXMLIntrospector().getConfiguration()
131: .setAttributesForPrimitives(true);
132: transcriber.getXMLIntrospector().getConfiguration()
133: .setAttributeNameMapper(new HyphenatedNameMapper());
134: Schema schema = transcriber.generate(OrderLineBean.class);
135:
136: StringWriter out = new StringWriter();
137: out.write("<?xml version='1.0'?>");
138: BeanWriter writer = new BeanWriter(out);
139: writer.setBindingConfiguration(transcriber
140: .createSchemaBindingConfiguration());
141: writer.getXMLIntrospector().setConfiguration(
142: transcriber.createSchemaIntrospectionConfiguration());
143: writer.write(schema);
144:
145: String xsd = out.getBuffer().toString();
146:
147: // different JVMs may return different orders during reflection
148: StringBuffer buffer = new StringBuffer(
149: "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
150: + "<xsd:element name='OrderLineBean' type='org.apache.commons.betwixt.schema.OrderLineBean'/>"
151: + "<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>"
152: + "<xsd:sequence>"
153: + "<xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>"
154: + "</xsd:sequence>"
155: + "<xsd:attribute name='quantity' type='xsd:string'/>"
156: + "</xsd:complexType>"
157: + "<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>"
158: + "<xsd:sequence/>");
159:
160: PropertyDescriptor[] propertyDescriptors = Introspector
161: .getBeanInfo(ProductBean.class)
162: .getPropertyDescriptors();
163: for (int i = 0; i < propertyDescriptors.length; i++) {
164: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
165: if ("barcode".equals(propertyDescriptor.getName())) {
166: buffer
167: .append("<xsd:attribute name='barcode' type='xsd:string'/>");
168: } else if ("code".equals(propertyDescriptor.getName())) {
169: buffer
170: .append("<xsd:attribute name='code' type='xsd:string'/>");
171: } else if ("displayName".equals(propertyDescriptor
172: .getName())) {
173: buffer
174: .append("<xsd:attribute name='display-name' type='xsd:string'/>");
175: } else if ("name".equals(propertyDescriptor.getName())) {
176: buffer
177: .append("<xsd:attribute name='name' type='xsd:string'/>");
178: }
179: }
180: buffer.append("</xsd:complexType>" + "</xsd:schema>");
181:
182: String expected = buffer.toString();
183:
184: xmlAssertIsomorphicContent(parseString(expected),
185: parseString(xsd), true);
186: }
187:
188: public void testOrder() throws Exception {
189: SchemaTranscriber transcriber = new SchemaTranscriber();
190: transcriber.getXMLIntrospector().getConfiguration()
191: .setElementNameMapper(new HyphenatedNameMapper());
192: transcriber.getXMLIntrospector().getConfiguration()
193: .setAttributeNameMapper(new HyphenatedNameMapper());
194: transcriber.getXMLIntrospector().getConfiguration()
195: .setAttributesForPrimitives(true);
196: transcriber.getXMLIntrospector().getConfiguration()
197: .setWrapCollectionsInElement(false);
198: Schema schema = transcriber.generate(OrderBean.class);
199:
200: StringWriter out = new StringWriter();
201: out.write("<?xml version='1.0'?>");
202: BeanWriter writer = new BeanWriter(out);
203: writer.setBindingConfiguration(transcriber
204: .createSchemaBindingConfiguration());
205: writer.getXMLIntrospector().setConfiguration(
206: transcriber.createSchemaIntrospectionConfiguration());
207: writer.write(schema);
208:
209: String xsd = out.getBuffer().toString();
210:
211: PropertyDescriptor[] propertyDescriptors = Introspector
212: .getBeanInfo(OrderBean.class).getPropertyDescriptors();
213: boolean linesFirst = false;
214: for (int i = 0; i < propertyDescriptors.length; i++) {
215: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
216: if ("lines".equals(propertyDescriptor.getName())) {
217: linesFirst = true;
218: break;
219: } else if ("customer".equals(propertyDescriptor.getName())) {
220: linesFirst = false;
221: break;
222: }
223: }
224:
225: StringBuffer buffer = new StringBuffer(
226: "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
227: + "<xsd:element name='order-bean' type='org.apache.commons.betwixt.schema.OrderBean'/>"
228: + ""
229: + "<xsd:complexType name='org.apache.commons.betwixt.schema.OrderBean'>"
230: + " <xsd:sequence>");
231:
232: if (linesFirst) {
233: buffer
234: .append(" <xsd:element name='line' type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' maxOccurs='unbounded'/>");
235: buffer
236: .append(" <xsd:element name='customer' type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' maxOccurs='1'/>");
237: } else {
238: buffer
239: .append(" <xsd:element name='customer' type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' maxOccurs='1'/>");
240: buffer
241: .append(" <xsd:element name='line' type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' maxOccurs='unbounded'/>");
242: }
243:
244: buffer.append(" </xsd:sequence>"
245: + " <xsd:attribute name='code' type='xsd:string'/>"
246: + "</xsd:complexType>" + "");
247:
248: if (linesFirst) {
249: writeExpectedOrderLineBeanType(buffer);
250: writeExpectedCustomerBeanType(buffer);
251: } else {
252: writeExpectedCustomerBeanType(buffer);
253: writeExpectedOrderLineBeanType(buffer);
254: }
255:
256: buffer.append("</xsd:schema>");
257:
258: String expected = buffer.toString();
259:
260: xmlAssertIsomorphicContent(parseString(xsd),
261: parseString(expected), true);
262: }
263:
264: /**
265: * @param buffer
266: * @throws IntrospectionException
267: */
268: private void writeExpectedOrderLineBeanType(StringBuffer buffer)
269: throws IntrospectionException {
270: PropertyDescriptor[] propertyDescriptors;
271: buffer
272: .append("<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>"
273: + " <xsd:sequence>"
274: + " <xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>"
275: + " </xsd:sequence>"
276: + " <xsd:attribute name='quantity' type='xsd:string'/>"
277: + "</xsd:complexType>"
278: + ""
279: + "<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>"
280: + " <xsd:sequence/>");
281:
282: propertyDescriptors = Introspector.getBeanInfo(
283: ProductBean.class).getPropertyDescriptors();
284: for (int i = 0; i < propertyDescriptors.length; i++) {
285: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
286: if ("barcode".equals(propertyDescriptor.getName())) {
287: buffer
288: .append("<xsd:attribute name='barcode' type='xsd:string'/>");
289: } else if ("code".equals(propertyDescriptor.getName())) {
290: buffer
291: .append("<xsd:attribute name='code' type='xsd:string'/>");
292: } else if ("displayName".equals(propertyDescriptor
293: .getName())) {
294: buffer
295: .append("<xsd:attribute name='display-name' type='xsd:string'/>");
296: } else if ("name".equals(propertyDescriptor.getName())) {
297: buffer
298: .append("<xsd:attribute name='name' type='xsd:string'/>");
299: }
300: }
301: buffer.append(" </xsd:complexType>");
302: }
303:
304: /**
305: * @param buffer
306: * @throws IntrospectionException
307: */
308: private void writeExpectedCustomerBeanType(StringBuffer buffer)
309: throws IntrospectionException {
310: PropertyDescriptor[] propertyDescriptors;
311: buffer
312: .append("<xsd:complexType name='org.apache.commons.betwixt.schema.CustomerBean'>"
313: + " <xsd:sequence/>");
314:
315: propertyDescriptors = Introspector.getBeanInfo(
316: CustomerBean.class).getPropertyDescriptors();
317: for (int i = 0; i < propertyDescriptors.length; i++) {
318: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
319: if ("code".equals(propertyDescriptor.getName())) {
320: buffer
321: .append("<xsd:attribute name='code' type='xsd:string'/>");
322: } else if ("country".equals(propertyDescriptor.getName())) {
323: buffer
324: .append("<xsd:attribute name='country' type='xsd:string'/>");
325: } else if ("name".equals(propertyDescriptor.getName())) {
326: buffer
327: .append("<xsd:attribute name='name' type='xsd:string'/>");
328: } else if ("postcode".equals(propertyDescriptor.getName())) {
329: buffer
330: .append("<xsd:attribute name='postcode' type='xsd:string'/>");
331: } else if ("street".equals(propertyDescriptor.getName())) {
332: buffer
333: .append("<xsd:attribute name='street' type='xsd:string'/>");
334: } else if ("town".equals(propertyDescriptor.getName())) {
335: buffer
336: .append("<xsd:attribute name='town' type='xsd:string'/>");
337: }
338: }
339:
340: buffer.append("</xsd:complexType>" + "");
341: }
342:
343: }
|