1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package tests.api.javax.xml.parsers; 17 18 import dalvik.annotation.TestTargets; 19 import dalvik.annotation.TestLevel; 20 import dalvik.annotation.TestTargetNew; 21 import dalvik.annotation.TestTargetClass; 22 23 import junit.framework.TestCase; 24 25 import org.w3c.dom.Document; 26 import org.w3c.dom.Node; 27 import org.w3c.dom.NodeList; 28 import org.xml.sax.ErrorHandler; 29 import org.xml.sax.SAXException; 30 import org.xml.sax.SAXParseException; 31 32 import java.io.ByteArrayInputStream; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.List; 36 import java.util.Properties; 37 38 import javax.xml.parsers.DocumentBuilder; 39 import javax.xml.parsers.DocumentBuilderFactory; 40 import javax.xml.parsers.FactoryConfigurationError; 41 import javax.xml.parsers.ParserConfigurationException; 42 43 @TestTargetClass(DocumentBuilderFactory.class) 44 public class DocumentBuilderFactoryTest extends TestCase { 45 46 DocumentBuilderFactory dbf; 47 48 List<String> cdataElements; 49 50 List<String> textElements; 51 52 List<String> commentElements; 53 54 protected void setUp() throws Exception { 55 super.setUp(); 56 dbf = DocumentBuilderFactory.newInstance(); 57 58 cdataElements = new ArrayList<String>(); 59 textElements = new ArrayList<String>(); 60 commentElements = new ArrayList<String>(); 61 } 62 63 protected void tearDown() throws Exception { 64 dbf = null; 65 cdataElements = null; 66 textElements = null; 67 commentElements = null; 68 super.tearDown(); 69 } 70 71 /** 72 * @tests javax.xml.parsers.DocumentBuilderFactory#DocumentBuilderFactory(). 73 */ 74 @TestTargetNew( 75 level = TestLevel.COMPLETE, 76 notes = "", 77 method = "DocumentBuilderFactory", 78 args = {} 79 ) 80 public void test_Constructor() { 81 try { 82 new DocumentBuilderFactoryChild(); 83 } catch (Exception e) { 84 fail("Unexpected exception " + e.toString()); 85 } 86 } 87 88 /** 89 * @tests javax.xml.parsers.DocumentBuilderFactory#getAttribute(String). 90 */ 91 // public void test_getAttributeLjava_lang_String() { 92 // String[] attributes = { 93 // "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 94 // "http://java.sun.com/xml/jaxp/properties/schemaSource" }; 95 // Object[] values = { "http://www.w3.org/2001/XMLSchema", "source" }; 96 // 97 // try { 98 // for (int i = 0; i < attributes.length; i++) { 99 // dbf.setAttribute(attributes[i], values[i]); 100 // assertEquals(values[i], dbf.getAttribute(attributes[i])); 101 // } 102 // } catch (IllegalArgumentException e) { 103 // fail("Unexpected IllegalArgumentException" + e.getMessage()); 104 // } catch (Exception e) { 105 // fail("Unexpected exception" + e.getMessage()); 106 // } 107 // 108 // try { 109 // for (int i = 0; i < attributes.length; i++) { 110 // dbf.setAttribute(null, null); 111 // fail("NullPointerException expected"); 112 // } 113 // } catch (NullPointerException e) { 114 // // expected 115 // } 116 // 117 // String[] badAttributes = {"bad1", "bad2", ""}; 118 // try { 119 // for (int i = 0; i < badAttributes.length; i++) { 120 // dbf.getAttribute(badAttributes[i]); 121 // fail("IllegalArgumentException expected"); 122 // } 123 // } catch (IllegalArgumentException e) { 124 // // expected 125 // } 126 // } 127 128 /** 129 * @tests javax.xml.parsers.DocumentBuilderFactory#getFeature(String). 130 */ 131 // TODO Fails on JDK. Why? 132 // public void test_getFeatureLjava_lang_String() { 133 // String[] features = { "http://xml.org/sax/features/namespaces", 134 // "http://xml.org/sax/features/validation", 135 // "http://xml.org/sax/features/external-general-entities" }; 136 // try { 137 // for (int i = 0; i < features.length; i++) { 138 // dbf.setFeature(features[i], true); 139 // assertTrue(dbf.getFeature(features[i])); 140 // } 141 // } catch (ParserConfigurationException e) { 142 // fail("Unexpected ParserConfigurationException " + e.getMessage()); 143 // } 144 // 145 // try { 146 // for (int i = 0; i < features.length; i++) { 147 // dbf.setFeature(features[i], false); 148 // assertFalse(dbf.getFeature(features[i])); 149 // } 150 // } catch (ParserConfigurationException e) { 151 // fail("Unexpected ParserConfigurationException " + e.getMessage()); 152 // } 153 // 154 // try { 155 // for (int i = 0; i < features.length; i++) { 156 // dbf.setFeature(null, false); 157 // fail("NullPointerException expected"); 158 // } 159 // } catch (NullPointerException e) { 160 // // expected 161 // } catch (ParserConfigurationException e) { 162 // fail("Unexpected ParserConfigurationException" + e.getMessage()); 163 // } 164 // 165 // String[] badFeatures = {"bad1", "bad2", ""}; 166 // try { 167 // for (int i = 0; i < badFeatures.length; i++) { 168 // dbf.getFeature(badFeatures[i]); 169 // fail("ParserConfigurationException expected"); 170 // } 171 // } catch (ParserConfigurationException e) { 172 // // expected 173 // } 174 // 175 // } 176 177 /** 178 * @tests javax.xml.parsers.DocumentBuilderFactory#getSchema(). 179 * TBD getSchemas() IS NOT SUPPORTED 180 */ 181 /* public void test_getSchema() { 182 assertNull(dbf.getSchema()); 183 SchemaFactory sf = 184 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 185 try { 186 Schema schema = sf.newSchema(); 187 dbf.setSchema(schema); 188 assertNotNull(dbf.getSchema()); 189 } catch (SAXException sax) { 190 fail("Unexpected exception " + sax.toString()); 191 } 192 } 193 */ 194 195 /** 196 * @tests javax.xml.parsers.DocumentBuilderFactory#isCoalescing(). 197 */ 198 @TestTargetNew( 199 level = TestLevel.COMPLETE, 200 notes = "", 201 method = "isCoalescing", 202 args = {} 203 ) 204 public void test_isCoalescing() { 205 dbf.setCoalescing(true); 206 assertTrue(dbf.isCoalescing()); 207 208 dbf.setCoalescing(false); 209 assertFalse(dbf.isCoalescing()); 210 } 211 212 /** 213 * @tests javax.xml.parsers.DocumentBuilderFactory#isExpandEntityReferences(). 214 */ 215 @TestTargetNew( 216 level = TestLevel.COMPLETE, 217 notes = "", 218 method = "isExpandEntityReferences", 219 args = {} 220 ) 221 public void test_isExpandEntityReferences() { 222 dbf.setExpandEntityReferences(true); 223 assertTrue(dbf.isExpandEntityReferences()); 224 225 dbf.setExpandEntityReferences(false); 226 assertFalse(dbf.isExpandEntityReferences()); 227 } 228 229 /** 230 * @tests javax.xml.parsers.DocumentBuilderFactory#isIgnoringComments(). 231 */ 232 @TestTargetNew( 233 level = TestLevel.COMPLETE, 234 notes = "", 235 method = "isIgnoringComments", 236 args = {} 237 ) 238 public void test_isIgnoringComments() { 239 dbf.setIgnoringComments(true); 240 assertTrue(dbf.isIgnoringComments()); 241 242 dbf.setIgnoringComments(false); 243 assertFalse(dbf.isIgnoringComments()); 244 } 245 246 /** 247 * @tests javax.xml.parsers.DocumentBuilderFactory#isIgnoringElementContentWhitespace(). 248 */ 249 @TestTargetNew( 250 level = TestLevel.COMPLETE, 251 notes = "", 252 method = "isIgnoringElementContentWhitespace", 253 args = {} 254 ) 255 public void test_isIgnoringElementContentWhitespace() { 256 dbf.setIgnoringElementContentWhitespace(true); 257 assertTrue(dbf.isIgnoringElementContentWhitespace()); 258 259 dbf.setIgnoringElementContentWhitespace(false); 260 assertFalse(dbf.isIgnoringElementContentWhitespace()); 261 } 262 263 /** 264 * @tests javax.xml.parsers.DocumentBuilderFactory#isNamespaceAware(). 265 */ 266 @TestTargetNew( 267 level = TestLevel.COMPLETE, 268 notes = "", 269 method = "isNamespaceAware", 270 args = {} 271 ) 272 public void test_isNamespaceAware() { 273 dbf.setNamespaceAware(true); 274 assertTrue(dbf.isNamespaceAware()); 275 276 dbf.setNamespaceAware(false); 277 assertFalse(dbf.isNamespaceAware()); 278 } 279 280 @TestTargets({ 281 @TestTargetNew( 282 level = TestLevel.COMPLETE, 283 notes = "", 284 method = "isValidating", 285 args = {} 286 ), 287 @TestTargetNew( 288 level = TestLevel.SUFFICIENT, 289 notes = "", 290 method = "setValidating", 291 args = {boolean.class} 292 ) 293 }) 294 public void test_setIsValidating() { 295 dbf.setValidating(true); 296 assertTrue(dbf.isValidating()); 297 298 dbf.setValidating(false); 299 assertFalse(dbf.isValidating()); 300 } 301 302 @TestTargets({ 303 @TestTargetNew( 304 level = TestLevel.COMPLETE, 305 notes = "", 306 method = "isXIncludeAware", 307 args = {} 308 ), 309 @TestTargetNew( 310 level = TestLevel.SUFFICIENT, 311 notes = "", 312 method = "setXIncludeAware", 313 args = {boolean.class} 314 ) 315 }) 316 public void test_isSetXIncludeAware() { 317 dbf.setXIncludeAware(true); 318 assertTrue(dbf.isXIncludeAware()); 319 320 dbf.setXIncludeAware(false); 321 assertFalse(dbf.isXIncludeAware()); 322 } 323 324 /** 325 * @tests javax.xml.parsers.DocumentBuilderFactory#newInstance(). 326 */ 327 @TestTargetNew( 328 level = TestLevel.COMPLETE, 329 notes = "", 330 method = "newInstance", 331 args = {} 332 ) 333 public void test_newInstance() { 334 String className = null; 335 try { 336 // case 1: Try to obtain a new instance of factory by default. 337 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 338 assertNotNull(dbf); 339 340 // case 2: Try to create a new instance of factory using 341 // property DATATYPEFACTORY_PROPERTY 342 className = System.getProperty("javax.xml.parsers.DocumentBuilderFactory"); 343 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", 344 "org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl"); 345 346 dbf = DocumentBuilderFactory.newInstance(); 347 assertNotNull(dbf); 348 assertTrue(dbf instanceof org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl); 349 350 // case 3: Try to create a new instance of factory using Property 351 String keyValuePair = "javax.xml.parsers.DocumentBuilderFactory" 352 + "=" + "org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl"; 353 ByteArrayInputStream bis = new ByteArrayInputStream(keyValuePair 354 .getBytes()); 355 Properties prop = System.getProperties(); 356 prop.load(bis); 357 dbf = DocumentBuilderFactory.newInstance(); 358 assertNotNull(dbf); 359 assertTrue(dbf instanceof org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl); 360 361 // case 4: Check FactoryConfiguration error 362 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", ""); 363 try { 364 DocumentBuilderFactory.newInstance(); 365 } catch (FactoryConfigurationError fce) { 366 // expected 367 } 368 369 } catch (Exception e) { 370 fail("Unexpected exception " + e.toString()); 371 } finally { 372 // Set default value of Datatype factory, 373 // because of this test modifies it. 374 if (className == null) { 375 System.clearProperty("javax.xml.parsers.DocumentBuilderFactory"); 376 } else { 377 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", 378 className); 379 } 380 } 381 } 382 383 @TestTargetNew( 384 level = TestLevel.SUFFICIENT, 385 notes = "SAXException untested; unused on Android", 386 method = "newDocumentBuilder", 387 args = {} 388 ) 389 public void test_newDocumentBuilder() { 390 // Ordinary case 391 try { 392 DocumentBuilder db = dbf.newDocumentBuilder(); 393 assertTrue(db instanceof DocumentBuilder); 394 db.parse(getClass().getResourceAsStream("/simple.xml")); 395 } catch(Exception e) { 396 throw new RuntimeException("Unexpected exception", e); 397 } 398 399 // Exception case 400 dbf.setValidating(true); 401 try { 402 DocumentBuilder db = dbf.newDocumentBuilder(); 403 } catch(ParserConfigurationException e) { 404 // Expected, since Android doesn't have a validating parser. 405 } 406 } 407 408 /** 409 * @tests javax.xml.parsers.DocumentBuilderFactory#setAttribute(java.lang.String, 410 * java.lang.Object). 411 */ 412 // public void test_setAttributeLjava_lang_StringLjava_lang_Object() { 413 // String[] attributes = { 414 // "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 415 // "http://java.sun.com/xml/jaxp/properties/schemaSource" }; 416 // Object[] values = { "http://www.w3.org/2001/XMLSchema", "source" }; 417 // 418 // try { 419 // for (int i = 0; i < attributes.length; i++) { 420 // dbf.setAttribute(attributes[i], values[i]); 421 // assertEquals(values[i], dbf.getAttribute(attributes[i])); 422 // } 423 // } catch (IllegalArgumentException e) { 424 // fail("Unexpected IllegalArgumentException" + e.getMessage()); 425 // } catch (Exception e) { 426 // fail("Unexpected exception" + e.getMessage()); 427 // } 428 // 429 // String[] badAttributes = {"bad1", "bad2", ""}; 430 // try { 431 // for (int i = 0; i < badAttributes.length; i++) { 432 // dbf.setAttribute(badAttributes[i], ""); 433 // fail("IllegalArgumentException expected"); 434 // } 435 // } catch (IllegalArgumentException iae) { 436 // // expected 437 // } 438 // 439 // try { 440 // for (int i = 0; i < attributes.length; i++) { 441 // dbf.setAttribute(null, null); 442 // fail("NullPointerException expected"); 443 // } 444 // } catch (NullPointerException e) { 445 // // expected 446 // } 447 // } 448 449 /** 450 * @tests javax.xml.parsers.DocumentBuilderFactory#setCoalescing(boolean). 451 */ 452 @TestTargetNew( 453 level = TestLevel.COMPLETE, 454 notes = "", 455 method = "setCoalescing", 456 args = {boolean.class} 457 ) 458 public void test_setCoalescingZ() { 459 dbf.setCoalescing(true); 460 assertTrue(dbf.isCoalescing()); 461 462 textElements.clear(); 463 cdataElements.clear(); 464 Exception parseException = null; 465 DocumentBuilder parser = null; 466 467 try { 468 parser = dbf.newDocumentBuilder(); 469 ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 470 parser.setErrorHandler(errorHandler); 471 472 Document document = parser.parse(getClass().getResourceAsStream( 473 "/recipt.xml")); 474 475 parseException = errorHandler.getFirstException(); 476 477 goThroughDocument((Node) document, ""); 478 assertTrue(textElements 479 .contains("BeefParmesan<title>withGarlicAngelHairPasta</title>")); 480 } catch (Exception ex) { 481 parseException = ex; 482 } 483 parser.setErrorHandler(null); 484 485 if (parseException != null) { 486 fail("Unexpected exception " + parseException.getMessage()); 487 } 488 489 dbf.setCoalescing(false); 490 assertFalse(dbf.isCoalescing()); 491 492 textElements.clear(); 493 cdataElements.clear(); 494 495 try { 496 parser = dbf.newDocumentBuilder(); 497 ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 498 parser.setErrorHandler(errorHandler); 499 500 Document document = parser.parse(getClass().getResourceAsStream( 501 "/recipt.xml")); 502 503 parseException = errorHandler.getFirstException(); 504 505 goThroughDocument((Node) document, ""); 506 507 assertFalse(textElements 508 .contains("BeefParmesan<title>withGarlicAngelHairPasta</title>")); 509 510 } catch (Exception ex) { 511 parseException = ex; 512 } 513 parser.setErrorHandler(null); 514 515 if (parseException != null) { 516 fail("Unexpected exception " + parseException.getMessage()); 517 } 518 } 519 520 /** 521 * @tests javax.xml.parsers.DocumentBuilderFactory#setExpandEntityReferences(boolean). 522 */ 523 @TestTargetNew( 524 level = TestLevel.COMPLETE, 525 notes = "", 526 method = "setExpandEntityReferences", 527 args = {boolean.class} 528 ) 529 public void test_setExpandEntityReferencesZ() { 530 dbf.setExpandEntityReferences(true); 531 assertTrue(dbf.isExpandEntityReferences()); 532 533 Exception parseException = null; 534 DocumentBuilder parser = null; 535 536 try { 537 parser = dbf.newDocumentBuilder(); 538 ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 539 parser.setErrorHandler(errorHandler); 540 541 Document document = parser.parse(getClass().getResourceAsStream( 542 "/recipt.xml")); 543 544 parseException = errorHandler.getFirstException(); 545 546 assertNotNull(document); 547 548 } catch (Exception ex) { 549 parseException = ex; 550 } 551 parser.setErrorHandler(null); 552 553 if (parseException != null) { 554 fail("Unexpected exception " + parseException.getMessage()); 555 } 556 557 dbf.setExpandEntityReferences(false); 558 assertFalse(dbf.isExpandEntityReferences()); 559 try { 560 parser = dbf.newDocumentBuilder(); 561 ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 562 parser.setErrorHandler(errorHandler); 563 564 Document document = parser.parse(getClass().getResourceAsStream( 565 "/recipt.xml")); 566 567 parseException = errorHandler.getFirstException(); 568 569 assertNotNull(document); 570 571 } catch (Exception ex) { 572 parseException = ex; 573 } 574 parser.setErrorHandler(null); 575 576 if (parseException != null) { 577 fail("Unexpected exception " + parseException.getMessage()); 578 } 579 } 580 581 /** 582 * @tests javax.xml.parsers.DocumentBuilderFactory#setFeature(java.lang.String). 583 */ 584 @TestTargets({ 585 @TestTargetNew( 586 level = TestLevel.COMPLETE, 587 notes = "", 588 method = "getFeature", 589 args = {java.lang.String.class} 590 ), 591 @TestTargetNew( 592 level = TestLevel.COMPLETE, 593 notes = "", 594 method = "setFeature", 595 args = {java.lang.String.class, boolean.class} 596 ) 597 }) 598 public void test_getSetFeatureLjava_lang_String() { 599 String[] features = { "http://xml.org/sax/features/namespaces", 600 "http://xml.org/sax/features/validation" }; 601 try { 602 for (int i = 0; i < features.length; i++) { 603 dbf.setFeature(features[i], true); 604 assertTrue(dbf.getFeature(features[i])); 605 } 606 } catch (ParserConfigurationException e) { 607 fail("Unexpected ParserConfigurationException" + e.getMessage()); 608 } 609 610 try { 611 for (int i = 0; i < features.length; i++) { 612 dbf.setFeature(features[i], false); 613 assertFalse(dbf.getFeature(features[i])); 614 } 615 } catch (ParserConfigurationException e) { 616 fail("Unexpected ParserConfigurationException" + e.getMessage()); 617 } 618 619 try { 620 for (int i = 0; i < features.length; i++) { 621 dbf.setFeature(null, false); 622 fail("NullPointerException expected"); 623 } 624 } catch (NullPointerException e) { 625 // expected 626 } catch (ParserConfigurationException e) { 627 fail("Unexpected ParserConfigurationException" + e.getMessage()); 628 } 629 630 String[] badFeatures = { "bad1", "bad2", "" }; 631 try { 632 for (int i = 0; i < badFeatures.length; i++) { 633 dbf.setFeature(badFeatures[i], false); 634 fail("ParserConfigurationException expected"); 635 } 636 } catch (ParserConfigurationException e) { 637 // expected 638 } 639 } 640 641 /** 642 * @tests javax.xml.parsers.DocumentBuilderFactory#setIgnoringComments(boolean). 643 */ 644 @TestTargetNew( 645 level = TestLevel.COMPLETE, 646 notes = "", 647 method = "setIgnoringComments", 648 args = {boolean.class} 649 ) 650 public void test_setIgnoringCommentsZ() { 651 commentElements.clear(); 652 653 dbf.setIgnoringComments(true); 654 assertTrue(dbf.isIgnoringComments()); 655 656 try { 657 DocumentBuilder parser = dbf.newDocumentBuilder(); 658 659 Document document = parser.parse(getClass().getResourceAsStream( 660 "/recipt.xml")); 661 662 goThroughDocument((Node) document, ""); 663 assertFalse(commentElements.contains("comment1")); 664 assertFalse(commentElements.contains("comment2")); 665 666 } catch (IOException e) { 667 fail("Unexpected IOException " + e.getMessage()); 668 } catch (ParserConfigurationException e) { 669 fail("Unexpected ParserConfigurationException " + e.getMessage()); 670 } catch (SAXException e) { 671 fail("Unexpected SAXException " + e.getMessage()); 672 } 673 674 commentElements.clear(); 675 676 dbf.setIgnoringComments(false); 677 assertFalse(dbf.isIgnoringComments()); 678 679 try { 680 DocumentBuilder parser = dbf.newDocumentBuilder(); 681 682 Document document = parser.parse(getClass().getResourceAsStream( 683 "/recipt.xml")); 684 685 goThroughDocument((Node) document, ""); 686 assertTrue(commentElements.contains("comment1")); 687 assertTrue(commentElements.contains("comment2")); 688 689 } catch (IOException e) { 690 fail("Unexpected IOException " + e.getMessage()); 691 } catch (ParserConfigurationException e) { 692 fail("Unexpected ParserConfigurationException " + e.getMessage()); 693 } catch (SAXException e) { 694 fail("Unexpected SAXException " + e.getMessage()); 695 } 696 } 697 698 /** 699 * @tests javax.xml.parsers.DocumentBuilderFactory#setIgnoringElementContentWhitespace(boolean). 700 */ 701 @TestTargetNew( 702 level = TestLevel.COMPLETE, 703 notes = "", 704 method = "setIgnoringElementContentWhitespace", 705 args = {boolean.class} 706 ) 707 public void test_setIgnoringElementContentWhitespaceZ() { 708 dbf.setIgnoringElementContentWhitespace(true); 709 assertTrue(dbf.isIgnoringElementContentWhitespace()); 710 711 try { 712 DocumentBuilder parser = dbf.newDocumentBuilder(); 713 714 Document document = parser.parse(getClass().getResourceAsStream( 715 "/recipt.xml")); 716 717 assertNotNull(document); 718 719 } catch (IOException e) { 720 fail("Unexpected IOException " + e.getMessage()); 721 } catch (ParserConfigurationException e) { 722 fail("Unexpected ParserConfigurationException " + e.getMessage()); 723 } catch (SAXException e) { 724 fail("Unexpected SAXException " + e.getMessage()); 725 } 726 727 dbf.setIgnoringElementContentWhitespace(false); 728 assertFalse(dbf.isIgnoringElementContentWhitespace()); 729 730 try { 731 DocumentBuilder parser = dbf.newDocumentBuilder(); 732 733 Document document = parser.parse(getClass().getResourceAsStream( 734 "/recipt.xml")); 735 736 assertNotNull(document); 737 738 } catch (IOException e) { 739 fail("Unexpected IOException " + e.getMessage()); 740 } catch (ParserConfigurationException e) { 741 fail("Unexpected ParserConfigurationException " + e.getMessage()); 742 } catch (SAXException e) { 743 fail("Unexpected SAXException " + e.getMessage()); 744 } 745 } 746 747 /** 748 * @tests javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean). 749 */ 750 @TestTargetNew( 751 level = TestLevel.COMPLETE, 752 notes = "", 753 method = "setNamespaceAware", 754 args = {boolean.class} 755 ) 756 public void test_setNamespaceAwareZ() { 757 dbf.setNamespaceAware(true); 758 assertTrue(dbf.isNamespaceAware()); 759 760 try { 761 DocumentBuilder parser = dbf.newDocumentBuilder(); 762 763 Document document = parser.parse(getClass().getResourceAsStream( 764 "/recipt.xml")); 765 766 assertNotNull(document); 767 768 } catch (IOException e) { 769 fail("Unexpected IOException " + e.getMessage()); 770 } catch (ParserConfigurationException e) { 771 fail("Unexpected ParserConfigurationException " + e.getMessage()); 772 } catch (SAXException e) { 773 fail("Unexpected SAXException " + e.getMessage()); 774 } 775 776 dbf.setNamespaceAware(false); 777 assertFalse(dbf.isNamespaceAware()); 778 779 try { 780 DocumentBuilder parser = dbf.newDocumentBuilder(); 781 782 Document document = parser.parse(getClass().getResourceAsStream( 783 "/recipt.xml")); 784 785 assertNotNull(document); 786 787 } catch (IOException e) { 788 fail("Unexpected IOException " + e.getMessage()); 789 } catch (ParserConfigurationException e) { 790 fail("Unexpected ParserConfigurationException " + e.getMessage()); 791 } catch (SAXException e) { 792 fail("Unexpected SAXException " + e.getMessage()); 793 } 794 } 795 796 @TestTargets({ 797 @TestTargetNew( 798 level = TestLevel.COMPLETE, 799 notes = "", 800 method = "getAttribute", 801 args = {java.lang.String.class} 802 ), 803 @TestTargetNew( 804 level = TestLevel.COMPLETE, 805 notes = "", 806 method = "setAttribute", 807 args = {java.lang.String.class, Object.class} 808 ) 809 }) 810 public void test_getSetAttribute() { 811 // Android SAX implementation doesn't support attributes, so 812 // we can only make sure the expected exception is thrown. 813 try { 814 dbf.setAttribute("foo", new Object()); 815 fail("IllegalArgumentException expected"); 816 } catch (IllegalArgumentException e) { 817 // Expected 818 } 819 820 try { 821 dbf.getAttribute("foo"); 822 fail("IllegalArgumentException expected"); 823 } catch (IllegalArgumentException e) { 824 // Expected 825 } 826 } 827 828 /** 829 * @tests javax.xml.parsers.DocumentBuilderFactory#setSchema(javax.xml.validation.Schema). 830 */ 831 /* public void test_setSchemaLjavax_xml_validation_Schema() { 832 SchemaFactory sf = 833 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 834 try { 835 Schema schema = sf.newSchema(); 836 dbf.setSchema(schema); 837 assertNotNull(dbf.getSchema()); 838 } catch (SAXException sax) { 839 fail("Unexpected exception " + sax.toString()); 840 } 841 } 842 */ 843 /** 844 * @tests javax.xml.parsers.DocumentBuilderFactory#setValidating(boolean). 845 */ 846 // public void test_setValidatingZ() { 847 // Exception parseException = null; 848 // DocumentBuilder parser = null; 849 // Document document = null; 850 // 851 // ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 852 // 853 // dbf.setValidating(false); 854 // assertFalse(dbf.isValidating()); 855 // 856 // // case 1: Validation is not set. Correct xml-file 857 // try { 858 // 859 // parser = dbf.newDocumentBuilder(); 860 // parser.setErrorHandler(errorHandler); 861 // 862 // document = parser.parse(getClass().getResourceAsStream( 863 // "/recipt.xml")); 864 // 865 // parseException = errorHandler.getFirstException(); 866 // 867 // assertNotNull(document); 868 // 869 // document = parser.parse(getClass().getResourceAsStream( 870 // "/reciptWrong.xml")); 871 // 872 // parseException = errorHandler.getFirstException(); 873 // 874 // assertNotNull(document); 875 // 876 // } catch (Exception ex) { 877 // parseException = ex; 878 // } 879 // parser.setErrorHandler(null); 880 // 881 // if (parseException != null) { 882 // fail("Unexpected exception " + parseException.getMessage()); 883 // } 884 // 885 // // case 2: Validation is not set. Wrong xml-file 886 // try { 887 // 888 // parser = dbf.newDocumentBuilder(); 889 // parser.setErrorHandler(errorHandler); 890 // 891 // document = parser.parse(getClass().getResourceAsStream( 892 // "/reciptWrong.xml")); 893 // parseException = errorHandler.getFirstException(); 894 // 895 // assertNotNull(document); 896 // 897 // } catch (Exception ex) { 898 // parseException = ex; 899 // } 900 // parser.setErrorHandler(null); 901 // 902 // if (parseException != null) { 903 // fail("Unexpected exception " + parseException.getMessage()); 904 // } 905 // 906 // // case 3: Validation is set. Correct xml-file 907 // dbf.setValidating(true); 908 // assertTrue(dbf.isValidating()); 909 // 910 // try { 911 // 912 // parser = dbf.newDocumentBuilder(); 913 // parser.setErrorHandler(errorHandler); 914 // 915 // document = parser.parse(getClass().getResourceAsStream( 916 // "/recipt.xml")); 917 // parseException = errorHandler.getFirstException(); 918 // 919 // assertNotNull(document); 920 // 921 // } catch (Exception ex) { 922 // parseException = ex; 923 // } 924 // parser.setErrorHandler(null); 925 // 926 // if (parseException != null) { 927 // fail("Unexpected exception " + parseException.getMessage()); 928 // } 929 // 930 // // case 4: Validation is set. Wrong xml-file 931 // try { 932 // 933 // parser = dbf.newDocumentBuilder(); 934 // parser.setErrorHandler(errorHandler); 935 // 936 // document = parser.parse(getClass().getResourceAsStream( 937 // "/reciptWrong.xml")); 938 // parseException = errorHandler.getFirstException(); 939 // 940 // assertNotNull(document); 941 // 942 // } catch (Exception ex) { 943 // parseException = ex; 944 // } 945 // parser.setErrorHandler(null); 946 // 947 // if (parseException == null) { 948 // fail("Unexpected exception " + parseException.getMessage()); 949 // } else { 950 // assertTrue(parseException 951 // .getMessage() 952 // .contains( 953 // "The content of element type \"collection\" must match \"(description,recipe+)\"")); 954 // } 955 // 956 // } 957 958 /** 959 * @tests javax.xml.parsers.DocumentBuilderFactory#setXIncludeAware(). 960 */ 961 // public void test_setXIncludeAware() { 962 // dbf.setXIncludeAware(true); 963 // assertTrue(dbf.isXIncludeAware()); 964 // 965 // try { 966 // DocumentBuilder parser = dbf.newDocumentBuilder(); 967 // 968 // Document document = parser.parse(getClass().getResourceAsStream( 969 // "/recipt.xml")); 970 // 971 // assertNotNull(document); 972 // 973 // } catch (IOException e) { 974 // fail("Unexpected IOException " + e.getMessage()); 975 // } catch (ParserConfigurationException e) { 976 // fail("Unexpected ParserConfigurationException " + e.getMessage()); 977 // } catch (SAXException e) { 978 // fail("Unexpected SAXException " + e.getMessage()); 979 // } 980 // 981 // dbf.setXIncludeAware(false); 982 // assertFalse(dbf.isXIncludeAware()); 983 // 984 // try { 985 // DocumentBuilder parser = dbf.newDocumentBuilder(); 986 // 987 // Document document = parser.parse(getClass().getResourceAsStream( 988 // "/recipt.xml")); 989 // 990 // assertNotNull(document); 991 // 992 // } catch (IOException e) { 993 // fail("Unexpected IOException " + e.getMessage()); 994 // } catch (ParserConfigurationException e) { 995 // fail("Unexpected ParserConfigurationException " + e.getMessage()); 996 // } catch (SAXException e) { 997 // fail("Unexpected SAXException " + e.getMessage()); 998 // } 999 // } 1000 1001 private void goThroughDocument(Node node, String indent) { 1002 String value = node.getNodeValue(); 1003 1004 if (value != null) { 1005 value = value.replaceAll(" ", ""); 1006 value = value.replaceAll("\n", ""); 1007 } 1008 1009 switch (node.getNodeType()) { 1010 case Node.CDATA_SECTION_NODE: 1011 cdataElements.add(value); 1012 // System.out.println(indent + "CDATA_SECTION_NODE " + value); 1013 break; 1014 case Node.COMMENT_NODE: 1015 commentElements.add(value); 1016 // System.out.println(indent + "COMMENT_NODE " + value); 1017 break; 1018 case Node.DOCUMENT_FRAGMENT_NODE: 1019 // System.out.println(indent + "DOCUMENT_FRAGMENT_NODE " + value); 1020 break; 1021 case Node.DOCUMENT_NODE: 1022 // System.out.println(indent + "DOCUMENT_NODE " + value); 1023 break; 1024 case Node.DOCUMENT_TYPE_NODE: 1025 // System.out.println(indent + "DOCUMENT_TYPE_NODE " + value); 1026 break; 1027 case Node.ELEMENT_NODE: 1028 // System.out.println(indent + "ELEMENT_NODE " + value); 1029 break; 1030 case Node.ENTITY_NODE: 1031 // System.out.println(indent + "ENTITY_NODE " + value); 1032 break; 1033 case Node.ENTITY_REFERENCE_NODE: 1034 // System.out.println(indent + "ENTITY_REFERENCE_NODE " + value); 1035 break; 1036 case Node.NOTATION_NODE: 1037 // System.out.println(indent + "NOTATION_NODE " + value); 1038 break; 1039 case Node.PROCESSING_INSTRUCTION_NODE: 1040 // System.out.println(indent + "PROCESSING_INSTRUCTION_NODE " + 1041 // value); 1042 break; 1043 case Node.TEXT_NODE: 1044 textElements.add(value); 1045 // System.out.println(indent + "TEXT_NODE " + value); 1046 break; 1047 default: 1048 // System.out.println(indent + "Unknown node " + value); 1049 break; 1050 } 1051 NodeList list = node.getChildNodes(); 1052 for (int i = 0; i < list.getLength(); i++) 1053 goThroughDocument(list.item(i), indent + " "); 1054 } 1055 1056 private class ValidationErrorHandler implements ErrorHandler { 1057 private SAXException parseException; 1058 1059 private int errorCount; 1060 1061 private int warningCount; 1062 1063 public ValidationErrorHandler() { 1064 parseException = null; 1065 errorCount = 0; 1066 warningCount = 0; 1067 } 1068 1069 public void error(SAXParseException ex) { 1070 errorCount++; 1071 if (parseException == null) { 1072 parseException = ex; 1073 } 1074 } 1075 1076 public void warning(SAXParseException ex) { 1077 warningCount++; 1078 } 1079 1080 public void fatalError(SAXParseException ex) { 1081 if (parseException == null) { 1082 parseException = ex; 1083 } 1084 } 1085 1086 public SAXException getFirstException() { 1087 return parseException; 1088 } 1089 } 1090 1091 private class DocumentBuilderFactoryChild extends DocumentBuilderFactory { 1092 public DocumentBuilderFactoryChild() { 1093 super(); 1094 } 1095 1096 public Object getAttribute(String name) { 1097 return null; 1098 } 1099 1100 public boolean getFeature(String name) { 1101 return false; 1102 } 1103 1104 public DocumentBuilder newDocumentBuilder() { 1105 return null; 1106 } 1107 1108 public void setAttribute(String name, Object value) { 1109 } 1110 1111 public void setFeature(String name, boolean value) { 1112 } 1113 1114 } 1115 } 1116