8068376: Validator fails valid XML files due to String == in XSD validator code
authorjoehw
Fri, 26 Jul 2019 17:15:17 +0000
changeset 57545 550a1a6ca596
parent 57544 99d2dd7b84a8
child 57546 1ca1cfdcb451
child 57548 1f05f7952295
8068376: Validator fails valid XML files due to String == in XSD validator code Reviewed-by: lancea
src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/SubstitutionGroupHandler.java
test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8068376.xml
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8068376.xsd
--- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/SubstitutionGroupHandler.java	Fri Jul 26 08:56:28 2019 -0700
+++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/SubstitutionGroupHandler.java	Fri Jul 26 17:15:17 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -30,6 +30,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 /**
  * To store and validate information about substitutionGroup
@@ -38,7 +39,7 @@
  *
  * @author Sandy Gao, IBM
  *
- * @LastModified: Nov 2017
+ * @LastModified: July 2019
  */
 public class SubstitutionGroupHandler {
 
@@ -57,8 +58,8 @@
     // 3.9.4 Element Sequence Locally Valid (Particle) 2.3.3
     // check whether one element decl matches an element with the given qname
     public XSElementDecl getMatchingElemDecl(QName element, XSElementDecl exemplar) {
-        if (element.localpart == exemplar.fName &&
-            element.uri == exemplar.fTargetNamespace) {
+        if (Objects.equals(element.localpart, exemplar.fName) &&
+            Objects.equals(element.uri, exemplar.fTargetNamespace)) {
             return exemplar;
         }
 
--- a/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java	Fri Jul 26 08:56:28 2019 -0700
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java	Fri Jul 26 17:15:17 2019 +0000
@@ -25,19 +25,27 @@
 
 
 import java.io.File;
-import java.net.URL;
-
+import java.io.FileInputStream;
 import javax.xml.XMLConstants;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.transform.Source;
+import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 import org.testng.annotations.DataProvider;
-
 import org.testng.annotations.Listeners;
 import org.testng.annotations.Test;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLFilter;
+import org.xml.sax.helpers.XMLFilterImpl;
 
 /*
  * @test
@@ -106,6 +114,50 @@
         validate(xsd, xml);
     }
 
+    /**
+     * @bug 8068376
+     * Verifies that validation performs normally with externally provided string
+     * parameters.
+     * @throws Exception if the test fails
+     */
+    @Test
+    public void testJDK8068376() throws Exception {
+
+        String xsdFile = getClass().getResource(FILE_PATH + "JDK8068376.xsd").getFile();
+        String xmlFile = getClass().getResource(FILE_PATH + "JDK8068376.xml").getFile();
+        String targetNamespace = getTargetNamespace(xsdFile);
+
+        XMLFilter namespaceFilter = new XMLFilterImpl(SAXParserFactory.newDefaultNSInstance().newSAXParser().getXMLReader()) {
+        @Override
+        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
+            uri = targetNamespace; // overwriting the uri with our own choice
+            super.startElement(uri, localName, qName, atts);
+        }
+        };
+
+        Source xmlSource = new SAXSource(namespaceFilter, new InputSource(xmlFile));
+        Source schemaSource = new StreamSource(xsdFile);
+        validate(schemaSource, xmlSource);
+
+    }
+
+    private static String getTargetNamespace(String xsdFile) throws Exception {
+        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile));
+        while (reader.hasNext()) {
+            int event = reader.next();
+
+            // Get the root element's "targetNamespace" attribute
+            if (event == XMLEvent.START_ELEMENT) {
+                // validation fails before patch
+                String value = reader.getAttributeValue(null, "targetNamespace"); // fails validation
+                // validation passes due to a reference comparison in the original code
+                // String value = "mynamespace";
+                return value;
+            }
+        }
+        return null;
+    }
+
     private void validate(String xsd, String xml) throws Exception {
         final SchemaFactory schemaFactory = SchemaFactory.newInstance(
                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
@@ -116,4 +168,12 @@
                 new File(getClass().getResource(FILE_PATH + xml).getFile())));
     }
 
+    private void validate(Source xsd, Source xml) throws Exception {
+        final SchemaFactory schemaFactory = SchemaFactory.newInstance(
+                XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        final Schema schema = schemaFactory.newSchema(xsd);
+        final Validator validator = schema.newValidator();
+        validator.validate(xml);
+    }
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8068376.xml	Fri Jul 26 17:15:17 2019 +0000
@@ -0,0 +1,1 @@
+<root><childtag></childtag></root>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8068376.xsd	Fri Jul 26 17:15:17 2019 +0000
@@ -0,0 +1,11 @@
+<xs:schema xmlns="mynamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="mynamespace" elementFormDefault="qualified">
+    <xs:element name="root">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element name="childtag" minOccurs="0" maxOccurs="1">
+                    <xs:complexType></xs:complexType>
+                </xs:element>
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+</xs:schema> 
\ No newline at end of file