commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit TestXmlTestCase.java XmlTestCase.java
Date Mon, 02 Feb 2004 22:21:44 GMT
rdonkin     2004/02/02 14:21:44

  Modified:    betwixt/src/java/org/apache/commons/betwixt/io Tag:
                        REFACTORING-BRANCH_2004-01-13
                        AbstractBeanWriter.java
               betwixt/src/java/org/apache/commons/betwixt/schema Tag:
                        REFACTORING-BRANCH_2004-01-13 ComplexType.java
                        SchemaTranscriber.java
               betwixt/src/test/org/apache/commons/betwixt/schema Tag:
                        REFACTORING-BRANCH_2004-01-13 SimplestBean.java
                        TestSchemaGeneration.java
                        TestSchemaTranscriber.java TestSchemaValidity.java
               betwixt/src/test/org/apache/commons/betwixt/xmlunit Tag:
                        REFACTORING-BRANCH_2004-01-13 TestXmlTestCase.java
                        XmlTestCase.java
  Log:
  Added w3c schema generation.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.22.2.6  +21 -12    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
  
  Index: AbstractBeanWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java,v
  retrieving revision 1.22.2.5
  retrieving revision 1.22.2.6
  diff -u -r1.22.2.5 -r1.22.2.6
  --- AbstractBeanWriter.java	1 Feb 2004 22:55:47 -0000	1.22.2.5
  +++ AbstractBeanWriter.java	2 Feb 2004 22:21:44 -0000	1.22.2.6
  @@ -165,7 +165,7 @@
               log.debug( bean );
           }
           start();
  -        write( null, bean );
  +        writeBean( null, null, null, bean, makeContext( bean ) );
           end();
           if (log.isDebugEnabled()) {
               log.debug( "Finished writing bean graph." );
  @@ -194,7 +194,9 @@
                           IOException, 
                           SAXException,
                           IntrospectionException {
  +        start();
           writeBean( "", qualifiedName, qualifiedName, bean, makeContext( bean ) );
  +        end();
       }
       
       /** 
  @@ -564,7 +566,8 @@
                   log.trace( "Element " + elementDescriptor + " is empty." );
               }
           
  -            Attributes attributes = addNamespaceDeclarations(new ElementAttributes( elementDescriptor,
context ));
  +            Attributes attributes = addNamespaceDeclarations(
  +                new ElementAttributes( elementDescriptor, context ), namespaceUri);
               
               startElement( 
                               namespaceUri, 
  @@ -582,13 +585,19 @@
       /**
        * Adds namespace declarations (if any are needed) to the given attributes.
        * @param attributes Attributes, not null
  +     * @param elementNamespaceUri the URI for the enclosing element, possibly null
        * @return Attributes, not null
        */
  -    private Attributes addNamespaceDeclarations(Attributes attributes) {
  +    private Attributes addNamespaceDeclarations(Attributes attributes, String elementNamespaceUri)
{
           Attributes result = attributes;
           AttributesImpl withDeclarations = null; 
  -        for (int i=0, size=attributes.getLength(); i<size ; i++) {
  -            String uri = attributes.getURI(i);
  +        for (int i=-1, size=attributes.getLength(); i<size ; i++) {
  +            String uri = null;
  +            if (i == -1) {
  +                uri = elementNamespaceUri;
  +            } else {
  +                uri = attributes.getURI(i);
  +            }
               if (uri != null && !"".equals(uri) && !namespacesDeclared.contains(uri))
{
                   if (withDeclarations == null) {
                       withDeclarations = new AttributesImpl(attributes);
  @@ -643,7 +652,7 @@
                           namespaceUri, 
                           localName, 
                           qualifiedName,
  -                        addNamespaceDeclarations(attributes));
  +                        addNamespaceDeclarations(attributes, namespaceUri));
       
               writeElementContent( elementDescriptor, context ) ;
               endElement( namespaceUri, localName, qualifiedName );
  @@ -714,7 +723,7 @@
                                   idrefAttributeName,
                                   "IDREF",
                                   idrefAttributeValue);
  -        startElement( uri, localName, qualifiedName, addNamespaceDeclarations(attributes));
       
  +        startElement( uri, localName, qualifiedName, addNamespaceDeclarations(attributes,
uri));        
           endElement( uri, localName, qualifiedName );
       }
       
  
  
  
  No                   revision
  No                   revision
  1.1.2.4   +8 -5      jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ComplexType.java
  
  Index: ComplexType.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ComplexType.java,v
  retrieving revision 1.1.2.3
  retrieving revision 1.1.2.4
  diff -u -r1.1.2.3 -r1.1.2.4
  --- ComplexType.java	31 Jan 2004 15:38:09 -0000	1.1.2.3
  +++ ComplexType.java	2 Feb 2004 22:21:44 -0000	1.1.2.4
  @@ -96,7 +96,10 @@
           for (int i=0,length=attributeDescriptors.length; i<length ; i++) {
               //TODO: need to think about computing schema types from descriptors
               // this will probably depend on the class mapped to
  -            attributes.add(new Attribute(attributeDescriptors[i]));
  +            String uri = attributeDescriptors[i].getURI();
  +            if (! SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI.equals(uri)) {
  +                attributes.add(new Attribute(attributeDescriptors[i]));
  +            }
           }
           
           ElementDescriptor[] elementDescriptors = elementDescriptor.getElementDescriptors();
  
  
  
  1.1.2.2   +46 -7     jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/SchemaTranscriber.java
  
  Index: SchemaTranscriber.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/SchemaTranscriber.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- SchemaTranscriber.java	18 Jan 2004 12:35:23 -0000	1.1.2.1
  +++ SchemaTranscriber.java	2 Feb 2004 22:21:44 -0000	1.1.2.2
  @@ -63,7 +63,9 @@
   
   import java.beans.IntrospectionException;
   
  +import org.apache.commons.betwixt.BindingConfiguration;
   import org.apache.commons.betwixt.ElementDescriptor;
  +import org.apache.commons.betwixt.IntrospectionConfiguration;
   import org.apache.commons.betwixt.XMLBeanInfo;
   import org.apache.commons.betwixt.XMLIntrospector;
   
  @@ -81,6 +83,9 @@
    */
   public class SchemaTranscriber {
   	
  +    public static final String W3C_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
  +    public static final String W3C_SCHEMA_INSTANCE_URI= "http://www.w3.org/2001/XMLSchema-instance";
  +    
   	/** Used to introspect beans in order to generate XML */
   	private XMLIntrospector introspector = new XMLIntrospector();
   	
  @@ -95,14 +100,17 @@
   	}
   	
   	/**
  -	 * Sets the XMLIntrospector used to create XMLInfoBeans'
  +	 * <p>Sets the XMLIntrospector used to create XMLInfoBeans.
  +     * </p></p>
  +     * <strong>Note:</strong> certain properties will be reconfigured so that

  +     * the introspection will produce correct results.
  +     * </p>
   	 * @param introspector XMLIntrospector used to create XMLInfoBean's used to generate schema,
not null
   	 */
   	public void setXMLIntrospector(XMLIntrospector introspector) {
   		this.introspector = introspector;
   	}
  -	
  -	
  +
   	/**
   	 * Generates an XML Schema model for the given class.
   	 * @param clazz not null
  @@ -124,4 +132,35 @@
          schema.addGlobalElementType(elementDescriptor);
          return schema;
   	}
  +    
  +    /**
  +     * <p>Gets an <code>IntrospectionConfiguration</code> that is suitable

  +     * for introspecting {@link Schema}.
  +     * </p><p>
  +     * <strong>Note:</strong> A new instance is created each time this method
is called.
  +     * It can therefore be safely be modified.
  +     * </p>
  +     * 
  +     * @return IntrospectionConfiguration, not null
  +     */
  +    public IntrospectionConfiguration createSchemaIntrospectionConfiguration() {
  +        IntrospectionConfiguration configuration = new IntrospectionConfiguration();
  +        configuration.getPrefixMapper().setPrefix(W3C_SCHEMA_URI, "xsd");
  +        configuration.getPrefixMapper().setPrefix(W3C_SCHEMA_INSTANCE_URI, "xsi");
  +        return configuration;
  +    }
  +    
  +    /**
  +     * <p>Gets a <code>BindingConfiguration</code> that is suitable for
mapping {@link Schema}.
  +     * </p><p>
  +     * <strong>Note:</strong> A new instance is created each time this method
is called.
  +     * It can therefore be safely be modified.
  +     * </p>
  +     * @return BindingConfiguration, not null
  +     */
  +    public BindingConfiguration createSchemaBindingConfiguration() {
  +        BindingConfiguration configuration = new BindingConfiguration();
  +        configuration.setMapIDs(false);
  +        return configuration;   
  +    }
   }
  
  
  
  No                   revision
  No                   revision
  1.1.2.2   +10 -4     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/SimplestBean.java
  
  Index: SimplestBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/SimplestBean.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- SimplestBean.java	31 Jan 2004 15:37:21 -0000	1.1.2.1
  +++ SimplestBean.java	2 Feb 2004 22:21:44 -0000	1.1.2.2
  @@ -69,6 +69,12 @@
   
       private String name;
       
  +    public SimplestBean() {}
  +    
  +    public SimplestBean(String name) {
  +        setName(name);
  +    }
  +    
       public String getName() {
           return name;
       }
  
  
  
  1.1.2.2   +21 -4     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaGeneration.java
  
  Index: TestSchemaGeneration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaGeneration.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- TestSchemaGeneration.java	1 Feb 2004 22:58:07 -0000	1.1.2.1
  +++ TestSchemaGeneration.java	2 Feb 2004 22:21:44 -0000	1.1.2.2
  @@ -82,6 +82,23 @@
           transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
           Schema schema = transcriber.generate(SimplestBean.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.write(schema);
           
  +        String xsd = out.getBuffer().toString();
  +        
  +        String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
+
  +        "<xsd:element name='SimplestBean' type='org.apache.commons.betwixt.schema.SimplestBean'/>"
+
  +        "<xsd:complexType name='org.apache.commons.betwixt.schema.SimplestBean'>"
+
  +        "<xsd:sequence/>" +
  +        "<xsd:attribute name='name' type='xsd:string'/>" +
  +        "</xsd:complexType>" +
  +        "</xsd:schema>";
  +            
  +        xmlAssertIsomorphicContent(parseString(expected), parseString(xsd));
       }
   }
  
  
  
  1.1.2.3   +7 -7      jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaTranscriber.java
  
  Index: TestSchemaTranscriber.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaTranscriber.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- TestSchemaTranscriber.java	31 Jan 2004 15:38:09 -0000	1.1.2.2
  +++ TestSchemaTranscriber.java	2 Feb 2004 22:21:44 -0000	1.1.2.3
  @@ -98,16 +98,16 @@
           Schema expected = new Schema();
           
           ComplexType simplestBeanType = new ComplexType();
  -        simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestBean");
  +        simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestElementBean");
           simplestBeanType.addElement(new Element("name", "xsd:string"));
           
  -        Element root = new Element("SimplestBean", "org.apache.commons.betwixt.schema.SimplestBean");
  +        Element root = new Element("SimplestBean", "org.apache.commons.betwixt.schema.SimplestElementBean");
           expected.addComplexType(simplestBeanType);
           expected.addElement(root);
           
           SchemaTranscriber transcriber = new SchemaTranscriber();
           transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
  -        Schema out = transcriber.generate(SimplestBean.class);
  +        Schema out = transcriber.generate(SimplestElementBean.class);
           
           assertEquals("Simplest bean schema", expected, out);
       }
  
  
  
  1.1.2.2   +36 -7     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaValidity.java
  
  Index: TestSchemaValidity.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaValidity.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- TestSchemaValidity.java	1 Feb 2004 22:57:49 -0000	1.1.2.1
  +++ TestSchemaValidity.java	2 Feb 2004 22:21:44 -0000	1.1.2.2
  @@ -61,7 +61,12 @@
   
   package org.apache.commons.betwixt.schema;
   
  +import java.io.StringReader;
  +import java.io.StringWriter;
  +
   import org.apache.commons.betwixt.AbstractTestCase;
  +import org.apache.commons.betwixt.io.BeanWriter;
  +import org.xml.sax.InputSource;
   
   /**
    * Tests for the validity of the schema produced.
  @@ -74,12 +79,36 @@
           super(name);
       }
       
  -    
  -    public void testSimplestBeanWithAttributes() throws Exception {
  +    private String generateSchema(Class clazz) throws Exception {
           SchemaTranscriber transcriber = new SchemaTranscriber();
           transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
  -        Schema schema = transcriber.generate(SimplestBean.class);
  +        Schema schema = transcriber.generate(clazz);
           
  +        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.write(schema);
           
  +        String xsd = out.getBuffer().toString();
  +        return xsd;
  +    }
  +    
  +    public void testSimplestBeanWithAttributes() throws Exception {
  +       String xsd = generateSchema(SimplestBean.class);
  +            
  +       StringWriter out = new StringWriter();
  +       out.write("<?xml version='1.0'?>");
  +       BeanWriter writer = new BeanWriter(out);
  +       writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
  +       writer.getXMLIntrospector().getConfiguration().getPrefixMapper().setPrefix(SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI,
"xsi");
  +       writer.getBindingConfiguration().setMapIDs(false);
  +       SimplestBean bean = new SimplestBean("Simon");
  +       writer.write(bean);
  +       
  +       String xml = out.getBuffer().toString();
  +       
  +       xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
       }   
   }
  
  
  
  No                   revision
  No                   revision
  1.4.2.2   +5 -4      jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/TestXmlTestCase.java
  
  Index: TestXmlTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/TestXmlTestCase.java,v
  retrieving revision 1.4.2.1
  retrieving revision 1.4.2.2
  diff -u -r1.4.2.1 -r1.4.2.2
  --- TestXmlTestCase.java	1 Feb 2004 13:48:27 -0000	1.4.2.1
  +++ TestXmlTestCase.java	2 Feb 2004 22:21:44 -0000	1.4.2.2
  @@ -196,4 +196,5 @@
               new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/personnel.xsd")));
           assertFalse(isValid(document, schema));
       }
  +    
   }
  
  
  
  1.11.2.2  +17 -3     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/XmlTestCase.java
  
  Index: XmlTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/XmlTestCase.java,v
  retrieving revision 1.11.2.1
  retrieving revision 1.11.2.2
  diff -u -r1.11.2.1 -r1.11.2.2
  --- XmlTestCase.java	1 Feb 2004 13:48:27 -0000	1.11.2.1
  +++ XmlTestCase.java	2 Feb 2004 22:21:44 -0000	1.11.2.2
  @@ -578,5 +578,19 @@
           
           return result;
       }
  +    
  +    public void xmlAssertIsValid(InputSource documentSource, InputSource schemaSource)

  +        throws ParserConfigurationException, IOException
  +    {
  +        try
  +        {
  +            validateWithSchema(documentSource, schemaSource);
  +        }
  +        catch (SAXException se)
  +        {
  +            se.printStackTrace();
  +            fail("Validation failure: " + se.getMessage());
  +        }   
  +    }
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message