1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
|
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package dom;
import java.io.IOException;
import java.io.StringReader;
import java.net.URISyntaxException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Entity;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSParser;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/*
* @test
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true dom.DOMConfigurationTest
* @run testng/othervm dom.DOMConfigurationTest
* @summary Test DOMConfiguration for supported properties.
*/
@Listeners({jaxp.library.FilePolicy.class})
public class DOMConfigurationTest {
static class TestHandler implements DOMErrorHandler {
private String warning;
private String error;
private String fatalError;
public String getError() {
return error;
}
public String getFatalError() {
return fatalError;
}
public String getWarning() {
return warning;
}
public boolean handleError(DOMError error) {
if (error.getSeverity() == DOMError.SEVERITY_ERROR) {
this.error = "" + error.getMessage();
return false;
}
if (error.getSeverity() == DOMError.SEVERITY_FATAL_ERROR) {
this.fatalError = "" + error.getMessage();
return false;
}
this.warning = "" + error.getMessage();
return true; // warning
}
}
static class TestFailureHandler implements DOMErrorHandler {
public boolean handleError(DOMError error) {
if (error.getSeverity() == DOMError.SEVERITY_ERROR) {
Assert.fail("Error: " + error.getMessage());
}
if (error.getSeverity() == DOMError.SEVERITY_FATAL_ERROR) {
Assert.fail("Fatal error: " + error.getMessage());
}
return true; // warning
}
}
void setHandler(Document doc) {
doc.getDomConfig().setParameter("error-handler", new TestFailureHandler());
}
static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
static final String XMLNS = "http://www.w3.org/2000/xmlns/";
static Document loadDocument(String schemaURL, String instanceText) {
Document document = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
if (schemaURL != null) {
dbf.setAttribute(SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);
dbf.setAttribute(SCHEMA_SOURCE, schemaURL);
}
DocumentBuilder parser = dbf.newDocumentBuilder();
InputSource inSource = new InputSource(new StringReader(instanceText));
inSource.setSystemId("doc.xml");
document = parser.parse(inSource);
} catch (ParserConfigurationException e) {
Assert.fail(e.toString());
} catch (IOException e) {
Assert.fail(e.toString());
} catch (SAXException e) {
Assert.fail(e.toString());
}
return document;
}
static final String test_xml = "<?xml version=\"1.0\"?>\n" + "<test:root xmlns:test=\"test\" \n"
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + ">	

 1 </test:root>\n";
