|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import nu.xom.Element; |
|
25 |
| import nu.xom.Attribute; |
|
26 |
| import nu.xom.MalformedURIException; |
|
27 |
| import nu.xom.NamespaceConflictException; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| public class NamespacesTest extends XOMTestCase { |
|
39 |
| |
|
40 |
53
| public NamespacesTest() {
|
|
41 |
53
| super("Namespaces tests");
|
|
42 |
| } |
|
43 |
| |
|
44 |
| private Element someNamespaces; |
|
45 |
| private Element noNamespaces; |
|
46 |
| private Element severalNamespaces; |
|
47 |
| |
|
48 |
| |
|
49 |
53
| protected void setUp() {
|
|
50 |
53
| noNamespaces = new Element("test");
|
|
51 |
| |
|
52 |
53
| someNamespaces = new Element("test");
|
|
53 |
53
| someNamespaces.addNamespaceDeclaration("xlink",
|
|
54 |
| "http://www.w3.org/2001/xlink"); |
|
55 |
53
| someNamespaces.addNamespaceDeclaration("xsl",
|
|
56 |
| "http://www.w3.org/1999/xslt"); |
|
57 |
| |
|
58 |
53
| severalNamespaces
|
|
59 |
| = new Element("test", "http://www.example.com/"); |
|
60 |
53
| severalNamespaces.addAttribute(
|
|
61 |
| new Attribute("xlink:type", |
|
62 |
| "http://www.w3.org/2001/xlink", "simple")); |
|
63 |
53
| severalNamespaces.addNamespaceDeclaration("xsl",
|
|
64 |
| "http://www.w3.org/1999/xslt"); |
|
65 |
53
| severalNamespaces.addNamespaceDeclaration("",
|
|
66 |
| "http://www.example.com/"); |
|
67 |
53
| severalNamespaces.addNamespaceDeclaration("xlink",
|
|
68 |
| "http://www.w3.org/2001/xlink"); |
|
69 |
| |
|
70 |
| } |
|
71 |
| |
|
72 |
| |
|
73 |
1
| public void testSetNamespacePrefixInConflictWithAdditionalNamespaceDeclaration() {
|
|
74 |
| |
|
75 |
1
| someNamespaces.setNamespaceURI("http://www.example.net");
|
|
76 |
1
| try {
|
|
77 |
1
| someNamespaces.setNamespacePrefix("xsl");
|
|
78 |
0
| fail("changed prefix to conflict with additional namespace declaration");
|
|
79 |
| } |
|
80 |
| catch (NamespaceConflictException success) { |
|
81 |
1
| assertNotNull(success.getMessage());
|
|
82 |
| } |
|
83 |
| |
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
1
| public void testSetNamespaceURIInConflictWithAdditionalNamespaceDeclaration() {
|
|
88 |
| |
|
89 |
1
| someNamespaces.setNamespaceURI("http://www.w3.org/2001/xlink");
|
|
90 |
1
| someNamespaces.setNamespacePrefix("xlink");
|
|
91 |
1
| try {
|
|
92 |
1
| someNamespaces.setNamespaceURI("http://www.example.net");
|
|
93 |
0
| fail("changed namespace URI to conflict with additional namespace declaration");
|
|
94 |
| } |
|
95 |
| catch (NamespaceConflictException ex) { |
|
96 |
1
| assertNotNull(ex.getMessage());
|
|
97 |
| } |
|
98 |
| |
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
1
| public void testAdditionalNamespaceDuplicatesElementNamespace() {
|
|
103 |
| |
|
104 |
1
| Element element = new Element("pre:element", "http://www.example.org");
|
|
105 |
1
| element.addNamespaceDeclaration("pre", "http://www.example.org");
|
|
106 |
1
| element.setNamespacePrefix("foo");
|
|
107 |
1
| assertEquals("http://www.example.org", element.getNamespaceURI("pre"));
|
|
108 |
| |
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
1
| public void testXMLNamespace() {
|
|
113 |
| |
|
114 |
1
| assertEquals(
|
|
115 |
| "http://www.w3.org/XML/1998/namespace", |
|
116 |
| noNamespaces.getNamespaceURI("xml") |
|
117 |
| ); |
|
118 |
1
| assertEquals(
|
|
119 |
| "http://www.w3.org/XML/1998/namespace", |
|
120 |
| severalNamespaces.getNamespaceURI("xml") |
|
121 |
| ); |
|
122 |
| |
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
1
| public void testWrongPrefixNotAllowedWithXMLURI() {
|
|
127 |
| |
|
128 |
1
| try {
|
|
129 |
1
| noNamespaces.addNamespaceDeclaration("pre",
|
|
130 |
| "http://www.w3.org/XML/1998/namespace"); |
|
131 |
0
| fail("Allowed XML namespace to be associated with non-xml prefix");
|
|
132 |
| } |
|
133 |
| catch (NamespaceConflictException success) { |
|
134 |
1
| assertNotNull(success.getMessage());
|
|
135 |
| } |
|
136 |
| |
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
1
| public void testUnmappingPrefix() {
|
|
141 |
| |
|
142 |
1
| try {
|
|
143 |
1
| noNamespaces.addNamespaceDeclaration("pre", "");
|
|
144 |
| } |
|
145 |
| catch (MalformedURIException success) { |
|
146 |
1
| assertNotNull(success.getMessage());
|
|
147 |
| } |
|
148 |
| |
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
1
| public void testAllowCapitalSchemes() {
|
|
153 |
1
| noNamespaces.addNamespaceDeclaration("pre", "HTTP://WWW.EXAMPLE.COM/");
|
|
154 |
1
| assertEquals(noNamespaces.getNamespaceURI("pre"), "HTTP://WWW.EXAMPLE.COM/");
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
1
| public void testBadSchemes() {
|
|
159 |
| |
|
160 |
1
| try {
|
|
161 |
1
| noNamespaces.addNamespaceDeclaration("pre", "uri!urn:somedata");
|
|
162 |
0
| fail("Allowed illegal characters in scheme");
|
|
163 |
| } |
|
164 |
| catch (MalformedURIException success) { |
|
165 |
1
| assertNotNull(success.getMessage());
|
|
166 |
| } |
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
1
| public void testXMLPrefixNotAllowedWithWrongURI() {
|
|
171 |
| |
|
172 |
1
| try {
|
|
173 |
1
| noNamespaces.addNamespaceDeclaration("xml", "http://www.example.org/");
|
|
174 |
0
| fail("Allowed xml prefix to be associated with wrong URI");
|
|
175 |
| } |
|
176 |
| catch (NamespaceConflictException success) { |
|
177 |
1
| assertNotNull(success.getMessage());
|
|
178 |
| } |
|
179 |
| |
|
180 |
| } |
|
181 |
| |
|
182 |
| |
|
183 |
1
| public void testXMLNSNamespace() {
|
|
184 |
1
| assertEquals("", noNamespaces.getNamespaceURI("xmlns"));
|
|
185 |
1
| assertEquals("", severalNamespaces.getNamespaceURI("xmlns"));
|
|
186 |
| } |
|
187 |
| |
|
188 |
| |
|
189 |
1
| public void testCantUseXMLNSPrefix() {
|
|
190 |
| |
|
191 |
1
| try {
|
|
192 |
1
| noNamespaces.addNamespaceDeclaration(
|
|
193 |
| "xmlns", "http://www.w3.org/2000/xmlns/"); |
|
194 |
0
| fail("added xmlns prefix");
|
|
195 |
| } |
|
196 |
| catch (NamespaceConflictException success) { |
|
197 |
1
| assertNotNull(success.getMessage());
|
|
198 |
| } |
|
199 |
| |
|
200 |
1
| try {
|
|
201 |
1
| noNamespaces.addNamespaceDeclaration("xmlns",
|
|
202 |
| "http://www.example.com"); |
|
203 |
0
| fail("added xmlns prefix");
|
|
204 |
| } |
|
205 |
| catch (NamespaceConflictException success) { |
|
206 |
1
| assertNotNull(success.getMessage());
|
|
207 |
| } |
|
208 |
| |
|
209 |
| } |
|
210 |
| |
|
211 |
| |
|
212 |
1
| public void testCantUseXMLPrefix() {
|
|
213 |
| |
|
214 |
1
| try {
|
|
215 |
1
| noNamespaces.addNamespaceDeclaration(
|
|
216 |
| "xml", "http://www.example.com"); |
|
217 |
0
| fail("added xmlns prefix");
|
|
218 |
| } |
|
219 |
| catch (NamespaceConflictException success) { |
|
220 |
1
| assertNotNull(success.getMessage());
|
|
221 |
| } |
|
222 |
| |
|
223 |
| } |
|
224 |
| |
|
225 |
| |
|
226 |
1
| public void testCanUseXMLPrefix() {
|
|
227 |
1
| noNamespaces.addNamespaceDeclaration(
|
|
228 |
| "xml", "http://www.w3.org/XML/1998/namespace"); |
|
229 |
1
| assertEquals(1, noNamespaces.getNamespaceDeclarationCount());
|
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
1
| public void testIndexedAccess() {
|
|
234 |
| |
|
235 |
1
| assertEquals("", noNamespaces.getNamespacePrefix(0));
|
|
236 |
| |
|
237 |
1
| assertNotNull(someNamespaces.getNamespacePrefix(0));
|
|
238 |
1
| assertNotNull(someNamespaces.getNamespacePrefix(1));
|
|
239 |
1
| assertNotNull(someNamespaces.getNamespacePrefix(2));
|
|
240 |
| |
|
241 |
1
| assertNotNull(severalNamespaces.getNamespacePrefix(0));
|
|
242 |
1
| assertNotNull(severalNamespaces.getNamespacePrefix(1));
|
|
243 |
1
| assertNotNull(severalNamespaces.getNamespacePrefix(2));
|
|
244 |
1
| try {
|
|
245 |
1
| severalNamespaces.getNamespacePrefix(3);
|
|
246 |
0
| fail("Got a namespace 3");
|
|
247 |
| } |
|
248 |
| catch (IndexOutOfBoundsException ex) { |
|
249 |
| |
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
1
| public void testSize() {
|
|
257 |
1
| assertEquals(1, noNamespaces.getNamespaceDeclarationCount());
|
|
258 |
1
| assertEquals(3, someNamespaces.getNamespaceDeclarationCount());
|
|
259 |
1
| assertEquals(3, severalNamespaces.getNamespaceDeclarationCount());
|
|
260 |
| } |
|
261 |
| |
|
262 |
| |
|
263 |
1
| public void testDefaultNamespace() {
|
|
264 |
1
| Element html = new Element("html", "http://www.w3.org/1999/xhtml");
|
|
265 |
1
| assertEquals(1, html.getNamespaceDeclarationCount());
|
|
266 |
1
| assertEquals("", html.getNamespacePrefix(0));
|
|
267 |
1
| assertEquals("http://www.w3.org/1999/xhtml", html.getNamespaceURI(""));
|
|
268 |
| } |
|
269 |
| |
|
270 |
| |
|
271 |
1
| public void testGetByPrefix() {
|
|
272 |
| |
|
273 |
1
| assertEquals("http://www.w3.org/2001/xlink",
|
|
274 |
| someNamespaces.getNamespaceURI("xlink")); |
|
275 |
1
| assertEquals("http://www.w3.org/1999/xslt",
|
|
276 |
| someNamespaces.getNamespaceURI("xsl")); |
|
277 |
1
| assertNull(someNamespaces.getNamespaceURI("fo"));
|
|
278 |
1
| assertNull(noNamespaces.getNamespaceURI("xsl"));
|
|
279 |
1
| assertEquals("", someNamespaces.getNamespaceURI(""));
|
|
280 |
1
| assertEquals("", noNamespaces.getNamespaceURI(""));
|
|
281 |
| |
|
282 |
| } |
|
283 |
| |
|
284 |
| |
|
285 |
1
| public void testGetNamespaceDeclarationCount() {
|
|
286 |
1
| Element test = new Element("test");
|
|
287 |
1
| assertEquals(1, test.getNamespaceDeclarationCount());
|
|
288 |
1
| test.setNamespaceURI("http://www.example.com");
|
|
289 |
1
| assertEquals(1, test.getNamespaceDeclarationCount());
|
|
290 |
1
| test.addAttribute(new Attribute("test", "test"));
|
|
291 |
1
| assertEquals(1, test.getNamespaceDeclarationCount());
|
|
292 |
1
| test.addAttribute(new Attribute("xlink:type",
|
|
293 |
| "http://www.w3.org/2001/xlink", "value")); |
|
294 |
1
| assertEquals(2, test.getNamespaceDeclarationCount());
|
|
295 |
1
| test.addAttribute(new Attribute("xlink:href",
|
|
296 |
| "http://www.w3.org/2001/xlink", "value")); |
|
297 |
1
| assertEquals(2, test.getNamespaceDeclarationCount());
|
|
298 |
1
| test.addNamespaceDeclaration("xlink", "http://www.w3.org/2001/xlink");
|
|
299 |
1
| assertEquals(2, test.getNamespaceDeclarationCount());
|
|
300 |
1
| test.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/xmlschema-instance");
|
|
301 |
1
| assertEquals(3, test.getNamespaceDeclarationCount());
|
|
302 |
| |
|
303 |
| } |
|
304 |
| |
|
305 |
| |
|
306 |
1
| public void testGetNegativeNamespacePrefix() {
|
|
307 |
| |
|
308 |
1
| Element test = new Element("pre:test", "http://www.example.com/");
|
|
309 |
1
| try {
|
|
310 |
1
| test.getNamespacePrefix(-1);
|
|
311 |
0
| fail("Got negative prefix");
|
|
312 |
| } |
|
313 |
| catch (IndexOutOfBoundsException success) { |
|
314 |
1
| assertNotNull(success.getMessage());
|
|
315 |
| } |
|
316 |
| |
|
317 |
| } |
|
318 |
| |
|
319 |
| |
|
320 |
1
| public void testRemoving() {
|
|
321 |
| |
|
322 |
1
| assertEquals("http://www.w3.org/2001/xlink",
|
|
323 |
| severalNamespaces.getNamespaceURI("xlink")); |
|
324 |
1
| assertEquals("http://www.w3.org/1999/xslt",
|
|
325 |
| severalNamespaces.getNamespaceURI("xsl")); |
|
326 |
1
| assertEquals("http://www.example.com/",
|
|
327 |
| severalNamespaces.getNamespaceURI("")); |
|
328 |
| |
|
329 |
1
| severalNamespaces.removeNamespaceDeclaration("xlink");
|
|
330 |
1
| severalNamespaces.removeNamespaceDeclaration("xsl");
|
|
331 |
1
| severalNamespaces.removeNamespaceDeclaration("");
|
|
332 |
1
| severalNamespaces.removeNamespaceDeclaration("nosuchdeclaration");
|
|
333 |
| |
|
334 |
1
| assertEquals("http://www.w3.org/2001/xlink",
|
|
335 |
| severalNamespaces.getNamespaceURI("xlink")); |
|
336 |
1
| assertNull(severalNamespaces.getNamespaceURI("xsl"));
|
|
337 |
1
| assertEquals("http://www.example.com/",
|
|
338 |
| severalNamespaces.getNamespaceURI("")); |
|
339 |
| |
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
1
| public void testAddSameNamespaceDeclaration() {
|
|
344 |
| |
|
345 |
1
| Element e = new Element("test", "http://www.example.com");
|
|
346 |
| |
|
347 |
1
| try {
|
|
348 |
1
| e.addNamespaceDeclaration("", "http://www.red.com");
|
|
349 |
0
| fail("added conflicting default namespace");
|
|
350 |
| } |
|
351 |
| catch (NamespaceConflictException success) { |
|
352 |
1
| assertNotNull(success.getMessage());
|
|
353 |
| } |
|
354 |
| |
|
355 |
1
| e.addNamespaceDeclaration("", "http://www.example.com");
|
|
356 |
1
| assertEquals("http://www.example.com", e.getNamespaceURI(""));
|
|
357 |
1
| assertEquals(1, e.getNamespaceDeclarationCount());
|
|
358 |
| |
|
359 |
| } |
|
360 |
| |
|
361 |
| |
|
362 |
1
| public void testAddEmptyNamespaceDeclaration() {
|
|
363 |
| |
|
364 |
1
| Element e = new Element("test");
|
|
365 |
| |
|
366 |
1
| try {
|
|
367 |
1
| e.addNamespaceDeclaration("", "http://www.example.com");
|
|
368 |
0
| fail("added conflicting default namespace");
|
|
369 |
| } |
|
370 |
| catch (NamespaceConflictException success) { |
|
371 |
1
| assertNotNull(success.getMessage());
|
|
372 |
| } |
|
373 |
| |
|
374 |
1
| e.setNamespaceURI("http://www.example.com");
|
|
375 |
1
| e.setNamespacePrefix("pre");
|
|
376 |
1
| e.addNamespaceDeclaration("", "http://www.example.net");
|
|
377 |
| |
|
378 |
1
| assertEquals("http://www.example.net", e.getNamespaceURI(""));
|
|
379 |
| |
|
380 |
| } |
|
381 |
| |
|
382 |
| |
|
383 |
1
| public void testAddNullPrefix() {
|
|
384 |
| |
|
385 |
1
| Element e = new Element("test");
|
|
386 |
| |
|
387 |
1
| try {
|
|
388 |
1
| e.addNamespaceDeclaration(null,
|
|
389 |
| "http://www.example.com"); |
|
390 |
0
| fail("added conflicting empty prefix to element in no namespace");
|
|
391 |
| } |
|
392 |
| catch (NamespaceConflictException success) { |
|
393 |
1
| assertNotNull(success.getMessage());
|
|
394 |
| } |
|
395 |
| |
|
396 |
1
| e.setNamespaceURI("http://www.example.com");
|
|
397 |
1
| e.setNamespacePrefix("pre");
|
|
398 |
1
| e.addNamespaceDeclaration(null, "http://www.example.net");
|
|
399 |
| |
|
400 |
1
| assertEquals("http://www.example.net", e.getNamespaceURI(""));
|
|
401 |
| |
|
402 |
| } |
|
403 |
| |
|
404 |
| |
|
405 |
1
| public void testAddNullURI() {
|
|
406 |
1
| Element parent = new Element("parent", "http://www.example.org/");
|
|
407 |
1
| Element e = new Element("pre:test", "http://www.example.com/");
|
|
408 |
1
| parent.appendChild(e);
|
|
409 |
1
| e.addNamespaceDeclaration("", null);
|
|
410 |
1
| assertEquals("", e.getNamespaceURI(""));
|
|
411 |
| } |
|
412 |
| |
|
413 |
| |
|
414 |
1
| public void testRemoveNullPrefix() {
|
|
415 |
1
| Element e = new Element("pre:test", "http://www.example.com/");
|
|
416 |
1
| e.addNamespaceDeclaration("", "http://www.example.net");
|
|
417 |
1
| e.removeNamespaceDeclaration(null);
|
|
418 |
1
| assertEquals("", e.getNamespaceURI(""));
|
|
419 |
| } |
|
420 |
| |
|
421 |
| |
|
422 |
1
| public void testBindXMLNSPrefix() {
|
|
423 |
| |
|
424 |
1
| Element e = new Element("pre:test", "http://www.example.com/");
|
|
425 |
1
| try {
|
|
426 |
1
| e.addNamespaceDeclaration("xmlns", "http://www.example.net");
|
|
427 |
0
| fail("Bound xmlns prefix to http://www.example.net");
|
|
428 |
| } |
|
429 |
| catch (NamespaceConflictException success) { |
|
430 |
1
| assertNotNull(success.getMessage());
|
|
431 |
| } |
|
432 |
| |
|
433 |
| } |
|
434 |
| |
|
435 |
| |
|
436 |
1
| public void testBindXMLNSPrefixToEmptyString() {
|
|
437 |
| |
|
438 |
1
| Element e = new Element("pre:test", "http://www.example.com/");
|
|
439 |
1
| assertEquals("", e.getNamespaceURI("xmlns"));
|
|
440 |
1
| e.addNamespaceDeclaration("xmlns", "");
|
|
441 |
1
| assertEquals("", e.getNamespaceURI("xmlns"));
|
|
442 |
| |
|
443 |
| } |
|
444 |
| |
|
445 |
| |
|
446 |
1
| public void testUndeclareDefaultNamespace() {
|
|
447 |
1
| Element parent = new Element("parent", "http://www.example.org/");
|
|
448 |
1
| Element e2 = new Element("pre:test", "http://www.example.net");
|
|
449 |
1
| parent.appendChild(e2);
|
|
450 |
1
| e2.addNamespaceDeclaration("", "");
|
|
451 |
1
| assertEquals("", e2.getNamespaceURI(""));
|
|
452 |
| } |
|
453 |
| |
|
454 |
| |
|
455 |
1
| public void testForConflictWithDefaultNamespace() {
|
|
456 |
1
| Element e = new Element("test", "http://www.example.net");
|
|
457 |
1
| try {
|
|
458 |
1
| e.addNamespaceDeclaration("", "http://www.example.com");
|
|
459 |
0
| fail("Conflicting default namespace");
|
|
460 |
| } |
|
461 |
| catch (NamespaceConflictException success) { |
|
462 |
1
| assertNotNull(success.getMessage());
|
|
463 |
| } |
|
464 |
| } |
|
465 |
| |
|
466 |
| |
|
467 |
1
| public void testConflictingUndeclarationOfDefaultNamespace() {
|
|
468 |
1
| Element e = new Element("test", "http://www.example.net");
|
|
469 |
1
| try {
|
|
470 |
1
| e.addNamespaceDeclaration("", "");
|
|
471 |
0
| fail("Conflicting undeclaration of default namespace");
|
|
472 |
| } |
|
473 |
| catch (NamespaceConflictException success) { |
|
474 |
1
| assertNotNull(success.getMessage());
|
|
475 |
| } |
|
476 |
| } |
|
477 |
| |
|
478 |
| |
|
479 |
1
| public void testAdding() {
|
|
480 |
| |
|
481 |
1
| try {
|
|
482 |
1
| noNamespaces.addNamespaceDeclaration("",
|
|
483 |
| "http://www.example.com/"); |
|
484 |
0
| fail("added conflicting default namespace");
|
|
485 |
| } |
|
486 |
| catch (NamespaceConflictException success) { |
|
487 |
1
| assertNotNull(success.getMessage());
|
|
488 |
| } |
|
489 |
| |
|
490 |
1
| try {
|
|
491 |
1
| severalNamespaces.addNamespaceDeclaration(
|
|
492 |
| "xlink", "http://www.example.com/"); |
|
493 |
0
| fail("added conflicting attribute prefix namespace");
|
|
494 |
| } |
|
495 |
| catch (NamespaceConflictException success) { |
|
496 |
1
| assertNotNull(success.getMessage());
|
|
497 |
| } |
|
498 |
| |
|
499 |
1
| try {
|
|
500 |
1
| someNamespaces.addNamespaceDeclaration("xsl",
|
|
501 |
| "http://www.example.com/"); |
|
502 |
0
| fail("added conflicting additional prefix namespace");
|
|
503 |
| } |
|
504 |
| catch (NamespaceConflictException success) { |
|
505 |
1
| assertNotNull(success.getMessage());
|
|
506 |
| } |
|
507 |
| |
|
508 |
1
| someNamespaces.addNamespaceDeclaration("foo",
|
|
509 |
| "http://www.example.com/"); |
|
510 |
1
| assertEquals("http://www.example.com/",
|
|
511 |
| someNamespaces.getNamespaceURI("foo")); |
|
512 |
| |
|
513 |
| } |
|
514 |
| |
|
515 |
| |
|
516 |
1
| public void testReplacingNamespaceDeclaration() {
|
|
517 |
| |
|
518 |
1
| assertEquals("http://www.w3.org/2001/xlink",
|
|
519 |
| someNamespaces.getNamespaceURI("xlink")); |
|
520 |
1
| try {
|
|
521 |
1
| someNamespaces.addNamespaceDeclaration("xlink",
|
|
522 |
| "http://www.example.com/"); |
|
523 |
0
| fail("Redeclared without removal");
|
|
524 |
| } |
|
525 |
| catch (NamespaceConflictException success) { |
|
526 |
1
| assertNotNull(success.getMessage());
|
|
527 |
| } |
|
528 |
1
| someNamespaces.removeNamespaceDeclaration("xlink");
|
|
529 |
1
| assertNull(someNamespaces.getNamespaceURI("xlink"));
|
|
530 |
1
| someNamespaces.addNamespaceDeclaration("xlink",
|
|
531 |
| "http://www.example.com/"); |
|
532 |
1
| assertEquals("http://www.example.com/",
|
|
533 |
| someNamespaces.getNamespaceURI("xlink")); |
|
534 |
| |
|
535 |
| |
|
536 |
| } |
|
537 |
| |
|
538 |
| |
|
539 |
1
| public void testSetPrefix() {
|
|
540 |
| |
|
541 |
1
| try {
|
|
542 |
1
| Attribute a = severalNamespaces.getAttribute(0);
|
|
543 |
1
| a.setNamespace("xsl", "http://www.example.com/");
|
|
544 |
0
| fail("added conflicting attribute prefix");
|
|
545 |
| } |
|
546 |
| catch (NamespaceConflictException success) { |
|
547 |
1
| assertNotNull(success.getMessage());
|
|
548 |
| } |
|
549 |
| |
|
550 |
| } |
|
551 |
| |
|
552 |
| |
|
553 |
1
| public void testElementConflict() {
|
|
554 |
| |
|
555 |
1
| Element element = new Element("pre:test",
|
|
556 |
| "http://www.example.com/"); |
|
557 |
1
| element.addNamespaceDeclaration("pre",
|
|
558 |
| "http://www.example.com/"); |
|
559 |
1
| try {
|
|
560 |
1
| element.setNamespaceURI("http://www.yahoo.com");
|
|
561 |
0
| fail("changed to conflicting element namespace");
|
|
562 |
| } |
|
563 |
| catch (NamespaceConflictException success) { |
|
564 |
1
| assertNotNull(success.getMessage());
|
|
565 |
| } |
|
566 |
| |
|
567 |
| } |
|
568 |
| |
|
569 |
| |
|
570 |
1
| public void testAttributeConflict() {
|
|
571 |
| |
|
572 |
1
| Element element = new Element("test");
|
|
573 |
1
| element.addNamespaceDeclaration("pre",
|
|
574 |
| "http://www.example.com/"); |
|
575 |
1
| Attribute a = new Attribute("pre:test",
|
|
576 |
| "http://www.example.com/", "value"); |
|
577 |
1
| element.addAttribute(a);
|
|
578 |
1
| try {
|
|
579 |
1
| a.setNamespace("pre", "http://www.yahoo.com/");
|
|
580 |
0
| fail("changed to conflicting attribute namespace");
|
|
581 |
| } |
|
582 |
| catch (NamespaceConflictException success) { |
|
583 |
1
| assertNotNull(success.getMessage());
|
|
584 |
| } |
|
585 |
| |
|
586 |
| } |
|
587 |
| |
|
588 |
| |
|
589 |
1
| public void testSetAttributePrefix() {
|
|
590 |
| |
|
591 |
1
| try {
|
|
592 |
1
| severalNamespaces.setNamespacePrefix("xlink");
|
|
593 |
0
| fail("added conflicting element prefix");
|
|
594 |
| } |
|
595 |
| catch (NamespaceConflictException success) { |
|
596 |
1
| assertNotNull(success.getMessage());
|
|
597 |
| } |
|
598 |
| |
|
599 |
1
| try {
|
|
600 |
1
| severalNamespaces.setNamespacePrefix("xsl");
|
|
601 |
0
| fail("added conflicting element prefix");
|
|
602 |
| } |
|
603 |
| catch (NamespaceConflictException success) { |
|
604 |
1
| assertNotNull(success.getMessage());
|
|
605 |
| } |
|
606 |
| |
|
607 |
| } |
|
608 |
| |
|
609 |
| |
|
610 |
| |
|
611 |
| |
|
612 |
| |
|
613 |
| |
|
614 |
| |
|
615 |
| |
|
616 |
1
| public void testLaurent() {
|
|
617 |
| |
|
618 |
1
| Element b = new Element("b", "urn:x-xom:b");
|
|
619 |
1
| b.addAttribute(new Attribute("id", "b"));
|
|
620 |
| |
|
621 |
1
| Element c = new Element("c:c", "urn:x-xom:c");
|
|
622 |
1
| c.addAttribute(new Attribute("id", "c"));
|
|
623 |
1
| b.appendChild(c);
|
|
624 |
| |
|
625 |
1
| assertEquals("urn:x-xom:b", b.getNamespaceURI());
|
|
626 |
1
| assertEquals("urn:x-xom:c", c.getNamespaceURI());
|
|
627 |
1
| assertEquals("urn:x-xom:b", b.getNamespaceURI(""));
|
|
628 |
1
| assertEquals("urn:x-xom:b", c.getNamespaceURI(""));
|
|
629 |
1
| assertEquals("urn:x-xom:c", c.getNamespaceURI("c"));
|
|
630 |
| |
|
631 |
| } |
|
632 |
| |
|
633 |
| |
|
634 |
1
| public void testCountNamespaces() {
|
|
635 |
| |
|
636 |
1
| Element html = new Element("html");
|
|
637 |
1
| assertEquals(1, html.getNamespaceDeclarationCount());
|
|
638 |
1
| html.setNamespaceURI("http://www.w3.org/1999/xhtml");
|
|
639 |
1
| assertEquals(1, html.getNamespaceDeclarationCount());
|
|
640 |
1
| html.addAttribute(new Attribute("pre:test",
|
|
641 |
| "http://www.examnple.org/", "value")); |
|
642 |
1
| assertEquals(2, html.getNamespaceDeclarationCount());
|
|
643 |
1
| html.addNamespaceDeclaration("rddl", "http://www.rddl.org");
|
|
644 |
1
| assertEquals(3, html.getNamespaceDeclarationCount());
|
|
645 |
1
| html.addNamespaceDeclaration("xlink",
|
|
646 |
| "http://www.w3.org/2001/xlink"); |
|
647 |
1
| assertEquals(4, html.getNamespaceDeclarationCount());
|
|
648 |
1
| html.addAttribute(new Attribute("xml:space",
|
|
649 |
| "http://www.w3.org/XML/1998/namespace", "default")); |
|
650 |
1
| assertEquals(4, html.getNamespaceDeclarationCount());
|
|
651 |
| |
|
652 |
| } |
|
653 |
| |
|
654 |
| |
|
655 |
1
| public void testGetNamespaceURIByPrefix() {
|
|
656 |
| |
|
657 |
1
| Element a = new Element("a");
|
|
658 |
1
| a.addNamespaceDeclaration("foo", "urn:foo");
|
|
659 |
1
| Element b = new Element("b");
|
|
660 |
1
| Element c = new Element("c");
|
|
661 |
1
| Element d = new Element("foo:d", "urn:foo");
|
|
662 |
1
| a.appendChild(b);
|
|
663 |
1
| b.appendChild(c);
|
|
664 |
1
| c.appendChild(d);
|
|
665 |
| |
|
666 |
1
| assertEquals("urn:foo", a.getNamespaceURI("foo"));
|
|
667 |
1
| assertEquals("urn:foo", b.getNamespaceURI("foo"));
|
|
668 |
1
| assertEquals("urn:foo", c.getNamespaceURI("foo"));
|
|
669 |
1
| assertEquals("urn:foo", d.getNamespaceURI("foo"));
|
|
670 |
| } |
|
671 |
| |
|
672 |
| |
|
673 |
1
| public void testNumbersAllowedInSchemes() {
|
|
674 |
| |
|
675 |
1
| String namespace = "u0123456789:schemespecificdata";
|
|
676 |
1
| Element e = new Element("test", namespace);
|
|
677 |
1
| assertEquals(namespace, e.getNamespaceURI());
|
|
678 |
| |
|
679 |
| } |
|
680 |
| |
|
681 |
| |
|
682 |
1
| public void testPunctuationMarksAllowedInSchemes() {
|
|
683 |
1
| String namespace = "u+-.:schemespecificdata";
|
|
684 |
1
| Element e = new Element("test", namespace);
|
|
685 |
1
| assertEquals(namespace, e.getNamespaceURI());
|
|
686 |
| } |
|
687 |
| |
|
688 |
| |
|
689 |
1
| public void testPunctuationMarksCantStartSchemes() {
|
|
690 |
1
| String namespace = "+:schemespecificdata";
|
|
691 |
1
| try {
|
|
692 |
1
| new Element("test", namespace);
|
|
693 |
0
| fail("Allowed scheme name to start with +");
|
|
694 |
| } |
|
695 |
| catch (MalformedURIException success) { |
|
696 |
1
| assertNotNull(success.getMessage());
|
|
697 |
| } |
|
698 |
| } |
|
699 |
| |
|
700 |
| |
|
701 |
1
| public void testNumbersCantStartSchemes() {
|
|
702 |
| |
|
703 |
1
| String namespace = "8uri:schemespecificdata";
|
|
704 |
1
| try {
|
|
705 |
1
| new Element("test", namespace);
|
|
706 |
0
| fail("Allowed scheme name to start with digit");
|
|
707 |
| } |
|
708 |
| catch (MalformedURIException success) { |
|
709 |
1
| assertNotNull(success.getMessage());
|
|
710 |
| } |
|
711 |
| |
|
712 |
| } |
|
713 |
| |
|
714 |
| |
|
715 |
| |
|
716 |
| |
|
717 |
| |
|
718 |
| |
|
719 |
1
| public void testPathCantStartWithDoubleSlash() {
|
|
720 |
| |
|
721 |
1
| String namespace = "prospero://host.dom//pros/name";
|
|
722 |
1
| try {
|
|
723 |
1
| new Element("test", namespace);
|
|
724 |
0
| fail("Allowed URI with path containing double slash");
|
|
725 |
| } |
|
726 |
| catch (MalformedURIException success) { |
|
727 |
1
| assertNotNull(success.getMessage());
|
|
728 |
| } |
|
729 |
| |
|
730 |
| } |
|
731 |
| |
|
732 |
| |
|
733 |
1
| public void testURIReferenceCantHaveTwoFragmentIDs() {
|
|
734 |
1
| String namespace = "uri:schemespecificdata#test#id";
|
|
735 |
1
| try {
|
|
736 |
1
| new Element("test", namespace);
|
|
737 |
0
| fail("Allowed URI reference to contain multiple fragment IDs");
|
|
738 |
| } |
|
739 |
| catch (MalformedURIException success) { |
|
740 |
1
| assertNotNull(success.getMessage());
|
|
741 |
| } |
|
742 |
| } |
|
743 |
| |
|
744 |
| |
|
745 |
1
| public void testHalfPercentEscape() {
|
|
746 |
1
| String namespace = "http://www.example.org/%ce%a";
|
|
747 |
1
| try {
|
|
748 |
1
| new Element("test", namespace);
|
|
749 |
0
| fail("Allowed path to contain only half a percent escape");
|
|
750 |
| } |
|
751 |
| catch (MalformedURIException success) { |
|
752 |
1
| assertNotNull(success.getMessage());
|
|
753 |
| } |
|
754 |
| |
|
755 |
| } |
|
756 |
| |
|
757 |
| |
|
758 |
1
| public void testUnescapedPercentSign() {
|
|
759 |
| |
|
760 |
1
| String namespace = "http://www.example.org/%ce%";
|
|
761 |
1
| try {
|
|
762 |
1
| new Element("test", namespace);
|
|
763 |
0
| fail("Allowed path to contain only percent");
|
|
764 |
| } |
|
765 |
| catch (MalformedURIException success) { |
|
766 |
1
| assertNotNull(success.getMessage());
|
|
767 |
| } |
|
768 |
| |
|
769 |
| } |
|
770 |
| |
|
771 |
| |
|
772 |
1
| public void testPercentSignFollowedByNonHexDigits() {
|
|
773 |
| |
|
774 |
1
| for (char c = 'G'; c <= '`'; c++) {
|
|
775 |
26
| String namespace = "http://www.example.org/%1" + c + "test/";
|
|
776 |
26
| try {
|
|
777 |
26
| new Element("test", namespace);
|
|
778 |
0
| fail("Allowed malformed namespace URI " + namespace);
|
|
779 |
| } |
|
780 |
| catch (MalformedURIException success) { |
|
781 |
26
| assertNotNull(success.getMessage());
|
|
782 |
| } |
|
783 |
| } |
|
784 |
| |
|
785 |
1
| for (char c = ':'; c <= '@'; c++) {
|
|
786 |
7
| String namespace = "http://www.example.org/%1" + c + "test/";
|
|
787 |
7
| try {
|
|
788 |
7
| new Element("test", namespace);
|
|
789 |
0
| fail("Allowed malformed namespace URI " + namespace);
|
|
790 |
| } |
|
791 |
| catch (MalformedURIException success) { |
|
792 |
7
| assertNotNull(success.getMessage());
|
|
793 |
| } |
|
794 |
| } |
|
795 |
| |
|
796 |
| } |
|
797 |
| |
|
798 |
| |
|
799 |
1
| public void testVariousSchemes() {
|
|
800 |
| |
|
801 |
1
| String[] urls = {
|
|
802 |
| "ftp://example.com", "FTP://example.com/pub/", |
|
803 |
| "MAILTO:elharo@metalab.unc.edu?Subject=XOM%20Namespace", |
|
804 |
| "mailto:elharo@metalab.unc.edu?Subject=XOM%20Namespace", |
|
805 |
| "telnet:namespaces.ibiblio.org", "TELNET:namespaces.ibiblio.org", |
|
806 |
| "gopher://gopher.uminn.edu/", "GOPHER://gopher.uminn.edu/", |
|
807 |
| "uri:urn:nwalsh:namespaces", "URI:urn:nwalsh:namespaces", |
|
808 |
| "news:comp.lang.xml", "NEWS:comp.lang.xml", |
|
809 |
| "wais://wais.example.com:78/database", "WAIS://wais.example.com:78/database", |
|
810 |
| "file://vms.host.edu/disk$user/my/notes/note12345.txt", |
|
811 |
| "FILE://vms.host.edu/disk$user/my/notes/note12345.txt", |
|
812 |
| "z39.50s://melvyl.ucop.edu/cat", "Z39.50S://melvyl.ucop.edu/cat", |
|
813 |
| "z39.50r://melvyl.ucop.edu/mags?elecworld.v30.n19", |
|
814 |
| "Z39.50R://melvyl.ucop.edu/mags?elecworld.v30.n19", |
|
815 |
| "z39.50r://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc", |
|
816 |
| "Z39.50R://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc", |
|
817 |
| "mid:960830.1639@XIson.com/partA.960830.1639@XIson.com", |
|
818 |
| "MID:960830.1639@XIson.com/partA.960830.1639@XIson.com", |
|
819 |
| "cid:foo4*foo1@bar.net", "CID:foo4*foo1@bar.net", |
|
820 |
| "vemmi://zeus.mctel.fr/demo", "VEMMI://zeus.mctel.fr/demo", |
|
821 |
| "vemmi://mctel.fr/demo;$USERDATA=smith;account=1234", |
|
822 |
| "opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6", |
|
823 |
| "OPAQUELOCKTOKEN:f81d4fae-7dec-11d0-a765-00a0c91e6bf6", |
|
824 |
| "fax:+358.555.1234567", "FAX:+358.555.1234567", |
|
825 |
| "modem:+3585551234567;type=v32b?7e1;type=v110", |
|
826 |
| "tel:0w003585551234567;phone-context=+3585551234", |
|
827 |
| "tel:+1234567890;phone-context=+1234;vnd.company.option=foo", |
|
828 |
| "xmlrpc.beeps://stateserver.example.com/NumberToName", |
|
829 |
| "XMLRPC.BEEPS://stateserver.example.com/NumberToName", |
|
830 |
| "h323:user@h323.example.com", "H323:user@h323.example.com", |
|
831 |
| "tn3270://login.example.com" |
|
832 |
| }; |
|
833 |
1
| for (int i = 0; i < urls.length; i++) {
|
|
834 |
41
| Element e = new Element("pre:test", urls[i]);
|
|
835 |
41
| assertEquals(e.getNamespaceURI("pre"), urls[i]);
|
|
836 |
| } |
|
837 |
| |
|
838 |
| } |
|
839 |
| |
|
840 |
| |
|
841 |
1
| public void testPercentEscapes() {
|
|
842 |
| |
|
843 |
| |
|
844 |
| |
|
845 |
1
| for (char c = ' '; c <= '~'; c++) {
|
|
846 |
95
| String url = "http://www.example.com/%" + Integer.toHexString(c) + "test/";
|
|
847 |
95
| Element e = new Element("pre:test", url);
|
|
848 |
95
| assertEquals(url, e.getNamespaceURI());
|
|
849 |
95
| assertEquals(url, e.getNamespaceURI("pre"));
|
|
850 |
| } |
|
851 |
| |
|
852 |
1
| for (char c = ' '; c <= '~'; c++) {
|
|
853 |
95
| String url = "http://www.example.com/%" + Integer.toHexString(c).toUpperCase() + "test/";
|
|
854 |
95
| Element e = new Element("pre:test", url);
|
|
855 |
95
| assertEquals(url, e.getNamespaceURI());
|
|
856 |
95
| assertEquals(url, e.getNamespaceURI("pre"));
|
|
857 |
| } |
|
858 |
| |
|
859 |
| } |
|
860 |
| |
|
861 |
| |
|
862 |
1
| public void testDelims() {
|
|
863 |
| |
|
864 |
1
| String[] delims = {"<", ">", "\""};
|
|
865 |
1
| for (int i = 0; i < delims.length; i++) {
|
|
866 |
3
| String url = "http://www.example.com/" + delims[i] + "/";
|
|
867 |
3
| try {
|
|
868 |
3
| new Element("test", url);
|
|
869 |
0
| fail("Allowed " + url + " as namespace URI");
|
|
870 |
| } |
|
871 |
| catch (MalformedURIException success) { |
|
872 |
3
| assertNotNull(success.getMessage());
|
|
873 |
| } |
|
874 |
| } |
|
875 |
| |
|
876 |
| } |
|
877 |
| |
|
878 |
| |
|
879 |
1
| public void testUnwise() {
|
|
880 |
| |
|
881 |
1
| char[] unwise = {'{', '}', '|', '\\', '^', '`'};
|
|
882 |
1
| for (int i = 0; i < unwise.length; i++) {
|
|
883 |
6
| String url = "http://www.example.com/" + unwise[i] + "/";
|
|
884 |
6
| try {
|
|
885 |
6
| new Element("test", url);
|
|
886 |
0
| fail("Allowed " + url + " as namespace URI");
|
|
887 |
| } |
|
888 |
| catch (MalformedURIException success) { |
|
889 |
6
| assertNotNull(success.getMessage());
|
|
890 |
| } |
|
891 |
| } |
|
892 |
| |
|
893 |
| } |
|
894 |
| |
|
895 |
| |
|
896 |
1
| public void testSetPrefixOnElementInNoNamespace() {
|
|
897 |
| |
|
898 |
1
| Element e = new Element("Seq");
|
|
899 |
1
| try {
|
|
900 |
1
| e.setNamespacePrefix("rdf");
|
|
901 |
0
| fail("Set prefix on element in no namespace");
|
|
902 |
| } |
|
903 |
| catch (NamespaceConflictException success) { |
|
904 |
1
| assertNotNull(success.getMessage());
|
|
905 |
| } |
|
906 |
| |
|
907 |
| } |
|
908 |
| |
|
909 |
| |
|
910 |
| } |