1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.jaxen.test;
47
48 import java.util.Iterator;
49 import java.util.List;
50
51 import javax.xml.parsers.DocumentBuilderFactory;
52 import javax.xml.parsers.ParserConfigurationException;
53
54 import org.jaxen.*;
55 import org.jaxen.dom.DOMXPath;
56 import org.jaxen.dom.NamespaceNode;
57 import org.w3c.dom.*;
58
59 import junit.framework.TestCase;
60
61 public class DOM3NamespaceTest extends TestCase {
62
63
64 private NamespaceNode xmlNS;
65 private NamespaceNode rootNS;
66 private NamespaceNode attributeNS;
67
68
69 public DOM3NamespaceTest(String name) {
70 super(name);
71 }
72
73 protected void setUp() throws ParserConfigurationException, JaxenException {
74
75 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
76 factory.setNamespaceAware(true);
77 Document doc = factory.newDocumentBuilder().newDocument();
78 org.w3c.dom.Element root = doc.createElementNS("http://www.root.com/", "root");
79 doc.appendChild(root);
80 Attr a = doc.createAttributeNS("http://www.example.org/", "pre:a");
81 a.setNodeValue("value");
82 root.setAttributeNode(a);
83
84 XPath xpath = new DOMXPath("namespace::node()");
85 List result = xpath.selectNodes(root);
86
87 Iterator iterator = result.iterator();
88 while (iterator.hasNext()) {
89 NamespaceNode node = (NamespaceNode) iterator.next();
90 if (node.getLocalName() == null) rootNS = node;
91 else if (node.getLocalName().equals("xml")) xmlNS = node;
92 else if (node.getLocalName().equals("pre")) attributeNS = node;
93 }
94
95 }
96
97
98 public void testGetTextContent() {
99 assertEquals("http://www.w3.org/XML/1998/namespace", xmlNS.getTextContent());
100 }
101
102 public void testSetTextContent() {
103
104 try {
105 rootNS.setTextContent("http://www.a.com");
106 fail("set text content");
107 }
108 catch (DOMException ex) {
109 assertEquals(DOMException.NO_MODIFICATION_ALLOWED_ERR, ex.code);
110 }
111 }
112
113
114 public void testGetFeature() {
115 assertNull(attributeNS.getFeature("name", "value"));
116 }
117
118
119
120 public void testIsEqualNode() {
121 assertFalse(rootNS.isEqualNode(xmlNS));
122 assertTrue(rootNS.isEqualNode(rootNS));
123 }
124
125 public void testIsSameNode() {
126 assertFalse(rootNS.isSameNode(xmlNS));
127 assertTrue(rootNS.isSameNode(rootNS));
128 }
129
130 }