1 7 8 package org.netbeans.modules.xml.xdm.nodes; 9 10 import java.io.File ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import javax.xml.XMLConstants ; 14 import javax.xml.namespace.QName ; 15 import junit.framework.*; 16 import org.netbeans.modules.xml.xam.TestComponent; 17 import org.netbeans.modules.xml.xdm.Util; 18 import org.netbeans.modules.xml.xdm.XDMModel; 19 import org.w3c.dom.NamedNodeMap ; 20 import org.w3c.dom.NodeList ; 21 22 26 public class ElementTest extends TestCase { 27 28 public ElementTest(String testName) { 29 super(testName); 30 } 31 32 protected void setUp() throws Exception { 33 xmlModel = Util.loadXDMModel("nodes/xdm.xml"); 34 xmlModel.sync(); 35 elem = (Element)xmlModel.getDocument().getChildNodes().item(0). 36 getChildNodes().item(1); 37 } 38 39 public static Test suite() { 40 TestSuite suite = new TestSuite(ElementTest.class); 41 42 return suite; 43 } 44 45 public void testGetNodeType() { 46 short expResult = org.w3c.dom.Node.ELEMENT_NODE; 47 short result = elem.getNodeType(); 48 assertEquals("getNodeType must return ATTRIBUTE_NODE",expResult, result); 49 } 50 51 public void testGetNodeName() { 52 String expResult = "employee"; 53 String result = elem.getNodeName(); 54 assertEquals(expResult, result); 55 } 56 57 public void testGetTagName() { 58 String expResult = "employee"; 59 String result = elem.getTagName(); 60 assertEquals(expResult, result); 61 62 Element instance = new Element("xs:element"); 63 expResult = "xs:element"; 64 result = instance.getTagName(); 65 assertEquals(expResult, result); 66 } 67 68 public void testGetPrefix() { 69 assertNull(elem.getPrefix()); 70 71 Element instance = new Element("xs:element"); 72 String expResult = "xs"; 73 String result = instance.getPrefix(); 74 assertEquals(expResult, result); 75 } 76 77 public void testSetPrefix() { 78 Element origElem = elem; 79 String origPrefix = elem.getPrefix(); 80 String newPrefix = "xs"; 81 try { 82 elem.setPrefix(newPrefix); 83 assertTrue("setPrefix must throw exception for element node in tree",false); 84 } catch (Exception e) { 85 assertTrue(true); 86 } 87 Element newElem = (Element)elem.clone(true,false,false); 88 try { 89 newElem.setPrefix(newPrefix); 90 assertTrue(true); 91 } catch (Exception e) { 92 assertTrue("setPrefix must not throw exception for element node not in tree",false); 93 } 94 xmlModel.modify(elem,newElem); 95 elem = (Element)xmlModel.getDocument().getChildNodes().item(0). 96 getChildNodes().item(1); 97 assertEquals(newPrefix,elem.getPrefix()); 98 assertEquals(origPrefix,origElem.getPrefix()); 100 101 Element modifiedElem = elem; 103 newElem = (Element)elem.clone(true,false,false); 104 try { 105 newElem.setPrefix(""); 106 assertTrue(true); 107 } catch (Exception e) { 108 assertTrue("setPrefix must not throw exception for element node not in tree",false); 109 } 110 xmlModel.modify(elem,newElem); 111 elem = (Element)xmlModel.getDocument().getChildNodes().item(0). 112 getChildNodes().item(1); 113 assertNull(elem.getPrefix()); 114 assertEquals(newPrefix,modifiedElem.getPrefix()); 116 } 117 118 public void testGetLocalName() { 119 String expResult = "employee"; 120 String result = elem.getLocalName(); 121 assertEquals(expResult, result); 122 123 Element instance = new Element("xs:element"); 124 expResult = "element"; 125 result = instance.getLocalName(); 126 assertEquals(expResult, result); 127 } 128 129 public void testSetLocalName() { 130 Element origElem = elem; 131 String origName = elem.getLocalName(); 132 String newName = "employee1"; 133 try { 134 elem.setLocalName(newName); 135 assertTrue("setLocalName must throw exception for element node in tree",false); 136 } catch (Exception e) { 137 assertTrue(true); 138 } 139 Element newElem = (Element)elem.clone(true,false,false); 140 try { 141 newElem.setLocalName(newName); 142 assertTrue(true); 143 } catch (Exception e) { 144 assertTrue("setLocalName must not throw exception for element node not in tree",false); 145 } 146 xmlModel.modify(elem,newElem); 147 elem = (Element)xmlModel.getDocument().getChildNodes().item(0). 148 getChildNodes().item(1); 149 assertEquals(newName,elem.getLocalName()); 150 assertEquals(origName,origElem.getLocalName()); 152 } 153 154 public void testGetAttributes() { 155 NamedNodeMap attributes = elem.getAttributes(); 156 assertEquals(4, attributes.getLength()); 157 assertNotNull(attributes.getNamedItem("ssn")); 158 assertSame(attributes.getNamedItem("ssn"),attributes.item(0)); 159 assertNotNull(attributes.getNamedItem("id")); 160 assertSame(attributes.getNamedItem("id"),attributes.item(1)); 161 assertNotNull(attributes.getNamedItem("address")); 162 assertSame(attributes.getNamedItem("address"),attributes.item(2)); 163 assertNotNull(attributes.getNamedItem("phone")); 164 assertSame(attributes.getNamedItem("phone"),attributes.item(3)); 165 assertNull(attributes.getNamedItem("ssn1")); 166 167 Element company = (Element)xmlModel.getDocument().getDocumentElement(); 168 NamedNodeMap companyAttrs = company.getAttributes(); 169 assertEquals(1, companyAttrs.getLength()); 170 assertNotNull(companyAttrs.getNamedItem("xmlns")); 171 } 172 173 public void testGetChildNodes() { 174 Element company = (Element)xmlModel.getDocument().getDocumentElement(); 175 NodeList children = company.getChildNodes(); 176 assertEquals(7, children.getLength()); 177 assertTrue(children.item(0) instanceof Text); 178 assertTrue(children.item(1) instanceof Element); 179 assertEquals("employee",children.item(1).getNodeName()); 180 assertTrue(children.item(2) instanceof Text); 181 assertTrue(children.item(3) instanceof Text); 182 assertEquals(" comment ",children.item(3).getNodeValue()); 183 assertTrue(children.item(4) instanceof Text); 184 assertTrue(children.item(5) instanceof Element); 185 assertEquals("employee",children.item(5).getNodeName()); 186 assertTrue(children.item(6) instanceof Text); 187 } 188 189 public void testGetAttribute() { 190 String name = "ssn"; 191 String expResult = "xx-xx-xxxx"; 192 String result = elem.getAttribute(name); 193 assertEquals(expResult, result); 194 assertNull(elem.getAttribute("ssn1")); 196 } 197 198 public void testGetAttributeNode() { 199 String name = "ssn"; 200 Attribute expResult = (Attribute)elem.getAttributes().item(0); 201 Attribute result = elem.getAttributeNode(name); 202 assertSame(expResult, result); 203 assertNull(elem.getAttributeNode("ssn1")); 205 } 206 207 public void testHasAttribute() { 208 String name="ssn"; 209 boolean expResult = true; 210 boolean result = elem.hasAttribute(name); 211 assertEquals(expResult, result); 212 assertFalse(elem.hasAttribute("ssn1")); 214 } 215 216 private Element getEmployee(XDMModel model) { 217 Element company = (Element) 218 xmlModel.getDocument().getChildNodes().item(0); 219 NodeList children = company.getChildNodes(); 220 Element employee = null; 221 for (int i = 0; i < children.getLength(); i++) { 222 org.w3c.dom.Node child = children.item(i); 223 if (child instanceof Element) { 224 Element e = (Element) child; 225 if (e.getLocalName().equals("employee")) { 226 employee = e; 227 } 228 } 229 230 } 231 return employee; 232 } 233 234 public void testConversionFromSelfClosingElement() throws Exception { 235 xmlModel = Util.loadXDMModel("nodes/self-closing.xml"); 236 Element employee = getEmployee(xmlModel); 237 assertNotNull("could not find employee element", employee); 238 239 final String TEST_ELEMENT_NAME = "testChild"; 240 org.w3c.dom.Element childNode = 241 xmlModel.getDocument().createElement(TEST_ELEMENT_NAME); 242 Element clonedEmployee = (Element) employee.cloneNode(true); 243 clonedEmployee.appendChild(childNode); 244 xmlModel.modify(employee,clonedEmployee); 245 246 xmlModel.flush(); 247 xmlModel.sync(); 248 249 employee = getEmployee(xmlModel); 250 assertNotNull("employee does not have child element", 251 employee.getFirstChild()); 252 assertEquals("childElement not equal to testChild", 253 employee.getFirstChild().getLocalName(), TEST_ELEMENT_NAME); 254 } 255 256 public void testCloneNodeNamespacesConsolidateToRoot() throws Exception { 257 xmlModel = Util.loadXDMModel("nodes/cloned-node.xml"); 258 Element root = (Element) xmlModel.getDocument().getDocumentElement(); 259 Element cloned = Util.getChildElementByTag(root, "a1:A"); 260 Element clone = (Element)cloned.cloneNode(true); 261 262 assertFalse(clone.isInTree()); 263 assertTrue(clone.getModel() == null); 264 assertEquals("namespaceA", clone.getNamespaceURI()); 265 assertEquals("namespaceA", clone.getAttribute("xmlns:a1")); 266 assertEquals("namespaceB", clone.lookupNamespaceURI("")); 267 Element cloneChildB = Util.getChildElementByTag(clone, "B"); 268 assertEquals("namespaceB", cloneChildB.lookupNamespaceURI("b1")); 269 270 javax.swing.text.Document doc = Util.getResourceAsDocument("nodes/clone-receiver.xml"); 271 XDMModel dest = Util.loadXDMModel(doc); 272 Element receiverRoot = (Element) dest.getDocument().getDocumentElement(); 273 Element middle = Util.getChildElementByTag(receiverRoot, "m:middle"); 274 middle = (Element) dest.append(middle, clone).get(0); 275 276 279 receiverRoot = (Element) dest.getDocument().getDocumentElement(); 280 assertTrue(middle == Util.getChildElementByTag(receiverRoot, "m:middle")); 281 Element pasted = Util.getChildElementByTag(middle, "a1:A"); 282 assertEquals("namespaceB", receiverRoot.lookupNamespaceURI("")); 283 assertEquals("namespaceA", receiverRoot.lookupNamespaceURI("a1")); 284 assertNull(pasted.getAttribute("xmlns:a1")); 285 assertNull(pasted.getAttribute("xmlns")); 286 } 287 288 public void testInsertWithFullConsolidation() throws Exception { 289 xmlModel = Util.loadXDMModel("nodes/cloned-node.xml"); 290 Element root = (Element) xmlModel.getDocument().getDocumentElement(); 291 Element cloned = Util.getChildElementByTag(root, "a1:A"); 292 Element clone = (Element)cloned.cloneNode(true); 293 assertEquals("namespaceB", clone.getAttribute("xmlns:b1")); 294 assertEquals("namespaceB", clone.getAttribute("xmlns")); 295 296 javax.swing.text.Document doc = Util.getResourceAsDocument("nodes/clone-receiver3.xml"); 297 XDMModel dest = Util.loadXDMModel(doc); 298 dest.setQNameValuedAttributes(new HashMap <QName ,List <QName >>()); 299 Element receiverRoot = (Element) dest.getDocument().getDocumentElement(); 300 Element middle = Util.getChildElementByTag(receiverRoot, "m:middle"); 301 middle = (Element) dest.add(middle, clone, 0).get(0); 302 303 dest.flush(); 304 305 receiverRoot = (Element) dest.getDocument().getDocumentElement(); 306 assertTrue(middle == Util.getChildElementByTag(receiverRoot, "m:middle")); 307 Element pasted = Util.getChildElementByTag(middle, "a2:A"); 308 assertNull(receiverRoot.lookupNamespaceURI("")); 309 assertNull(receiverRoot.lookupNamespaceURI("a1")); 310 assertNull(pasted.getAttribute("xmlns:a1")); 311 assertNull(pasted.getAttribute("xmlns:b1")); 312 assertNull(pasted.getAttribute("xmlns")); 313 Element pastedChild = Util.getChildElementByTag(pasted, "b1:B"); 314 assertEquals("value1", pastedChild.getAttributeNS("namespaceB","attrB")); 315 assertNull(pastedChild.getAttribute("xmlns:b1")); 316 } 317 318 public void testInsertWithLimitedConsolidationDueToPrefixedAttribute() throws Exception { 319 xmlModel = Util.loadXDMModel("nodes/cloned-node.xml"); 320 Element root = (Element) xmlModel.getDocument().getDocumentElement(); 321 Element cloned = Util.getChildElementByTag(root, "a1:A"); 322 Element clone = (Element)cloned.cloneNode(true); 323 assertEquals("namespaceB", clone.getAttribute("xmlns:b1")); 324 assertEquals("namespaceB", clone.getAttribute("xmlns")); 325 326 javax.swing.text.Document doc = Util.getResourceAsDocument("nodes/clone-receiver2.xml"); 327 XDMModel dest = Util.loadXDMModel(doc); 328 dest.setQNameValuedAttributes(new HashMap <QName ,List <QName >>()); 329 Element receiverRoot = (Element) dest.getDocument().getDocumentElement(); 330 Element middle = Util.getChildElementByTag(receiverRoot, "m:middle"); 331 middle = (Element) dest.add(middle, clone, 0).get(0); 332 333 dest.flush(); 334 336 receiverRoot = (Element) dest.getDocument().getDocumentElement(); 337 assertTrue(middle == Util.getChildElementByTag(receiverRoot, "m:middle")); 338 Element pasted = Util.getChildElementByTag(middle, "a2:A"); 339 assertNull(receiverRoot.lookupNamespaceURI("")); 340 assertNull(receiverRoot.lookupNamespaceURI("a1")); 341 assertNull(pasted.getAttribute("xmlns:a1")); 342 assertNull(pasted.getAttribute("xmlns:b1")); 343 assertNull(pasted.getAttribute("xmlns")); 344 Element pastedChild = Util.getChildElementByTag(pasted, "B"); 345 assertEquals("value1", pastedChild.getAttributeNS("namespaceB","attrB")); 346 assertEquals("namespaceB", pastedChild.getAttribute("xmlns:b1")); 347 } 348 349 public void testElementGetAttributes() throws Exception { 350 xmlModel = Util.loadXDMModel("nodes/elementAttributes.xml"); 351 364 Element test = (Element) xmlModel.getDocument().getChildNodes().item(0); 365 Element e = (Element) test.getChildNodes().item(1); 366 String attrVal1 = e.getAttributeNS("http://org.company/schemas/test1.xsd", "myAttribute"); 367 assertEquals("someNs:myAttribute", "7", attrVal1); 368 String attrVal2 = e.getAttribute("myAttribute"); 369 assertEquals("myAttribute", "8", attrVal2); 370 String attrVal3 = e.getAttributeNS("http://org.company/schemas/test2.xsd", "myAttribute"); 371 assertEquals("otherNs:myAttribute", "9", attrVal3); 372 } 373 374 public void testConsolidateNamespace() throws Exception { 375 javax.swing.text.Document doc = Util.getResourceAsDocument("nodes/test2.xml"); 376 XDMModel model = Util.loadXDMModel(doc); 377 model.setQNameValuedAttributes(new HashMap <QName ,List <QName >>()); 378 Element root = (Element) model.getDocument().getDocumentElement(); 379 Element a1 = (Element) model.getDocument().createElementNS(TestComponent.NS_URI, "a1"); 380 assertEquals(TestComponent.NS_URI, a1.getAttribute(XMLConstants.XMLNS_ATTRIBUTE)); 381 382 Element a2 = (Element) model.getDocument().createElementNS(TestComponent.NS_URI, "a2"); 383 a1.appendChild(a2); 384 assertNull(a2.getAttribute(XMLConstants.XMLNS_ATTRIBUTE)); 385 386 model.append(root, a1); 387 assertNull(a1.getAttribute(XMLConstants.XMLNS_ATTRIBUTE)); 388 } 389 390 public void testConsolidateDefaultToDeclaredPrefix() throws Exception { 391 xmlModel = Util.loadXDMModel("nodes/cloned-node4.xml"); 392 Element root = (Element) xmlModel.getDocument().getDocumentElement(); 393 Element cloned = Util.getChildElementByTag(root, "A"); 394 Element clone = (Element)cloned.cloneNode(true); 395 396 assertFalse(clone.isInTree()); 397 assertTrue(clone.getModel() == null); 398 assertEquals("namespaceA", clone.getNamespaceURI()); 399 assertEquals("namespaceA", clone.getAttribute("xmlns")); 400 assertEquals("namespaceA", clone.lookupNamespaceURI("")); 401 Element cloneChildB = Util.getChildElementByTag(clone, "B"); 402 assertEquals("namespaceB", cloneChildB.lookupNamespaceURI("b1")); 403 404 javax.swing.text.Document doc = Util.getResourceAsDocument("nodes/clone-receiver4.xml"); 405 XDMModel dest = Util.loadXDMModel(doc); 406 dest.setQNameValuedAttributes(new HashMap <QName ,List <QName >>()); 407 Element receiverRoot = (Element) dest.getDocument().getDocumentElement(); 408 Element middle = Util.getChildElementByTag(receiverRoot, "m:middle"); 409 middle = (Element) dest.append(middle, clone).get(0); 410 411 dest.flush(); 412 414 receiverRoot = (Element) dest.getDocument().getDocumentElement(); 415 assertTrue(middle == Util.getChildElementByTag(receiverRoot, "m:middle")); 416 Element pasted = Util.getChildElementByTag(middle, "a1:A"); 417 assertEquals("namespaceA", receiverRoot.lookupNamespaceURI("a1")); 418 assertNull(pasted.getAttribute("xmlns:a1")); 419 assertNull(pasted.getAttribute("xmlns")); 420 } 421 422 public void testSetAttributeWithXmlEscape() throws Exception { 423 javax.swing.text.Document doc = Util.getResourceAsDocument("nodes/test2.xml"); 424 XDMModel model = Util.loadXDMModel(doc); 425 Element root = (Element) model.getDocument().getDocumentElement(); 426 Element a1 = Util.getChildElementByTag(root, "tst:a"); 427 String shouldEscapeXML = "a < b && c == '3'"; 428 model.setAttribute(a1, "newattr", shouldEscapeXML); 429 model.flush(); 430 String doctext = doc.getText(0, doc.getLength()-1); 431 System.out.println(doctext); 432 assertEquals(-1, doctext.indexOf(shouldEscapeXML)); 433 assertEquals(124, doctext.indexOf("<")); 434 assertEquals(131, doctext.indexOf("&&")); 435 assertEquals(147, doctext.indexOf("'3'")); 436 } 437 438 private XDMModel xmlModel; 439 private Element elem; 440 } 441 | Popular Tags |