8051547: Convert JAXP function tests: javax.xml.validation.* to jtreg (testng) tests
authorjoehw
Wed, 28 Jan 2015 22:49:58 -0800
changeset 28697 fae50549d70d
parent 28696 dd8c7efde993
child 28698 f9e5640d832e
8051547: Convert JAXP function tests: javax.xml.validation.* to jtreg (testng) tests Reviewed-by: lancea, joehw Contributed-by: frank.yuan@oracle.com
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/SchemaFactoryTest.java
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/TypeInfoProviderTest.java
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/ValidatorHandlerTest.java
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/ValidatorTest.java
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder11.xml
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder11.xsd
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder12.xml
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder12.xsd
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/test.xml
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/test.xsd
jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/test1.xsd
jaxp/test/javax/xml/jaxp/libs/javax/xml/validation/ptests/MyErrorHandler.java
jaxp/test/javax/xml/jaxp/libs/javax/xml/validation/ptests/ValidationTestConst.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/SchemaFactoryTest.java	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,297 @@
+/*
+ * Copyright (c) 1999, 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.ptests;
+
+import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
+import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertSame;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.SAXParseException;
+
+/*
+ * @summary Class containing the test cases for SchemaFactory
+ */
+@Test(singleThreaded = true)
+public class SchemaFactoryTest {
+
+    @BeforeClass
+    public void setup() throws SAXException, IOException, ParserConfigurationException {
+        sf = newSchemaFactory();
+
+        assertNotNull(sf);
+
+        xsd1 = Files.readAllBytes(Paths.get(XML_DIR + "test.xsd"));
+        xsd2 = Files.readAllBytes(Paths.get(XML_DIR + "test1.xsd"));
+
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setNamespaceAware(true);
+        DocumentBuilder db = dbf.newDocumentBuilder();
+        xsdDoc1 = db.parse(newInputStream(xsd1));
+        xsdDoc2 = db.parse(newInputStream(xsd2));
+
+        xml = Files.readAllBytes(Paths.get(XML_DIR + "test.xml"));
+    }
+
+    @Test(expectedExceptions = SAXParseException.class)
+    public void testNewSchemaDefault() throws SAXException, IOException {
+        validate(sf.newSchema());
+    }
+
+    @Test
+    public void testNewSchemaWithFile() throws SAXException, IOException {
+        validate(sf.newSchema(new File(XML_DIR + "test.xsd")));
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testNewSchemaWithNullFile() throws SAXException {
+        sf.newSchema((File) null);
+    }
+
+    @DataProvider(name = "valid-source")
+    public Object[][] getValidSource() {
+        return new Object[][] {
+                { streamSource(xsd1) },
+                { saxSource(xsd1) },
+                { domSource(xsdDoc1) } };
+
+    }
+
+    @Test(dataProvider = "valid-source")
+    public void testNewSchemaWithValidSource(Source schema) throws SAXException, IOException {
+        validate(sf.newSchema(schema));
+    }
+
+    @DataProvider(name = "invalid-source")
+    public Object[][] getInvalidSource() {
+        return new Object[][] {
+                { nullStreamSource() },
+                { nullSaxSource() } };
+    }
+
+    @Test(dataProvider = "invalid-source", expectedExceptions = SAXParseException.class)
+    public void testNewSchemaWithInvalidSource(Source schema) throws SAXException {
+        sf.newSchema(schema);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testNewSchemaWithNullSource() throws SAXException {
+        sf.newSchema((Source)null);
+    }
+
+    @DataProvider(name = "valid-sources")
+    public Object[][] getValidSources() {
+        return new Object[][] {
+                { streamSource(xsd1), streamSource(xsd2) },
+                { saxSource(xsd1), saxSource(xsd2) },
+                { domSource(xsdDoc1), domSource(xsdDoc2) } };
+
+    }
+
+    @Test(dataProvider = "valid-sources")
+    public void testNewSchemaWithValidSourceArray(Source schema1, Source schema2) throws SAXException, IOException {
+        validate(sf.newSchema(new Source[] { schema1, schema2 }));
+    }
+
+    @DataProvider(name = "invalid-sources")
+    public Object[][] getInvalidSources() {
+        return new Object[][] {
+                { streamSource(xsd1), nullStreamSource() },
+                { nullStreamSource(), nullStreamSource() },
+                { saxSource(xsd1), nullSaxSource() },
+                { nullSaxSource(), nullSaxSource() } };
+    }
+
+    @Test(dataProvider = "invalid-sources", expectedExceptions = SAXParseException.class)
+    public void testNewSchemaWithInvalidSourceArray(Source schema1, Source schema2) throws SAXException {
+        sf.newSchema(new Source[] { schema1, schema2 });
+    }
+
+    @DataProvider(name = "null-sources")
+    public Object[][] getNullSources() {
+        return new Object[][] {
+                { new Source[] { domSource(xsdDoc1), null } },
+                { new Source[] { null, null } },
+                { null } };
+
+    }
+
+    @Test(dataProvider = "null-sources", expectedExceptions = NullPointerException.class)
+    public void testNewSchemaWithNullSourceArray(Source[] schemas) throws SAXException {
+        sf.newSchema(schemas);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testNewSchemaWithNullUrl() throws SAXException {
+        sf.newSchema((URL) null);
+    }
+
+
+    @Test
+    public void testErrorHandler() {
+        SchemaFactory sf = newSchemaFactory();
+        assertNull(sf.getErrorHandler(), "When SchemaFactory is created, initially ErrorHandler should not be set.");
+
+        ErrorHandler handler = new MyErrorHandler();
+        sf.setErrorHandler(handler);
+        assertSame(sf.getErrorHandler(), handler);
+
+        sf.setErrorHandler(null);
+        assertNull(sf.getErrorHandler());
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        sf.getProperty(UNRECOGNIZED_NAME);
+
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        sf.setProperty(UNRECOGNIZED_NAME, "test");
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        assertNotNull(sf);
+        sf.getProperty(null);
+
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        assertNotNull(sf);
+        sf.setProperty(null, "test");
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testGetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        sf.getFeature(UNRECOGNIZED_NAME);
+
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testSetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        sf.setFeature(UNRECOGNIZED_NAME, true);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        assertNotNull(sf);
+        sf.getFeature(null);
+
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        SchemaFactory sf = newSchemaFactory();
+        assertNotNull(sf);
+        sf.setFeature(null, true);
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void testInvalidSchemaLanguage() {
+        final String INVALID_SCHEMA_LANGUAGE = "http://relaxng.org/ns/structure/1.0";
+        SchemaFactory.newInstance(INVALID_SCHEMA_LANGUAGE);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testNullSchemaLanguage() {
+        SchemaFactory.newInstance(null);
+    }
+
+    private void validate(Schema schema) throws SAXException, IOException {
+        schema.newValidator().validate(new StreamSource(new ByteArrayInputStream(xml)));
+    }
+    private InputStream newInputStream(byte[] xsd) {
+        return new ByteArrayInputStream(xsd);
+    }
+
+    private Source streamSource(byte[] xsd) {
+        return new StreamSource(newInputStream(xsd));
+    }
+
+    private Source nullStreamSource() {
+        return new StreamSource((InputStream) null);
+    }
+
+    private Source saxSource(byte[] xsd) {
+        return new SAXSource(new InputSource(newInputStream(xsd)));
+    }
+
+    private Source nullSaxSource() {
+        return new SAXSource(new InputSource((InputStream) null));
+    }
+
+    private Source domSource(Document xsdDoc) {
+        return new DOMSource(xsdDoc);
+    }
+
+    private SchemaFactory newSchemaFactory() {
+        return SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
+    }
+
+    private static final String UNRECOGNIZED_NAME = "http://xml.org/sax/features/namespace-prefixes";
+
+    private SchemaFactory sf;
+    private byte[] xsd1;
+    private byte[] xsd2;
+    private Document xsdDoc1;
+    private Document xsdDoc2;
+    private byte[] xml;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/TypeInfoProviderTest.java	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 1999, 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.ptests;
+
+import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
+import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR;
+import static jaxp.library.JAXPTestUtilities.filenameToURL;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.TypeInfoProvider;
+import javax.xml.validation.ValidatorHandler;
+
+import jaxp.library.JAXPFileBaseTest;
+
+import org.testng.annotations.Test;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+/*
+ * @summary test ValidatorHandler.getTypeInfoProvider()
+ */
+public class TypeInfoProviderTest extends JAXPFileBaseTest {
+
+    private ValidatorHandler validatorHandler;
+
+    @Test
+    public void test() throws SAXException, ParserConfigurationException, IOException {
+
+        SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
+        Schema schema = sf.newSchema(new File(XML_DIR + "shiporder11.xsd"));
+        validatorHandler = schema.newValidatorHandler();
+        MyDefaultHandler myDefaultHandler = new MyDefaultHandler();
+        validatorHandler.setContentHandler(myDefaultHandler);
+
+        InputSource is = new InputSource(filenameToURL(XML_DIR + "shiporder11.xml"));
+
+        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+        parserFactory.setNamespaceAware(true);
+        XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
+        xmlReader.setContentHandler(validatorHandler);
+        xmlReader.parse(is);
+
+    }
+
+    private class MyDefaultHandler extends DefaultHandler {
+
+        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
+            TypeInfoProvider typeInfoProvider = validatorHandler.getTypeInfoProvider();
+            int index = atts.getIndex("orderid");
+            if (index != -1) {
+                System.out.println(" Index " + index);
+                System.out.println(" ElementType " + typeInfoProvider.getElementTypeInfo().getTypeName());
+                assertEquals(typeInfoProvider.getAttributeTypeInfo(index).getTypeName(), "string");
+                assertTrue(typeInfoProvider.isSpecified(index));
+                assertFalse(typeInfoProvider.isIdAttribute(index));
+            }
+
+        }
+
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/ValidatorHandlerTest.java	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 1999, 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.ptests;
+
+import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
+import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR;
+import static org.testng.Assert.assertFalse;
+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.File;
+
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.ValidatorHandler;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/*
+ * @summary Class containing the test cases for ValidatorHandler API
+ */
+public class ValidatorHandlerTest {
+    @BeforeClass
+    public void setup() throws SAXException {
+        schema = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new File(XML_DIR + "test.xsd"));
+
+        assertNotNull(schema);
+    }
+
+    @Test
+    public void testErrorHandler() {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        assertNull(validatorHandler.getErrorHandler(), "When ValidatorHandler is created, initially ErrorHandler should not be set.");
+
+        ErrorHandler handler = new MyErrorHandler();
+        validatorHandler.setErrorHandler(handler);
+        assertSame(validatorHandler.getErrorHandler(), handler);
+
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        validatorHandler.getProperty(FEATURE_NAME);
+
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        validatorHandler.setProperty(FEATURE_NAME, "test");
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        assertNotNull(validatorHandler);
+        validatorHandler.getProperty(null);
+
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        assertNotNull(validatorHandler);
+        validatorHandler.setProperty(null, "test");
+    }
+
+    public void testFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        assertFalse(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");
+
+        validatorHandler.setFeature(FEATURE_NAME, true);
+        assertTrue(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");
+
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        assertNotNull(validatorHandler);
+        validatorHandler.getFeature(null);
+
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        assertNotNull(validatorHandler);
+        validatorHandler.setFeature(null, true);
+    }
+
+    @Test
+    public void testContentHandler() {
+        ValidatorHandler validatorHandler = getValidatorHandler();
+        assertNull(validatorHandler.getContentHandler(), "When ValidatorHandler is created, initially ContentHandler should not be set.");
+
+        ContentHandler handler = new DefaultHandler();
+        validatorHandler.setContentHandler(handler);
+        assertSame(validatorHandler.getContentHandler(), handler);
+
+        validatorHandler.setContentHandler(null);
+        assertNull(validatorHandler.getContentHandler());
+
+    }
+
+    private ValidatorHandler getValidatorHandler() {
+        return schema.newValidatorHandler();
+    }
+
+    private static final String FEATURE_NAME = "http://xml.org/sax/features/namespace-prefixes";
+
+    private Schema schema;
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/ptests/ValidatorTest.java	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 1999, 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.ptests;
+
+import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
+import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR;
+import static jaxp.library.JAXPTestUtilities.filenameToURL;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertSame;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+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 jaxp.library.JAXPFileBaseTest;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/*
+ * @summary Class containing the test cases for Validator API
+ */
+public class ValidatorTest extends JAXPFileBaseTest {
+
+    @BeforeClass
+    public void setup() throws SAXException, IOException, ParserConfigurationException {
+        schema = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new File(XML_DIR + "test.xsd"));
+
+        assertNotNull(schema);
+
+        xmlFileUri = filenameToURL(XML_DIR + "test.xml");
+
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setNamespaceAware(true);
+        xmlDoc = dbf.newDocumentBuilder().parse(xmlFileUri);
+    }
+
+    @Test
+    public void testValidateStreamSource() throws SAXException, IOException {
+        Validator validator = getValidator();
+        validator.setErrorHandler(new MyErrorHandler());
+        validator.validate(getStreamSource());
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testValidateNullSource() throws SAXException, IOException {
+        Validator validator = getValidator();
+        assertNotNull(validator);
+        validator.validate(null);
+    }
+
+    @Test
+    public void testErrorHandler() {
+        Validator validator = getValidator();
+        assertNull(validator.getErrorHandler(), "When Validator is created, initially ErrorHandler should not be set.");
+
+        ErrorHandler mh = new MyErrorHandler();
+        validator.setErrorHandler(mh);
+        assertSame(validator.getErrorHandler(), mh);
+
+    }
+
+    @DataProvider(name = "source-result")
+    public Object[][] getSourceAndResult() {
+        return new Object[][] {
+                { getStreamSource(), null },
+                { getSAXSource(), getSAXResult() },
+                { getDOMSource(), getDOMResult() },
+                { getSAXSource(), null },
+                { getDOMSource(), null } };
+    }
+
+    @Test(dataProvider = "source-result")
+    public void testValidateWithResult(Source source, Result result) throws SAXException, IOException {
+        Validator validator = getValidator();
+        validator.validate(source, result);
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        validator.getProperty(UNRECOGNIZED_NAME);
+
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        validator.setProperty(UNRECOGNIZED_NAME, "test");
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        assertNotNull(validator);
+        validator.getProperty(null);
+
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        assertNotNull(validator);
+        validator.setProperty(null, "test");
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testGetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        validator.getFeature(UNRECOGNIZED_NAME);
+
+    }
+
+    @Test(expectedExceptions = SAXNotRecognizedException.class)
+    public void testSetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        validator.setFeature(UNRECOGNIZED_NAME, true);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        assertNotNull(validator);
+        validator.getFeature(null);
+
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
+        Validator validator = getValidator();
+        assertNotNull(validator);
+        validator.setFeature(null, true);
+    }
+
+    private Validator getValidator() {
+        return schema.newValidator();
+    }
+
+    private Source getStreamSource() {
+        return new StreamSource(xmlFileUri);
+    }
+
+    private Source getSAXSource() {
+        return new SAXSource(new InputSource(xmlFileUri));
+    }
+
+    private Result getSAXResult() {
+        SAXResult saxResult = new SAXResult();
+        saxResult.setHandler(new DefaultHandler());
+        return saxResult;
+    }
+
+    private Source getDOMSource() {
+        return new DOMSource(xmlDoc);
+    }
+
+    private Result getDOMResult() {
+        return new DOMResult();
+    }
+
+    private static final String UNRECOGNIZED_NAME = "http://xml.org/sax/features/namespace-prefixes";
+    private String xmlFileUri;
+    private Schema schema;
+    private Document xmlDoc;
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder11.xml	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<shiporder orderid="889923"
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+xsi:noNamespaceSchemaLocation="shiporder.xsd">
+<orderperson>John Smith</orderperson>
+<shipto>
+<name>Ola Nordmann</name>
+<address>Langgt 23</address>
+<city>4000 Stavanger</city>
+<country>Norway</country>
+</shipto>
+<item>
+<title>Empire Burlesque</title>
+<note>Special Edition</note>
+<quantity>1</quantity>
+<price>10.90</price>
+</item>
+<item>
+<title>Hide your heart</title>
+<quantity>1</quantity>
+<price>9.90</price>
+</item>
+</shiporder>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder11.xsd	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+<!-- definition of simple elements -->
+<xs:element name="orderperson" type="xs:string"/>
+<xs:element name="name" type="xs:string"/>
+<xs:element name="address" type="xs:string"/>
+<xs:element name="city" type="xs:string"/>
+<xs:element name="country" type="xs:string"/>
+<xs:element name="title" type="xs:string"/>
+<xs:element name="note" type="xs:string"/>
+<xs:element name="quantity" type="xs:positiveInteger"/>
+<xs:element name="price" type="xs:decimal"/>
+
+<!-- definition of attributes -->
+<xs:attribute name="orderid" type="xs:string"/>
+
+<!-- definition of complex elements -->
+<xs:element name="shipto">
+<xs:complexType>
+<xs:sequence>
+<xs:element ref="name"/>
+<xs:element ref="address"/>
+<xs:element ref="city"/>
+<xs:element ref="country"/>
+</xs:sequence>
+</xs:complexType>
+</xs:element>
+<xs:element name="item">
+<xs:complexType>
+<xs:sequence>
+<xs:element ref="title"/>
+<xs:element ref="note" minOccurs="0"/>
+<xs:element ref="quantity"/>
+<xs:element ref="price"/>
+</xs:sequence>
+</xs:complexType>
+</xs:element>
+
+<xs:element name="shiporder">
+<xs:complexType>
+<xs:sequence>
+<xs:element ref="orderperson"/>
+<xs:element ref="shipto"/>
+<xs:element ref="item" maxOccurs="unbounded"/>
+</xs:sequence>
+<xs:attribute ref="orderid" use="required"/>
+</xs:complexType>
+</xs:element>
+
+</xs:schema>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder12.xml	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<shiporder orderid="889923"
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+xsi:noNamespaceSchemaLocation="shiporder.xsd">
+<orderperson>John Smith</orderperson>
+<shipto>
+<name>Ola Nordmann</name>
+<address>Langgt 23</address>
+<city>4000 Stavanger</city>
+<country>Norway</country>
+</shipto>
+<item>
+<title>Empire Burlesque</title>
+<note>Special Edition</note>
+<quantity>1</quantity>
+<price>10.90</price>
+</item>
+<item>
+<title>Hide your heart</title>
+<quantity>1</quantity>
+<price>9.90</price>
+</item>
+</shiporder>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/shiporder12.xsd	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+<!-- definition of simple elements -->
+<xs:element name="orderperson" type="xs:string"/>
+<xs:element name="name" type="xs:string"/>
+<xs:element name="address" type="xs:string"/>
+<xs:element name="city" type="xs:string"/>
+<xs:element name="country" type="xs:string"/>
+<xs:element name="title" type="xs:string"/>
+<xs:element name="note" type="xs:string"/>
+<xs:element name="quantity" type="xs:positiveInteger"/>
+<xs:element name="price" type="xs:decimal"/>
+
+<!-- definition of attributes -->
+<xs:attribute name="orderid" type="xs:string"/>
+
+<!-- definition of complex elements -->
+<xs:element name="shipto">
+<xs:complexType>
+<xs:sequence>
+<xs:element ref="name"/>
+<xs:element ref="address"/>
+<xs:element ref="city"/>
+<xs:element ref="country"/>
+</xs:sequence>
+</xs:complexType>
+</xs:element>
+<xs:element name="item">
+<xs:complexType>
+<xs:sequence>
+<xs:element ref="title"/>
+<xs:element ref="note" minOccurs="0"/>
+<xs:element ref="quantity"/>
+<xs:element ref="price"/>
+</xs:sequence>
+</xs:complexType>
+</xs:element>
+
+<xs:element name="shiporder">
+<xs:complexType>
+<xs:sequence>
+<xs:element ref="orderperson"/>
+<xs:element ref="shipto"/>
+<xs:element ref="item" maxOccurs="unbounded"/>
+</xs:sequence>
+<xs:attribute ref="orderid" use="required"/>
+</xs:complexType>
+</xs:element>
+
+</xs:schema>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/test.xml	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+ <contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
+<name> John </name>
+<phone>444-121-3434</phone>
+</contact>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/test.xsd	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+<xs:element name="contact">
+<xs:complexType>
+<xs:sequence>
+  <xs:element name="name" type="xs:string"/>
+  <xs:element name="phone" type="xs:string"/>
+</xs:sequence>
+</xs:complexType>
+</xs:element>
+</xs:schema>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/functional/javax/xml/validation/xmlfiles/test1.xsd	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+<xs:element name="address">
+<xs:complexType>
+<xs:sequence>
+  <xs:element name="street" type="xs:string"/>
+  <xs:element name="city" type="xs:string"/>
+</xs:sequence>
+</xs:complexType>
+</xs:element>
+</xs:schema>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/libs/javax/xml/validation/ptests/MyErrorHandler.java	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,40 @@
+/*
+ * 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.ptests;
+
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXParseException;
+
+class MyErrorHandler implements ErrorHandler {
+    public void error(SAXParseException exception) throws SAXParseException {
+        throw exception;
+    }
+
+    public void warning(SAXParseException exception) throws SAXParseException {
+        throw exception;
+    }
+
+    public void fatalError(SAXParseException exception) throws SAXParseException {
+        throw exception;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/libs/javax/xml/validation/ptests/ValidationTestConst.java	Wed Jan 28 22:49:58 2015 -0800
@@ -0,0 +1,43 @@
+/*
+ * 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.ptests;
+
+import static jaxp.library.JAXPTestUtilities.FILE_SEP;
+import static jaxp.library.JAXPTestUtilities.getPathByClassName;
+
+/**
+ * This class defines the path constant
+ */
+public class ValidationTestConst {
+    /**
+     * XML source file directory.
+     */
+    public static final String XML_DIR = getPathByClassName(ValidationTestConst.class,
+            ".." + FILE_SEP + "xmlfiles");
+
+    /**
+     * Golden validation files directory.
+     */
+    public static final String GOLDEN_DIR = getPathByClassName(ValidationTestConst.class,
+            ".." + FILE_SEP + "xmlfiles" + FILE_SEP + "out");
+}