8080907: Develop test for Xerces Update: XML Schema Validation
authorjoehw
Mon, 15 Jun 2015 09:25:23 -0700
changeset 31206 cfbd14bd78d6
parent 31205 4beb56e4dc51
child 31207 3a7fb5940d2f
8080907: Develop test for Xerces Update: XML Schema Validation Reviewed-by: lancea, joehw Contributed-by: frank.yuan@oracle.com
jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DocumentBuilderFactoryTest.java
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/SchemaFactoryTest.java
jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/AnyElementTest.java
jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/ProcessContents-lax-error.xml
jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/ProcessContents-ok.xml
jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/ProcessContents.xsd
--- a/jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DocumentBuilderFactoryTest.java	Fri Jun 12 16:39:25 2015 -0400
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DocumentBuilderFactoryTest.java	Mon Jun 15 09:25:23 2015 -0700
@@ -23,28 +23,32 @@
 
 package javax.xml.parsers.ptests;
 
+import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
+import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
+import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
+import static jaxp.library.JAXPTestUtilities.USER_DIR;
+import static jaxp.library.JAXPTestUtilities.compareWithGold;
+import static jaxp.library.JAXPTestUtilities.filenameToURL;
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
 
 import java.io.BufferedReader;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FilePermission;
 import java.io.FileReader;
 
-import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
-
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.FactoryConfigurationError;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
-
-import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
-import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
-
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
@@ -52,10 +56,6 @@
 
 import jaxp.library.JAXPDataProvider;
 import jaxp.library.JAXPFileBaseTest;
-import static jaxp.library.JAXPTestUtilities.USER_DIR;
-import static jaxp.library.JAXPTestUtilities.compareWithGold;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
 
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
@@ -67,6 +67,7 @@
 import org.xml.sax.helpers.DefaultHandler;
 
 /**
+ * @bug 8080907
  * This checks the methods of DocumentBuilderFactoryImpl.
  */
 public class DocumentBuilderFactoryTest extends JAXPFileBaseTest {
@@ -134,28 +135,11 @@
         assertFalse(eh.isErrorOccured());
     }
 
-    /**
-     * Test the default functionality of schema support method. In
-     * this case the schema source property is set.
-     * @throws Exception If any errors occur.
-     */
-    @Test
-    public void testCheckSchemaSupport2() throws Exception {
-        try (FileInputStream fis = new FileInputStream(new File(
-                XML_DIR, "test.xsd"))) {
-            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-            dbf.setValidating(true);
-            dbf.setNamespaceAware(true);
-            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
-                    W3C_XML_SCHEMA_NS_URI);
-            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
-                    new InputSource(fis));
-            MyErrorHandler eh = MyErrorHandler.newInstance();
-            DocumentBuilder db = dbf.newDocumentBuilder();
-            db.setErrorHandler(eh);
-            db.parse(new File(XML_DIR, "test1.xml"));
-            assertFalse(eh.isErrorOccured());
-        }
+    @DataProvider(name = "schema-source")
+    public Object[][] getSchemaSource() throws FileNotFoundException {
+        return new Object[][] {
+                { new FileInputStream(new File(XML_DIR, "test.xsd")) },
+                { new InputSource(filenameToURL(XML_DIR + "test.xsd")) } };
     }
 
     /**
@@ -163,22 +147,50 @@
      * this case the schema source property is set.
      * @throws Exception If any errors occur.
      */
