Author: rdonkin
Date: Sun Jun 12 13:35:45 2005
New Revision: 190316

URL: http://svn.apache.org/viewcvs?rev=190316&view=rev
Log:
Improved consistency across JVMs.

Modified:
    
jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java

Modified: 
jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java?rev=190316&r1=190315&r2=190316&view=diff
==============================================================================
--- 
jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java
 (original)
+++ 
jakarta/commons/proper/betwixt/branches/RELEASE_0_6_1_BRANCH/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java
 Sun Jun 12 13:35:45 2005
@@ -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 @@
         
         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>"
+                        + "");
     }
     
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to