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