1package tests.org.w3c.dom; 2 3import org.w3c.dom.Document; 4import org.w3c.dom.Element; 5import org.w3c.dom.Node; 6import org.w3c.dom.Attr; 7import org.w3c.dom.NodeList; 8import org.w3c.dom.DOMException; 9import org.w3c.dom.DocumentType; 10import org.w3c.dom.DOMImplementation; 11import org.w3c.dom.DocumentFragment; 12import org.w3c.dom.ProcessingInstruction; 13 14import javax.xml.parsers.DocumentBuilder; 15 16/** 17 * The importNode method imports a node from another document to this document. 18 * The returned node has no parent; (parentNode is null). The source node is not 19 * altered or removed from the original document but a new copy of the source 20 * node is created. 21 * 22 * Using the method importNode with deep=true, import the attribute, "street" of 23 * the second element node, from a list of nodes whose local names are "address" 24 * and namespaceURI "http://www.nist.gov" into the same document. Check the 25 * parentNode, nodeName, nodeType and nodeValue of the imported node to verify 26 * if it has been imported correctly. 27 * 28 * @author IBM 29 * @author Neil Delima 30 * @see <a 31 * href="http://www.w3.org/TR/DOM-Level-2-Core/core">http://www.w3.org/TR/DOM-Level-2-Core/core</a> 32 * @see <a 33 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#Core-Document-importNode">http://www.w3.org/TR/DOM-Level-2-Core/core#Core-Document-importNode</a> 34 */ 35public final class DocumentImportNode extends DOMTestCase { 36 37 DOMDocumentBuilderFactory factory; 38 39 DocumentBuilder builder; 40 41 protected void setUp() throws Exception { 42 super.setUp(); 43 try { 44 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 45 .getConfiguration2()); 46 builder = factory.getBuilder(); 47 } catch (Exception e) { 48 fail("Unexpected exception" + e.getMessage()); 49 } 50 } 51 52 protected void tearDown() throws Exception { 53 factory = null; 54 builder = null; 55 super.tearDown(); 56 } 57 58 /** 59 * Runs the test case. 60 * 61 * @throws Throwable 62 * Any uncaught exception causes test to fail 63 */ 64// Assumes validation. 65// public void testImportNode1() throws Throwable { 66// Document doc; 67// Element element; 68// Attr attr; 69// NodeList childList; 70// Node importedAttr; 71// String nodeName; 72// int nodeType; 73// String nodeValue; 74// doc = (Document) load("staffNS", builder); 75// childList = doc 76// .getElementsByTagNameNS("http://www.nist.gov", "address"); 77// element = (Element) childList.item(1); 78// attr = element.getAttributeNode("street"); 79// importedAttr = doc.importNode(attr, false); 80// nodeName = importedAttr.getNodeName(); 81// nodeValue = importedAttr.getNodeValue(); 82// nodeType = (int) importedAttr.getNodeType(); 83// assertEquals("documentimportnode01_nodeName", "street", nodeName); 84// assertEquals("documentimportnode01_nodeType", 2, nodeType); 85// assertEquals("documentimportnode01_nodeValue", "Yes", nodeValue); 86// } 87 public void testImportNode2() throws Throwable { 88 Document doc; 89 Document docImported; 90 Element element; 91 Attr attr; 92 Node importedAttr; 93 String nodeName; 94 int nodeType; 95 String nodeValue; 96 NodeList addresses; 97 Node attrsParent; 98 doc = (Document) load("staffNS", builder); 99 docImported = (Document) load("staff", builder); 100 addresses = doc 101 .getElementsByTagNameNS("http://www.nist.gov", "address"); 102 element = (Element) addresses.item(1); 103 attr = element.getAttributeNodeNS("http://www.nist.gov", "zone"); 104 importedAttr = docImported.importNode(attr, false); 105 nodeName = importedAttr.getNodeName(); 106 nodeType = (int) importedAttr.getNodeType(); 107 nodeValue = importedAttr.getNodeValue(); 108 attrsParent = importedAttr.getParentNode(); 109 assertNull("documentimportnode02_parentNull", attrsParent); 110 assertEquals("documentimportnode02_nodeName", "emp:zone", nodeName); 111 assertEquals("documentimportnode02_nodeType", 2, nodeType); 112 assertEquals("documentimportnode02_nodeValue", "CANADA", nodeValue); 113 } 114 115// Assumes validation. 116// public void testImportNode3() throws Throwable { 117// Document doc; 118// Element element; 119// Attr attr; 120// NodeList childList; 121// Node importedAttr; 122// String nodeName; 123// int nodeType; 124// String nodeValue; 125// doc = (Document) load("staffNS", builder); 126// childList = doc.getElementsByTagNameNS("http://www.nist.gov", 127// "employee"); 128// element = (Element) childList.item(1); 129// attr = element.getAttributeNode("defaultAttr"); 130// importedAttr = doc.importNode(attr, false); 131// nodeName = importedAttr.getNodeName(); 132// nodeValue = importedAttr.getNodeValue(); 133// nodeType = (int) importedAttr.getNodeType(); 134// assertEquals("documentimportnode03_nodeName", "defaultAttr", nodeName); 135// assertEquals("documentimportnode03_nodeType", 2, nodeType); 136// assertEquals("documentimportnode03_nodeValue", "defaultVal", nodeValue); 137// } 138 139// Assumes validation. 140// public void testImportNode4() throws Throwable { 141// Document doc; 142// Document newDoc; 143// DocumentType docType = null; 144// 145// DOMImplementation domImpl; 146// Element element; 147// Attr attr; 148// NodeList childList; 149// Node importedAttr; 150// String nodeName; 151// int nodeType; 152// String nodeValue; 153// doc = (Document) load("staffNS", builder); 154// domImpl = doc.getImplementation(); 155// newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test", 156// "l2:root", docType); 157// childList = doc.getElementsByTagNameNS("http://www.nist.gov", 158// "employee"); 159// element = (Element) childList.item(1); 160// attr = element.getAttributeNode("defaultAttr"); 161// importedAttr = newDoc.importNode(attr, true); 162// nodeName = importedAttr.getNodeName(); 163// nodeValue = importedAttr.getNodeValue(); 164// nodeType = (int) importedAttr.getNodeType(); 165// assertEquals("documentimportnode04_nodeName", "defaultAttr", nodeName); 166// assertEquals("documentimportnode04_nodeType", 2, nodeType); 167// assertEquals("documentimportnode04_nodeValue", "defaultVal", nodeValue); 168// } 169 public void testImportNode5() throws Throwable { 170 Document doc; 171 Document docImported; 172 Attr attr; 173 Node importedAttr; 174 String nodeName; 175 int nodeType; 176 String nodeValue; 177 String namespaceURI; 178 doc = (Document) load("staffNS", builder); 179 docImported = (Document) load("staff", builder); 180 attr = doc.createAttributeNS("http://www.w3.org/DOM/Test", "a_:b0"); 181 importedAttr = docImported.importNode(attr, false); 182 nodeName = importedAttr.getNodeName(); 183 nodeValue = importedAttr.getNodeValue(); 184 nodeType = (int) importedAttr.getNodeType(); 185 namespaceURI = importedAttr.getNamespaceURI(); 186 assertEquals("documentimportnode05_nodeName", "a_:b0", nodeName); 187 assertEquals("documentimportnode05_nodeType", 2, nodeType); 188 assertEquals("documentimportnode05_nodeValue", "", nodeValue); 189 assertEquals("documentimportnode05_namespaceURI", 190 "http://www.w3.org/DOM/Test", namespaceURI); 191 } 192 public void testImportNode6() throws Throwable { 193 Document doc; 194 195 doc = (Document) load("staffNS", builder); 196 197 { 198 boolean success = false; 199 try { 200 doc.importNode(doc, false); 201 } catch (DOMException ex) { 202 success = (ex.code == DOMException.NOT_SUPPORTED_ERR); 203 } 204 assertTrue("throw_NOT_SUPPORTED_ERR", success); 205 } 206 } 207 public void testImportNode7() throws Throwable { 208 Document doc; 209 210 DocumentType docType; 211 doc = (Document) load("staffNS", builder); 212 docType = doc.getDoctype(); 213 214 { 215 boolean success = false; 216 try { 217 doc.importNode(docType, true); 218 } catch (DOMException ex) { 219 success = (ex.code == DOMException.NOT_SUPPORTED_ERR); 220 } 221 assertTrue("throw_NOT_SUPPORTED_ERR", success); 222 } 223 } 224 public void testImportNode8() throws Throwable { 225 Document doc; 226 227 DocumentType docType; 228 DOMImplementation domImpl; 229 String nullNS = null; 230 231 doc = (Document) load("staffNS", builder); 232 domImpl = doc.getImplementation(); 233 docType = domImpl.createDocumentType("test:root", nullNS, nullNS); 234 235 { 236 boolean success = false; 237 try { 238 doc.importNode(docType, true); 239 } catch (DOMException ex) { 240 success = (ex.code == DOMException.NOT_SUPPORTED_ERR); 241 } 242 assertTrue("throw_NOT_SUPPORTED_ERR", success); 243 } 244 } 245 public void testImportNode9() throws Throwable { 246 Document doc; 247 DocumentFragment docFragment; 248 NodeList childList; 249 boolean success; 250 Node addressNode; 251 252 Node importedDocFrag; 253 doc = (Document) load("staffNS", builder); 254 docFragment = doc.createDocumentFragment(); 255 childList = doc.getElementsByTagNameNS("*", "address"); 256 addressNode = childList.item(0); 257 docFragment.appendChild(addressNode); 258 importedDocFrag = doc.importNode(docFragment, false); 259 success = importedDocFrag.hasChildNodes(); 260 assertFalse("documentimportnode09", success); 261 } 262 public void testImportNode10() throws Throwable { 263 Document doc; 264 DocumentFragment docFragment; 265 NodeList childList; 266 boolean success; 267 Node addressNode; 268 269 Node importedDocFrag; 270 doc = (Document) load("staffNS", builder); 271 docFragment = doc.createDocumentFragment(); 272 childList = doc.getElementsByTagNameNS("*", "address"); 273 addressNode = childList.item(0); 274 docFragment.appendChild(addressNode); 275 importedDocFrag = doc.importNode(docFragment, true); 276 success = importedDocFrag.hasChildNodes(); 277 assertTrue("documentimportnode10", success); 278 } 279 public void testImportNode11() throws Throwable { 280 Document doc; 281 Element docElement; 282 Node imported; 283 boolean success; 284 String nodeNameOrig; 285 String nodeNameImported; 286 doc = (Document) load("staffNS", builder); 287 docElement = doc.getDocumentElement(); 288 imported = doc.importNode(docElement, false); 289 success = imported.hasChildNodes(); 290 assertFalse("documentimportnode11", success); 291 nodeNameImported = imported.getNodeName(); 292 nodeNameOrig = docElement.getNodeName(); 293 assertEquals("documentimportnode11_NodeName", nodeNameImported, 294 nodeNameOrig); 295 } 296 public void testImportNode12() throws Throwable { 297 Document doc; 298 NodeList childList; 299 Node imported; 300 Node addressElem; 301 NodeList addressElemChildren; 302 NodeList importedChildren; 303 int addressElemLen; 304 int importedLen; 305 doc = (Document) load("staffNS", builder); 306 childList = doc.getElementsByTagNameNS("*", "address"); 307 addressElem = childList.item(0); 308 imported = doc.importNode(addressElem, true); 309 addressElemChildren = addressElem.getChildNodes(); 310 importedChildren = imported.getChildNodes(); 311 addressElemLen = (int) addressElemChildren.getLength(); 312 importedLen = (int) importedChildren.getLength(); 313 assertEquals("documentimportnode12", importedLen, addressElemLen); 314 } 315 public void testImportNode13() throws Throwable { 316 Document doc; 317 NodeList childList; 318 Node imported; 319 NodeList importedList; 320 Node employeeElem; 321 int importedLen; 322 doc = (Document) load("staffNS", builder); 323 childList = doc.getElementsByTagNameNS("*", "employee"); 324 employeeElem = childList.item(0); 325 imported = doc.importNode(employeeElem, false); 326 importedList = imported.getChildNodes(); 327 importedLen = (int) importedList.getLength(); 328 assertEquals("documentimportnode13", 0, importedLen); 329 } 330 331// Assumes validation. 332// public void testImportNode14() throws Throwable { 333// Document doc; 334// Document newDoc; 335// DOMImplementation domImpl; 336// DocumentType nullDocType = null; 337// 338// NodeList childList; 339// Node imported; 340// Node employeeElem; 341// Attr attrNode; 342// String attrValue; 343// String nullNS = null; 344// 345// doc = (Document) load("staffNS", builder); 346// childList = doc.getElementsByTagNameNS("*", "employee"); 347// employeeElem = childList.item(3); 348// domImpl = builder.getDOMImplementation(); 349// newDoc = domImpl.createDocument(nullNS, "staff", nullDocType); 350// imported = newDoc.importNode(employeeElem, true); 351// attrNode = ((Element) /* Node */imported).getAttributeNodeNS(nullNS, 352// "defaultAttr"); 353// assertNull("defaultAttrNotImported", attrNode); 354// attrValue = ((Element) /* Node */imported).getAttributeNS( 355// "http://www.w3.org/2000/xmlns/", "emp"); 356// assertEquals("explicitAttrImported", "http://www.nist.gov", attrValue); 357// } 358 public void testImportNode15() throws Throwable { 359 Document doc; 360 361 Node textImport; 362 Node textToImport; 363 String nodeValue; 364 doc = (Document) load("staffNS", builder); 365 366 textToImport = doc 367 .createTextNode("Document.importNode test for a TEXT_NODE"); 368 textImport = doc.importNode(textToImport, true); 369 nodeValue = textImport.getNodeValue(); 370 assertEquals("documentimportnode15", 371 "Document.importNode test for a TEXT_NODE", nodeValue); 372 } 373 public void testImportNode17() throws Throwable { 374 Document doc; 375 376 Node commentImport; 377 Node commentToImport; 378 String nodeValue; 379 doc = (Document) load("staffNS", builder); 380 381 commentToImport = doc 382 .createComment("Document.importNode test for a COMMENT_NODE"); 383 commentImport = doc.importNode(commentToImport, true); 384 nodeValue = commentImport.getNodeValue(); 385 assertEquals("documentimportnode17", 386 "Document.importNode test for a COMMENT_NODE", nodeValue); 387 } 388 public void testImportNode18() throws Throwable { 389 Document doc; 390 391 ProcessingInstruction piImport; 392 ProcessingInstruction piToImport; 393 String piData; 394 String piTarget; 395 doc = (Document) load("staffNS", builder); 396 397 piToImport = doc.createProcessingInstruction("Target", "Data"); 398 piImport = (ProcessingInstruction) doc.importNode(piToImport, false); 399 piTarget = piImport.getTarget(); 400 piData = piImport.getData(); 401 assertEquals("documentimportnode18_Target", "Target", piTarget); 402 assertEquals("documentimportnode18_Data", "Data", piData); 403 } 404 405// Assumes validation. 406// public void testImportNode19() throws Throwable { 407// Document doc; 408// DocumentType docTypeNull = null; 409// 410// Document docImp; 411// DOMImplementation domImpl; 412// DocumentType docType; 413// NamedNodeMap nodeMap; 414// Entity entity2; 415// Entity entity6; 416// Entity entityImp2; 417// Entity entityImp6; 418// String nodeName; 419// String systemId; 420// String notationName; 421// String nodeNameImp; 422// String systemIdImp; 423// String notationNameImp; 424// doc = (Document) load("staffNS", builder); 425// domImpl = doc.getImplementation(); 426// docType = doc.getDoctype(); 427// docImp = domImpl.createDocument("http://www.w3.org/DOM/Test", "a:b", 428// docTypeNull); 429// nodeMap = docType.getEntities(); 430// assertNotNull("entitiesNotNull", nodeMap); 431// entity2 = (Entity) nodeMap.getNamedItem("ent2"); 432// entity6 = (Entity) nodeMap.getNamedItem("ent6"); 433// entityImp2 = (Entity) docImp.importNode(entity2, false); 434// entityImp6 = (Entity) docImp.importNode(entity6, true); 435// nodeName = entity2.getNodeName(); 436// nodeNameImp = entityImp2.getNodeName(); 437// assertEquals("documentimportnode19_Ent2NodeName", nodeName, nodeNameImp); 438// nodeName = entity6.getNodeName(); 439// nodeNameImp = entityImp6.getNodeName(); 440// assertEquals("documentimportnode19_Ent6NodeName", nodeName, nodeNameImp); 441// systemId = entity2.getSystemId(); 442// systemIdImp = entityImp2.getSystemId(); 443// assertEquals("documentimportnode19_Ent2SystemId", systemId, systemIdImp); 444// systemId = entity6.getSystemId(); 445// systemIdImp = entityImp6.getSystemId(); 446// assertEquals("documentimportnode19_Ent6SystemId", systemId, systemIdImp); 447// notationName = entity2.getNotationName(); 448// notationNameImp = entityImp2.getNotationName(); 449// assertEquals("documentimportnode19_Ent2NotationName", notationName, 450// notationNameImp); 451// notationName = entity6.getNotationName(); 452// notationNameImp = entityImp6.getNotationName(); 453// assertEquals("documentimportnode19_Ent6NotationName", notationName, 454// notationNameImp); 455// } 456 457// Assumes validation. 458// public void testImportNode20() throws Throwable { 459// Document doc; 460// Document docImp; 461// DOMImplementation domImpl; 462// DocumentType docType; 463// DocumentType docTypeNull = null; 464// 465// NamedNodeMap nodeMap; 466// Entity entity4; 467// Entity entityImp4; 468// Element element; 469// CharacterData cdata; 470// ProcessingInstruction pi; 471// NodeList childList; 472// NodeList elemchildList; 473// String ent4Name; 474// String ent4ImpName; 475// String cdataVal; 476// String piTargetVal; 477// String piDataVal; 478// doc = (Document) load("staffNS", builder); 479// domImpl = doc.getImplementation(); 480// docType = doc.getDoctype(); 481// docImp = domImpl.createDocument("http://www.w3.org/DOM/Test", "a:b", 482// docTypeNull); 483// nodeMap = docType.getEntities(); 484// entity4 = (Entity) nodeMap.getNamedItem("ent4"); 485// entityImp4 = (Entity) docImp.importNode(entity4, true); 486// childList = entityImp4.getChildNodes(); 487// element = (Element) childList.item(0); 488// elemchildList = element.getChildNodes(); 489// cdata = (CharacterData) elemchildList.item(0); 490// pi = (ProcessingInstruction) childList.item(1); 491// ent4Name = entity4.getNodeName(); 492// ent4ImpName = entityImp4.getNodeName(); 493// cdataVal = cdata.getData(); 494// piTargetVal = pi.getTarget(); 495// piDataVal = pi.getData(); 496// assertEquals("documentimportnode20_Ent4NodeName", ent4Name, ent4ImpName); 497// assertEquals("documentimportnode20_Cdata", "Element data", cdataVal); 498// assertEquals("documentimportnode20_PITarget", "PItarget", piTargetVal); 499// assertEquals("documentimportnode20_PIData", "PIdata", piDataVal); 500// } 501 502// TODO Fails on JDK. Why? 503// public void testImportNode21() throws Throwable { 504// 505// 506// Document doc; 507// DocumentType docTypeNull = null; 508// 509// Document docImp; 510// DOMImplementation domImpl; 511// NodeList addressList; 512// NodeList addressChildList; 513// Element element; 514// EntityReference entRef2; 515// EntityReference entRefImp2; 516// EntityReference entRef3; 517// EntityReference entRefImp3; 518// String nodeName2; 519// String nodeName3; 520// String nodeNameImp2; 521// String nodeNameImp3; 522// NodeList nodes; 523// Node nodeImp3; 524// Node nodeImp2; 525// String nodeValueImp2; 526// String nodeValueImp3; 527// doc = (Document) load("staffNS", builder); 528// domImpl = doc.getImplementation(); 529// docImp = domImpl.createDocument("http://www.w3.org/DOM/Test", "a:b", 530// docTypeNull); 531// addressList = doc.getElementsByTagName("address"); 532// element = (Element) addressList.item(1); 533// addressChildList = element.getChildNodes(); 534// entRef2 = (EntityReference) addressChildList.item(0); 535// entRef3 = (EntityReference) addressChildList.item(2); 536// entRefImp2 = (EntityReference) docImp.importNode(entRef2, true); 537// entRefImp3 = (EntityReference) docImp.importNode(entRef3, false); 538// nodeName2 = entRef2.getNodeName(); 539// nodeName3 = entRef3.getNodeName(); 540// nodeNameImp2 = entRefImp2.getNodeName(); 541// nodeNameImp3 = entRefImp3.getNodeName(); 542// assertEquals("documentimportnode21_Ent2NodeName", nodeName2, 543// nodeNameImp2); 544// assertEquals("documentimportnode21_Ent3NodeName", nodeName3, 545// nodeNameImp3); 546// entRefImp2 = (EntityReference) doc.importNode(entRef2, true); 547// entRefImp3 = (EntityReference) doc.importNode(entRef3, false); 548// nodes = entRefImp2.getChildNodes(); 549// nodeImp2 = nodes.item(0); 550// nodeValueImp2 = nodeImp2.getNodeValue(); 551// nodes = entRefImp3.getChildNodes(); 552// nodeImp3 = nodes.item(0); 553// nodeValueImp3 = nodeImp3.getNodeValue(); 554// assertEquals("documentimportnode21_Ent2NodeValue", "1900 Dallas Road", 555// nodeValueImp2); 556// assertEquals("documentimportnode21_Ent3Nodevalue", "Texas", 557// nodeValueImp3); 558// 559// } 560 561// Assumes validation. 562// public void testImportNode22() throws Throwable { 563// Document doc; 564// DocumentType docTypeNull = null; 565// 566// Document docImp; 567// DOMImplementation domImpl; 568// DocumentType docType; 569// NamedNodeMap nodeMap; 570// Notation notation1; 571// Notation notation2; 572// 573// String publicId1; 574// String publicId1Imp; 575// String publicId1NewImp; 576// String publicId2Imp; 577// 578// String systemId1Imp; 579// String systemId1NewImp; 580// String systemId2; 581// String systemId2Imp; 582// String systemId2NewImp; 583// doc = (Document) load("staffNS", builder); 584// domImpl = doc.getImplementation(); 585// docType = doc.getDoctype(); 586// docImp = domImpl.createDocument("http://www.w3.org/DOM/Test", "a:b", 587// docTypeNull); 588// nodeMap = docType.getNotations(); 589// assertNotNull("notationsNotNull", nodeMap); 590// notation1 = (Notation) nodeMap.getNamedItem("notation1"); 591// notation2 = (Notation) nodeMap.getNamedItem("notation2"); 592// doc.importNode(notation1, true); 593// doc.importNode(notation2, false); 594// docImp.importNode(notation1, false); 595// docImp.importNode(notation2, true); 596// publicId1 = notation1.getPublicId(); 597// publicId1Imp = notation1.getPublicId(); 598// publicId1NewImp = notation1.getPublicId(); 599// systemId1Imp = notation1.getSystemId(); 600// systemId1NewImp = notation1.getSystemId(); 601// publicId2Imp = notation2.getPublicId(); 602// notation2.getPublicId(); 603// systemId2 = notation2.getSystemId(); 604// systemId2Imp = notation2.getSystemId(); 605// systemId2NewImp = notation2.getSystemId(); 606// assertEquals("documentimportnode22_N1PID", publicId1, publicId1Imp); 607// assertEquals("documentimportnode22_N1NPID", publicId1, publicId1NewImp); 608// assertNull("documentimportnode22_N1SID", systemId1Imp); 609// assertNull("documentimportnode22_N1NSID", systemId1NewImp); 610// assertEquals("documentimportnode22_N2SID", systemId2, systemId2Imp); 611// assertEquals("documentimportnode22_N2NSID", systemId2, systemId2NewImp); 612// assertNull("documentimportnode22_N2PID", publicId2Imp); 613// assertNull("documentimportnode22_N2NPID", publicId2Imp); 614// } 615} 616