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
49
50
51
52
53
54
55
56
57
58
59
60
61
62 package org.jaxen.function;
63
64 import java.util.List;
65
66 import javax.xml.parsers.DocumentBuilder;
67 import javax.xml.parsers.DocumentBuilderFactory;
68 import javax.xml.parsers.ParserConfigurationException;
69
70 import junit.framework.TestCase;
71
72 import org.jaxen.BaseXPath;
73 import org.jaxen.FunctionCallException;
74 import org.jaxen.JaxenException;
75 import org.jaxen.dom.DOMXPath;
76 import org.w3c.dom.Document;
77 import org.w3c.dom.Element;
78
79 /***
80 * @author Elliotte Rusty Harold
81 *
82 */
83 public class LangTest extends TestCase {
84
85 private Document doc;
86 private DocumentBuilder builder;
87
88 public void setUp() throws ParserConfigurationException
89 {
90 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
91 factory.setNamespaceAware(true);
92 builder = factory.newDocumentBuilder();
93 doc = builder.newDocument();
94 }
95
96 public LangTest(String name) {
97 super(name);
98 }
99
100 public void testLangFunction()
101 throws JaxenException {
102
103 BaseXPath xpath = new DOMXPath("//*[lang('en')]");
104 Element a = doc.createElementNS("", "a");
105 Element b = doc.createElementNS("", "b");
106 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
107 doc.appendChild(a);
108 a.appendChild(b);
109 Element x2 = doc.createElementNS("", "x");
110 Element x3 = doc.createElementNS("", "x");
111 Element x4 = doc.createElementNS("", "x");
112 a.appendChild(x4);
113 b.appendChild(x2);
114 b.appendChild(x3);
115 x2.appendChild(doc.createTextNode("2"));
116 x3.appendChild(doc.createTextNode("3"));
117 x4.appendChild(doc.createTextNode("4"));
118
119 List result = xpath.selectNodes(doc);
120 assertEquals(3, result.size());
121 assertEquals(b, result.get(0));
122 assertEquals(x2, result.get(1));
123 assertEquals(x3, result.get(2));
124
125 }
126
127 public void testLangFunctionSelectsNothing()
128 throws JaxenException {
129
130 BaseXPath xpath = new DOMXPath("//*[lang('fr')]");
131 Element a = doc.createElementNS("", "a");
132 Element b = doc.createElementNS("", "b");
133 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
134 doc.appendChild(a);
135 a.appendChild(b);
136 Element x2 = doc.createElementNS("", "x");
137 Element x3 = doc.createElementNS("", "x");
138 Element x4 = doc.createElementNS("", "x");
139 a.appendChild(x4);
140 b.appendChild(x2);
141 b.appendChild(x3);
142 x2.appendChild(doc.createTextNode("2"));
143 x3.appendChild(doc.createTextNode("3"));
144 x4.appendChild(doc.createTextNode("4"));
145
146 List result = xpath.selectNodes(doc);
147 assertEquals(0, result.size());
148
149 }
150
151 public void testLangFunctionSelectsSubcode()
152 throws JaxenException {
153
154 BaseXPath xpath = new DOMXPath("//*[lang('fr')]");
155 Element a = doc.createElementNS("", "a");
156 Element b = doc.createElementNS("", "b");
157 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
158 doc.appendChild(a);
159 a.appendChild(b);
160 Element x2 = doc.createElementNS("", "x");
161 Element x3 = doc.createElementNS("", "x");
162 Element x4 = doc.createElementNS("", "x");
163 a.appendChild(x4);
164 b.appendChild(x2);
165 b.appendChild(x3);
166 x2.appendChild(doc.createTextNode("2"));
167 x3.appendChild(doc.createTextNode("3"));
168 x4.appendChild(doc.createTextNode("4"));
169
170 List result = xpath.selectNodes(doc);
171 assertEquals(3, result.size());
172 assertEquals(b, result.get(0));
173 assertEquals(x2, result.get(1));
174 assertEquals(x3, result.get(2));
175
176 }
177
178 public void testHyphenRequiredAtEnd()
179 throws JaxenException {
180
181 BaseXPath xpath = new DOMXPath("//*[lang('f')]");
182 Element a = doc.createElementNS("", "a");
183 Element b = doc.createElementNS("", "b");
184 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
185 doc.appendChild(a);
186 a.appendChild(b);
187 Element x2 = doc.createElementNS("", "x");
188 Element x3 = doc.createElementNS("", "x");
189 Element x4 = doc.createElementNS("", "x");
190 a.appendChild(x4);
191 b.appendChild(x2);
192 b.appendChild(x3);
193 x2.appendChild(doc.createTextNode("2"));
194 x3.appendChild(doc.createTextNode("3"));
195 x4.appendChild(doc.createTextNode("4"));
196
197 List result = xpath.selectNodes(doc);
198 assertEquals(0, result.size());
199
200 }
201
202 public void testLangFunctionSelectsEmptyNodeSet()
203 throws JaxenException {
204
205 BaseXPath xpath = new DOMXPath("//*[lang(d)]");
206
207
208 Element a = doc.createElementNS("", "a");
209 Element b = doc.createElementNS("", "b");
210 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
211 doc.appendChild(a);
212 a.appendChild(b);
213 Element x2 = doc.createElementNS("", "x");
214 Element x3 = doc.createElementNS("", "x");
215 Element x4 = doc.createElementNS("", "x");
216 a.appendChild(x4);
217 b.appendChild(x2);
218 b.appendChild(x3);
219 x2.appendChild(doc.createTextNode("2"));
220 x3.appendChild(doc.createTextNode("3"));
221 x4.appendChild(doc.createTextNode("4"));
222
223 List result = xpath.selectNodes(doc);
224 assertEquals(0, result.size());
225
226 }
227
228 public void testLangFunctionSelectsNonEmptyNodeSet()
229 throws JaxenException {
230
231 BaseXPath xpath = new DOMXPath("//*[lang(x)]");
232 Element a = doc.createElementNS("", "a");
233 Element b = doc.createElementNS("", "b");
234 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
235 doc.appendChild(a);
236 a.appendChild(b);
237 Element x2 = doc.createElementNS("", "x");
238 Element x3 = doc.createElementNS("", "x");
239 Element x4 = doc.createElementNS("", "x");
240 a.appendChild(x4);
241 b.appendChild(x2);
242 b.appendChild(x3);
243 x2.appendChild(doc.createTextNode("fr"));
244 x3.appendChild(doc.createTextNode("3"));
245 x4.appendChild(doc.createTextNode("4"));
246
247 List result = xpath.selectNodes(doc);
248 assertEquals(1, result.size());
249 assertEquals(b, result.get(0));
250
251 }
252
253 public void testLangFunctionAppliedToNonElement()
254 throws JaxenException {
255
256 BaseXPath xpath = new DOMXPath("//text()[lang('fr')]");
257 Element a = doc.createElementNS("", "a");
258 Element b = doc.createElementNS("", "b");
259 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
260 doc.appendChild(a);
261 a.appendChild(b);
262 Element x2 = doc.createElementNS("", "x");
263 Element x3 = doc.createElementNS("", "x");
264 Element x4 = doc.createElementNS("", "x");
265 a.appendChild(x4);
266 b.appendChild(x2);
267 b.appendChild(x3);
268 x2.appendChild(doc.createTextNode("fr"));
269 x3.appendChild(doc.createTextNode("3"));
270 x4.appendChild(doc.createTextNode("4"));
271
272 List result = xpath.selectNodes(doc);
273 assertEquals(2, result.size());
274 assertEquals(x2.getFirstChild(), result.get(0));
275 assertEquals(x3.getFirstChild(), result.get(1));
276
277 }
278
279 public void testLangFunctionAppliedToDocument()
280 throws JaxenException {
281
282 BaseXPath xpath = new DOMXPath("lang('fr')");
283 Element a = doc.createElementNS("", "a");
284 Element b = doc.createElementNS("", "b");
285 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
286 doc.appendChild(a);
287 a.appendChild(b);
288 Element x2 = doc.createElementNS("", "x");
289 Element x3 = doc.createElementNS("", "x");
290 Element x4 = doc.createElementNS("", "x");
291 a.appendChild(x4);
292 b.appendChild(x2);
293 b.appendChild(x3);
294 x2.appendChild(doc.createTextNode("fr"));
295 x3.appendChild(doc.createTextNode("3"));
296 x4.appendChild(doc.createTextNode("4"));
297
298 Boolean result = (Boolean) xpath.evaluate(doc);
299 assertEquals(Boolean.FALSE, result);
300
301 }
302
303 public void testLangFunctionSelectsNumber()
304 throws JaxenException {
305
306 BaseXPath xpath = new DOMXPath("//*[lang(3)]");
307
308 Element a = doc.createElementNS("", "a");
309 Element b = doc.createElementNS("", "b");
310 b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
311 doc.appendChild(a);
312 a.appendChild(b);
313 Element x2 = doc.createElementNS("", "x");
314 Element x3 = doc.createElementNS("", "x");
315 Element x4 = doc.createElementNS("", "x");
316 a.appendChild(x4);
317 b.appendChild(x2);
318 b.appendChild(x3);
319 x2.appendChild(doc.createTextNode("2"));
320 x3.appendChild(doc.createTextNode("3"));
321 x4.appendChild(doc.createTextNode("4"));
322
323 List result = xpath.selectNodes(doc);
324 assertEquals(0, result.size());
325
326 }
327
328 public void testLangFunctionRequiresOneArgument()
329 throws JaxenException {
330
331 try {
332 BaseXPath xpath = new DOMXPath("lang()");
333 org.w3c.dom.Element a = doc.createElementNS("", "a");
334 doc.appendChild(a);
335 xpath.selectNodes(doc);
336 fail("Allowed empty lang() function");
337 }
338 catch (FunctionCallException success) {
339 assertNotNull(success.getMessage());
340 }
341
342 }
343
344 public void testLangFunctionRequiresAtMostOneArgument()
345 throws JaxenException {
346
347 try {
348 BaseXPath xpath = new DOMXPath("lang('en', 'fr')");
349 org.w3c.dom.Element a = doc.createElementNS("", "a");
350 doc.appendChild(a);
351 xpath.selectNodes(doc);
352 fail("Allowed empty lang() function");
353 }
354 catch (FunctionCallException success) {
355 assertNotNull(success.getMessage());
356 }
357
358 }
359
360
361 }