1 /* 2 * Copyright (c) 2014, 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 jaxp.library.JAXPTestUtilities.FILE_SEP; 27 import static jaxp.library.JAXPTestUtilities.failUnexpected; 28 import static org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertFalse; 30 import static org.testng.Assert.assertNotNull; 31 import static org.testng.Assert.assertNull; 32 import static org.testng.Assert.assertTrue; 33 34 import java.io.BufferedReader; 35 import java.io.File; 36 import java.io.FileInputStream; 37 import java.io.FileReader; 38 import java.io.IOException; 39 40 import javax.xml.parsers.DocumentBuilder; 41 import javax.xml.parsers.DocumentBuilderFactory; 42 import javax.xml.parsers.ParserConfigurationException; 43 import javax.xml.parsers.SAXParser; 44 import javax.xml.parsers.SAXParserFactory; 45 46 import org.testng.annotations.Test; 47 import org.w3c.dom.Document; 48 import org.w3c.dom.Element; 49 import org.w3c.dom.NodeList; 50 import org.xml.sax.InputSource; 51 import org.xml.sax.SAXException; 52 import org.xml.sax.helpers.DefaultHandler; 53 54 /** 55 * This checks the methods of DocumentBuilderFactoryImpl 56 */ 57 public class DocumentBuilderFactory01 { 58 /** 59 * Testcase to test the default functionality of schema support method. 60 */ 61 @Test 62 public void testCheckSchemaSupport1() { 63 try { 64 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 65 dbf.setValidating(true); 66 dbf.setNamespaceAware(true); 67 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); 68 MyErrorHandler eh = MyErrorHandler.newInstance(); 69 DocumentBuilder db = dbf.newDocumentBuilder(); 70 db.setErrorHandler(eh); 71 Document doc = db.parse(new File(TestUtils.XML_DIR, "test.xml")); 72 assertFalse(eh.errorOccured); 73 } catch (ParserConfigurationException | SAXException | IOException e) { 74 failUnexpected(e); 75 } 76 } 77 78 /** 79 * Testcase to test the default functionality of schema support method. In 80 * this case the schema source property is set. 81 */ 82 @Test 83 public void testCheckSchemaSupport2() { 84 try { 85 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 86 dbf.setValidating(true); 87 dbf.setNamespaceAware(true); 88 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); 89 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", new InputSource(new FileInputStream( 90 new File(TestUtils.XML_DIR, "test.xsd")))); 91 MyErrorHandler eh = MyErrorHandler.newInstance(); 92 DocumentBuilder db = dbf.newDocumentBuilder(); 93 db.setErrorHandler(eh); 94 Document doc = db.parse(new File(TestUtils.XML_DIR, "test1.xml")); 95 assertFalse(eh.errorOccured); 96 } catch (IllegalArgumentException | ParserConfigurationException | SAXException | IOException e) { 97 failUnexpected(e); 98 } 99 100 } 101 102 /** 103 * Testcase to test the default functionality of schema support method. In 104 * this case the schema source property is set. 105 */ 106 @Test 107 public void testCheckSchemaSupport3() { 108 try { 109 SAXParserFactory spf = SAXParserFactory.newInstance(); 110 spf.setNamespaceAware(true); 111 spf.setValidating(true); 112 spf.setNamespaceAware(true); 113 SAXParser sp = spf.newSAXParser(); 114 sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); 115 sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", 116 new InputSource(new FileInputStream(new File(TestUtils.XML_DIR, "test.xsd")))); 117 DefaultHandler dh = new DefaultHandler(); 118 sp.parse(new File(TestUtils.XML_DIR, "test1.xml"), dh); 119 } catch (ParserConfigurationException | SAXException | IOException e) { 120 failUnexpected(e); 121 } 122 } 123 124 /** 125 * Testcase to test the default functionality of newInstance method. To test 126 * the isCoalescing method and setCoalescing This checks to see if the CDATA 127 * and text nodes got combined In that case it will print "<xml>This 128 * is not parsed</xml> yet". 129 */ 130 @Test 131 public void testCheckDocumentBuilderFactory02() { 132 try { 133 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 134 dbf.setCoalescing(true); 135 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 136 Document doc = docBuilder.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory01.xml")); 137 Element e = (Element) doc.getElementsByTagName("html").item(0); 138 NodeList nl = e.getChildNodes(); 139 assertEquals(nl.item(0).getNodeValue().trim(), "<xml>This is not parsed</xml> yet"); 140 } catch (IOException | SAXException | ParserConfigurationException e) { 141 failUnexpected(e); 142 } 143 } 144 145 /** 146 * Testcase to test the isIgnoringComments. By default it is false. 147 */ 148 @Test 149 public void testCheckDocumentBuilderFactory03() { 150 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 151 assertFalse(dbf.isIgnoringComments()); 152 } 153 154 /** 155 * Testcase to test the isValidating. By default it is false, set it to true 156 * and then use a document which is not valid. It should throw a warning or 157 * an error at least. The test passes in case retval 0 is set in the error 158 * method . 159 */ 160 @Test 161 public void testCheckDocumentBuilderFactory04() { 162 try { 163 MyErrorHandler eh = MyErrorHandler.newInstance(); 164 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 165 dbf.setValidating(true); 166 DocumentBuilder db = dbf.newDocumentBuilder(); 167 db.setErrorHandler(eh); 168 Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory05.xml")); 169 assertTrue(eh.errorOccured); 170 } catch (ParserConfigurationException | IOException | SAXException e) { 171 failUnexpected(e); 172 } 173 } 174 175 /** 176 * Testcase to test the setValidating. By default it is false, use a 177 * document which is not valid. It should not throw a warning or an error. 178 * The test passes in case the retval equals 1 . 179 */ 180 @Test 181 public void testCheckDocumentBuilderFactory16() { 182 try { 183 MyErrorHandler eh = MyErrorHandler.newInstance(); 184 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 185 DocumentBuilder db = dbf.newDocumentBuilder(); 186 db.setErrorHandler(eh); 187 Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory05.xml")); 188 assertFalse(eh.errorOccured); 189 } catch (ParserConfigurationException | IOException | SAXException e) { 190 failUnexpected(e); 191 } 192 193 } 194 195 /** 196 * Testcase to test the setValidating. By default it is false, use a 197 * document which is valid. It should not throw a warning or an error. The 198 * test passes in case the retval equals 1. 199 */ 200 @Test 201 public void testCheckDocumentBuilderFactory17() { 202 try { 203 MyErrorHandler eh = MyErrorHandler.newInstance(); 204 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 205 DocumentBuilder db = dbf.newDocumentBuilder(); 206 db.setErrorHandler(eh); 207 Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory04.xml")); 208 assertFalse(eh.errorOccured); 209 } catch (ParserConfigurationException | IOException | SAXException e) { 210 failUnexpected(e); 211 } 212 213 } 214 215 /** 216 * To test the isExpandEntityReferences. By default it is true. 217 */ 218 @Test 219 public void testCheckDocumentBuilderFactory05() { 220 try { 221 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 222 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 223 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml"))); 224 Element e = (Element) doc.getElementsByTagName("title").item(0); 225 NodeList nl = e.getChildNodes(); 226 assertTrue(dbf.isExpandEntityReferences()); 227 assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W'); 228 } catch (ParserConfigurationException | IOException | SAXException e) { 229 failUnexpected(e); 230 } 231 } 232 233 /** 234 * Testcase to test the default functionality of setValidating method. The 235 * xml file has a DTD which has namespaces defined. The parser takes care to 236 * check if the namespaces using elements and defined attributes are there 237 * or not. 238 */ 239 @Test 240 public void testCheckDocumentBuilderFactory06() { 241 try { 242 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 243 dbf.setValidating(true); 244 DocumentBuilder db = dbf.newDocumentBuilder(); 245 MyErrorHandler eh = MyErrorHandler.newInstance(); 246 db.setErrorHandler(eh); 247 Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory04.xml")); 248 assertTrue(doc instanceof Document); 249 assertFalse(eh.errorOccured); 250 } catch (ParserConfigurationException | IOException | SAXException e) { 251 failUnexpected(e); 252 } 253 254 } 255 256 /** 257 * Testcase to test the setExpandEntityReferences. 258 */ 259 @Test 260 public void testCheckDocumentBuilderFactory07() { 261 try { 262 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 263 dbf.setExpandEntityReferences(true); 264 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 265 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml"))); 266 Element e = (Element) doc.getElementsByTagName("title").item(0); 267 NodeList nl = e.getChildNodes(); 268 assertTrue(dbf.isExpandEntityReferences()); 269 assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W'); 270 } catch (ParserConfigurationException | IOException | SAXException e) { 271 failUnexpected(e); 272 } 273 } 274 275 /** 276 * Testcase to test the setExpandEntityReferences. 277 */ 278 @Test 279 public void testCheckDocumentBuilderFactory08() { 280 try { 281 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 282 dbf.setExpandEntityReferences(false); 283 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 284 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml"))); 285 Element e = (Element) doc.getElementsByTagName("title").item(0); 286 NodeList nl = e.getChildNodes(); 287 assertNull(nl.item(0).getNodeValue()); 288 } catch (ParserConfigurationException | IOException | SAXException e) { 289 failUnexpected(e); 290 } 291 } 292 293 /** 294 * Testcase to test the setIgnoringComments. By default it is set to false. 295 * explicitly setting it to false, it recognizes the comment which is in 296 * Element Node Hence the Element's child node is not null. 297 */ 298 @Test 299 public void testCheckDocumentBuilderFactory09() { 300 try { 301 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 302 dbf.setIgnoringComments(false); 303 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 304 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml"))); 305 Element e = (Element) doc.getElementsByTagName("body").item(0); 306 NodeList nl = e.getChildNodes(); 307 assertNotNull(nl.item(0).getNodeValue()); 308 } catch (ParserConfigurationException | IOException | SAXException e) { 309 failUnexpected(e); 310 } 311 312 } 313 314 /** 315 * This tests for the parse(InputSource). 316 */ 317 @Test 318 public void testCheckDocumentBuilderFactory10() { 319 try { 320 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 321 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 322 Document doc = docBuilder.parse(new InputSource(new BufferedReader(new FileReader(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml"))))); 323 assertTrue(doc instanceof Document); 324 } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) { 325 failUnexpected(e); 326 } 327 } 328 329 /** 330 * This tests for the parse InputStream with SystemID as a second parameter. 331 */ 332 @Test 333 public void testCheckDocumentBuilderFactory11() { 334 try { 335 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 336 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 337 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "dbf10import.xsl")), "file:///" + TestUtils.XML_DIR + FILE_SEP); 338 assertTrue(doc instanceof Document); 339 } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) { 340 failUnexpected(e); 341 } 342 } 343 344 /** 345 * This tests for the parse InputStream with empty SystemID as a second 346 * parameter. 347 */ 348 @Test 349 public void testCheckDocumentBuilderFactory12() { 350 try { 351 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 352 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 353 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "dbf10import.xsl")), " "); 354 assertTrue(doc instanceof Document); 355 } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) { 356 failUnexpected(e); 357 } 358 } 359 360 /** 361 * This tests for the parse(uri). 362 */ 363 @Test 364 public void testCheckDocumentBuilderFactory13() { 365 try { 366 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 367 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 368 Document doc = docBuilder.parse("file:///" + TestUtils.XML_DIR + FILE_SEP + "dbf10import.xsl"); 369 assertTrue(doc instanceof Document); 370 } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) { 371 failUnexpected(e); 372 } 373 } 374 375 /** 376 * This tests for the parse (uri) with empty string as parameter should 377 * throw Sax Exception. 378 * 379 * @throws SAXException 380 * If any parse errors occur. 381 */ 382 @Test(expectedExceptions = SAXException.class) 383 public void testCheckDocumentBuilderFactory14() throws SAXException { 384 try { 385 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 386 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 387 docBuilder.parse(""); 388 } catch (ParserConfigurationException | IOException e) { 389 failUnexpected(e); 390 } 391 } 392 393 /** 394 * This tests for the parse (uri) with null uri as parameter should throw 395 * IllegalArgumentException. 396 * 397 */ 398 @Test(expectedExceptions = IllegalArgumentException.class) 399 public void testCheckDocumentBuilderFactory15() { 400 try { 401 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 402 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 403 String uri = null; 404 docBuilder.parse(uri); 405 } catch (ParserConfigurationException | SAXException | IOException e) { 406 failUnexpected(e); 407 } 408 } 409 410 /** 411 * Testcase to test the setIgnoringComments. By default it is set to false, 412 * setting this to true, It does not recognize the comment, Here the 413 * nodelist has a length 0 because the ignoring comments is true. 414 */ 415 @Test 416 public void testCheckIgnoringComments() { 417 try { 418 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 419 dbf.setIgnoringComments(true); 420 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 421 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory08.xml"))); 422 Element e = (Element) doc.getElementsByTagName("body").item(0); 423 NodeList nl = e.getChildNodes(); 424 assertEquals(nl.getLength(), 0); 425 } catch (ParserConfigurationException | SAXException | IOException e) { 426 failUnexpected(e); 427 } 428 429 } 430 431 /** 432 * Testcase to test the default behaviour of setIgnoringComments. By default 433 * it is set to false, this is similar to case 9 but not setIgnoringComments 434 * explicitly, it does not recognize the comment. 435 */ 436 @Test 437 public void testCheckIgnoringComments1() { 438 try { 439 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 440 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 441 Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml"))); 442 Element e = (Element) doc.getElementsByTagName("body").item(0); 443 NodeList nl = e.getChildNodes(); 444 assertFalse(dbf.isIgnoringComments()); 445 assertNotNull(nl.item(0).getNodeValue()); 446 } catch (ParserConfigurationException | SAXException | IOException e) { 447 failUnexpected(e); 448 } 449 } 450 }