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
47
48 package org.jaxen.test;
49
50 import java.io.IOException;
51 import java.io.StringReader;
52 import java.util.List;
53
54 import javax.xml.parsers.DocumentBuilder;
55 import javax.xml.parsers.DocumentBuilderFactory;
56 import javax.xml.parsers.ParserConfigurationException;
57
58 import junit.framework.TestCase;
59
60 import org.jaxen.BaseXPath;
61 import org.jaxen.FunctionCallException;
62 import org.jaxen.JaxenException;
63 import org.jaxen.dom.DOMXPath;
64 import org.w3c.dom.Attr;
65 import org.w3c.dom.Document;
66 import org.w3c.dom.Element;
67 import org.xml.sax.InputSource;
68 import org.xml.sax.SAXException;
69
70 /***
71 * @author Elliotte Rusty Harold
72 *
73 */
74 public class IdTest extends TestCase {
75
76 private Document doc;
77 private DocumentBuilder builder;
78
79 public void setUp() throws ParserConfigurationException
80 {
81 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
82 factory.setNamespaceAware(true);
83 builder = factory.newDocumentBuilder();
84 doc = builder.newDocument();
85 }
86
87 public IdTest(String name) {
88 super(name);
89 }
90
91 public void testIDFunctionSelectsNothingInDocumentWithNoIds()
92 throws JaxenException {
93
94 BaseXPath xpath = new DOMXPath("id('p1')");
95 org.w3c.dom.Element a = doc.createElementNS("", "a");
96 org.w3c.dom.Element b = doc.createElementNS("", "b");
97 doc.appendChild(a);
98 a.appendChild(b);
99 org.w3c.dom.Element x2 = doc.createElementNS("", "x");
100 org.w3c.dom.Element x3 = doc.createElementNS("", "x");
101 org.w3c.dom.Element x4 = doc.createElementNS("", "x");
102 a.appendChild(x4);
103 b.appendChild(x2);
104 b.appendChild(x3);
105 x2.appendChild(doc.createTextNode("2"));
106 x3.appendChild(doc.createTextNode("3"));
107 x4.appendChild(doc.createTextNode("4"));
108 Attr id = doc.createAttribute("id");
109 id.setNodeValue("p1");
110 x2.setAttributeNode(id);
111
112 List result = xpath.selectNodes(doc);
113 assertEquals(0, result.size());
114
115 }
116
117 public void testIDFunctionRequiresAtLeastOneArgument()
118 throws JaxenException {
119
120 try {
121 BaseXPath xpath = new DOMXPath("id()");
122 org.w3c.dom.Element a = doc.createElementNS("", "a");
123 doc.appendChild(a);
124 xpath.selectNodes(doc);
125 fail("Allowed empty id() function");
126 }
127 catch (FunctionCallException success) {
128 assertNotNull(success.getMessage());
129 }
130
131 }
132
133 public void testIDFunctionRequiresAtMostOneArgument()
134 throws JaxenException {
135
136 try {
137 BaseXPath xpath = new DOMXPath("id('p', 'q')");
138 org.w3c.dom.Element a = doc.createElementNS("", "a");
139 doc.appendChild(a);
140 xpath.selectNodes(doc);
141 fail("Allowed two-argument id() function");
142 }
143 catch (FunctionCallException success) {
144 assertNotNull(success.getMessage());
145 }
146
147 }
148
149 public void testFindElementById()
150 throws JaxenException, SAXException, IOException {
151
152 BaseXPath xpath = new DOMXPath("id('p1')");
153 String text = "<!DOCTYPE root [<!ATTLIST a id ID #REQUIRED>]><root><a id='p1'/></root>";
154 StringReader reader = new StringReader(text);
155 InputSource in = new InputSource(reader);
156 Document doc = builder.parse(in);
157 List result = xpath.selectNodes(doc);
158 assertEquals(1, result.size());
159 Element a = (Element) result.get(0);
160 assertEquals("a", a.getNodeName());
161
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public void testFindMultipleElementsByMultipleIDs()
180 throws JaxenException, SAXException, IOException {
181
182 BaseXPath xpath = new DOMXPath("id(//id)");
183 String text = "<!DOCTYPE root [<!ATTLIST a id ID #REQUIRED>]><root><id>p1</id><id>p2</id><id>p3</id><a id='p1'/><a id='p2'/></root>";
184 StringReader reader = new StringReader(text);
185 InputSource in = new InputSource(reader);
186 Document doc = builder.parse(in);
187 List result = xpath.selectNodes(doc);
188 assertEquals(2, result.size());
189 Element a1 = (Element) result.get(0);
190 Element a2 = (Element) result.get(1);
191 assertEquals("a", a1.getNodeName());
192 assertEquals("a", a2.getNodeName());
193
194 }
195
196 public void testIdReturnsFirstElementWithMatchingId()
197 throws JaxenException, SAXException, IOException {
198
199 BaseXPath xpath = new DOMXPath("id('p1')");
200 String text = "<!DOCTYPE root [<!ATTLIST a id ID #REQUIRED>" +
201 "<!ATTLIST b id ID #REQUIRED>]><root><a id='p1'/><b id='p1'/></root>";
202 StringReader reader = new StringReader(text);
203 InputSource in = new InputSource(reader);
204 Document doc = builder.parse(in);
205 List result = xpath.selectNodes(doc);
206 assertEquals(1, result.size());
207 Element a = (Element) result.get(0);
208 assertEquals("a", a.getNodeName());
209
210 }
211
212 }