/[Apache-SVN]/jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java
ViewVC logotype

Diff of /jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java	2005/06/12 20:34:17	190315
+++ jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java	2005/06/12 20:35:45	190316
@@ -17,6 +17,9 @@
 
 package org.apache.commons.betwixt.schema;
 
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
 import java.io.StringWriter;
 
 import org.apache.commons.betwixt.AbstractTestCase;
@@ -130,83 +133,188 @@ public class TestSchemaGeneration extend
         
         String xsd = out.getBuffer().toString();
         
-        String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
-        		"<xsd:element name='OrderLineBean' type='org.apache.commons.betwixt.schema.OrderLineBean'/>" +
-        		"<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
-        		"<xsd:sequence>" +
-        		"<xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>" +
-        		"</xsd:sequence>" +
-        		"<xsd:attribute name='quantity' type='xsd:string'/>" +
-        		"</xsd:complexType>" +
-        		"<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>" +
-        		"<xsd:sequence/>" +
-        		"<xsd:attribute name='barcode' type='xsd:string'/>" +
-        		"<xsd:attribute name='code' type='xsd:string'/>" +
-        		"<xsd:attribute name='display-name' type='xsd:string'/>" +
-        		"<xsd:attribute name='name' type='xsd:string'/>" +
-        		"</xsd:complexType>" +
-        		"</xsd:schema>";
+        // different JVMs may return different orders during reflection
+        StringBuffer buffer = new StringBuffer("<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
+                "<xsd:element name='OrderLineBean' type='org.apache.commons.betwixt.schema.OrderLineBean'/>" +
+                "<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
+                "<xsd:sequence>" +
+                "<xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>" +
+                "</xsd:sequence>" +
+                "<xsd:attribute name='quantity' type='xsd:string'/>" +
+                "</xsd:complexType>" +
+                "<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>" +
+                "<xsd:sequence/>");
+        
+        PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(ProductBean.class).getPropertyDescriptors();
+        for (int i=0; i<propertyDescriptors.length; i++)
+        {
+            PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
+            if ("barcode".equals(propertyDescriptor.getName()))
+            {
+                buffer.append("<xsd:attribute name='barcode' type='xsd:string'/>");
+            }
+            else if ("code".equals(propertyDescriptor.getName()))
+            {
+                buffer.append("<xsd:attribute name='code' type='xsd:string'/>");
+            }
+            else if ("displayName".equals(propertyDescriptor.getName()))
+            {
+                buffer.append("<xsd:attribute name='display-name' type='xsd:string'/>");
+            }
+            else if ("name".equals(propertyDescriptor.getName()))
+            {
+                buffer.append("<xsd:attribute name='name' type='xsd:string'/>");
+            }
+        }
+        buffer.append("</xsd:complexType>" +
+                "</xsd:schema>");
+        
+        String expected = buffer.toString();
 
-            
         xmlAssertIsomorphicContent(parseString(expected), parseString(xsd), true);
     }
     
     public void testOrder() throws Exception {
         SchemaTranscriber transcriber = new SchemaTranscriber();
-        transcriber.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
-        transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
-        transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
-        transcriber.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
+        transcriber.getXMLIntrospector().getConfiguration()
+                .setElementNameMapper(new HyphenatedNameMapper());
+        transcriber.getXMLIntrospector().getConfiguration()
+                .setAttributeNameMapper(new HyphenatedNameMapper());
+        transcriber.getXMLIntrospector().getConfiguration()
+                .setAttributesForPrimitives(true);
+        transcriber.getXMLIntrospector().getConfiguration()
+                .setWrapCollectionsInElement(false);
         Schema schema = transcriber.generate(OrderBean.class);
-        
+
         StringWriter out = new StringWriter();
         out.write("<?xml version='1.0'?>");
         BeanWriter writer = new BeanWriter(out);
-        writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
-        writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
+        writer.setBindingConfiguration(transcriber
+                .createSchemaBindingConfiguration());
+        writer.getXMLIntrospector().setConfiguration(
+                transcriber.createSchemaIntrospectionConfiguration());
         writer.write(schema);
-        
+
         String xsd = out.getBuffer().toString();
+
+        PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(
+                OrderBean.class).getPropertyDescriptors();
+        boolean linesFirst = false;
+        for (int i = 0; i < propertyDescriptors.length; i++) {
+            PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
+            if ("lines".equals(propertyDescriptor.getName())) {
+                linesFirst = true;
+                break;
+            } else if ("customer".equals(propertyDescriptor.getName())) {
+                linesFirst = false;
+                break;
+            }
+        }
+
         
-        String expected = "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
-        		"<xsd:element name='order-bean' type='org.apache.commons.betwixt.schema.OrderBean'/>" +
-        		"" +
-        		"<xsd:complexType name='org.apache.commons.betwixt.schema.OrderBean'>" +
-        		"	<xsd:sequence>" +
-        		"		<xsd:element name='customer' type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' maxOccurs='1'/>" +
-        		"		<xsd:element name='line' type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' maxOccurs='unbounded'/>" +
-        		"	</xsd:sequence>" +
-        		"	<xsd:attribute name='code' type='xsd:string'/>" +
-        		"</xsd:complexType>" +
-        		"" +
-        		"<xsd:complexType name='org.apache.commons.betwixt.schema.CustomerBean'>" +
-        		"	<xsd:sequence/>" +
-        		"	<xsd:attribute name='code' type='xsd:string'/>" +
-        		"	<xsd:attribute name='country' type='xsd:string'/>" +
-        		"	<xsd:attribute name='name' type='xsd:string'/>" +
-        		"	<xsd:attribute name='postcode' type='xsd:string'/>" +
-        		"	<xsd:attribute name='street' type='xsd:string'/>" +
-        		"	<xsd:attribute name='town' type='xsd:string'/>" +
-        		"</xsd:complexType>" +
-        		"" +
-        		"<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
-        		"	<xsd:sequence>" +
-        		"		<xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>" +
-        		"	</xsd:sequence>" +
-        		"	<xsd:attribute name='quantity' type='xsd:string'/>" +
-        		"</xsd:complexType>" +
-        		"" +
-        		"<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>" +
-        		"	<xsd:sequence/>" +
-        		"		<xsd:attribute name='barcode' type='xsd:string'/>" +
-        		"		<xsd:attribute name='code' type='xsd:string'/>" +
-        		"		<xsd:attribute name='display-name' type='xsd:string'/>" +
-        		"		<xsd:attribute name='name' type='xsd:string'/>" +
-        		"	</xsd:complexType>" +
-        		"" +
-        		"</xsd:schema>";
-    
+        StringBuffer buffer = new StringBuffer(
+                "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
+                        + "<xsd:element name='order-bean' type='org.apache.commons.betwixt.schema.OrderBean'/>"
+                        + ""
+                        + "<xsd:complexType name='org.apache.commons.betwixt.schema.OrderBean'>"
+                        + "	<xsd:sequence>");
+
+        if (linesFirst) {
+            buffer.append("     <xsd:element name='line' type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' maxOccurs='unbounded'/>");
+            buffer.append("        <xsd:element name='customer' type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' maxOccurs='1'/>");
+        } else {
+            buffer.append("        <xsd:element name='customer' type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' maxOccurs='1'/>");
+            buffer.append("     <xsd:element name='line' type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' maxOccurs='unbounded'/>");
+        }
+
+        buffer.append("	</xsd:sequence>"
+                        + "	<xsd:attribute name='code' type='xsd:string'/>"
+                        + "</xsd:complexType>"
+                        + "");
+                        
+        if (linesFirst) {
+            writeExpectedOrderLineBeanType(buffer);
+            writeExpectedCustomerBeanType(buffer);            
+        } else {
+            writeExpectedCustomerBeanType(buffer);            
+            writeExpectedOrderLineBeanType(buffer);
+        }
+                
+        buffer.append("</xsd:schema>");
+
+        String expected = buffer.toString();
+
          xmlAssertIsomorphicContent(parseString(xsd), parseString(expected), true);
     }
