1 60 package org.jaxen.dom; 61 62 import java.util.Iterator ; 63 import java.util.List ; 64 65 import javax.xml.parsers.DocumentBuilderFactory ; 66 import javax.xml.parsers.ParserConfigurationException ; 67 68 import org.jaxen.*; 69 import org.w3c.dom.*; 70 71 import junit.framework.TestCase; 72 73 public class DOM3NamespaceTest extends TestCase { 74 75 76 private NamespaceNode xmlNS; 77 private NamespaceNode rootNS; 78 private NamespaceNode attributeNS; 79 80 81 public DOM3NamespaceTest(String name) { 82 super(name); 83 } 84 85 protected void setUp() throws ParserConfigurationException , JaxenException { 86 87 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 88 factory.setNamespaceAware(true); 89 Document doc = factory.newDocumentBuilder().newDocument(); 90 org.w3c.dom.Element root = doc.createElementNS("http://www.root.com/", "root"); 91 doc.appendChild(root); 92 Attr a = doc.createAttributeNS("http://www.example.org/", "pre:a"); 93 a.setNodeValue("value"); 94 root.setAttributeNode(a); 95 96 XPath xpath = new DOMXPath("namespace::node()"); 97 List result = xpath.selectNodes(root); 98 99 Iterator iterator = result.iterator(); 100 while (iterator.hasNext()) { 101 NamespaceNode node = (NamespaceNode) iterator.next(); 102 if (node.getLocalName() == null) rootNS = node; 103 else if (node.getLocalName().equals("xml")) xmlNS = node; 104 else if (node.getLocalName().equals("pre")) attributeNS = node; 105 } 106 107 } 108 109 110 public void testGetTextContent() { 111 assertEquals("http://www.w3.org/XML/1998/namespace", xmlNS.getTextContent()); 112 } 113 114 public void testSetTextContent() { 115 116 try { 117 rootNS.setTextContent("http://www.a.com"); 118 fail("set text content"); 119 } 120 catch (DOMException ex) { 121 assertEquals(DOMException.NO_MODIFICATION_ALLOWED_ERR, ex.code); 122 } 123 } 124 125 126 public void testGetFeature() { 127 assertNull(attributeNS.getFeature("name", "value")); 128 } 129 130 131 public void testIsEqualNode() { 133 assertFalse(rootNS.isEqualNode(xmlNS)); 134 assertTrue(rootNS.isEqualNode(rootNS)); 135 } 136 137 public void testIsSameNode() { 138 assertFalse(rootNS.isSameNode(xmlNS)); 139 assertTrue(rootNS.isSameNode(rootNS)); 140 } 141 142 } 143 | Popular Tags |