1 package tests.org.w3c.dom; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.w3c.dom.Element; 7 import org.w3c.dom.Attr; 8 import org.w3c.dom.Document; 9 import org.w3c.dom.NodeList; 10 import org.w3c.dom.DOMException; 11 import org.w3c.dom.Node; 12 13 import javax.xml.parsers.DocumentBuilder; 14 15 /** 16 * The method setAttributeNS adds a new attribute. Create a new element and add 17 * a new attribute node to it using the setAttributeNS method. Check if the 18 * attribute was correctly set by invoking the getAttributeNodeNS method and 19 * checking the nodeName and nodeValue of the returned nodes. 20 * 21 * @author IBM 22 * @author Neil Delima 23 * @see <a 24 * href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS</a> 25 */ 26 public final class ElementSetAttributeNS extends DOMTestCase { 27 28 DOMDocumentBuilderFactory factory; 29 30 DocumentBuilder builder; 31 setUp()32 protected void setUp() throws Exception { 33 super.setUp(); 34 try { 35 factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory 36 .getConfiguration2()); 37 builder = factory.getBuilder(); 38 } catch (Exception e) { 39 fail("Unexpected exception" + e.getMessage()); 40 } 41 } 42 tearDown()43 protected void tearDown() throws Exception { 44 factory = null; 45 builder = null; 46 super.tearDown(); 47 } 48 49 /** 50 * Runs the test case. 51 * 52 * @throws Throwable 53 * Any uncaught exception causes test to fail 54 */ testSetAttributeNS1()55 public void testSetAttributeNS1() throws Throwable { 56 Document doc; 57 Element element; 58 Attr attribute; 59 String attrName; 60 String attrValue; 61 doc = (Document) load("staff", builder); 62 element = doc.createElementNS("http://www.w3.org/DOM", "dom:elem"); 63 element.setAttributeNS("http://www.w3.org/DOM/Test/setAttributeNS", 64 "attr", "value"); 65 attribute = element.getAttributeNodeNS( 66 "http://www.w3.org/DOM/Test/setAttributeNS", "attr"); 67 attrName = attribute.getNodeName(); 68 attrValue = attribute.getNodeValue(); 69 assertEquals("elementsetattributens01_attrName", "attr", attrName); 70 assertEquals("elementsetattributens01_attrValue", "value", attrValue); 71 } testSetAttributeNS2()72 public void testSetAttributeNS2() throws Throwable { 73 Document doc; 74 Element element; 75 Attr attribute; 76 NodeList elementList; 77 String attrName; 78 String attrValue; 79 doc = (Document) load("staff", builder); 80 elementList = doc.getElementsByTagNameNS("*", "address"); 81 element = (Element) elementList.item(0); 82 element.setAttributeNS("http://www.w3.org/DOM/Test/setAttributeNS", 83 "this:street", "Silver Street"); 84 attribute = element.getAttributeNodeNS( 85 "http://www.w3.org/DOM/Test/setAttributeNS", "street"); 86 attrName = attribute.getNodeName(); 87 attrValue = attribute.getNodeValue(); 88 assertEquals("elementsetattributens02_attrName", "this:street", 89 attrName); 90 assertEquals("elementsetattributens02_attrValue", "Silver Street", 91 attrValue); 92 } testSetAttributeNS3()93 public void testSetAttributeNS3() throws Throwable { 94 Document doc; 95 Element element; 96 Attr attribute; 97 NodeList elementList; 98 String attrName; 99 String attrValue; 100 doc = (Document) load("staffNS", builder); 101 elementList = doc.getElementsByTagName("emp:employee"); 102 element = (Element) elementList.item(0); 103 assertNotNull("empEmployeeNotNull", element); 104 element.setAttributeNS("http://www.w3.org/DOM/Test/1", "defaultAttr", 105 "default1"); 106 element.setAttributeNS("http://www.w3.org/DOM/Test/2", "defaultAttr", 107 "default2"); 108 attribute = element.getAttributeNodeNS("http://www.w3.org/DOM/Test/1", 109 "defaultAttr"); 110 attrName = attribute.getNodeName(); 111 attrValue = attribute.getNodeValue(); 112 assertEquals("elementsetattributens03_attrName", "defaultAttr", 113 attrName); 114 assertEquals("elementsetattributens03_attrValue", "default1", attrValue); 115 } testSetAttributeNS4()116 public void testSetAttributeNS4() throws Throwable { 117 Document doc; 118 Element element; 119 String qualifiedName; 120 List<String> qualifiedNames = new ArrayList<String>(); 121 qualifiedNames.add("/"); 122 qualifiedNames.add("//"); 123 qualifiedNames.add("\\"); 124 qualifiedNames.add(";"); 125 qualifiedNames.add("&"); 126 qualifiedNames.add("*"); 127 qualifiedNames.add("]]"); 128 qualifiedNames.add(">"); 129 qualifiedNames.add("<"); 130 131 doc = (Document) load("staffNS", builder); 132 element = doc.createElementNS("http://www.w3.org/DOM/Test/L2", 133 "dom:elem"); 134 for (int indexN10058 = 0; indexN10058 < qualifiedNames.size(); indexN10058++) { 135 qualifiedName = (String) qualifiedNames.get(indexN10058); 136 137 { 138 boolean success = false; 139 try { 140 element.setAttributeNS("http://www.w3.org/DOM/Test/L2", 141 qualifiedName, "test"); 142 } catch (DOMException ex) { 143 success = (ex.code == DOMException.INVALID_CHARACTER_ERR); 144 } 145 assertTrue("elementsetattributens04", success); 146 } 147 } 148 } testSetAttributeNS5()149 public void testSetAttributeNS5() throws Throwable { 150 Document doc; 151 Element element; 152 String nullNS = null; 153 154 doc = (Document) load("staffNS", builder); 155 element = doc.createElementNS("http://www.w3.org/DOM/Test/L2", 156 "dom:elem"); 157 158 { 159 boolean success = false; 160 try { 161 element.setAttributeNS(nullNS, "dom:root", "test"); 162 } catch (DOMException ex) { 163 success = (ex.code == DOMException.NAMESPACE_ERR); 164 } 165 assertTrue("elementsetattributens05", success); 166 } 167 } testSetAttributeNS8()168 public void testSetAttributeNS8() throws Throwable { 169 Document doc; 170 Element element; 171 doc = (Document) load("staffNS", builder); 172 element = doc.createElementNS("http://www.w3.org/DOMTest/level2", 173 "dom:elem"); 174 175 { 176 boolean success = false; 177 try { 178 element.setAttributeNS("http://www.w3.org/DOMTest/level2", 179 "xmlns", "test"); 180 } catch (DOMException ex) { 181 success = (ex.code == DOMException.NAMESPACE_ERR); 182 } 183 assertTrue("elementsetattributens08_Err1", success); 184 } 185 186 { 187 boolean success = false; 188 try { 189 element.setAttributeNS("http://www.w3.org/DOMTest/level2", 190 "xmlns:root", "test"); 191 } catch (DOMException ex) { 192 success = (ex.code == DOMException.NAMESPACE_ERR); 193 } 194 assertTrue("elementsetattributens08_Err2", success); 195 } 196 } testSetAttributeNSURINull()197 public void testSetAttributeNSURINull() throws Throwable { 198 String namespaceURI = null; 199 200 String qualifiedName = "emp:qualifiedName"; 201 Document doc; 202 NodeList elementList; 203 Node testAddr; 204 doc = (Document) load("staff", builder); 205 elementList = doc.getElementsByTagName("employee"); 206 testAddr = elementList.item(0); 207 208 { 209 boolean success = false; 210 try { 211 ((Element) /*Node */testAddr).setAttributeNS(namespaceURI, qualifiedName, "newValue"); 212 } catch (DOMException ex) { 213 success = (ex.code == DOMException.NAMESPACE_ERR); 214 } 215 assertTrue("throw_NAMESPACE_ERR", success); 216 } 217 } 218 } 219