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 24 package javax.xml.parsers.ptests; 25 26 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 27 import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR; 28 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR; 29 import static jaxp.library.JAXPTestUtilities.USER_DIR; 30 import static jaxp.library.JAXPTestUtilities.compareWithGold; 31 import static jaxp.library.JAXPTestUtilities.filenameToURL; 32 import static org.testng.Assert.assertEquals; 33 import static org.testng.Assert.assertFalse; 34 import static org.testng.Assert.assertNotNull; 35 import static org.testng.Assert.assertNull; 36 import static org.testng.Assert.assertTrue; 37 38 import java.io.BufferedReader; 39 import java.io.Closeable; 40 import java.io.File; 41 import java.io.FileInputStream; 42 import java.io.FileNotFoundException; 43 import java.io.FileReader; 44 45 import javax.xml.parsers.DocumentBuilder; 46 import javax.xml.parsers.DocumentBuilderFactory; 47 import javax.xml.parsers.FactoryConfigurationError; 48 import javax.xml.parsers.ParserConfigurationException; 49 import javax.xml.parsers.SAXParser; 50 import javax.xml.parsers.SAXParserFactory; 51 import javax.xml.transform.Transformer; 52 import javax.xml.transform.TransformerFactory; 53 import javax.xml.transform.dom.DOMSource; 54 import javax.xml.transform.sax.SAXResult; 55 56 import jaxp.library.JAXPDataProvider; 57 58 import org.testng.annotations.DataProvider; 59 import org.testng.annotations.Listeners; 60 import org.testng.annotations.Test; 61 import org.w3c.dom.Document; 62 import org.w3c.dom.Element; 63 import org.w3c.dom.NodeList; 64 import org.xml.sax.InputSource; 65 import org.xml.sax.SAXException; 66 import org.xml.sax.helpers.DefaultHandler; 67 68 /** 69 * @bug 8080907 70 * This checks the methods of DocumentBuilderFactoryImpl. 71 */ 72 /* 73 * @test 74 * @library /javax/xml/jaxp/libs 75 * @run testng/othervm -DrunSecMngr=true javax.xml.parsers.ptests.DocumentBuilderFactoryTest 76 * @run testng/othervm javax.xml.parsers.ptests.DocumentBuilderFactoryTest 77 */ 78 @Listeners({jaxp.library.FilePolicy.class}) 79 public class DocumentBuilderFactoryTest { 80 /** 81 * DocumentBuilderFactory implementation class name. 82 */ 83 private static final String DOCUMENT_BUILDER_FACTORY_CLASSNAME = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"; 84 85 /** 86 * Provide valid DocumentBuilderFactory instantiation parameters. 87 * 88 * @return a data provider contains DocumentBuilderFactory instantiation parameters. 89 */ 90 @DataProvider(name = "parameters") 91 public Object[][] getValidateParameters() { 92 return new Object[][] { { DOCUMENT_BUILDER_FACTORY_CLASSNAME, null }, { DOCUMENT_BUILDER_FACTORY_CLASSNAME, this.getClass().getClassLoader() } }; 93 } 94 95 /** 96 * Test for DocumentBuilderFactory.newInstance(java.lang.String 97 * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName 98 * points to correct implementation of 99 * javax.xml.parsers.DocumentBuilderFactory , should return newInstance of 100 * DocumentBuilderFactory 101 * 102 * @param factoryClassName 103 * @param classLoader 104 * @throws ParserConfigurationException 105 */ 106 @Test(dataProvider = "parameters") 107 public void testNewInstance(String factoryClassName, ClassLoader classLoader) throws ParserConfigurationException { 108 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(factoryClassName, classLoader); 109 DocumentBuilder builder = dbf.newDocumentBuilder(); 110 assertNotNull(builder); 111 } 112 113 /** 114 * test for DocumentBuilderFactory.newInstance(java.lang.String 115 * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName is 116 * null , should throw FactoryConfigurationError 117 * 118 * @param factoryClassName 119 * @param classLoader 120 */ 121 @Test(expectedExceptions = FactoryConfigurationError.class, dataProvider = "new-instance-neg", dataProviderClass = JAXPDataProvider.class) 122 public void testNewInstanceNeg(String factoryClassName, ClassLoader classLoader) { 123 DocumentBuilderFactory.newInstance(factoryClassName, classLoader); 124 } 125 126 /** 127 * Test the default functionality of schema support method. 128 * @throws Exception If any errors occur. 129 */ 130 @Test 131 public void testCheckSchemaSupport1() throws Exception { 132 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 133 dbf.setValidating(true); 134 dbf.setNamespaceAware(true); 135 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 136 W3C_XML_SCHEMA_NS_URI); 137 MyErrorHandler eh = MyErrorHandler.newInstance(); 138 DocumentBuilder db = dbf.newDocumentBuilder(); 139 db.setErrorHandler(eh); 140 db.parse(new File(XML_DIR, "test.xml")); 141 assertFalse(eh.isErrorOccured()); 142 } 143 144 @DataProvider(name = "schema-source") 145 public Object[][] getSchemaSource() throws FileNotFoundException { 146 return new Object[][] { 147 { new FileInputStream(new File(XML_DIR, "test.xsd")) }, 148 { new InputSource(filenameToURL(XML_DIR + "test.xsd")) } }; 149 } 150 151 /** 152 * Test the default functionality of schema support method. In 153 * this case the schema source property is set. 154 * @throws Exception If any errors occur. 155 */ 156 @Test(dataProvider = "schema-source") 157 public void testCheckSchemaSupport2(Object schemaSource) throws Exception { 158 try { 159 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 160 dbf.setValidating(true); 161 dbf.setNamespaceAware(true); 162 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 163 W3C_XML_SCHEMA_NS_URI); 164 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource); 165 MyErrorHandler eh = MyErrorHandler.newInstance(); 166 DocumentBuilder db = dbf.newDocumentBuilder(); 167 db.setErrorHandler(eh); 168 db.parse(new File(XML_DIR, "test1.xml")); 169 assertFalse(eh.isErrorOccured()); 170 } finally { 171 if (schemaSource instanceof Closeable) { 172 ((Closeable) schemaSource).close(); 173 } 174 } 175 176 } 177 178 /** 179 * Test the default functionality of schema support method. In 180 * this case the schema source property is set. 181 * @throws Exception If any errors occur. 182 */ 183 @Test(dataProvider = "schema-source") 184 public void testCheckSchemaSupport3(Object schemaSource) throws Exception { 185 try { 186 SAXParserFactory spf = SAXParserFactory.newInstance(); 187 spf.setValidating(true); 188 spf.setNamespaceAware(true); 189 SAXParser sp = spf.newSAXParser(); 190 sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 191 W3C_XML_SCHEMA_NS_URI); 192 sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource); 193 DefaultHandler dh = new DefaultHandler(); 194 // Not expect any unrecoverable error here. 195 sp.parse(new File(XML_DIR, "test1.xml"), dh); 196 } finally { 197 if (schemaSource instanceof Closeable) { 198 ((Closeable) schemaSource).close(); 199 } 200 } 201 } 202 203 /** 204 * Test the default functionality of newInstance method. To test 205 * the isCoalescing method and setCoalescing This checks to see if the CDATA 206 * and text nodes got combined In that case it will print "<xml>This 207 * is not parsed</xml> yet". 208 * @throws Exception If any errors occur. 209 */ 210 @Test 211 public void testCheckDocumentBuilderFactory02() throws Exception { 212 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 213 dbf.setCoalescing(true); 214 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 215 Document doc = docBuilder.parse(new File(XML_DIR, "DocumentBuilderFactory01.xml")); 216 Element e = (Element) doc.getElementsByTagName("html").item(0); 217 NodeList nl = e.getChildNodes(); 218 assertEquals(nl.getLength(), 1); 219 } 220 221 /** 222 * Test the isIgnoringComments. By default it is false. 223 */ 224 @Test 225 public void testCheckDocumentBuilderFactory03() { 226 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 227 assertFalse(dbf.isIgnoringComments()); 228 } 229 230 /** 231 * Test the isValidating. By default it is false, set it to true and then 232 * use a document which is not valid. It should throw a warning or 233 * an error at least. The test passes in case retval 0 is set in the error 234 * method . 235 * @throws Exception If any errors occur. 236 */ 237 @Test 238 public void testCheckDocumentBuilderFactory04() throws Exception { 239 MyErrorHandler eh = MyErrorHandler.newInstance(); 240 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 241 dbf.setValidating(true); 242 DocumentBuilder db = dbf.newDocumentBuilder(); 243 db.setErrorHandler(eh); 244 db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml")); 245 assertTrue(eh.isErrorOccured()); 246 } 247 248 /** 249 * Test the setValidating. By default it is false, use a 250 * document which is not valid. It should not throw a warning or an error. 251 * The test passes in case the return value equals 1. 252 * @throws Exception If any errors occur. 253 */ 254 @Test 255 public void testCheckDocumentBuilderFactory16() throws Exception { 256 MyErrorHandler eh = MyErrorHandler.newInstance(); 257 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 258 DocumentBuilder db = dbf.newDocumentBuilder(); 259 db.setErrorHandler(eh); 260 db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml")); 261 assertFalse(eh.isErrorOccured()); 262 } 263 264 /** 265 * Test the setValidating. By default it is false, use a 266 * document which is valid. It should not throw a warning or an error. The 267 * test passes in case the return value equals 1. 268 * @throws Exception If any errors occur. 269 */ 270 @Test 271 public void testCheckDocumentBuilderFactory17() throws Exception { 272 MyErrorHandler eh = MyErrorHandler.newInstance(); 273 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 274 DocumentBuilder db = dbf.newDocumentBuilder(); 275 db.setErrorHandler(eh); 276 db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml")); 277 assertFalse(eh.isErrorOccured()); 278 } 279 280 /** 281 * Test the isExpandEntityReferences. By default it is true. 282 * @throws Exception If any errors occur. 283 */ 284 @Test 285 public void testCheckDocumentBuilderFactory05() throws Exception { 286 try(FileInputStream fis = new FileInputStream(new File( 287 XML_DIR, "DocumentBuilderFactory02.xml"))) { 288 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 289 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 290 Document doc = docBuilder.parse(fis); 291 Element e = (Element) doc.getElementsByTagName("title").item(0); 292 NodeList nl = e.getChildNodes(); 293 assertTrue(dbf.isExpandEntityReferences()); 294 assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W'); 295 } 296 } 297 298 /** 299 * Test the default functionality of setValidating method. The 300 * XML file has a DTD which has namespaces defined. The parser takes care to 301 * check if the namespaces using elements and defined attributes are there 302 * or not. 303 * @throws Exception If any errors occur. 304 */ 305 @Test 306 public void testCheckDocumentBuilderFactory06() throws Exception { 307 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 308 dbf.setValidating(true); 309 DocumentBuilder db = dbf.newDocumentBuilder(); 310 MyErrorHandler eh = MyErrorHandler.newInstance(); 311 db.setErrorHandler(eh); 312 Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml")); 313 assertTrue(doc instanceof Document); 314 assertFalse(eh.isErrorOccured()); 315 } 316 317 /** 318 * Test the setExpandEntityReferences. 319 * @throws Exception If any errors occur. 320 */ 321 @Test 322 public void testCheckDocumentBuilderFactory07() throws Exception { 323 try (FileInputStream fis = new FileInputStream(new File( 324 XML_DIR, "DocumentBuilderFactory02.xml"))) { 325 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 326 dbf.setExpandEntityReferences(true); 327 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 328 Document doc = docBuilder.parse(fis); 329 Element e = (Element) doc.getElementsByTagName("title").item(0); 330 NodeList nl = e.getChildNodes(); 331 assertTrue(dbf.isExpandEntityReferences()); 332 assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W'); 333 } 334 } 335 336 /** 337 * Test the setExpandEntityReferences. 338 * @throws Exception If any errors occur. 339 */ 340 @Test 341 public void testCheckDocumentBuilderFactory08() throws Exception { 342 try (FileInputStream fis = new FileInputStream(new File( 343 XML_DIR, "DocumentBuilderFactory02.xml"))) { 344 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 345 dbf.setExpandEntityReferences(false); 346 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 347 Document doc = docBuilder.parse(fis); 348 Element e = (Element) doc.getElementsByTagName("title").item(0); 349 NodeList nl = e.getChildNodes(); 350 assertNull(nl.item(0).getNodeValue()); 351 } 352 } 353 354 /** 355 * Test the setIgnoringComments. By default it is set to false. 356 * explicitly setting it to false, it recognizes the comment which is in 357 * Element Node Hence the Element's child node is not null. 358 * @throws Exception If any errors occur. 359 */ 360 @Test 361 public void testCheckDocumentBuilderFactory09() throws Exception { 362 try (FileInputStream fis = new FileInputStream(new File( 363 XML_DIR, "DocumentBuilderFactory07.xml"))) { 364 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 365 dbf.setIgnoringComments(false); 366 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 367 Document doc = docBuilder.parse(fis); 368 Element e = (Element) doc.getElementsByTagName("body").item(0); 369 NodeList nl = e.getChildNodes(); 370 assertNotNull(nl.item(0).getNodeValue()); 371 } 372 } 373 374 /** 375 * This tests for the parse(InputSource). 376 * @throws Exception If any errors occur. 377 */ 378 @Test 379 public void testCheckDocumentBuilderFactory10() throws Exception { 380 try (BufferedReader br = new BufferedReader(new FileReader(new File( 381 XML_DIR, "DocumentBuilderFactory07.xml")))) { 382 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 383 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 384 Document doc = docBuilder.parse(new InputSource(br)); 385 assertNotNull(doc); 386 } 387 } 388 389 /** 390 * This tests for the parse InputStream with SystemID as a second parameter. 391 * @throws Exception If any errors occur. 392 */ 393 @Test 394 public void testCheckDocumentBuilderFactory11() throws Exception { 395 try (FileInputStream fis = new FileInputStream(new File( 396 XML_DIR, "dbf10import.xsl"))) { 397 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 398 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 399 Document doc = docBuilder.parse(fis, new File(XML_DIR).toURI() 400 .toASCIIString()); 401 assertNotNull(doc); 402 } 403 } 404 405 /** 406 * This tests for the parse InputStream with empty SystemID as a second 407 * parameter. 408 * @throws Exception If any errors occur. 409 */ 410 @Test 411 public void testCheckDocumentBuilderFactory12() throws Exception { 412 try (FileInputStream fis = new FileInputStream(new File( 413 XML_DIR, "dbf10import.xsl"))) { 414 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 415 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 416 Document doc = docBuilder.parse(fis, " "); 417 assertNotNull(doc); 418 } 419 } 420 421 /** 422 * This tests for the parse(uri). 423 * @throws Exception If any errors occur. 424 */ 425 @Test 426 public void testCheckDocumentBuilderFactory13() throws Exception { 427 // Accesing default working directory. 428 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 429 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 430 Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl") 431 .toURI().toASCIIString()); 432 assertNotNull(doc); 433 } 434 435 /** 436 * This tests for the parse(uri) with empty string as parameter should 437 * throw Sax Exception. 438 * @throws Exception If any errors occur. 439 */ 440 @Test(expectedExceptions = SAXException.class) 441 public void testCheckDocumentBuilderFactory14() throws Exception { 442 // Accesing default working directory. 443 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 444 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 445 docBuilder.parse(""); 446 } 447 448 /** 449 * This tests for the parse (uri) with null uri as parameter should throw 450 * IllegalArgumentException. 451 * @throws Exception If any errors occur. 452 * 453 */ 454 @Test(expectedExceptions = IllegalArgumentException.class) 455 public void testCheckDocumentBuilderFactory15() throws Exception { 456 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 457 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 458 String uri = null; 459 docBuilder.parse(uri); 460 } 461 462 /** 463 * Test the setIgnoringComments. By default it is set to false, 464 * setting this to true, It does not recognize the comment, Here the 465 * nodelist has a length 0 because the ignoring comments is true. 466 * @throws Exception If any errors occur. 467 */ 468 @Test 469 public void testCheckIgnoringComments() throws Exception { 470 try (FileInputStream fis = new FileInputStream(new File( 471 XML_DIR, "DocumentBuilderFactory08.xml"))) { 472 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 473 dbf.setIgnoringComments(true); 474 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 475 Document doc = docBuilder.parse(fis); 476 Element e = (Element) doc.getElementsByTagName("body").item(0); 477 NodeList nl = e.getChildNodes(); 478 assertEquals(nl.getLength(), 0); 479 } 480 } 481 482 /** 483 * Test the default behaviour of setIgnoringComments. By default 484 * it is set to false, this is similar to case 9 but not setIgnoringComments 485 * explicitly, it does not recognize the comment. 486 * @throws Exception If any errors occur. 487 */ 488 @Test 489 public void testCheckIgnoringComments1() throws Exception { 490 try (FileInputStream fis = new FileInputStream(new File( 491 XML_DIR, "DocumentBuilderFactory07.xml"))) { 492 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 493 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 494 Document doc = docBuilder.parse(fis); 495 Element e = (Element) doc.getElementsByTagName("body").item(0); 496 NodeList nl = e.getChildNodes(); 497 assertFalse(dbf.isIgnoringComments()); 498 assertNotNull(nl.item(0).getNodeValue()); 499 } 500 } 501 502 /** 503 * Test for the isIgnoringElementContentWhitespace and the 504 * setIgnoringElementContentWhitespace. The xml file has all kinds of 505 * whitespace,tab and newline characters, it uses the MyNSContentHandler 506 * which does not invoke the characters callback when this 507 * setIgnoringElementContentWhitespace is set to true. 508 * @throws Exception If any errors occur. 509 */ 510 @Test 511 public void testCheckElementContentWhitespace() throws Exception { 512 String goldFile = GOLDEN_DIR + "dbfactory02GF.out"; 513 String outputFile = USER_DIR + "dbfactory02.out"; 514 MyErrorHandler eh = MyErrorHandler.newInstance(); 515 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 516 dbf.setValidating(true); 517 assertFalse(dbf.isIgnoringElementContentWhitespace()); 518 dbf.setIgnoringElementContentWhitespace(true); 519 DocumentBuilder db = dbf.newDocumentBuilder(); 520 db.setErrorHandler(eh); 521 Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml")); 522 assertFalse(eh.isErrorOccured()); 523 DOMSource domSource = new DOMSource(doc); 524 TransformerFactory tfactory = TransformerFactory.newInstance(); 525 Transformer transformer = tfactory.newTransformer(); 526 SAXResult saxResult = new SAXResult(); 527 try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) { 528 saxResult.setHandler(handler); 529 transformer.transform(domSource, saxResult); 530 } 531 assertTrue(compareWithGold(goldFile, outputFile)); 532 } 533 }