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 invalid-personnel-schema.xml TestXmlTestCase.java XmlTestCase.java
Date Sun, 01 Feb 2004 13:48:27 GMT
rdonkin     2004/02/01 05:48:27

  Modified:    betwixt  Tag: REFACTORING-BRANCH_2004-01-13 project.xml
               betwixt/src/test/org/apache/commons/betwixt/xmlunit Tag:
                        REFACTORING-BRANCH_2004-01-13 TestXmlTestCase.java
                        XmlTestCase.java
  Added:       betwixt/src/test/org/apache/commons/betwixt/xmlunit Tag:
                        REFACTORING-BRANCH_2004-01-13
                        invalid-personnel-schema.xml
  Log:
  Tests for validation tests.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.32.2.1  +1 -1      jakarta-commons/betwixt/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/project.xml,v
  retrieving revision 1.32
  retrieving revision 1.32.2.1
  diff -u -r1.32 -r1.32.2.1
  --- project.xml	11 Jan 2004 20:59:09 -0000	1.32
  +++ project.xml	1 Feb 2004 13:48:27 -0000	1.32.2.1
  @@ -131,7 +131,7 @@
   <!-- runtime dependencies only required for testing and sample programs -->
       <dependency>
         <id>xerces</id>
  -      <version>2.2.1</version>
  +      <version>2.4.0</version>
         <url>http://xml.apache.org/xerces2-j/index.html</url>
       </dependency>
   
  
  
  
  No                   revision
  No                   revision
  1.4.2.1   +48 -5     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
  retrieving revision 1.4.2.1
  diff -u -r1.4 -r1.4.2.1
  --- TestXmlTestCase.java	19 Oct 2003 14:44:53 -0000	1.4
  +++ TestXmlTestCase.java	1 Feb 2004 13:48:27 -0000	1.4.2.1
  @@ -9,7 +9,7 @@
    * 
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2004 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -61,8 +61,13 @@
    *
    */ 
   
  +import java.io.File;
  +import java.io.FileInputStream;
  +
   import junit.framework.AssertionFailedError;
   
  +import org.xml.sax.InputSource;
  +
   /**
    * Test harness which test xml unit
    *
  @@ -152,5 +157,43 @@
           if (failed) {
               fail("Expected unit test to fail!");
           }
  +    }
  +    
  +    public void testValidateSchemaValidOne() throws Exception {
  +        String basedir = System.getProperty("basedir");
  +        InputSource document = new InputSource(new FileInputStream(
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/valid.xml")));
  +        InputSource schema = new InputSource(new FileInputStream(
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/test.xsd")));
  +        assertTrue(isValid(document, schema));
  +    }
  +  
  +   
  +    public void testValidateSchemaInvalidOne() throws Exception {
  +        String basedir = System.getProperty("basedir");
  +        InputSource document = new InputSource(new FileInputStream(
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/invalid.xml")));
  +        InputSource schema = new InputSource(new FileInputStream( 
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/test.xsd")));
  +        assertFalse(isValid(document, schema));
  +    }
  +    
  +    public void testValidateSchemaValidTwo() throws Exception {
  +        String basedir = System.getProperty("basedir");
  +        InputSource document = new InputSource(new FileInputStream(
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/valid-personnel-schema.xml")));
  +        InputSource schema = new InputSource(new FileInputStream(
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/personnel.xsd")));
  +        assertTrue(isValid(document, schema));
  +    }
  +  
  +   
  +    public void testValidateSchemaInvalidTwo() throws Exception {
  +        String basedir = System.getProperty("basedir");
  +        InputSource document = new InputSource(new FileInputStream(
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/invalid-personnel-schema.xml")));
  +        InputSource schema = new InputSource(new FileInputStream( 
  +            new File(basedir,"src/test/org/apache/commons/betwixt/xmlunit/personnel.xsd")));
  +        assertFalse(isValid(document, schema));
       }
   }
  
  
  
  1.11.2.1  +120 -4    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
  retrieving revision 1.11.2.1
  diff -u -r1.11 -r1.11.2.1
  --- XmlTestCase.java	19 Oct 2003 14:44:53 -0000	1.11
  +++ XmlTestCase.java	1 Feb 2004 13:48:27 -0000	1.11.2.1
  @@ -7,7 +7,7 @@
    * 
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2004 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -78,6 +78,7 @@
   import junit.framework.AssertionFailedError;
   import junit.framework.TestCase;
   
  +import org.apache.xerces.parsers.SAXParser;
   import org.w3c.dom.Attr;
   import org.w3c.dom.DOMException;
   import org.w3c.dom.Document;
  @@ -86,15 +87,40 @@
   import org.w3c.dom.NodeList;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  +import org.xml.sax.SAXParseException;
  +import org.xml.sax.helpers.DefaultHandler;
   
   /** 
     * Provides xml test utilities. 
     * Hopefully, these might be moved into [xmlunit] sometime.
     *
     * @author Robert Burrell Donkin
  +  * @author Khaled Noaman, IBM (some portions derived from test code originally contributed
to the Apache Xerces project)
     */
   public class XmlTestCase extends TestCase {
   
  +    private static final String NAMESPACES_FEATURE_ID 
  +                            = "http://xml.org/sax/features/namespaces";
  +
  +    private static final String NAMESPACE_PREFIXES_FEATURE_ID 
  +                            = "http://xml.org/sax/features/namespace-prefixes";
  +
  +    private static final String VALIDATION_FEATURE_ID 
  +                            = "http://xml.org/sax/features/validation";
  +
  +    private static final String SCHEMA_VALIDATION_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/schema";
  +
  +    private static final String SCHEMA_FULL_CHECKING_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/schema-full-checking";
  +
  +    private static final String DYNAMIC_VALIDATION_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/dynamic";
  +
  +    private static final String NONAMESPACE_SCHEMA_LOCATION_PROPERTY_ID 
  +        = "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
  +
  +
       protected static boolean debug = false;
   
       DocumentBuilderFactory domFactory;
  @@ -415,6 +441,15 @@
               System.out.println("[XmlTestCase]" + message);
           }
       }
  +
  +    
  +    private void log(String message, Exception e)
  +    {
  +        if (debug) {
  +            System.out.println("[XmlTestCase]" + message);
  +            e.printStackTrace();
  +        }
  +    }
       
       private String trim(String trimThis)
       {
  @@ -461,6 +496,87 @@
               
               return nameOne.compareTo(nameTwo);
           }
  +    }
  +    
  +    
  +    public void validateWithSchema(InputSource documentSource, final InputSource schemaSource)

  +            throws ParserConfigurationException, SAXException, IOException
  +    {
  +        class XMLUnitHandler extends DefaultHandler {
  +            ArrayList errors = new ArrayList();
  +            ArrayList warnings = new ArrayList();
  +            InputSource schemaSource;
  +    
  +            XMLUnitHandler(InputSource schemaSource) {
  +                this.schemaSource = schemaSource;
  +                schemaSource.setSystemId("schema.xsd");
  +            }
  +    
  +            public InputSource resolveEntity(String publicId, String systemId) {
  +                return schemaSource;
  +            }
  +    
  +            public void error(SAXParseException ex) {
  +                errors.add(ex);
  +            }
  +    
  +            public void warning(SAXParseException ex) {
  +                warnings.add(ex);
  +            }
  +    
  +            void reportErrors() throws SAXException {
  +                if (errors.size() > 0) {
  +                    throw (SAXException) errors.get(0);
  +                }
  +            }
  +    
  +        }
  +
  +        // it's not all that good to have a concrete dependency on Xerces
  +        // and a particular version, at that.
  +        // but schema support in the Xerces series of parsers is variable
  +        // and some of the configuration details differ.
  +        // At least this way seems reliable
  +        SAXParser parser = new SAXParser();
  +    
  +        // Set features
  +        parser.setFeature(NAMESPACES_FEATURE_ID, true);
  +        parser.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, false);
  +        parser.setFeature(VALIDATION_FEATURE_ID, true);
  +        parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
  +        parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
  +        parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);
  +    
  +        // Set properties
  +        parser.setProperty(NONAMESPACE_SCHEMA_LOCATION_PROPERTY_ID, "schema.xsd");
  +    
  +        XMLUnitHandler handler = new XMLUnitHandler(schemaSource);
  +    
  +        // Set handlers
  +        parser.setContentHandler(handler);
  +        parser.setErrorHandler(handler);
  +        parser.setEntityResolver(handler);
  +    
  +        // parse document
  +        parser.parse(documentSource);
  +        handler.reportErrors();
  +    }
  +    
  +    public boolean isValid(InputSource documentSource, InputSource schemaSource) 
  +            throws ParserConfigurationException, IOException
  +    {
  +        boolean result = false;
  +        try
  +        {
  +            validateWithSchema(documentSource, schemaSource);
  +            result = true;
  +        }
  +        catch (SAXException se)
  +        {
  +            log("Validation failed.", se);
  +        }
  +        
  +        return result;
       }
   }
   
  
  
  
  No                   revision
  
  Index: XmlTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/XmlTestCase.java,v
  retrieving revision 1.11
  retrieving revision 1.11.2.1
  diff -u -r1.11 -r1.11.2.1
  --- XmlTestCase.java	19 Oct 2003 14:44:53 -0000	1.11
  +++ XmlTestCase.java	1 Feb 2004 13:48:27 -0000	1.11.2.1
  @@ -7,7 +7,7 @@
    * 
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2004 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -78,6 +78,7 @@
   import junit.framework.AssertionFailedError;
   import junit.framework.TestCase;
   
  +import org.apache.xerces.parsers.SAXParser;
   import org.w3c.dom.Attr;
   import org.w3c.dom.DOMException;
   import org.w3c.dom.Document;
  @@ -86,15 +87,40 @@
   import org.w3c.dom.NodeList;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  +import org.xml.sax.SAXParseException;
  +import org.xml.sax.helpers.DefaultHandler;
   
   /** 
     * Provides xml test utilities. 
     * Hopefully, these might be moved into [xmlunit] sometime.
     *
     * @author Robert Burrell Donkin
  +  * @author Khaled Noaman, IBM (some portions derived from test code originally contributed
to the Apache Xerces project)
     */
   public class XmlTestCase extends TestCase {
   
  +    private static final String NAMESPACES_FEATURE_ID 
  +                            = "http://xml.org/sax/features/namespaces";
  +
  +    private static final String NAMESPACE_PREFIXES_FEATURE_ID 
  +                            = "http://xml.org/sax/features/namespace-prefixes";
  +
  +    private static final String VALIDATION_FEATURE_ID 
  +                            = "http://xml.org/sax/features/validation";
  +
  +    private static final String SCHEMA_VALIDATION_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/schema";
  +
  +    private static final String SCHEMA_FULL_CHECKING_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/schema-full-checking";
  +
  +    private static final String DYNAMIC_VALIDATION_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/dynamic";
  +
  +    private static final String NONAMESPACE_SCHEMA_LOCATION_PROPERTY_ID 
  +        = "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
  +
  +
       protected static boolean debug = false;
   
       DocumentBuilderFactory domFactory;
  @@ -415,6 +441,15 @@
               System.out.println("[XmlTestCase]" + message);
           }
       }
  +
  +    
  +    private void log(String message, Exception e)
  +    {
  +        if (debug) {
  +            System.out.println("[XmlTestCase]" + message);
  +            e.printStackTrace();
  +        }
  +    }
       
       private String trim(String trimThis)
       {
  @@ -461,6 +496,87 @@
               
               return nameOne.compareTo(nameTwo);
           }
  +    }
  +    
  +    
  +    public void validateWithSchema(InputSource documentSource, final InputSource schemaSource)

  +            throws ParserConfigurationException, SAXException, IOException
  +    {
  +        class XMLUnitHandler extends DefaultHandler {
  +            ArrayList errors = new ArrayList();
  +            ArrayList warnings = new ArrayList();
  +            InputSource schemaSource;
  +    
  +            XMLUnitHandler(InputSource schemaSource) {
  +                this.schemaSource = schemaSource;
  +                schemaSource.setSystemId("schema.xsd");
  +            }
  +    
  +            public InputSource resolveEntity(String publicId, String systemId) {
  +                return schemaSource;
  +            }
  +    
  +            public void error(SAXParseException ex) {
  +                errors.add(ex);
  +            }
  +    
  +            public void warning(SAXParseException ex) {
  +                warnings.add(ex);
  +            }
  +    
  +            void reportErrors() throws SAXException {
  +                if (errors.size() > 0) {
  +                    throw (SAXException) errors.get(0);
  +                }
  +            }
  +    
  +        }
  +
  +        // it's not all that good to have a concrete dependency on Xerces
  +        // and a particular version, at that.
  +        // but schema support in the Xerces series of parsers is variable
  +        // and some of the configuration details differ.
  +        // At least this way seems reliable
  +        SAXParser parser = new SAXParser();
  +    
  +        // Set features
  +        parser.setFeature(NAMESPACES_FEATURE_ID, true);
  +        parser.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, false);
  +        parser.setFeature(VALIDATION_FEATURE_ID, true);
  +        parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
  +        parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
  +        parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);
  +    
  +        // Set properties
  +        parser.setProperty(NONAMESPACE_SCHEMA_LOCATION_PROPERTY_ID, "schema.xsd");
  +    
  +        XMLUnitHandler handler = new XMLUnitHandler(schemaSource);
  +    
  +        // Set handlers
  +        parser.setContentHandler(handler);
  +        parser.setErrorHandler(handler);
  +        parser.setEntityResolver(handler);
  +    
  +        // parse document
  +        parser.parse(documentSource);
  +        handler.reportErrors();
  +    }
  +    
  +    public boolean isValid(InputSource documentSource, InputSource schemaSource) 
  +            throws ParserConfigurationException, IOException
  +    {
  +        boolean result = false;
  +        try
  +        {
  +            validateWithSchema(documentSource, schemaSource);
  +            result = true;
  +        }
  +        catch (SAXException se)
  +        {
  +            log("Validation failed.", se);
  +        }
  +        
  +        return result;
       }
   }
   
  
  
  
  No                   revision
  
  Index: XmlTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/XmlTestCase.java,v
  retrieving revision 1.11
  retrieving revision 1.11.2.1
  diff -u -r1.11 -r1.11.2.1
  --- XmlTestCase.java	19 Oct 2003 14:44:53 -0000	1.11
  +++ XmlTestCase.java	1 Feb 2004 13:48:27 -0000	1.11.2.1
  @@ -7,7 +7,7 @@
    * 
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2004 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -78,6 +78,7 @@
   import junit.framework.AssertionFailedError;
   import junit.framework.TestCase;
   
  +import org.apache.xerces.parsers.SAXParser;
   import org.w3c.dom.Attr;
   import org.w3c.dom.DOMException;
   import org.w3c.dom.Document;
  @@ -86,15 +87,40 @@
   import org.w3c.dom.NodeList;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  +import org.xml.sax.SAXParseException;
  +import org.xml.sax.helpers.DefaultHandler;
   
   /** 
     * Provides xml test utilities. 
     * Hopefully, these might be moved into [xmlunit] sometime.
     *
     * @author Robert Burrell Donkin
  +  * @author Khaled Noaman, IBM (some portions derived from test code originally contributed
to the Apache Xerces project)
     */
   public class XmlTestCase extends TestCase {
   
  +    private static final String NAMESPACES_FEATURE_ID 
  +                            = "http://xml.org/sax/features/namespaces";
  +
  +    private static final String NAMESPACE_PREFIXES_FEATURE_ID 
  +                            = "http://xml.org/sax/features/namespace-prefixes";
  +
  +    private static final String VALIDATION_FEATURE_ID 
  +                            = "http://xml.org/sax/features/validation";
  +
  +    private static final String SCHEMA_VALIDATION_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/schema";
  +
  +    private static final String SCHEMA_FULL_CHECKING_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/schema-full-checking";
  +
  +    private static final String DYNAMIC_VALIDATION_FEATURE_ID 
  +                            = "http://apache.org/xml/features/validation/dynamic";
  +
  +    private static final String NONAMESPACE_SCHEMA_LOCATION_PROPERTY_ID 
  +        = "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
  +
  +
       protected static boolean debug = false;
   
       DocumentBuilderFactory domFactory;
  @@ -415,6 +441,15 @@
               System.out.println("[XmlTestCase]" + message);
           }
       }
  +
  +    
  +    private void log(String message, Exception e)
  +    {
  +        if (debug) {
  +            System.out.println("[XmlTestCase]" + message);
  +            e.printStackTrace();
  +        }
  +    }
       
       private String trim(String trimThis)
       {
  @@ -461,6 +496,87 @@
               
               return nameOne.compareTo(nameTwo);
           }
  +    }
  +    
  +    
  +    public void validateWithSchema(InputSource documentSource, final InputSource schemaSource)

  +            throws ParserConfigurationException, SAXException, IOException
  +    {
  +        class XMLUnitHandler extends DefaultHandler {
  +            ArrayList errors = new ArrayList();
  +            ArrayList warnings = new ArrayList();
  +            InputSource schemaSource;
  +    
  +            XMLUnitHandler(InputSource schemaSource) {
  +                this.schemaSource = schemaSource;
  +                schemaSource.setSystemId("schema.xsd");
  +            }
  +    
  +            public InputSource resolveEntity(String publicId, String systemId) {
  +                return schemaSource;
  +            }
  +    
  +            public void error(SAXParseException ex) {
  +                errors.add(ex);
  +            }
  +    
  +            public void warning(SAXParseException ex) {
  +                warnings.add(ex);
  +            }
  +    
  +            void reportErrors() throws SAXException {
  +                if (errors.size() > 0) {
  +                    throw (SAXException) errors.get(0);
  +                }
  +            }
  +    
  +        }
  +
  +        // it's not all that good to have a concrete dependency on Xerces
  +        // and a particular version, at that.
  +        // but schema support in the Xerces series of parsers is variable
  +        // and some of the configuration details differ.
  +        // At least this way seems reliable
  +        SAXParser parser = new SAXParser();
  +    
  +        // Set features
  +        parser.setFeature(NAMESPACES_FEATURE_ID, true);
  +        parser.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, false);
  +        parser.setFeature(VALIDATION_FEATURE_ID, true);
  +        parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
  +        parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
  +        parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);
  +    
  +        // Set properties
  +        parser.setProperty(NONAMESPACE_SCHEMA_LOCATION_PROPERTY_ID, "schema.xsd");
  +    
  +        XMLUnitHandler handler = new XMLUnitHandler(schemaSource);
  +    
  +        // Set handlers
  +        parser.setContentHandler(handler);
  +        parser.setErrorHandler(handler);
  +        parser.setEntityResolver(handler);
  +    
  +        // parse document
  +        parser.parse(documentSource);
  +        handler.reportErrors();
  +    }
  +    
  +    public boolean isValid(InputSource documentSource, InputSource schemaSource) 
  +            throws ParserConfigurationException, IOException
  +    {
  +        boolean result = false;
  +        try
  +        {
  +            validateWithSchema(documentSource, schemaSource);
  +            result = true;
  +        }
  +        catch (SAXException se)
  +        {
  +            log("Validation failed.", se);
  +        }
  +        
  +        return result;
       }
   }
   
  
  
  
  1.1.2.1   +44 -0     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/Attic/invalid-personnel-schema.xml
  
  
  
  

---------------------------------------------------------------------
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