8220818: Validator does not find missing match for keyref error jdk-13+19
authorjoehw
Wed, 01 May 2019 14:35:28 -0700
changeset 54672 a43d6467317d
parent 54671 41339a468716
child 54673 67b040623a12
8220818: Validator does not find missing match for keyref error Reviewed-by: lancea
src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java
test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a.xsd
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Invalid.xml
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid.xml
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid1.xml
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b.xsd
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Invalid.xml
test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Valid.xml
--- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java	Wed May 01 13:25:54 2019 -0700
+++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java	Wed May 01 14:35:28 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -111,7 +111,7 @@
  * @author Elena Litani IBM
  * @author Andy Clark IBM
  * @author Neeraj Bajaj, Sun Microsystems, inc.
- * @LastModified: Nov 2017
+ * @LastModified: Apr 2019
  */
 public class XMLSchemaValidator
     implements XMLComponent, XMLDocumentFilter, FieldActivator, RevalidationHandler, XSElementDeclHelper {
@@ -2429,7 +2429,7 @@
                             fValueStoreCache.getValueStoreFor(id, selMatcher.getInitialDepth());
                         // nothing to do if nothing matched, or if not all
                         // fields are present.
-                        if (values != null && values.fValuesCount == values.fFieldCount)
+                        if (values != null && values.fHasValue)
                             values.endDocumentFragment();
                     }
                 }
@@ -3718,6 +3718,7 @@
 
         /** Current data value count. */
         protected int fValuesCount;
+        protected boolean fHasValue = false;
 
         /** global data */
         public final Vector<Object> fValues = new Vector<>();
@@ -3885,6 +3886,7 @@
             }
             else {
                 fValuesCount++;
+                fHasValue = true;
             }
             fLocalValues[i] = actualValue;
             fLocalValueTypes[i] = valueType;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2019, 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 validation;
