From 05a9f3541b5da41d55f84f064c3dff39e84b0546 Mon Sep 17 00:00:00 2001 From: Joe Wang Date: Wed, 1 May 2019 14:35:28 -0700 Subject: [PATCH] 8220818: Validator does not find missing match for keyref error Reviewed-by: lancea --- .../internal/impl/xs/XMLSchemaValidator.java | 8 +- .../unittest/validation/ValidationTest.java | 93 +++++++++++++++++++ .../unittest/validation/files/JDK8220818a.xsd | 23 +++++ .../validation/files/JDK8220818a_Invalid.xml | 3 + .../validation/files/JDK8220818a_Valid.xml | 4 + .../validation/files/JDK8220818a_Valid1.xml | 3 + .../unittest/validation/files/JDK8220818b.xsd | 84 +++++++++++++++++ .../validation/files/JDK8220818b_Invalid.xml | 18 ++++ .../validation/files/JDK8220818b_Valid.xml | 24 +++++ 9 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a.xsd create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Invalid.xml create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid.xml create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid1.xml create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b.xsd create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Invalid.xml create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Valid.xml diff --git a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java index 23e558d30a0..7f52f82deb9 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java @@ -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 @@ import jdk.xml.internal.JdkXmlUtils; * @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 @@ public class XMLSchemaValidator 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 @@ public class XMLSchemaValidator /** Current data value count. */ protected int fValuesCount; + protected boolean fHasValue = false; /** global data */ public final Vector fValues = new Vector<>(); @@ -3885,6 +3886,7 @@ public class XMLSchemaValidator } else { fValuesCount++; + fHasValue = true; } fLocalValues[i] = actualValue; fLocalValueTypes[i] = valueType; diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java new file mode 100644 index 00000000000..42bfe7cf147 --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java @@ -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()))); + } +} diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a.xsd b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a.xsd new file mode 100644 index 00000000000..95962030d4f --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a.xsd @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Invalid.xml b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Invalid.xml new file mode 100644 index 00000000000..95073df49d9 --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Invalid.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid.xml b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid.xml new file mode 100644 index 00000000000..8e7189e49da --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid.xml @@ -0,0 +1,4 @@ + + xyz + + diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid1.xml b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid1.xml new file mode 100644 index 00000000000..d27e1c0f74e --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818a_Valid1.xml @@ -0,0 +1,3 @@ + + + diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b.xsd b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b.xsd new file mode 100644 index 00000000000..cdb51749d0d --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b.xsd @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Invalid.xml b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Invalid.xml new file mode 100644 index 00000000000..67607c8c04b --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Invalid.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Valid.xml b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Valid.xml new file mode 100644 index 00000000000..186ff6cf5be --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/files/JDK8220818b_Valid.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + +