1 package tests.org.w3c.dom; 2 3 import org.w3c.dom.Attr; 4 import org.w3c.dom.Document; 5 import org.w3c.dom.Element; 6 import org.w3c.dom.Node; 7 import org.w3c.dom.NodeList; 8 import org.w3c.dom.NamedNodeMap; 9 10 import javax.xml.parsers.DocumentBuilder; 11 12 public final class AttrGetOwnerElement extends DOMTestCase { 13 14 DOMDocumentBuilderFactory factory; 15 16 DocumentBuilder builder; 17 setUp()18 protected void setUp() throws Exception { 19 super.setUp(); 20 try { 21 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 22 .getConfiguration2()); 23 builder = factory.getBuilder(); 24 } catch (Exception e) { 25 fail("Unexpected exception " + e.getMessage()); 26 } 27 } 28 tearDown()29 protected void tearDown() throws Exception { 30 factory = null; 31 builder = null; 32 super.tearDown(); 33 } 34 35 // Assumes validation. 36 // public void testGetOwnerElement1() throws Throwable { 37 // Document doc; 38 // Attr attr; 39 // Element element; 40 // Element ownerElement; 41 // String ownerElementName; 42 // NodeList elementList; 43 // NamedNodeMap attributes; 44 // String nullNS = null; 45 // 46 // doc = (Document) load("staffNS", builder); 47 // 48 // elementList = doc.getElementsByTagNameNS("http://www.nist.gov", 49 // "employee"); 50 // element = (Element) elementList.item(1); 51 // attributes = element.getAttributes(); 52 // attr = (Attr) attributes.getNamedItemNS(nullNS, "defaultAttr"); 53 // ownerElement = attr.getOwnerElement(); 54 // ownerElementName = ownerElement.getNodeName(); 55 // assertEquals("attrgetownerelement01", "emp:employee", ownerElementName); 56 // 57 // } testGetOwnerElement2()58 public void testGetOwnerElement2() throws Throwable { 59 Document doc; 60 Element element; 61 Element ownerElement; 62 String ownerElementName; 63 Attr attr; 64 65 doc = (Document) load("staffNS", builder); 66 element = doc.createElement("root"); 67 attr = doc.createAttributeNS("http://www.w3.org/DOM/L1", "L1:att"); 68 element.setAttributeNodeNS(attr); 69 ownerElement = attr.getOwnerElement(); 70 ownerElementName = ownerElement.getNodeName(); 71 assertEquals("attrgetownerelement02", "root", ownerElementName); 72 } testGetOwnerElement3()73 public void testGetOwnerElement3() throws Throwable { 74 Document doc; 75 Node ownerElement; 76 Attr attr; 77 doc = (Document) load("staffNS", builder); 78 attr = doc.createAttributeNS("http://www.w3.org/DOM", "dom:attr"); 79 ownerElement = attr.getOwnerElement(); 80 assertNull("attrgetownerelement03", ownerElement); 81 } testGetOwnerElement4()82 public void testGetOwnerElement4() throws Throwable { 83 Document doc; 84 Document docImp; 85 Node ownerElement; 86 Element element; 87 Attr attr; 88 Attr attrImp; 89 NodeList addresses; 90 91 doc = (Document) load("staffNS", builder); 92 docImp = (Document) load("staff", builder); 93 94 addresses = doc 95 .getElementsByTagNameNS("http://www.nist.gov", "address"); 96 element = (Element) addresses.item(1); 97 assertNotNull("empAddressNotNull", element); 98 attr = element.getAttributeNodeNS("http://www.nist.gov", "zone"); 99 attrImp = (Attr) docImp.importNode(attr, true); 100 ownerElement = attrImp.getOwnerElement(); 101 assertNull("attrgetownerelement04", ownerElement); 102 } testGetOwnerElement5()103 public void testGetOwnerElement5() throws Throwable { 104 Document doc; 105 Node element; 106 Element ownerElement; 107 Element parentElement; 108 NodeList elementList; 109 String ownerElementName; 110 Attr attr; 111 112 NamedNodeMap nodeMap; 113 String nullNS = null; 114 115 doc = (Document) load("staffNS", builder); 116 elementList = doc.getElementsByTagNameNS("*", "address"); 117 element = elementList.item(1); 118 parentElement = (Element) element.getParentNode(); 119 nodeMap = element.getAttributes(); 120 parentElement.removeChild(element); 121 attr = (Attr) nodeMap.getNamedItemNS(nullNS, "street"); 122 ownerElement = attr.getOwnerElement(); 123 ownerElementName = ownerElement.getNodeName(); 124 assertEquals("attrgetownerelement05", "address", ownerElementName); 125 } 126 } 127