static final String test1_xml = "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE root [\n" + " <!ELEMENT root ANY>\n" + " <!ENTITY x \"X\">\n" + "]>\n"
+ "<root/>\n";
static final String test2_xml = "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE root [\n" + " <!ELEMENT root ANY>\n"
+ " <!ATTLIST root attr CDATA #REQUIRED>\n" + " <!ENTITY x \"<\">\n" + "]>\n" + "<root attr=\"x\"/>\n";
static final String test3_xml = "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE root [\n" + " <!ELEMENT root (elem*)>\n" + " <!ELEMENT elem EMPTY>\n"
+ "]>\n" + "<root/>\n";
static String test1_xsd_url;
static {
try {
test1_xsd_url = DOMConfigurationTest.class.getResource("DOMConfigurationTest.xsd").toURI().toString();
} catch (URISyntaxException uriSyntaxException) {
Assert.fail(uriSyntaxException.toString());
}
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the doc contains two subsequent processing
* instrictions, <br>
* <b>name</b>: canonical-form <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the subsequent processing instrictions are
* separated with a single line break
*/
@Test
public void testCanonicalForm001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
Element root = doc.getDocumentElement();
ProcessingInstruction pi1 = doc.createProcessingInstruction("target1", "data1");
ProcessingInstruction pi2 = doc.createProcessingInstruction("target2", "data2");
root.appendChild(pi1);
root.appendChild(pi2);
if (!config.canSetParameter("canonical-form", Boolean.TRUE)) {
System.out.println("OK, setting 'canonical-form' to true is not supported");
return;
}
config.setParameter("canonical-form", Boolean.TRUE);
setHandler(doc);
doc.normalizeDocument();
Node child1 = root.getFirstChild();
Node child2 = child1.getNextSibling();
if (child2.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
Assert.fail("the second child is expected to be a" + "single line break, returned: " + child2);
}
// return Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the parameters "namespaces",
* "namespace-declarations", "well-formed", "element-content-whitespace" are
* set to false if possible; the parameters "entities",
* "normalize-characters", "cdata-sections" are set to true if possible, <br>
* <b>name</b>: canonical-form <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the parameters "namespaces",
* "namespace-declarations", "well-formed", "element-content-whitespace" are
* set to true; the parameters "entities", "normalize-characters",
* "cdata-sections" are set to false
*/
@Test
public void testCanonicalForm002() {
Object[][] params = { { "namespaces", Boolean.TRUE }, { "namespace-declarations", Boolean.TRUE }, { "well-formed", Boolean.TRUE },
{ "element-content-whitespace", Boolean.TRUE },
{ "entities", Boolean.FALSE }, { "normalize-characters", Boolean.FALSE }, { "cdata-sections", Boolean.FALSE }, };
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("canonical-form", Boolean.TRUE)) {
System.out.println("OK, setting 'canonical-form' to true is not supported");
return;
}
for (int i = params.length; --i >= 0;) {
Boolean reset = params[i][1].equals(Boolean.TRUE) ? Boolean.FALSE : Boolean.TRUE;
if (config.canSetParameter(params[i][0].toString(), reset)) {
config.setParameter(params[i][0].toString(), reset);
}
}
config.setParameter("canonical-form", Boolean.TRUE);
StringBuffer result = new StringBuffer();
for (int i = params.length; --i >= 0;) {
Object param = config.getParameter(params[i][0].toString());
if (!params[i][1].equals(param)) {
result.append("; the parameter \'" + params[i][0] + "\' is set to " + param + ", expected: " + params[i][1]);
}
}
if (result.length() > 0) {
Assert.fail(result.toString().substring(2));
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the doc's root element contains superfluous
* namespace declarations, <br>
* <b>name</b>: canonical-form <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the superfluous namespace declarations are
* removed
*/
@Test
public void testCanonicalForm003() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
Element root = doc.getDocumentElement();
String XMLNS = "http://www.w3.org/2000/xmlns/";
root.setAttributeNS(XMLNS, "xmlns:extra1", "ExtraNS1");
root.setAttributeNS(XMLNS, "xmlns:extra2", "ExtraNS2");
if (!config.canSetParameter("canonical-form", Boolean.TRUE)) {
System.out.println("OK, setting 'canonical-form' to true is not supported");
return;
}
config.setParameter("canonical-form", Boolean.TRUE);
setHandler(doc);
doc.normalizeDocument();
String xmlns2 = root.getAttributeNS(XMLNS, "extra1");
if (xmlns2 == null || xmlns2.length() != 0) {
Assert.fail("superfluous namespace declarations is not removed: xmlns:extra2 = " + xmlns2);
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: setting the "canonical-form" to true is supported, <br>
* <b>name</b>: canonical-form <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: one of the following parameters is changed:
* "namespaces", "namespace-declarations", "well-formed",
* "element-content-whitespace", "entities", "normalize-characters",
* "cdata-sections" then "canonical-form" becomes false
*/
@Test
public void testCanonicalForm004() {
Object[][] params = { { "namespaces", Boolean.TRUE }, { "namespace-declarations", Boolean.TRUE }, { "well-formed", Boolean.TRUE },
{ "element-content-whitespace", Boolean.TRUE },
{ "entities", Boolean.FALSE }, { "normalize-characters", Boolean.FALSE }, { "cdata-sections", Boolean.FALSE }, };
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("canonical-form", Boolean.TRUE)) {
System.out.println("OK, setting 'canonical-form' to true is not supported");
return;
}
StringBuffer result = new StringBuffer();
for (int i = params.length; --i >= 0;) {
config.setParameter("canonical-form", Boolean.TRUE);
Boolean changedValue = (params[i][1].equals(Boolean.TRUE)) ? Boolean.FALSE : Boolean.TRUE;
if (config.canSetParameter(params[i][0].toString(), changedValue)) {
config.setParameter(params[i][0].toString(), changedValue);
Object param = config.getParameter("canonical-form");
if (!Boolean.FALSE.equals(param)) {
result.append("; setting the parameter '" + params[i][0] + "' to " + changedValue + " does not change 'canonical-form' to false");
}
}
}
if (result.length() > 0) {
Assert.fail(result.toString().substring(2));
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element has one CDATASection followed by
* one Text node, <br>
* <b>name</b>: cdata-sections <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the CDATASection is left intact
*/
@Test
public void testCdataSections001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
String cdataText = "CDATA CDATA CDATA";
String textText = "text text text";
CDATASection cdata = doc.createCDATASection(cdataText);
Text text = doc.createTextNode(textText);
DOMConfiguration config = doc.getDomConfig();
config.setParameter("cdata-sections", Boolean.TRUE);
Element root = doc.getDocumentElement();
root.appendChild(cdata);
root.appendChild(text);
setHandler(doc);
doc.normalizeDocument();
Node returned = root.getFirstChild();
if (returned.getNodeType() != Node.CDATA_SECTION_NODE) {
Assert.fail("reurned: " + returned + ", expected: CDATASection");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element has one CDATASection followed by
* one Text node, <br>
* <b>name</b>: cdata-sections <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: the root element has one Text node with text of
* the CDATASection and the Text node
*/
@Test
public void testCdataSections002() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
String cdataText = "CDATA CDATA CDATA";
String textText = "text text text";
CDATASection cdata = doc.createCDATASection(cdataText);
Text text = doc.createTextNode(textText);
DOMConfiguration config = doc.getDomConfig();
config.setParameter("cdata-sections", Boolean.FALSE);
Element root = doc.getDocumentElement();
root.appendChild(cdata);
root.appendChild(text);
setHandler(doc);
doc.normalizeDocument();
Node returned = root.getFirstChild();
if (returned.getNodeType() != Node.TEXT_NODE) {
Assert.fail("reurned: " + returned + ", expected: TEXT_NODE");
}
String returnedText = returned.getNodeValue();
if (!(cdataText + textText).equals(returnedText)) {
Assert.fail("reurned: " + returnedText + ", expected: \"" + cdataText + textText + "\"");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element has one Text node with not fully
* normalized characters, the 'check-character-normalization' parameter set
* to true, <br>
* <b>name</b>: error-handler <br>
* <b>value</b>: DOMErrorHandler. <br>
* <b>Expected results</b>: LSParser calls the specified error handler
*/
@Test
public void testCheckCharNorm001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
if (lsImpl == null) {
System.out.println("OK, the DOM implementation does not support the LS 3.0");
return;
}
LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = lsParser.getDomConfig();
if (!config.canSetParameter("check-character-normalization", Boolean.TRUE)) {
System.out.println("OK, setting 'check-character-normalization' to true is not supported");
return;
}
config.setParameter("check-character-normalization", Boolean.TRUE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
LSInput lsInput = lsImpl.createLSInput();
lsInput.setStringData("<root>\u0073\u0075\u0063\u0327\u006F\u006E</root>");
Document doc = lsParser.parse(lsInput);
if (null == testHandler.getError()) {
Assert.fail("no error is reported, expected 'check-character-normalization-failure'");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element contains a fully-normalized text, <br>
* <b>name</b>: check-character-normalization <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: LSParser reports no errors
*/
@Test
public void testCheckCharNorm002() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
if (lsImpl == null) {
System.out.println("OK, the DOM implementation does not support the LS 3.0");
return;
}
LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = lsParser.getDomConfig();
if (!config.canSetParameter("check-character-normalization", Boolean.FALSE)) {
Assert.fail("setting 'check-character-normalization' to false is not supported");
}
config.setParameter("check-character-normalization", Boolean.FALSE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
LSInput lsInput = lsImpl.createLSInput();
lsInput.setStringData("<root>fully-normalized</root>");
Document doc = lsParser.parse(lsInput);
if (null != testHandler.getError()) {
Assert.fail("no error is expected, but reported: " + testHandler.getError());
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element has two Comment nodes, <br>
* <b>name</b>: comments <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the Comment nodes belong to the root element
*/
@Test
public void testComments001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
Comment comment1 = doc.createComment("comment1");
Comment comment2 = doc.createComment("comment2");
DOMConfiguration config = doc.getDomConfig();
config.setParameter("comments", Boolean.TRUE);
Element root = doc.getDocumentElement();
root.appendChild(comment1);
root.appendChild(comment2);
setHandler(doc);
doc.normalizeDocument();
if (comment1.getParentNode() != root) {
Assert.fail("comment1 is attached to " + comment1.getParentNode() + ", but expected to be a child of root");
}
if (comment2.getParentNode() != root) {
Assert.fail("comment1 is attached to " + comment2.getParentNode() + ", but expected to be a child of root");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element has two Comment nodes, <br>
* <b>name</b>: comments <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: the root element has no children
*/
@Test
public void testComments002() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
Comment comment1 = doc.createComment("comment1");
Comment comment2 = doc.createComment("comment2");
DOMConfiguration config = doc.getDomConfig();
config.setParameter("comments", Boolean.FALSE);
Element root = doc.getDocumentElement();
root.appendChild(comment1);
root.appendChild(comment2);
doc.normalizeDocument();
if (root.getFirstChild() != null) {
Assert.fail("root has a child " + root.getFirstChild() + ", but expected to has none");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element is declared as int and its value
* has subsequent characters #x9 (tab), #xA (line feed) and #xD (carriage
* return) , #x20 (space), '1', #x20 (space), <br>
* <b>name</b>: datatype-normalization <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: after Document.normalizeDocument() is called the
* content of the root is '1'
*/
@Test
public void testDatatypeNormalization001() {
Document doc = null;
try {
doc = loadDocument(test1_xsd_url, test_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to '" + test1_xsd_url + "' and '"
+ XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively");
return;
}
config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI);
config.setParameter("schema-location", test1_xsd_url);
if (!config.canSetParameter("validate", Boolean.TRUE)) {
System.out.println("OK, setting 'validate' to true is not supported");
return;
}
config.setParameter("validate", Boolean.TRUE);
if (!config.canSetParameter("datatype-normalization", Boolean.TRUE)) {
System.out.println("OK, setting 'datatype-normalization' to true is not supported");
return;
}
config.setParameter("datatype-normalization", Boolean.TRUE);
Element root = doc.getDocumentElement();
while (root.getFirstChild() != null) {
root.removeChild(root.getFirstChild());
}
root.appendChild(doc.createTextNode("\t\r\n 1 "));
setHandler(doc);
doc.normalizeDocument();
Node child = root.getFirstChild();
if (child == null || child.getNodeType() != Node.TEXT_NODE || !"1".equals(child.getNodeValue())) {
Assert.fail("child: " + child + ", expected: text node '1'");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the root element is declared as int and its value
* has subsequent characters #x9 (tab), #xA (line feed) and #xD (carriage
* return) , #x20 (space), '1', #x20 (space), <br>
* <b>name</b>: datatype-normalization <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: after Document.normalizeDocument() is called the
* value is left unchanged
*/
@Test
public void testDatatypeNormalization002() {
Document doc = null;
try {
doc = loadDocument(test1_xsd_url, test_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to '" + test1_xsd_url + "' and '"
+ XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively");
return;
}
config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI);
config.setParameter("schema-location", test1_xsd_url);
if (config.canSetParameter("validate", Boolean.TRUE)) {
config.setParameter("validate", Boolean.TRUE);
}
if (!config.canSetParameter("datatype-normalization", Boolean.FALSE)) {
Assert.fail("datatype-normalization' to false is not supported");
}
config.setParameter("datatype-normalization", Boolean.FALSE);
Element root = doc.getDocumentElement();
while (root.getFirstChild() != null) {
root.removeChild(root.getFirstChild());
}
String value = "\t\r\n 1 ";
root.appendChild(doc.createTextNode(value));
setHandler(doc);
doc.normalizeDocument();
Node child = root.getFirstChild();
if (child == null || child.getNodeType() != Node.TEXT_NODE || !value.equals(child.getNodeValue())) {
Assert.fail("child: " + child + ", expected: '\\t\\r\\n 1 '");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the doc contains one entity and one entity
* reference, <br>
* <b>name</b>: entities <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the entity and the entity reference are left
* unchanged
*/
@Test
public void testEntities001() {
Document doc = null;
try {
doc = loadDocument(null, test1_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("entities", Boolean.TRUE)) {
Assert.fail("setting 'entities' to true is not supported");
}
Element root = doc.getDocumentElement();
root.appendChild(doc.createEntityReference("x"));
config.setParameter("entities", Boolean.TRUE);
setHandler(doc);
doc.normalizeDocument();
Node child = root.getFirstChild();
if (child == null) {
Assert.fail("root has no child");
}
if (child.getNodeType() != Node.ENTITY_REFERENCE_NODE) {
Assert.fail("root's child is " + child + ", expected entity reference &x;");
}
if (doc.getDoctype() == null) {
Assert.fail("no doctype found");
}
if (doc.getDoctype().getEntities() == null) {
Assert.fail("no entitiy found");
}
if (doc.getDoctype().getEntities().getNamedItem("x") == null) {
Assert.fail("no entitiy with name 'x' found");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the doc contains one entity and one entity
* reference, <br>
* <b>name</b>: entities <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: the entity and the entity reference are removed
*/
@Test
public void testEntities002() {
Document doc = null;
try {
doc = loadDocument(null, test1_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("entities", Boolean.FALSE)) {
Assert.fail("setting 'entities' to false is not supported");
}
Element root = doc.getDocumentElement();
root.appendChild(doc.createEntityReference("x"));
// TODO: remove debug
NamedNodeMap entities = doc.getDoctype().getEntities();
Entity entityX = (Entity) entities.getNamedItem("x");
System.err.println();
System.err.println("Entity x: " + entityX.getTextContent());
System.err.println();
config.setParameter("entities", Boolean.FALSE);
setHandler(doc);
doc.normalizeDocument();
Node child = root.getFirstChild();
// TODO: restore test, exclude for now to allow other tests to run
/*
* if (child == null) { fail("root has no child"); } if
* (child.getNodeType() != Node.TEXT_NODE ||
* !"X".equals(child.getNodeValue())) { fail("root's child is " + child
* + ", expected text node with value 'X'"); }
*
* if (doc.getDoctype() == null) { fail("no doctype found"); }
*
* if (doc.getDoctype().getEntities() != null &&
* doc.getDoctype().getEntities().getNamedItem("x") != null) {
* fail("entity with name 'x' is found, expected to be removed"); }
*/
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the 'infoset' parameter is set to true, <br>
* <b>name</b>: infoset <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: the parameters "validate-if-schema", "entities",
* "datatype-normalization", "cdata-sections", "namespace-declarations",
* "well-formed", "element-content-whitespace", "comments", "namespaces" are
* left unchanged
*/
@Test
public void testInfoset001() {
Object[][] params = { { "validate-if-schema", Boolean.FALSE }, { "entities", Boolean.FALSE }, { "datatype-normalization", Boolean.FALSE },
{ "cdata-sections", Boolean.FALSE },
{ "namespace-declarations", Boolean.TRUE }, { "well-formed", Boolean.TRUE }, { "element-content-whitespace", Boolean.TRUE },
{ "comments", Boolean.TRUE }, { "namespaces", Boolean.TRUE }, };
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("infoset", Boolean.TRUE)) {
Assert.fail("setting 'infoset' to true is not supported");
}
for (int i = params.length; --i >= 0;) {
Boolean reset = params[i][1].equals(Boolean.TRUE) ? Boolean.FALSE : Boolean.TRUE;
if (config.canSetParameter(params[i][0].toString(), reset)) {
config.setParameter(params[i][0].toString(), reset);
}
}
config.setParameter("infoset", Boolean.TRUE);
config.setParameter("infoset", Boolean.FALSE); // has no effect
StringBuffer result = new StringBuffer();
for (int i = params.length; --i >= 0;) {
Object param = config.getParameter(params[i][0].toString());
if (!params[i][1].equals(param)) {
result.append("; the parameter \'" + params[i][0] + "\' is set to " + param + ", expected: " + params[i][1]);
}
}
if (result.length() > 0) {
Assert.fail(result.toString().substring(2));
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: A document with one root element created. The
* prefix 'ns' is bound to 'namespaceURI'. The 'namespaces' parameter is set
* to true, <br>
* <b>name</b>: namespace-declarations <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: Attribute xmlns:ns="namespaceURI" is not added
* to the root element
*/
@Test
public void testNamespaces001() {
DOMImplementation domImpl = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
domImpl = dbf.newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
setHandler(doc);
Element root = doc.getDocumentElement();
DOMConfiguration config = doc.getDomConfig();
StringBuffer result = new StringBuffer();
if (config.canSetParameter("namespaces", Boolean.FALSE)) {
config.setParameter("namespaces", Boolean.FALSE);
// namespaces = false
// namespace-declarations = true (default)
doc.normalizeDocument();
String xmlnsNS = root.getAttributeNS(XMLNS, "ns");
if (xmlnsNS.length() > 0) {
result.append("; the 'namespaces' parameter is set to false but" + "Namespace normalization is performed, attribute" + " xmlns:ns=\"" + xmlnsNS
+ "\" is added");
}
}
doc = domImpl.createDocument("namespaceURI", "ns:root", null);
root = doc.getDocumentElement();
config = doc.getDomConfig();
if (!config.canSetParameter("namespaces", Boolean.TRUE)) {
result.append("; setting 'namespaces' to true is not supported");
} else {
config.setParameter("namespaces", Boolean.TRUE);
if (!config.canSetParameter("namespace-declarations", Boolean.FALSE)) {
result.append("; setting 'namespace-declarations' to false is not supported");
} else {
config.setParameter("namespace-declarations", Boolean.FALSE);
// namespaces = true
// namespace-declarations = false
doc.normalizeDocument();
String xmlnsNS = root.getAttributeNS(XMLNS, "ns");
if (xmlnsNS.length() > 0) {
result.append("; namespaces = true, namespace-declarations = false, but" + " xmlns:ns=\"" + xmlnsNS + "\"");
}
}
doc = domImpl.createDocument("namespaceURI", "ns:root", null);
setHandler(doc);
root = doc.getDocumentElement();
config = doc.getDomConfig();
config.setParameter("namespaces", Boolean.TRUE);
if (!config.canSetParameter("namespace-declarations", Boolean.TRUE)) {
result.append("; setting 'namespace-declarations' to true is not supported");
} else {
config.setParameter("namespace-declarations", Boolean.TRUE);
// namespaces = true
// namespace-declarations = true
doc.normalizeDocument();
String xmlnsNS = root.getAttributeNS(XMLNS, "ns");
if (!"namespaceURI".equals(xmlnsNS)) {
result.append("; namespaces = true, namespace-declarations = true, but" + " xmlns:ns=\"" + xmlnsNS + "\"");
}
}
}
if (result.length() > 0) {
Assert.fail(result.toString().substring(2));
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: an attribute value is not fully normalized, <br>
* <b>name</b>: normalize-characters <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: Node.normalize() leaves the value unchanged
*/
@Test
public void testNormalizeCharacters001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument(null, null, null);
Attr attr = doc.createAttribute("attr");
String notNormalized = " \u0073\u0075\u0063\u0327\u006F\u006E ";
attr.setValue(notNormalized);
DOMConfiguration config = doc.getDomConfig();
StringBuffer result = new StringBuffer();
if (!config.canSetParameter("normalize-characters", Boolean.FALSE)) {
result.append("; setting 'normalize-characters' to false is not supported");
} else {
config.setParameter("normalize-characters", Boolean.FALSE);
attr.normalize();
String value = attr.getValue();
if (!notNormalized.equals(value)) {
result.append("; the value is normalized to '" + value + "', expected to stay unchanged");
}
}
if (config.canSetParameter("normalize-characters", Boolean.TRUE)) {
config.setParameter("normalize-characters", Boolean.TRUE);
attr.setValue(notNormalized);
attr.normalize();
String value = attr.getValue();
if (notNormalized.equals(value)) {
result.append("; the value is not normalized: '" + value + "', expected: '\u0073\u0075\u00E7\u006F\u006E'");
}
}
if (result.length() > 0) {
Assert.fail(result.toString().substring(2));
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: The root element has invalid content. The
* 'validate' parameter is set to true. The 'schema-location' parameter is
* set to 'DOMConfigurationTest.xsd'., <br>
* <b>name</b>: schema-type <br>
* <b>value</b>: http://www.w3.org/2001/XMLSchema. <br>
* <b>Expected results</b>: An error is reported
*/
@Test
public void testValidate001() {
DOMImplementation domImpl = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
domImpl = dbf.newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("test", "ns:root", null);
Element root = doc.getDocumentElement();
root.appendChild(doc.createTextNode("xxx")); // invalid value
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to '" + test1_xsd_url + "' and '"
+ XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively");
return;
}
config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI);
config.setParameter("schema-location", test1_xsd_url);
String resultOK = "OK";
StringBuffer result = new StringBuffer();
if (!config.canSetParameter("validate", Boolean.TRUE)) {
resultOK = "OK, setting the parameter 'validate' to true is not supported";
} else {
config.setParameter("validate", Boolean.TRUE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() == null && null == testHandler.getFatalError()) {
result.append("; no error was reported when the 'validate' is set to true");
}
}
if (!config.canSetParameter("validate", Boolean.FALSE)) {
result.append("; cannot set the parameters 'validate' to false");
} else {
config.setParameter("validate", Boolean.FALSE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() != null || null != testHandler.getFatalError()) {
result.append("; unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError());
}
}
if (result.length() > 0) {
Assert.fail(result.toString().substring(2));
}
return; // Status.passed(resultOK);
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: The root contains a CDATASection with the
* termination marker ']]>', <br>
* <b>name</b>: split-cdata-sections <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: A warning is reported when the section is
* splitted
*/
@Test
public void testSplitCDATA001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
CDATASection cdata = doc.createCDATASection("text]" + "]>text");
doc.getDocumentElement().appendChild(cdata);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
if (!config.canSetParameter("split-cdata-sections", Boolean.TRUE)) {
Assert.fail("cannot set the parameters 'split-cdata-sections' to true");
}
config.setParameter("split-cdata-sections", Boolean.TRUE);
doc.normalizeDocument();
if (null == testHandler.getWarning()) {
Assert.fail("no warning is reported");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: The root contains a CDATASection with the
* termination marker ']]>', <br>
* <b>name</b>: split-cdata-sections <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: No warning is reported
*/
@Test
public void testSplitCDATA002() {
DOMImplementation domImpl = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
domImpl = dbf.newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
CDATASection cdata = doc.createCDATASection("text]" + "]>text");
doc.getDocumentElement().appendChild(cdata);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
if (!config.canSetParameter("split-cdata-sections", Boolean.FALSE)) {
Assert.fail("cannot set the parameters 'split-cdata-sections' to false");
}
config.setParameter("split-cdata-sections", Boolean.FALSE);
doc.normalizeDocument();
if (null == testHandler.getError()) {
Assert.fail("no error is reported");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: The root element has invalid content. The schema
* is specified by setting the 'schema-location' and the 'schema-type'
* parameters., <br>
* <b>name</b>: validate-if-schema <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: No error is reported
*/
@Test
public void testValidateIfSchema001() {
DOMImplementation domImpl = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
domImpl = dbf.newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("test", "ns:root", null);
Element root = doc.getDocumentElement();
root.appendChild(doc.createTextNode("xxx")); // invalid value
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to 'DOMConfigurationTest.xsd' and '"
+ XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively");
return;
}
config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI);
config.setParameter("schema-location", test1_xsd_url);
String resultOK = "OK";
StringBuffer result = new StringBuffer();
if (!config.canSetParameter("validate-if-schema", Boolean.FALSE)) {
result.append("; cannot set the parameters 'validate-if-schema' to false");
} else {
config.setParameter("validate-if-schema", Boolean.FALSE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() != null || null != testHandler.getFatalError()) {
result.append("; unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError());
}
}
if (!config.canSetParameter("validate-if-schema", Boolean.TRUE)) {
resultOK = "OK, setting the parameter 'validate-if-schema' to true is not supported";
} else {
config.setParameter("validate-if-schema", Boolean.TRUE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() == null && null == testHandler.getFatalError()) {
result.append("; no error was reported when the 'validate-if-schema' is set to true");
}
}
if (result.length() > 0) {
Assert.fail(result.toString().substring(2));
}
return; // Status.passed(resultOK);
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: The root element is not declared in the schema
* specified by setting the 'schema-location' and the 'schema-type'
* parameters., <br>
* <b>name</b>: validate-if-schema <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: No error is reported
*/
@Test
public void testValidateIfSchema002() {
DOMImplementation domImpl = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
domImpl = dbf.newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("test", "ns:undeclared_root", null);
Element root = doc.getDocumentElement();
root.appendChild(doc.createTextNode("xxx"));
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to 'DOMConfigurationTest.xsd' and '"
+ XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively");
return;
}
config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI);
config.setParameter("schema-location", test1_xsd_url);
if (!config.canSetParameter("validate-if-schema", Boolean.TRUE)) {
System.out.println("OK, setting the parameter 'validate-if-schema'" + " to true is not supported");
return;
}
config.setParameter("validate-if-schema", Boolean.TRUE);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() != null || null != testHandler.getFatalError()) {
Assert.fail("unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError());
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the attribute has EntityReference to '<', <br>
* <b>name</b>: well-formed <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: An error is reported
*/
@Test
public void testWellFormed001() {
Document doc = null;
try {
doc = loadDocument(null, test2_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("well-formed", Boolean.TRUE)) {
Assert.fail("setting 'well-formed' to true is not supported");
}
config.setParameter("well-formed", Boolean.TRUE);
Element root = doc.getDocumentElement();
Attr attr = doc.createAttributeNS(null, "attr");
try {
attr.appendChild(doc.createEntityReference("<"));
} catch (DOMException domException) {
System.out.println("testWellFormed001: Expected DOMException for Attribute value = '<'" + domException.toString());
return; // OK
}
root.setAttributeNode(attr);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() == null && null == testHandler.getFatalError()) {
Assert.fail("no error was reported when attribute has <");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the attribute has EntityReference to '<', <br>
* <b>name</b>: well-formed <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: No error is reported
*/
@Test
public void testWellFormed002() {
Document doc = null;
try {
doc = loadDocument(null, test2_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("well-formed", Boolean.FALSE)) {
System.out.println("OK, setting 'well-formed' to false is not supported");
return;
}
config.setParameter("well-formed", Boolean.FALSE);
Element root = doc.getDocumentElement();
Attr attr = doc.createAttributeNS(null, "attr");
attr.appendChild(doc.createEntityReference("x"));
root.setAttributeNode(attr);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
doc.normalizeDocument();
if (testHandler.getError() != null || null != testHandler.getFatalError()) {
Assert.fail("unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError());
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the document root element has a text node with
* four white space characters, <br>
* <b>name</b>: element-content-whitespace <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the text node is preserved
*/
@Test
public void testECWhitespace001() {
Document doc = null;
try {
doc = loadDocument(null, test3_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Element root = doc.getDocumentElement();
Text text = doc.createTextNode("\t\n\r ");
root.appendChild(text);
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("element-content-whitespace", Boolean.TRUE)) {
Assert.fail("setting 'element-content-whitespace' to true is not supported");
}
config.setParameter("element-content-whitespace", Boolean.TRUE);
if (!config.canSetParameter("validate", Boolean.TRUE)) {
System.out.println("OK, setting 'validate' to true is not supported");
return;
}
config.setParameter("validate", Boolean.TRUE);
setHandler(doc);
doc.normalizeDocument();
Node firstChild = root.getFirstChild();
if (firstChild == null || firstChild.getNodeType() != Node.TEXT_NODE || !((Text) firstChild).isElementContentWhitespace()) {
Assert.fail("the first child is " + firstChild + ", expected a text node with the four whitespace characters");
}
return; // Status.passed("OK");
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: the document root element has a text node with
* four white space characters, <br>
* <b>name</b>: element-content-whitespace <br>
* <b>value</b>: false. <br>
* <b>Expected results</b>: the text node is discarded
*/
@Test
public void testECWhitespace002() {
Document doc = null;
try {
doc = loadDocument(null, test3_xml);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Element root = doc.getDocumentElement();
Text text = doc.createTextNode("\t\n\r ");
root.appendChild(text);
DOMConfiguration config = doc.getDomConfig();
if (!config.canSetParameter("element-content-whitespace", Boolean.FALSE)) {
System.out.println("OK, setting 'element-content-whitespace' to false is not supported");
return;
}
config.setParameter("element-content-whitespace", Boolean.FALSE);
if (!config.canSetParameter("validate", Boolean.TRUE)) {
System.out.println("OK, setting 'validate' to true is not supported");
return;
}
config.setParameter("validate", Boolean.TRUE);
setHandler(doc);
doc.normalizeDocument();
Node firstChild = root.getFirstChild();
if (firstChild != null) {
Assert.fail("the first child is " + firstChild + ", but no child is expected");
}
return; // Status.passed("OK");
}
}
|