+
+    /**
+     * @param buffer
+     * @throws IntrospectionException
+     */
+    private void writeExpectedOrderLineBeanType(StringBuffer buffer) throws IntrospectionException {
+        PropertyDescriptor[] propertyDescriptors;
+        buffer.append("<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>"
+                        + "	<xsd:sequence>"
+                        + "		<xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' maxOccurs='1'/>"
+                        + "	</xsd:sequence>"
+                        + "	<xsd:attribute name='quantity' type='xsd:string'/>"
+                        + "</xsd:complexType>"
+                        + ""
+                        + "<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>"
+                        + "	<xsd:sequence/>");
+
+        propertyDescriptors = Introspector.getBeanInfo(ProductBean.class)
+                .getPropertyDescriptors();
+        for (int i = 0; i < propertyDescriptors.length; i++) {
+            PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
+            if ("barcode".equals(propertyDescriptor.getName())) {
+                buffer
+                        .append("<xsd:attribute name='barcode' type='xsd:string'/>");
+            } else if ("code".equals(propertyDescriptor.getName())) {
+                buffer.append("<xsd:attribute name='code' type='xsd:string'/>");
+            } else if ("displayName".equals(propertyDescriptor.getName())) {
+                buffer
+                        .append("<xsd:attribute name='display-name' type='xsd:string'/>");
+            } else if ("name".equals(propertyDescriptor.getName())) {
+                buffer.append("<xsd:attribute name='name' type='xsd:string'/>");
+            }
+        }
+        buffer.append("	</xsd:complexType>");
+    }
+
+    /**
+     * @param buffer
+     * @throws IntrospectionException
+     */
+    private void writeExpectedCustomerBeanType(StringBuffer buffer) throws IntrospectionException {
+        PropertyDescriptor[] propertyDescriptors;
+        buffer.append("<xsd:complexType name='org.apache.commons.betwixt.schema.CustomerBean'>"
+                        + "	<xsd:sequence/>");
+
+        propertyDescriptors = Introspector.getBeanInfo(CustomerBean.class)
+                .getPropertyDescriptors();
+        for (int i = 0; i < propertyDescriptors.length; i++) {
+            PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
+            if ("code".equals(propertyDescriptor.getName())) {
+                buffer.append("<xsd:attribute name='code' type='xsd:string'/>");
+            } else if ("country".equals(propertyDescriptor.getName())) {
+                buffer
+                        .append("<xsd:attribute name='country' type='xsd:string'/>");
+            } else if ("name".equals(propertyDescriptor.getName())) {
+                buffer.append("<xsd:attribute name='name' type='xsd:string'/>");
+            } else if ("postcode".equals(propertyDescriptor.getName())) {
+                buffer
+                        .append("<xsd:attribute name='postcode' type='xsd:string'/>");
+            } else if ("street".equals(propertyDescriptor.getName())) {
+                buffer
+                        .append("<xsd:attribute name='street' type='xsd:string'/>");
+            } else if ("town".equals(propertyDescriptor.getName())) {
+                buffer.append("<xsd:attribute name='town' type='xsd:string'/>");
+            }
+        }
+
+        buffer.append("</xsd:complexType>"
+                        + "");
+    }
     
 }

 

infrastructure at apache.org
ViewVC Help
Powered by ViewVC 1.1.26