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