+
+
+import java.io.File;
+import java.net.URL;
+
+import javax.xml.XMLConstants;
+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.SAXParseException;
+
+/*
+ * @test
+ * @bug 8220818
+ * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
+ * @run testng/othervm validation.ValidationTest
+ * @summary Runs validations with schemas and sources
+ */
+@Listeners({jaxp.library.FilePolicy.class})
+public class ValidationTest {
+    static final String FILE_PATH = "files/";
+    /*
+     DataProvider: valid xml
+     */
+    @DataProvider(name = "valid")
+    Object[][] getValid() {
+        return new Object[][]{
+            {"JDK8220818a.xsd", "JDK8220818a_Valid.xml"},
+            {"JDK8220818a.xsd", "JDK8220818a_Valid1.xml"},
+            {"JDK8220818b.xsd", "JDK8220818b_Valid.xml"},
+        };
+    }
+
+    /*
+     DataProvider: invalid xml
+     */
+    @DataProvider(name = "invalid")
+    Object[][] getInvalid() {
+        return new Object[][]{
+            {"JDK8220818a.xsd", "JDK8220818a_Invalid.xml"},
+            {"JDK8220818b.xsd", "JDK8220818b_Invalid.xml"},
+        };
+    }
+
+    @Test(dataProvider = "invalid", expectedExceptions = SAXParseException.class)
+    public void testValidateRefType(String xsd, String xml) throws Exception {
+        validate(xsd, xml);
+    }
+
+    @Test(dataProvider = "valid")
+    public void testValidateRefType1(String xsd, String xml) throws Exception {
+        validate(xsd, xml);
+    }
+
+    private void validate(String xsd, String xml) throws Exception {
+        final SchemaFactory schemaFactory = SchemaFactory.newInstance(
+                XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        final Schema schema = schemaFactory.newSchema(
+                new File(getClass().getResource(FILE_PATH + xsd).getFile()));
+        final Validator validator = schema.newValidator();
+        validator.validate(new StreamSource(
+                new File(getClass().getResource(FILE_PATH + xml).getFile())));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a.xsd	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,23 @@
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+    <xs:element name="root">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element name="key" type="xs:string" minOccurs="0">
+                    <xs:key name="key">
+                        <xs:selector xpath="."/>
+                        <xs:field xpath="."/>
+                    </xs:key>
+                </xs:element>
+                <xs:element name="keyref">
+                    <xs:complexType>
+                        <xs:attribute name="att" type="xs:string"/>
+                    </xs:complexType>
+                </xs:element>
+            </xs:sequence>
+        </xs:complexType>
+        <xs:keyref name="keyref" refer="key">
+            <xs:selector xpath="keyref"/>
+            <xs:field xpath="@att"/>
+        </xs:keyref>
+    </xs:element>
+</xs:schema>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Invalid.xml	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,3 @@
+<root>
+    <keyref att="xyz"/>
+</root>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid.xml	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,4 @@
+<root>
+    <key>xyz</key>
+    <keyref att="xyz"/>
+</root>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid1.xml	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,3 @@
+<root>
+    <keyref/>
+</root>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b.xsd	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,84 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+           xmlns:example="http://www.example.org/root"
+           xmlns="http://www.example.org/root"
+           targetNamespace="http://www.example.org/root"
+           elementFormDefault="qualified" attributeFormDefault="unqualified">
+    <xs:element name="Root">
+        <xs:complexType>
+            <xs:choice maxOccurs="unbounded">
+                <xs:element ref="Object" />
+            </xs:choice>
+        </xs:complexType>
+        <xs:key name="name_key">
+            <xs:selector xpath=".//example:Object" />
+            <xs:field xpath="@name" />
+        </xs:key>
+        <xs:keyref name="Collectiontype_defined" refer="name_key">
+            <xs:selector xpath=".//example:CollectionMember" />
+            <xs:field xpath="@type" />
+        </xs:keyref>
+    </xs:element>
+    <xs:element name="Members">
+        <xs:complexType>
+            <xs:group ref="example:memberTypes" minOccurs="0"
+                      maxOccurs="unbounded" />
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="Object">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="Members" minOccurs="0" />
+            </xs:sequence>
+            <xs:attributeGroup ref="example:typedMemberAttributes" />
+            <xs:attributeGroup ref="example:namedMemberAttributes" />
+        </xs:complexType>
+        <xs:unique name="ObjectElementNameUniqueness">
+            <xs:selector xpath="example:Members/*" />
+            <xs:field xpath="@name" />
+        </xs:unique>
+    </xs:element>
+    <xs:element name="Collection">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="CollectionMembers" />
+            </xs:sequence>
+            <xs:attributeGroup ref="example:namedMemberAttributes" />
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="CollectionMembers">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="CollectionMember" minOccurs="2"
+                            maxOccurs="unbounded" />
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="CollectionMember">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="Members" minOccurs="0"
+                            maxOccurs="unbounded" />
+            </xs:sequence>
+            <xs:attributeGroup ref="example:typedMemberAttributes" />
+            <xs:attributeGroup ref="example:namedMemberAttributes" />
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="String">
+        <xs:complexType>
+            <xs:attributeGroup ref="example:namedMemberAttributes" />
+        </xs:complexType>
+    </xs:element>
+    <xs:group name="memberTypes">
+        <xs:choice>
+            <xs:element ref="String" />
+            <xs:element ref="Collection" />
+        </xs:choice>
+    </xs:group>
+    <xs:attributeGroup name="namedMemberAttributes">
+        <xs:attribute name="name" type="xs:Name" use="required" />
+    </xs:attributeGroup>
+    <xs:attributeGroup name="typedMemberAttributes">
+        <xs:attribute name="type" type="xs:Name" />
+    </xs:attributeGroup>
+</xs:schema> 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Invalid.xml	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<Root xmlns="http://www.example.org/root">
+
+    <Object name="object2">
+        <Members>
+            <Collection name="Collection1">
+                <CollectionMembers>
+                    <CollectionMember name="Collectionmember1" type="object1" />
+                    <CollectionMember name="Collectionmember2" >
+                        <Members>
+                            <String name="string1" />
+                        </Members>
+                    </CollectionMember>
+                </CollectionMembers>
+            </Collection>
+        </Members>
+    </Object>
+</Root> 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Valid.xml	Wed May 01 14:35:28 2019 -0700
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<Root xmlns="http://www.example.org/root">
+
+    <Object name="object1">
+        <Members>
+            <String name="string1" />
+        </Members>
+    </Object>
+
+    <Object name="object2">
+        <Members>
+            <Collection name="Collection1">
+                <CollectionMembers>
+                    <CollectionMember name="Collectionmember1" type="object1" />
+                    <CollectionMember name="Collectionmember2" >
+                        <Members>
+                            <String name="string1" />
+                        </Members>
+                    </CollectionMember>
+                </CollectionMembers>
+            </Collection>
+        </Members>
+    </Object>
+</Root>