-    @Test
-    public void testCheckSchemaSupport3() throws Exception {
-        try (FileInputStream fis = new FileInputStream(new File(
-                XML_DIR, "test.xsd"))) {
+    @Test(dataProvider = "schema-source")
+    public void testCheckSchemaSupport2(Object schemaSource) throws Exception {
+        try {
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            dbf.setValidating(true);
+            dbf.setNamespaceAware(true);
+            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
+                    W3C_XML_SCHEMA_NS_URI);
+            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource);
+            MyErrorHandler eh = MyErrorHandler.newInstance();
+            DocumentBuilder db = dbf.newDocumentBuilder();
+            db.setErrorHandler(eh);
+            db.parse(new File(XML_DIR, "test1.xml"));
+            assertFalse(eh.isErrorOccured());
+        } finally {
+            if (schemaSource instanceof Closeable) {
+                ((Closeable) schemaSource).close();
+            }
+        }
+
+    }
+
+    /**
+     * Test the default functionality of schema support method. In
+     * this case the schema source property is set.
+     * @throws Exception If any errors occur.
+     */
+    @Test(dataProvider = "schema-source")
+    public void testCheckSchemaSupport3(Object schemaSource) throws Exception {
+        try {
             SAXParserFactory spf = SAXParserFactory.newInstance();
-            spf.setNamespaceAware(true);
             spf.setValidating(true);
             spf.setNamespaceAware(true);
             SAXParser sp = spf.newSAXParser();
             sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                     W3C_XML_SCHEMA_NS_URI);
-            sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
-                    new InputSource(fis));
+            sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource);
             DefaultHandler dh = new DefaultHandler();
             // Not expect any unrecoverable error here.
             sp.parse(new File(XML_DIR, "test1.xml"), dh);
+        } finally {
+            if (schemaSource instanceof Closeable) {
+                ((Closeable) schemaSource).close();
+            }
         }
     }
 
--- a/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/SchemaFactoryTest.java	Fri Jun 12 16:39:25 2015 -0400
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/SchemaFactoryTest.java	Mon Jun 15 09:25:23 2015 -0700
@@ -27,6 +27,7 @@
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -39,9 +40,12 @@
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
@@ -60,6 +64,7 @@
 import org.xml.sax.SAXParseException;
 
 /*
+ * @bug 8080907
  * @summary Class containing the test cases for SchemaFactory
  */
 @Test(singleThreaded = true)
@@ -68,8 +73,9 @@
     @BeforeClass
     public void setup() throws SAXException, IOException, ParserConfigurationException {
         sf = newSchemaFactory();
+        assertNotNull(sf);
 
-        assertNotNull(sf);
+        ifac = XMLInputFactory.newInstance();
 
         xsd1 = Files.readAllBytes(Paths.get(XML_DIR + "test.xsd"));
         xsd2 = Files.readAllBytes(Paths.get(XML_DIR + "test1.xsd"));
@@ -152,11 +158,13 @@
     }
 
     @DataProvider(name = "valid-source")
-    public Object[][] getValidSource() {
+    public Object[][] getValidSource() throws XMLStreamException {
         return new Object[][] {
                 { streamSource(xsd1) },
                 { saxSource(xsd1) },
-                { domSource(xsdDoc1) } };
+                { domSource(xsdDoc1) },
+                { staxStreamSource(xsd1) },
+                { staxEventSource(xsd1) } };
 
     }
 
@@ -299,6 +307,34 @@
         sf.setFeature(null, true);
     }
 
+    @DataProvider(name = "source-feature")
+    public Object[][] getSourceFeature() {
+        return new Object[][] {
+                { StreamSource.FEATURE },
+                { SAXSource.FEATURE },
+                { DOMSource.FEATURE },
+                { DOMSource.FEATURE } };
+
+    }
+
+    /*
+     * Return true for each of the JAXP Source features to indicate that this
+     * SchemaFactory supports all of the built-in JAXP Source types.
+     */
+    @Test(dataProvider = "source-feature")
+    public void testSourceFeatureGet(String sourceFeature) throws Exception {
+        assertTrue(newSchemaFactory().getFeature(sourceFeature));
+    }
+
+    /*
+     * JAXP Source features are read-only because this SchemaFactory always
+     * supports all JAXP Source types.
+     */
+    @Test(dataProvider = "source-feature", expectedExceptions = SAXNotSupportedException.class)
+    public void testSourceFeatureSet(String sourceFeature) throws Exception {
+        newSchemaFactory().setFeature(sourceFeature, false);
+    }
+
     @Test(expectedExceptions = IllegalArgumentException.class)
     public void testInvalidSchemaLanguage() {
         final String INVALID_SCHEMA_LANGUAGE = "http://relaxng.org/ns/structure/1.0";
@@ -337,6 +373,15 @@
         return new DOMSource(xsdDoc);
     }
 
