1 /* 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package javax.xml.validation.ptests; 24 25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 26 import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR; 27 import static jaxp.library.JAXPTestUtilities.filenameToURL; 28 import static org.testng.Assert.assertNotNull; 29 import static org.testng.Assert.assertNull; 30 import static org.testng.Assert.assertSame; 31 32 import java.io.File; 33 import java.io.IOException; 34 35 import javax.xml.parsers.DocumentBuilderFactory; 36 import javax.xml.parsers.ParserConfigurationException; 37 import javax.xml.transform.Result; 38 import javax.xml.transform.Source; 39 import javax.xml.transform.dom.DOMResult; 40 import javax.xml.transform.dom.DOMSource; 41 import javax.xml.transform.sax.SAXResult; 42 import javax.xml.transform.sax.SAXSource; 43 import javax.xml.transform.stream.StreamSource; 44 import javax.xml.validation.Schema; 45 import javax.xml.validation.SchemaFactory; 46 import javax.xml.validation.Validator; 47 48 import jaxp.library.JAXPFileBaseTest; 49 50 import org.testng.annotations.BeforeClass; 51 import org.testng.annotations.DataProvider; 52 import org.testng.annotations.Test; 53 import org.w3c.dom.Document; 54 import org.xml.sax.ErrorHandler; 55 import org.xml.sax.InputSource; 56 import org.xml.sax.SAXException; 57 import org.xml.sax.SAXNotRecognizedException; 58 import org.xml.sax.SAXNotSupportedException; 59 import org.xml.sax.helpers.DefaultHandler; 60 61 /* 62 * @summary Class containing the test cases for Validator API 63 */ 64 public class ValidatorTest extends JAXPFileBaseTest { 65 66 @BeforeClass 67 public void setup() throws SAXException, IOException, ParserConfigurationException { 68 schema = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new File(XML_DIR + "test.xsd")); 69 70 assertNotNull(schema); 71 72 xmlFileUri = filenameToURL(XML_DIR + "test.xml"); 73 74 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 75 dbf.setNamespaceAware(true); 76 xmlDoc = dbf.newDocumentBuilder().parse(xmlFileUri); 77 } 78 79 @Test 80 public void testValidateStreamSource() throws SAXException, IOException { 81 Validator validator = getValidator(); 82 validator.setErrorHandler(new MyErrorHandler()); 83 validator.validate(getStreamSource()); 84 } 85 86 @Test(expectedExceptions = NullPointerException.class) 87 public void testValidateNullSource() throws SAXException, IOException { 88 Validator validator = getValidator(); 89 assertNotNull(validator); 90 validator.validate(null); 91 } 92 93 @Test 94 public void testErrorHandler() { 95 Validator validator = getValidator(); 96 assertNull(validator.getErrorHandler(), "When Validator is created, initially ErrorHandler should not be set."); 97 98 ErrorHandler mh = new MyErrorHandler(); 99 validator.setErrorHandler(mh); 100 assertSame(validator.getErrorHandler(), mh); 101 102 } 103 104 @DataProvider(name = "source-result") 105 public Object[][] getSourceAndResult() { 106 return new Object[][] { 107 { getStreamSource(), null }, 108 { getSAXSource(), getSAXResult() }, 109 { getDOMSource(), getDOMResult() }, 110 { getSAXSource(), null }, 111 { getDOMSource(), null } }; 112 } 113 114 @Test(dataProvider = "source-result") 115 public void testValidateWithResult(Source source, Result result) throws SAXException, IOException { 116 Validator validator = getValidator(); 117 validator.validate(source, result); 118 } 119 120 @Test(expectedExceptions = SAXNotRecognizedException.class) 121 public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 122 Validator validator = getValidator(); 123 validator.getProperty(UNRECOGNIZED_NAME); 124 125 } 126 127 @Test(expectedExceptions = SAXNotRecognizedException.class) 128 public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 129 Validator validator = getValidator(); 130 validator.setProperty(UNRECOGNIZED_NAME, "test"); 131 } 132 133 @Test(expectedExceptions = NullPointerException.class) 134 public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 135 Validator validator = getValidator(); 136 assertNotNull(validator); 137 validator.getProperty(null); 138 139 } 140 141 @Test(expectedExceptions = NullPointerException.class) 142 public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 143 Validator validator = getValidator(); 144 assertNotNull(validator); 145 validator.setProperty(null, "test"); 146 } 147 148 @Test(expectedExceptions = SAXNotRecognizedException.class) 149 public void testGetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 150 Validator validator = getValidator(); 151 validator.getFeature(UNRECOGNIZED_NAME); 152 153 } 154 155 @Test(expectedExceptions = SAXNotRecognizedException.class) 156 public void testSetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 157 Validator validator = getValidator(); 158 validator.setFeature(UNRECOGNIZED_NAME, true); 159 } 160 161 @Test(expectedExceptions = NullPointerException.class) 162 public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 163 Validator validator = getValidator(); 164 assertNotNull(validator); 165 validator.getFeature(null); 166 167 } 168 169 @Test(expectedExceptions = NullPointerException.class) 170 public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 171 Validator validator = getValidator(); 172 assertNotNull(validator); 173 validator.setFeature(null, true); 174 } 175 176 private Validator getValidator() { 177 return schema.newValidator(); 178 } 179 180 private Source getStreamSource() { 181 return new StreamSource(xmlFileUri); 182 } 183 184 private Source getSAXSource() { 185 return new SAXSource(new InputSource(xmlFileUri)); 186 } 187 188 private Result getSAXResult() { 189 SAXResult saxResult = new SAXResult(); 190 saxResult.setHandler(new DefaultHandler()); 191 return saxResult; 192 } 193 194 private Source getDOMSource() { 195 return new DOMSource(xmlDoc); 196 } 197 198 private Result getDOMResult() { 199 return new DOMResult(); 200 } 201 202 private static final String UNRECOGNIZED_NAME = "http://xml.org/sax/features/namespace-prefixes"; 203 private String xmlFileUri; 204 private Schema schema; 205 private Document xmlDoc; 206 207 }