1/* 2 This Java source file was generated by test-to-java.xsl 3 and is a derived work from the source document. 4 The source document contained the following notice: 5 6 7 8 Copyright (c) 2001-2004 World Wide Web Consortium, 9 (Massachusetts Institute of Technology, Institut National de 10 Recherche en Informatique et en Automatique, Keio University). All 11 Rights Reserved. This program is distributed under the W3C's Software 12 Intellectual Property License. This program is distributed in the 13 hope that it will be useful, but WITHOUT ANY WARRANTY; without even 14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 15 PURPOSE. 16 17 See W3C License http://www.w3.org/Consortium/Legal/ for more details. 18 19 20 */ 21 22package tests.org.w3c.dom; 23 24import org.w3c.dom.Element; 25import org.w3c.dom.Document; 26import org.w3c.dom.Node; 27import org.w3c.dom.NodeList; 28import org.w3c.dom.Attr; 29import org.w3c.dom.DOMException; 30 31import javax.xml.parsers.DocumentBuilder; 32 33/** 34 * The "setAttributeNode(newAttr)" method raises an "INUSE_ATTRIBUTE_ERR 35 * DOMException if the "newAttr" is already an attribute of another element. 36 * 37 * Retrieve the first emp:address and append a newly created element. The 38 * "createAttributeNS(namespaceURI,qualifiedName)" and 39 * "setAttributeNodeNS(newAttr)" methods are invoked to create and add a new 40 * attribute to the newly created Element. The "setAttributeNodeNS(newAttr)" 41 * method is once again called to add the new attribute causing an exception to 42 * be raised since the attribute is already an attribute of another element. 43 * 44 * @author NIST 45 * @author Mary Brady 46 * @see <a 47 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INUSE_ATTRIBUTE_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INUSE_ATTRIBUTE_ERR'])</a> 48 * @see <a 49 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS</a> 50 * @see <a 51 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-ElSetAtNodeNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INUSE_ATTRIBUTE_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-ElSetAtNodeNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INUSE_ATTRIBUTE_ERR'])</a> 52 */ 53public final class SetAttributeNodeNS extends DOMTestCase { 54 55 DOMDocumentBuilderFactory factory; 56 57 DocumentBuilder builder; 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 try { 62 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 63 .getConfiguration2()); 64 builder = factory.getBuilder(); 65 } catch (Exception e) { 66 fail("Unexpected exception" + e.getMessage()); 67 } 68 } 69 70 protected void tearDown() throws Exception { 71 factory = null; 72 builder = null; 73 super.tearDown(); 74 } 75 76 /** 77 * Runs the test case. 78 * 79 * @throws Throwable 80 * Any uncaught exception causes test to fail 81 */ 82 public void testSetAttributeNode1() throws Throwable { 83 String namespaceURI = "http://www.newattr.com"; 84 String qualifiedName = "emp:newAttr"; 85 Document doc; 86 Element newElement; 87 Attr newAttr; 88 NodeList elementList; 89 Node testAddr; 90 91 doc = (Document) load("staffNS", builder); 92 elementList = doc.getElementsByTagName("emp:address"); 93 testAddr = elementList.item(0); 94 assertNotNull("empAddrNotNull", testAddr); 95 newElement = doc.createElement("newElement"); 96 testAddr.appendChild(newElement); 97 newAttr = doc.createAttributeNS(namespaceURI, qualifiedName); 98 newElement.setAttributeNodeNS(newAttr); 99 100 { 101 boolean success = false; 102 try { 103 ((Element) /* Node */testAddr).setAttributeNodeNS(newAttr); 104 } catch (DOMException ex) { 105 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR); 106 } 107 assertTrue("throw_INUSE_ATTRIBUTE_ERR", success); 108 } 109 } 110 111// Assumes validation. 112// public void testSetAttributeNode2() throws Throwable { 113// Document doc; 114// NodeList genderList; 115// Node gender; 116// NodeList genList; 117// Node gen; 118// NodeList gList; 119// Element genElement; 120// Attr newAttr; 121// 122// doc = (Document) load("staffNS", builder); 123// 124// if (!factory.isExpandEntityReferences()) { 125// genderList = doc.getElementsByTagName("gender"); 126// gender = genderList.item(2); 127// genList = gender.getChildNodes(); 128// gen = genList.item(0); 129// } else { 130// gen = doc.createEntityReference("ent4"); 131// } 132// 133// gList = gen.getChildNodes(); 134// genElement = (Element) gList.item(0); 135// assertNotNull("notnull", genElement); 136// newAttr = doc.createAttributeNS("www.xyz.com", "emp:local1"); 137// 138// { 139// boolean success = false; 140// try { 141// genElement.setAttributeNodeNS(newAttr); 142// } catch (DOMException ex) { 143// success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); 144// } 145// assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR", success); 146// } 147// } 148 public void testSetAttributeNode3() throws Throwable { 149 String namespaceURI = "http://www.newattr.com"; 150 String qualifiedName = "emp:newAttr"; 151 Document doc; 152 NodeList elementList; 153 Node testAddr; 154 Attr newAttr; 155 Attr newAddrAttr; 156 doc = (Document) load("staffNS", builder); 157 elementList = doc.getElementsByTagName("emp:address"); 158 testAddr = elementList.item(0); 159 assertNotNull("empAddrNotNull", testAddr); 160 newAttr = doc.createAttributeNS(namespaceURI, qualifiedName); 161 newAddrAttr = ((Element) /* Node */testAddr) 162 .setAttributeNodeNS(newAttr); 163 assertNull("throw_Null", newAddrAttr); 164 } 165 public void testSetAttributeNode4() throws Throwable { 166 Document doc; 167 NodeList elementList; 168 Node testAddr; 169 Attr newAttr; 170 Attr newAddrAttr; 171 String newName; 172 doc = (Document) load("staffNS", builder); 173 elementList = doc.getElementsByTagName("emp:address"); 174 testAddr = elementList.item(0); 175 assertNotNull("empAddrNotNull", testAddr); 176 newAttr = doc.createAttributeNS("http://www.nist.gov", "xxx:domestic"); 177 newAddrAttr = ((Element) /* Node */testAddr) 178 .setAttributeNodeNS(newAttr); 179 newName = newAddrAttr.getNodeName(); 180 assertEquals("nodeName", "emp:domestic", newName); 181 } 182 public void testSetAttributeNode5() throws Throwable { 183 String namespaceURI = "http://www.newattr.com"; 184 String qualifiedName = "emp:newAttr"; 185 Document doc1; 186 Document doc2; 187 Attr newAttr; 188 NodeList elementList; 189 Node testAddr; 190 191 doc1 = (Document) load("staffNS", builder); 192 doc2 = (Document) load("staffNS", builder); 193 newAttr = doc2.createAttributeNS(namespaceURI, qualifiedName); 194 elementList = doc1.getElementsByTagName("emp:address"); 195 testAddr = elementList.item(0); 196 197 { 198 boolean success = false; 199 try { 200 ((Element) /* Node */testAddr).setAttributeNodeNS(newAttr); 201 } catch (DOMException ex) { 202 success = (ex.code == DOMException.WRONG_DOCUMENT_ERR); 203 } 204 assertTrue("throw_WRONG_DOCUMENT_ERR", success); 205 } 206 } 207} 208