+    private Source staxStreamSource(byte[] xsd) throws XMLStreamException {
+        return new StAXSource(ifac.createXMLStreamReader(newInputStream(xsd)));
+    }
+
+    private Source staxEventSource(byte[] xsd) throws XMLStreamException {
+        return new StAXSource(ifac.createXMLEventReader(newInputStream(xsd)));
+    }
+
+
     private SchemaFactory newSchemaFactory() {
         return SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
     }
@@ -346,6 +391,7 @@
     private static final String SCHEMA_FACTORY_CLASSNAME = "com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory";
 
     private SchemaFactory sf;
+    private XMLInputFactory ifac;
     private byte[] xsd1;
     private byte[] xsd2;
     private Document xsdDoc1;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/AnyElementTest.java	Mon Jun 15 09:25:23 2015 -0700
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package javax.xml.validation;
+
+/*
+ * @bug 8080907
+ * @summary Test processContents attribute of any element
+ */
+import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
+
+import java.net.URISyntaxException;
+
+import javax.xml.transform.stream.StreamSource;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+public class AnyElementTest {
+    @BeforeClass
+    public void setup() throws URISyntaxException, SAXException {
+        validator = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(getUri("ProcessContents.xsd"))).newValidator();
+    }
+
+    /*
+     * processContents attribute - Specifies how the XML processor should handle
+     * validation against the elements specified by this any element. Can be set
+     * to one of the following:
+     * strict - the XML processor must obtain the schema for the required
+     * namespaces and validate the elements (this is default)
+     * lax - same as strict, but if the schema cannot be obtained, no errors
+     * will occur
+     * skip - The XML processor does not attempt to validate any elements from
+     * the specified namespaces
+     */
+    @Test
+    public void testProcessContents() throws Exception {
+        validator.validate(new StreamSource(getUri("ProcessContents-ok.xml")));
+    }
+
+    /*
+     * When processContents="lax", validation will be performed when the element
+     * is declared in the schema.
+     */
+    @Test(expectedExceptions = SAXParseException.class)
+    public void testProcessContentsLax() throws Exception {
+        validator.validate(new StreamSource(getUri("ProcessContents-lax-error.xml")));
+    }
+
+    /*
+     * Get the URI of the file, which is in the same path as this class
+     */
+    private String getUri(String fileName) throws URISyntaxException {
+        return this.getClass().getResource(fileName).toURI().toASCIIString();
+    }
+
+    private Validator validator;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/ProcessContents-lax-error.xml	Mon Jun 15 09:25:23 2015 -0700
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<my_lax>
+   <my_int>25.5</my_int>
+</my_lax>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/ProcessContents-ok.xml	Mon Jun 15 09:25:23 2015 -0700
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<my_strict>
+   <my_int>255</my_int>
+   <my_skip>
+      <my_int>2.55</my_int>
+      <un_define/>
+      <my_lax>
+         <my_int>25.5</my_int>
+      </my_lax>
+      <my_strict>
+         <un_define>TTT</un_define>
+      </my_strict>
+   </my_skip>   
+   <my_lax>
+         <my_int>2555</my_int>
+         <un_define>TTT</un_define>
+         <my_strict>
+            <my_int>20</my_int>
+         </my_strict>
+   </my_lax>
+</my_strict>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/javax/xml/validation/ProcessContents.xsd	Mon Jun 15 09:25:23 2015 -0700
@@ -0,0 +1,34 @@
+<?xml version="1.0"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+<xs:element name="my_lax">
+   <xs:complexType>
+      <xs:sequence>
+         <xs:any processContents="lax" maxOccurs="unbounded"/>			  
+      </xs:sequence>
+   </xs:complexType>
+</xs:element>
+
+<xs:element name="my_skip">
+   <xs:complexType>
+      <xs:sequence>
+         <xs:any processContents="skip" maxOccurs="unbounded"/>			  
+      </xs:sequence>
+   </xs:complexType>
+</xs:element>
+
+<xs:element name="my_strict">
+   <xs:complexType>
+      <xs:sequence>
+         <xs:any maxOccurs="unbounded"/> <!-- by default, processContents="strict" -->
+      </xs:sequence>
+   </xs:complexType>
+</xs:element>
+
+<xs:element name="my_int">
+   <xs:simpleType>
+         <xs:restriction base="xs:int"/>
+   </xs:simpleType>
+</xs:element>
+
+</xs:schema>
\ No newline at end of file