1 /* 2 * Copyright (c) 1999, 2016, 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 org.testng.annotations.BeforeClass; 49 import org.testng.annotations.DataProvider; 50 import org.testng.annotations.Listeners; 51 import org.testng.annotations.Test; 52 import org.w3c.dom.Document; 53 import org.xml.sax.ErrorHandler; 54 import org.xml.sax.InputSource; 55 import org.xml.sax.SAXException; 56 import org.xml.sax.SAXNotRecognizedException; 57 import org.xml.sax.SAXNotSupportedException; 58 import org.xml.sax.helpers.DefaultHandler; 59 60 /* 61 * @test 62 * @library /javax/xml/jaxp/libs 63 * @run testng/othervm -DrunSecMngr=true javax.xml.validation.ptests.ValidatorTest 64 * @run testng/othervm javax.xml.validation.ptests.ValidatorTest 65 * @summary Class containing the test cases for Validator API 66 */ 67 @Listeners({jaxp.library.FilePolicy.class}) 68 public class ValidatorTest { 69 70 @BeforeClass 71 public void setup() throws SAXException, IOException, ParserConfigurationException { 72 schema = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new File(XML_DIR + "test.xsd")); 73 74 assertNotNull(schema); 75 76 xmlFileUri = filenameToURL(XML_DIR + "test.xml"); 77 78 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 79 dbf.setNamespaceAware(true); 80 xmlDoc = dbf.newDocumentBuilder().parse(xmlFileUri); 81 } 82 83 @Test 84 public void testValidateStreamSource() throws SAXException, IOException { 85 Validator validator = getValidator(); 86 validator.setErrorHandler(new MyErrorHandler()); 87 validator.validate(getStreamSource()); 88 } 89 90 @Test(expectedExceptions = NullPointerException.class) 91 public void testValidateNullSource() throws SAXException, IOException { 92 Validator validator = getValidator(); 93 assertNotNull(validator); 94 validator.validate(null); 95 } 96 97 @Test 98 public void testErrorHandler() { 99 Validator validator = getValidator(); 100 assertNull(validator.getErrorHandler(), "When Validator is created, initially ErrorHandler should not be set."); 101 102 ErrorHandler mh = new MyErrorHandler(); 103 validator.setErrorHandler(mh); 104 assertSame(validator.getErrorHandler(), mh); 105 106 } 107 108 @DataProvider(name = "source-result") 109 public Object[][] getSourceAndResult() { 110 return new Object[][] { 111 { getStreamSource(), null }, 112 { getSAXSource(), getSAXResult() }, 113 { getDOMSource(), getDOMResult() }, 114 { getSAXSource(), null }, 115 { getDOMSource(), null } }; 116 } 117 118 @Test(dataProvider = "source-result") 119 public void testValidateWithResult(Source source, Result result) throws SAXException, IOException { 120 Validator validator = getValidator(); 121 validator.validate(source, result); 122 } 123 124 @Test(expectedExceptions = SAXNotRecognizedException.class) 125 public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 126 Validator validator = getValidator(); 127 validator.getProperty(UNRECOGNIZED_NAME); 128 129 } 130 131 @Test(expectedExceptions = SAXNotRecognizedException.class) 132 public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 133 Validator validator = getValidator(); 134 validator.setProperty(UNRECOGNIZED_NAME, "test"); 135 } 136 137 @Test(expectedExceptions = NullPointerException.class) 138 public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 139 Validator validator = getValidator(); 140 assertNotNull(validator); 141 validator.getProperty(null); 142 143 } 144 145 @Test(expectedExceptions = NullPointerException.class) 146 public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 147 Validator validator = getValidator(); 148 assertNotNull(validator); 149 validator.setProperty(null, "test"); 150 } 151 152 @Test(expectedExceptions = SAXNotRecognizedException.class) 153 public void testGetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 154 Validator validator = getValidator(); 155 validator.getFeature(UNRECOGNIZED_NAME); 156 157 } 158 159 @Test(expectedExceptions = SAXNotRecognizedException.class) 160 public void testSetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 161 Validator validator = getValidator(); 162 validator.setFeature(UNRECOGNIZED_NAME, true); 163 } 164 165 @Test(expectedExceptions = NullPointerException.class) 166 public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 167 Validator validator = getValidator(); 168 assertNotNull(validator); 169 validator.getFeature(null); 170 171 } 172 173 @Test(expectedExceptions = NullPointerException.class) 174 public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 175 Validator validator = getValidator(); 176 assertNotNull(validator); 177 validator.setFeature(null, true); 178 } 179 180 private Validator getValidator() { 181 return schema.newValidator(); 182 } 183 184 private Source getStreamSource() { 185 return new StreamSource(xmlFileUri); 186 } 187 188 private Source getSAXSource() { 189 return new SAXSource(new InputSource(xmlFileUri)); 190 } 191 192 private Result getSAXResult() { 193 SAXResult saxResult = new SAXResult(); 194 saxResult.setHandler(new DefaultHandler()); 195 return saxResult; 196 } 197 198 private Source getDOMSource() { 199 return new DOMSource(xmlDoc); 200 } 201 202 private Result getDOMResult() { 203 return new DOMResult(); 204 } 205 206 private static final String UNRECOGNIZED_NAME = "http://xml.org/sax/features/namespace-prefixes"; 207 private String xmlFileUri; 208 private Schema schema; 209 private Document xmlDoc; 210 211 } 212