KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > NamespacesTest


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
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  * <p>
31  * Tests that namespace well-formedness is maintained.
32  * </p>
33  *
34  * @author Elliotte Rusty Harold
35  * @version 1.0
36  *
37  */

38 public class NamespacesTest extends XOMTestCase {
39
40     public NamespacesTest() {
41         super("Namespaces tests");
42     }
43
44     private Element someNamespaces;
45     private Element noNamespaces;
46     private Element severalNamespaces;
47
48
49     protected void setUp() {
50         noNamespaces = new Element("test");
51         
52         someNamespaces = new Element("test");
53         someNamespaces.addNamespaceDeclaration("xlink",
54          "http://www.w3.org/2001/xlink");
55         someNamespaces.addNamespaceDeclaration("xsl",
56          "http://www.w3.org/1999/xslt");
57          
58         severalNamespaces
59           = new Element("test", "http://www.example.com/");
60         severalNamespaces.addAttribute(
61           new Attribute("xlink:type",
62          "http://www.w3.org/2001/xlink", "simple"));
63         severalNamespaces.addNamespaceDeclaration("xsl",
64          "http://www.w3.org/1999/xslt");
65         severalNamespaces.addNamespaceDeclaration("",
66          "http://www.example.com/");
67         severalNamespaces.addNamespaceDeclaration("xlink",
68          "http://www.w3.org/2001/xlink");
69         
70     }
71     
72     
73     public void testSetNamespacePrefixInConflictWithAdditionalNamespaceDeclaration() {
74         
75         someNamespaces.setNamespaceURI("http://www.example.net");
76         try {
77             someNamespaces.setNamespacePrefix("xsl");
78             fail("changed prefix to conflict with additional namespace declaration");
79         }
80         catch (NamespaceConflictException success) {
81             assertNotNull(success.getMessage());
82         }
83         
84     }
85     
86     
87     public void testSetNamespaceURIInConflictWithAdditionalNamespaceDeclaration() {
88         
89         someNamespaces.setNamespaceURI("http://www.w3.org/2001/xlink");
90         someNamespaces.setNamespacePrefix("xlink");
91         try {
92             someNamespaces.setNamespaceURI("http://www.example.net");
93             fail("changed namespace URI to conflict with additional namespace declaration");
94         }
95         catch (NamespaceConflictException ex) {
96             assertNotNull(ex.getMessage());
97         }
98         
99     }
100     
101     
102     public void testXMLNamespace() {
103         
104         assertEquals(
105           "http://www.w3.org/XML/1998/namespace",
106           noNamespaces.getNamespaceURI("xml")
107         );
108         assertEquals(
109           "http://www.w3.org/XML/1998/namespace",
110           severalNamespaces.getNamespaceURI("xml")
111         );
112         
113     }
114     
115     
116     public void testWrongPrefixNotAllowedWithXMLURI() {
117         
118         try {
119             noNamespaces.addNamespaceDeclaration("pre",
120               "http://www.w3.org/XML/1998/namespace");
121             fail("Allowed XML namespace to be associated with non-xml prefix");
122         }
123         catch (NamespaceConflictException success) {
124             assertNotNull(success.getMessage());
125         }
126         
127     }
128     
129     
130     public void testUnmappingPrefix() {
131         
132         try {
133             noNamespaces.addNamespaceDeclaration("pre", "");
134         }
135         catch (MalformedURIException success) {
136             assertNotNull(success.getMessage());
137         }
138         
139     }
140     
141     
142     public void testAllowCapitalSchemes() {
143         noNamespaces.addNamespaceDeclaration("pre", "HTTP://WWW.EXAMPLE.COM/");
144         assertEquals(noNamespaces.getNamespaceURI("pre"), "HTTP://WWW.EXAMPLE.COM/");
145     }
146
147     
148     public void testBadSchemes() {
149         
150         try {
151             noNamespaces.addNamespaceDeclaration("pre", "uri!urn:somedata");
152             fail("Allowed illegal characters in scheme");
153         }
154         catch (MalformedURIException success) {
155             assertNotNull(success.getMessage());
156         }
157     }
158
159
160     public void testXMLPrefixNotAllowedWithWrongURI() {
161         
162         try {
163             noNamespaces.addNamespaceDeclaration("xml", "http://www.example.org/");
164             fail("Allowed xml prefix to be associated with wrong URI");
165         }
166         catch (NamespaceConflictException success) {
167             assertNotNull(success.getMessage());
168         }
169         
170     }
171     
172     
173     public void testXMLNSNamespace() {
174         assertEquals("", noNamespaces.getNamespaceURI("xmlns"));
175         assertEquals("", severalNamespaces.getNamespaceURI("xmlns"));
176     }
177     
178     
179     public void testCantUseXMLNSPrefix() {
180         
181         try {
182             noNamespaces.addNamespaceDeclaration(
183               "xmlns", "http://www.w3.org/2000/xmlns/");
184             fail("added xmlns prefix");
185         }
186         catch (NamespaceConflictException success) {
187             assertNotNull(success.getMessage());
188         }
189         
190         try {
191             noNamespaces.addNamespaceDeclaration("xmlns",
192               "http://www.example.com");
193             fail("added xmlns prefix");
194         }
195         catch (NamespaceConflictException success) {
196             assertNotNull(success.getMessage());
197         }
198         
199     }
200     
201     
202     public void testCantUseXMLPrefix() {
203         
204         try {
205             noNamespaces.addNamespaceDeclaration(
206               "xml", "http://www.example.com");
207             fail("added xmlns prefix");
208         }
209         catch (NamespaceConflictException success) {
210             assertNotNull(success.getMessage());
211         }
212         
213     }
214     
215     
216     public void testCanUseXMLPrefix() {
217         noNamespaces.addNamespaceDeclaration(
218           "xml", "http://www.w3.org/XML/1998/namespace");
219         assertEquals(1, noNamespaces.getNamespaceDeclarationCount());
220     }
221    
222     
223     public void testIndexedAccess() {
224      
225         assertEquals("", noNamespaces.getNamespacePrefix(0));
226         
227         assertNotNull(someNamespaces.getNamespacePrefix(0));
228         assertNotNull(someNamespaces.getNamespacePrefix(1));
229         assertNotNull(someNamespaces.getNamespacePrefix(2));
230         
231         assertNotNull(severalNamespaces.getNamespacePrefix(0));
232         assertNotNull(severalNamespaces.getNamespacePrefix(1));
233         assertNotNull(severalNamespaces.getNamespacePrefix(2));
234         try {
235             severalNamespaces.getNamespacePrefix(3);
236             fail("Got a namespace 3");
237         }
238         catch (IndexOutOfBoundsException JavaDoc ex) {
239            // success;
240
}
241         
242         
243     }
244
245     
246     public void testSize() {
247         assertEquals(1, noNamespaces.getNamespaceDeclarationCount());
248         assertEquals(3, someNamespaces.getNamespaceDeclarationCount());
249         assertEquals(3, severalNamespaces.getNamespaceDeclarationCount());
250     }
251
252     
253     public void testDefaultNamespace() {
254         Element html = new Element("html", "http://www.w3.org/1999/xhtml");
255         assertEquals(1, html.getNamespaceDeclarationCount());
256         assertEquals("", html.getNamespacePrefix(0));
257         assertEquals("http://www.w3.org/1999/xhtml", html.getNamespaceURI(""));
258     }
259
260
261     public void testGetByPrefix() {
262     
263         assertEquals("http://www.w3.org/2001/xlink",
264           someNamespaces.getNamespaceURI("xlink"));
265         assertEquals("http://www.w3.org/1999/xslt",
266           someNamespaces.getNamespaceURI("xsl"));
267         assertNull(someNamespaces.getNamespaceURI("fo"));
268         assertNull(noNamespaces.getNamespaceURI("xsl"));
269         assertEquals("", someNamespaces.getNamespaceURI(""));
270         assertEquals("", noNamespaces.getNamespaceURI(""));
271
272     }
273    
274
275     public void testGetNamespaceDeclarationCount() {
276         Element test = new Element("test");
277         assertEquals(1, test.getNamespaceDeclarationCount());
278         test.setNamespaceURI("http://www.example.com");
279         assertEquals(1, test.getNamespaceDeclarationCount());
280         test.addAttribute(new Attribute("test", "test"));
281         assertEquals(1, test.getNamespaceDeclarationCount());
282         test.addAttribute(new Attribute("xlink:type",
283           "http://www.w3.org/2001/xlink", "value"));
284         assertEquals(2, test.getNamespaceDeclarationCount());
285         test.addAttribute(new Attribute("xlink:href",
286           "http://www.w3.org/2001/xlink", "value"));
287         assertEquals(2, test.getNamespaceDeclarationCount());
288         test.addNamespaceDeclaration("xlink", "http://www.w3.org/2001/xlink");
289         assertEquals(2, test.getNamespaceDeclarationCount());
290         test.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/xmlschema-instance");
291         assertEquals(3, test.getNamespaceDeclarationCount());
292            
293     }
294
295
296     public void testRemoving() {
297     
298         assertEquals("http://www.w3.org/2001/xlink",
299           severalNamespaces.getNamespaceURI("xlink"));
300         assertEquals("http://www.w3.org/1999/xslt",
301           severalNamespaces.getNamespaceURI("xsl"));
302         assertEquals("http://www.example.com/",
303           severalNamespaces.getNamespaceURI(""));
304
305         severalNamespaces.removeNamespaceDeclaration("xlink");
306         severalNamespaces.removeNamespaceDeclaration("xsl");
307         severalNamespaces.removeNamespaceDeclaration("");
308         severalNamespaces.removeNamespaceDeclaration("nosuchdeclaration");
309
310         assertEquals("http://www.w3.org/2001/xlink",
311           severalNamespaces.getNamespaceURI("xlink"));
312         assertNull(severalNamespaces.getNamespaceURI("xsl"));
313         assertEquals("http://www.example.com/",
314           severalNamespaces.getNamespaceURI(""));
315
316     }
317
318     
319     public void testAddSameNamespaceDeclaration() {
320         
321         Element e = new Element("test", "http://www.example.com");
322        
323         try {
324             e.addNamespaceDeclaration("", "http://www.red.com");
325             fail("added conflicting default namespace");
326         }
327         catch (NamespaceConflictException success) {
328             assertNotNull(success.getMessage());
329         }
330        
331         e.addNamespaceDeclaration("", "http://www.example.com");
332         assertEquals("http://www.example.com", e.getNamespaceURI(""));
333         assertEquals(1, e.getNamespaceDeclarationCount());
334         
335     }
336
337     
338     public void testAddEmptyNamespaceDeclaration() {
339         
340         Element e = new Element("test");
341        
342         try {
343             e.addNamespaceDeclaration("", "http://www.example.com");
344             fail("added conflicting default namespace");
345         }
346         catch (NamespaceConflictException success) {
347             assertNotNull(success.getMessage());
348         }
349        
350         e.setNamespaceURI("http://www.example.com");
351         e.setNamespacePrefix("pre");
352         e.addNamespaceDeclaration("", "http://www.example.net");
353     
354         assertEquals("http://www.example.net", e.getNamespaceURI(""));
355        
356     }
357
358     
359     public void testAddNullPrefix() {
360         
361         Element e = new Element("test");
362        
363         try {
364             e.addNamespaceDeclaration(null,
365               "http://www.example.com");
366             fail("added conflicting empty prefix to element in no namespace");
367         }
368         catch (NamespaceConflictException success) {
369             assertNotNull(success.getMessage());
370         }
371        
372         e.setNamespaceURI("http://www.example.com");
373         e.setNamespacePrefix("pre");
374         e.addNamespaceDeclaration(null, "http://www.example.net");
375
376         assertEquals("http://www.example.net", e.getNamespaceURI(""));
377        
378     }
379
380     
381     public void testAddNullURI() {
382         Element parent = new Element("parent", "http://www.example.org/");
383         Element e = new Element("pre:test", "http://www.example.com/");
384         parent.appendChild(e);
385         e.addNamespaceDeclaration("", null);
386         assertEquals("", e.getNamespaceURI(""));
387     }
388
389     
390     public void testRemoveNullPrefix() {
391         Element e = new Element("pre:test", "http://www.example.com/");
392         e.addNamespaceDeclaration("", "http://www.example.net");
393         e.removeNamespaceDeclaration(null);
394         assertEquals("", e.getNamespaceURI(""));
395     }
396
397     
398     public void testBindXMLNSPrefix() {
399         
400         Element e = new Element("pre:test", "http://www.example.com/");
401         try {
402             e.addNamespaceDeclaration("xmlns", "http://www.example.net");
403             fail("Bound xmlns prefix to http://www.example.net");
404         }
405         catch (NamespaceConflictException success) {
406             assertNotNull(success.getMessage());
407         }
408         
409     }
410
411     
412     public void testBindXMLNSPrefixToEmptyString() {
413         
414         Element e = new Element("pre:test", "http://www.example.com/");
415         assertEquals("", e.getNamespaceURI("xmlns"));
416         e.addNamespaceDeclaration("xmlns", "");
417         assertEquals("", e.getNamespaceURI("xmlns"));
418         
419     }
420
421     
422     public void testUndeclareDefaultNamespace() {
423         Element parent = new Element("parent", "http://www.example.org/");
424         Element e2 = new Element("pre:test", "http://www.example.net");
425         parent.appendChild(e2);
426         e2.addNamespaceDeclaration("", "");
427         assertEquals("", e2.getNamespaceURI(""));
428     }
429
430     
431     public void testForConflictWithDefaultNamespace() {
432         Element e = new Element("test", "http://www.example.net");
433         try {
434             e.addNamespaceDeclaration("", "http://www.example.com");
435             fail("Conflicting default namespace");
436         }
437         catch (NamespaceConflictException success) {
438             assertNotNull(success.getMessage());
439         }
440     }
441
442     
443     public void testConflictingUndeclarationOfDefaultNamespace() {
444         Element e = new Element("test", "http://www.example.net");
445         try {
446             e.addNamespaceDeclaration("", "");
447             fail("Conflicting undeclaration of default namespace");
448         }
449         catch (NamespaceConflictException success) {
450             assertNotNull(success.getMessage());
451         }
452     }
453
454     
455     public void testAdding() {
456     
457         try {
458             noNamespaces.addNamespaceDeclaration("",
459               "http://www.example.com/");
460             fail("added conflicting default namespace");
461         }
462         catch (NamespaceConflictException success) {
463             assertNotNull(success.getMessage());
464         }
465
466         try {
467             severalNamespaces.addNamespaceDeclaration(
468               "xlink", "http://www.example.com/");
469             fail("added conflicting attribute prefix namespace");
470         }
471         catch (NamespaceConflictException success) {
472             assertNotNull(success.getMessage());
473         }
474
475         try {
476             someNamespaces.addNamespaceDeclaration("xsl",
477               "http://www.example.com/");
478             fail("added conflicting additional prefix namespace");
479         }
480         catch (NamespaceConflictException success) {
481             assertNotNull(success.getMessage());
482         }
483
484         someNamespaces.addNamespaceDeclaration("foo",
485           "http://www.example.com/");
486         assertEquals("http://www.example.com/",
487           someNamespaces.getNamespaceURI("foo"));
488
489     }
490
491     
492     public void testReplacingNamespaceDeclaration() {
493     
494         assertEquals("http://www.w3.org/2001/xlink",
495           someNamespaces.getNamespaceURI("xlink"));
496         try {
497             someNamespaces.addNamespaceDeclaration("xlink",
498               "http://www.example.com/");
499             fail("Redeclared without removal");
500         }
501         catch (NamespaceConflictException success) {
502             assertNotNull(success.getMessage());
503         }
504         someNamespaces.removeNamespaceDeclaration("xlink");
505         assertNull(someNamespaces.getNamespaceURI("xlink"));
506         someNamespaces.addNamespaceDeclaration("xlink",
507           "http://www.example.com/");
508         assertEquals("http://www.example.com/",
509           someNamespaces.getNamespaceURI("xlink"));
510         
511
512     }
513
514     
515     public void testSetPrefix() {
516     
517         try {
518             Attribute a = severalNamespaces.getAttribute(0);
519             a.setNamespace("xsl", "http://www.example.com/");
520             fail("added conflicting attribute prefix");
521         }
522         catch (NamespaceConflictException success) {
523             assertNotNull(success.getMessage());
524         }
525
526     }
527
528
529     public void testElementConflict() {
530     
531         Element element = new Element("pre:test",
532           "http://www.example.com/");
533         element.addNamespaceDeclaration("pre",
534           "http://www.example.com/");
535         try {
536            element.setNamespaceURI("http://www.yahoo.com");
537            fail("changed to conflicting element namespace");
538         }
539         catch (NamespaceConflictException success) {
540             assertNotNull(success.getMessage());
541         }
542
543     }
544
545     
546     public void testAttributeConflict() {
547     
548         Element element = new Element("test");
549         element.addNamespaceDeclaration("pre",
550           "http://www.example.com/");
551         Attribute a = new Attribute("pre:test",
552           "http://www.example.com/", "value");
553         element.addAttribute(a);
554         try {
555            a.setNamespace("pre", "http://www.yahoo.com/");
556            fail("changed to conflicting attribute namespace");
557         }
558         catch (NamespaceConflictException success) {
559             assertNotNull(success.getMessage());
560         }
561
562     }
563
564
565     public void testSetAttributePrefix() {
566     
567         try {
568            severalNamespaces.setNamespacePrefix("xlink");
569            fail("added conflicting element prefix");
570         }
571         catch (NamespaceConflictException success) {
572             assertNotNull(success.getMessage());
573         }
574
575         try {
576            severalNamespaces.setNamespacePrefix("xsl");
577            fail("added conflicting element prefix");
578         }
579         catch (NamespaceConflictException success) {
580             assertNotNull(success.getMessage());
581         }
582
583     }
584    
585     
586    /*
587     * Test document submitted by Laurent Bihanic
588     <b xmlns="urn:x-xom:b" id="b">
589       <c:c xmlns:c="urn:x-xom:c" id="c" />
590     </b>
591    */

592     public void testLaurent() {
593     
594          Element b = new Element("b", "urn:x-xom:b");
595          b.addAttribute(new Attribute("id", "b"));
596         
597          Element c = new Element("c:c", "urn:x-xom:c");
598          c.addAttribute(new Attribute("id", "c"));
599          b.appendChild(c);
600          
601          assertEquals("urn:x-xom:b", b.getNamespaceURI());
602          assertEquals("urn:x-xom:c", c.getNamespaceURI());
603          assertEquals("urn:x-xom:b", b.getNamespaceURI(""));
604          assertEquals("urn:x-xom:b", c.getNamespaceURI(""));
605          assertEquals("urn:x-xom:c", c.getNamespaceURI("c"));
606      
607     }
608
609     
610     public void testCountNamespaces() {
611         
612         Element html = new Element("html");
613         assertEquals(1, html.getNamespaceDeclarationCount());
614         html.setNamespaceURI("http://www.w3.org/1999/xhtml");
615         assertEquals(1, html.getNamespaceDeclarationCount());
616         html.addAttribute(new Attribute("pre:test",
617           "http://www.examnple.org/", "value"));
618         assertEquals(2, html.getNamespaceDeclarationCount());
619         html.addNamespaceDeclaration("rddl", "http://www.rddl.org");
620         assertEquals(3, html.getNamespaceDeclarationCount());
621         html.addNamespaceDeclaration("xlink",
622           "http://www.w3.org/2001/xlink");
623         assertEquals(4, html.getNamespaceDeclarationCount());
624         html.addAttribute(new Attribute("xml:space",
625           "http://www.w3.org/XML/1998/namespace", "default"));
626         assertEquals(4, html.getNamespaceDeclarationCount());
627         
628     }
629
630     
631     public void testGetNamespaceURIByPrefix() {
632
633         Element a = new Element("a");
634         a.addNamespaceDeclaration("foo", "urn:foo");
635         Element b = new Element("b");
636         Element c = new Element("c");
637         Element d = new Element("foo:d", "urn:foo");
638         a.appendChild(b);
639         b.appendChild(c);
640         c.appendChild(d);
641         
642         assertEquals("urn:foo", a.getNamespaceURI("foo"));
643         assertEquals("urn:foo", b.getNamespaceURI("foo"));
644         assertEquals("urn:foo", c.getNamespaceURI("foo"));
645         assertEquals("urn:foo", d.getNamespaceURI("foo"));
646     }
647     
648     
649     public void testNumbersAllowedInSchemes() {
650         
651         String JavaDoc namespace = "u0123456789:schemespecificdata";
652         Element e = new Element("test", namespace);
653         assertEquals(namespace, e.getNamespaceURI());
654         
655     }
656
657     
658     public void testPunctuationMarksAllowedInSchemes() {
659         String JavaDoc namespace = "u+-.:schemespecificdata";
660         Element e = new Element("test", namespace);
661         assertEquals(namespace, e.getNamespaceURI());
662     }
663
664     
665     public void testPunctuationMarksCantStartSchemes() {
666         String JavaDoc namespace = "+:schemespecificdata";
667         try {
668             new Element("test", namespace);
669             fail("Allowed scheme name to start with +");
670         }
671         catch (MalformedURIException success) {
672             assertNotNull(success.getMessage());
673         }
674     }
675
676     
677     public void testNumbersCantStartSchemes() {
678         
679         String JavaDoc namespace = "8uri:schemespecificdata";
680         try {
681             new Element("test", namespace);
682             fail("Allowed scheme name to start with digit");
683         }
684         catch (MalformedURIException success) {
685             assertNotNull(success.getMessage());
686         }
687         
688     }
689
690     
691     /** This is a very funny test case. RFC 1738 allows URIs like
692      * <code>prospero://host.dom//pros/name</code> but RFC 2396 and
693      * RFC2396bis forbid them.
694      */

695     public void testPathCantStartWithDoubleSlash() {
696         
697         String JavaDoc namespace = "prospero://host.dom//pros/name";
698         try {
699             new Element("test", namespace);
700             fail("Allowed URI with path containing double slash");
701         }
702         catch (MalformedURIException success) {
703             assertNotNull(success.getMessage());
704         }
705         
706     }
707
708     
709     public void testURIReferenceCantHaveTwoFragmentIDs() {
710         String JavaDoc namespace = "uri:schemespecificdata#test#id";
711         try {
712             new Element("test", namespace);
713             fail("Allowed URI reference to contain multiple fragment IDs");
714         }
715         catch (MalformedURIException success) {
716             assertNotNull(success.getMessage());
717         }
718     }
719
720     
721     public void testHalfPercentEscape() {
722         String JavaDoc namespace = "http://www.example.org/%ce%a";
723         try {
724             new Element("test", namespace);
725             fail("Allowed path to contain only half a percent escape");
726         }
727         catch (MalformedURIException success) {
728             assertNotNull(success.getMessage());
729         }
730         
731     }
732
733     
734     public void testUnescapedPercentSign() {
735         
736         String JavaDoc namespace = "http://www.example.org/%ce%";
737         try {
738             new Element("test", namespace);
739             fail("Allowed path to contain only percent");
740         }
741         catch (MalformedURIException success) {
742             assertNotNull(success.getMessage());
743         }
744         
745     }
746
747     
748     public void testPercentSignFollowedByNonHexDigits() {
749         
750         for (char c = 'G'; c <= '`'; c++) {
751             String JavaDoc namespace = "http://www.example.org/%1" + c + "test/";
752             try {
753                 new Element("test", namespace);
754                 fail("Allowed malformed namespace URI " + namespace);
755             }
756             catch (MalformedURIException success) {
757                 assertNotNull(success.getMessage());
758             }
759         }
760         
761         for (char c = ':'; c <= '@'; c++) {
762             String JavaDoc namespace = "http://www.example.org/%1" + c + "test/";
763             try {
764                 new Element("test", namespace);
765                 fail("Allowed malformed namespace URI " + namespace);
766             }
767             catch (MalformedURIException success) {
768                 assertNotNull(success.getMessage());
769             }
770         }
771         
772     }
773
774     
775     public void testVariousSchemes() {
776         
777         String JavaDoc[] urls = {
778           "ftp://example.com", "FTP://example.com/pub/",
779           "MAILTO:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
780           "mailto:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
781           "telnet:namespaces.ibiblio.org", "TELNET:namespaces.ibiblio.org",
782           "gopher://gopher.uminn.edu/", "GOPHER://gopher.uminn.edu/",
783           "uri:urn:nwalsh:namespaces", "URI:urn:nwalsh:namespaces",
784           "news:comp.lang.xml", "NEWS:comp.lang.xml",
785           "wais://wais.example.com:78/database", "WAIS://wais.example.com:78/database",
786           "file://vms.host.edu/disk$user/my/notes/note12345.txt",
787           "FILE://vms.host.edu/disk$user/my/notes/note12345.txt",
788           "z39.50s://melvyl.ucop.edu/cat", "Z39.50S://melvyl.ucop.edu/cat",
789           "z39.50r://melvyl.ucop.edu/mags?elecworld.v30.n19",
790           "Z39.50R://melvyl.ucop.edu/mags?elecworld.v30.n19",
791           "z39.50r://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
792           "Z39.50R://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
793           "mid:960830.1639@XIson.com/partA.960830.1639@XIson.com",
794           "MID:960830.1639@XIson.com/partA.960830.1639@XIson.com",
795           "cid:foo4*foo1@bar.net", "CID:foo4*foo1@bar.net",
796           "vemmi://zeus.mctel.fr/demo", "VEMMI://zeus.mctel.fr/demo",
797           "vemmi://mctel.fr/demo;$USERDATA=smith;account=1234",
798           "opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
799           "OPAQUELOCKTOKEN:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
800           "fax:+358.555.1234567", "FAX:+358.555.1234567",
801           "modem:+3585551234567;type=v32b?7e1;type=v110",
802           "tel:0w003585551234567;phone-context=+3585551234",
803           "tel:+1234567890;phone-context=+1234;vnd.company.option=foo",
804           "xmlrpc.beeps://stateserver.example.com/NumberToName",
805           "XMLRPC.BEEPS://stateserver.example.com/NumberToName",
806           "h323:user@h323.example.com", "H323:user@h323.example.com",
807           "tn3270://login.example.com"
808         };
809         for (int i = 0; i < urls.length; i++) {
810             Element e = new Element("pre:test", urls[i]);
811             assertEquals(e.getNamespaceURI("pre"), urls[i]);
812         }
813         
814     }
815
816     
817     public void testPercentEscapes() {
818
819         // Namespace URIs are compared for direct string equality;
820
// no de-escaping is performed
821
for (char c = ' '; c <= '~'; c++) {
822             String JavaDoc url = "http://www.example.com/%" + Integer.toHexString(c) + "test/";
823             Element e = new Element("pre:test", url);
824             assertEquals(url, e.getNamespaceURI());
825             assertEquals(url, e.getNamespaceURI("pre"));
826         }
827         // repeat in upper case
828
for (char c = ' '; c <= '~'; c++) {
829             String JavaDoc url = "http://www.example.com/%" + Integer.toHexString(c).toUpperCase() + "test/";
830             Element e = new Element("pre:test", url);
831             assertEquals(url, e.getNamespaceURI());
832             assertEquals(url, e.getNamespaceURI("pre"));
833         }
834
835     }
836     
837
838     public void testDelims() {
839
840         String JavaDoc[] delims = {"<", ">", "\""};
841         for (int i = 0; i < delims.length; i++) {
842             String JavaDoc url = "http://www.example.com/" + delims[i] + "/";
843             try {
844                 new Element("test", url);
845                 fail("Allowed " + url + " as namespace URI");
846             }
847             catch (MalformedURIException success) {
848                 assertNotNull(success.getMessage());
849             }
850         }
851         
852     }
853     
854     
855     public void testUnwise() {
856
857         char[] unwise = {'{', '}', '|', '\\', '^', '`'};
858         for (int i = 0; i < unwise.length; i++) {
859             String JavaDoc url = "http://www.example.com/" + unwise[i] + "/";
860             try {
861                 new Element("test", url);
862                 fail("Allowed " + url + " as namespace URI");
863             }
864             catch (MalformedURIException success) {
865                 assertNotNull(success.getMessage());
866             }
867         }
868         
869     }
870     
871     
872     public void testSetPrefixOnElementInNoNamespace() {
873         
874         Element e = new Element("Seq");
875         try {
876             e.setNamespacePrefix("rdf");
877             fail("Set prefix on element in no namespace");
878         }
879         catch (NamespaceConflictException success) {
880             assertNotNull(success.getMessage());
881         }
882         
883     }
884      
885     
886 }
Popular Tags