KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import nu.xom.Attribute;
31 import nu.xom.Builder;
32 import nu.xom.Comment;
33 import nu.xom.Document;
34 import nu.xom.Element;
35 import nu.xom.MalformedURIException;
36 import nu.xom.Node;
37 import nu.xom.ParsingException;
38 import nu.xom.Text;
39
40 /**
41  * <p>
42  * Tests the getting and setting of base URI information
43  * on nodes. It's important to note that
44  * this is really a URI, not an IRI. The <code>xml:base</code>
45  * attribute may contain an unescaped URI; i.e. an IRI. However,
46  * the base URI is determined after this is converted to a
47  * real URI with all percent escapes in place. See the <a
48  * HREF="http://www.w3.org/TR/2001/REC-xmlbase-20010627/">XML
49  * Base specification</a> for elucidation of this point.
50  * </p>
51  *
52  * @author Elliotte Rusty Harold
53  * @version 1.0
54  *
55  */

56 public class BaseURITest extends XOMTestCase {
57
58     
59     public BaseURITest(String JavaDoc name) {
60         super(name);
61     }
62
63     
64     private Document doc;
65     private String JavaDoc base1 = "http://www.base1.com/";
66     private String JavaDoc base2 = "http://www.base2.com/";
67     private String JavaDoc base3 = "base3.html";
68     private Builder builder = new Builder();
69     
70     
71     protected void setUp() {
72         
73         Element root = new Element("root");
74         doc = new Document(root);
75         doc.setBaseURI(base1);
76         Element child = new Element("child");
77         root.appendChild(child);
78         child.setBaseURI(base2);
79         child.appendChild(new Comment("here I am"));
80         
81         Element child2 = new Element("child2");
82         root.appendChild(child2);
83  
84         Element child3 = new Element("child3");
85         root.appendChild(child3);
86         child3.addAttribute(new Attribute("xml:base",
87           "http://www.w3.org/XML/1998/namespace", base2));
88  
89         Element child4 = new Element("child4");
90         root.appendChild(child4);
91         child4.addAttribute(new Attribute("xml:base",
92           "http://www.w3.org/XML/1998/namespace", base3));
93  
94     }
95
96      
97     public void testDocBase() {
98         assertEquals(base1, doc.getBaseURI());
99     }
100
101     
102     public void testUnsetBase() {
103         Element root = new Element("test");
104         root.setBaseURI(base1);
105         root.setBaseURI(null);
106         assertEquals("", root.getBaseURI());
107     }
108
109     
110     public void testInheritBaseFromDocument() {
111         Element root = doc.getRootElement();
112         root.setBaseURI("");
113         assertEquals(doc.getBaseURI(), root.getBaseURI());
114     }
115
116     
117     public void testAllowEmptyBase() {
118         Element root = new Element("test");
119         root.setBaseURI(base1);
120         root.setBaseURI("");
121         assertEquals("", root.getBaseURI());
122     }
123
124     
125     public void testIPv6Base() {
126         String JavaDoc ipv6
127           = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/test.xml";
128         Element root = new Element("test");
129         root.setBaseURI(ipv6);
130         assertEquals(ipv6, root.getBaseURI());
131     }
132     
133     
134     public void testBaseWithNonASCIICharacter() {
135         
136         String JavaDoc uri = "http://www.w3.org/\u00A9testing";
137         Element root = new Element("test");
138         try {
139             root.setBaseURI(uri);
140             fail("Allowed base URI containing non-ASCII character");
141         }
142         catch (MalformedURIException success) {
143             assertNotNull(success.getMessage());
144         }
145         
146         root.setBaseURI("http://www.example.org/D%C3%BCrst");
147         assertEquals("http://www.example.org/D%C3%BCrst", root.getBaseURI());
148         
149     }
150
151     
152     public void testDocumentBaseWithNonASCIICharacter() {
153         
154         String JavaDoc uri = "http://www.w3.org/\u00A9testing";
155         Element root = new Element("test");
156         Document doc = new Document(root);
157         try {
158             doc.setBaseURI(uri);
159             fail("Allowed base URI containing non-ASCII character");
160         }
161         catch (MalformedURIException success) {
162             assertNotNull(success.getMessage());
163         }
164         
165         doc.setBaseURI("http://www.example.org/D%C3%BCrst");
166         assertEquals("http://www.example.org/D%C3%BCrst", doc.getBaseURI());
167         
168     }
169
170     
171     public void testUppercaseBase() {
172         String JavaDoc base = "HTTP://WWW.EXAMPLE.COM/TEST.XML";
173         Element root = new Element("test");
174         root.setBaseURI(base);
175         assertEquals(base, root.getBaseURI());
176     }
177
178     
179     public void testASCIILettersWithXMLBaseAttribute() {
180
181         String JavaDoc alphabet = "abcdefghijklmnopqrstuvwxyz";
182
183         String JavaDoc base = "HTTP://WWW.EXAMPLE.COM/" + alphabet;
184         Element root = new Element("test");
185         root.addAttribute(new Attribute("xml:base",
186           "http://www.w3.org/XML/1998/namespace", base));
187         assertEquals(base, root.getBaseURI());
188         
189         base = "HTTP://WWW.EXAMPLE.COM/" + alphabet.toUpperCase(Locale.ENGLISH);
190         root.addAttribute(new Attribute("xml:base",
191           "http://www.w3.org/XML/1998/namespace", base));
192         assertEquals(base, root.getBaseURI());
193         
194     }
195
196     
197     public void testXMLBaseWithParameters() {
198         String JavaDoc base = "scheme://authority/data/name;v=1.1/test.db";
199         Element root = new Element("test");
200         root.addAttribute(new Attribute("xml:base",
201           "http://www.w3.org/XML/1998/namespace", base));
202         assertEquals(base, root.getBaseURI());
203     }
204     
205     
206     public void testXMLBaseWithCommaParameter() {
207         
208         String JavaDoc base = "scheme://authority/data/name,1.1/test.db";
209         Element root = new Element("test");
210         root.addAttribute(new Attribute("xml:base",
211           "http://www.w3.org/XML/1998/namespace", base));
212         assertEquals(base, root.getBaseURI());
213         
214     }
215     
216     
217     // This one appears to be mostly theoretical
218
public void testXMLBaseWithDollarSign() {
219         
220         String JavaDoc base = "scheme://authority/data$important";
221         Element root = new Element("test");
222         root.addAttribute(new Attribute("xml:base",
223           "http://www.w3.org/XML/1998/namespace", base));
224         assertEquals(base, root.getBaseURI());
225         
226     }
227     
228     
229     public void testFragmentIDWithXMLBaseAttribute() {
230
231         String JavaDoc base = "HTTP://WWW.EXAMPLE.COM/#test";
232         Element root = new Element("test");
233         root.addAttribute(new Attribute("xml:base",
234           "http://www.w3.org/XML/1998/namespace", base));
235         assertEquals(base, root.getBaseURI());
236         
237     }
238
239     
240     public void testQueryString() {
241
242         String JavaDoc base = "http://www.example.com/test?name=value&data=important";
243         Element root = new Element("test");
244         root.addAttribute(new Attribute("xml:base",
245           "http://www.w3.org/XML/1998/namespace", base));
246         assertEquals(base, root.getBaseURI());
247         
248     }
249
250     // -" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
251
public void testUnreserved() {
252
253         String JavaDoc unreserved = "-.!~*'()";
254         String JavaDoc base = "http://www.example.com/" + unreserved;
255         Element root = new Element("test");
256         root.addAttribute(new Attribute("xml:base",
257           "http://www.w3.org/XML/1998/namespace", base));
258         assertEquals(base, root.getBaseURI());
259         
260     }
261     
262     
263     public void testDelims() {
264
265         String JavaDoc[] delims = {"<", ">", "\""};
266         for (int i = 0; i < delims.length; i++) {
267             String JavaDoc base = "http://www.example.com/" + delims[i] + "/";
268             Element root = new Element("test");
269             root.addAttribute(new Attribute("xml:base",
270               "http://www.w3.org/XML/1998/namespace", base));
271             assertEquals("http://www.example.com/%"
272               + Integer.toHexString(delims[i].charAt(0)).toUpperCase()
273               + "/", root.getBaseURI());
274         }
275         
276     }
277     
278     
279     public void testUnwise() {
280
281         char[] unwise = {'{', '}', '|', '\\', '^', '`'};
282         for (int i = 0; i < unwise.length; i++) {
283             String JavaDoc base = "http://www.example.com/" + unwise[i] + "/";
284             Element root = new Element("test");
285             root.addAttribute(new Attribute("xml:base",
286               "http://www.w3.org/XML/1998/namespace", base));
287             assertEquals("http://www.example.com/%"
288               + Integer.toHexString(unwise[i]).toUpperCase()
289               + "/", root.getBaseURI());
290         }
291         
292     }
293     
294     
295     public void testBaseWithUnusualParts() {
296         String JavaDoc base = "HTTP://user@WWW.EXAMPLE.COM:65130/TEST-2+final.XML?name=value&name2=value2";
297         Element root = new Element("test");
298         root.setBaseURI(base);
299         assertEquals(base, root.getBaseURI());
300     }
301
302     
303     public void testBaseWithEscapedParts() {
304         String JavaDoc base = "http://www.example.com/test%20test";
305         Element root = new Element("test");
306         root.setBaseURI(base);
307         assertEquals(base, root.getBaseURI());
308     }
309
310     
311     public void testXMLBaseWithPlus() {
312         String JavaDoc base = "http://www.example.com/test+test";
313         Element root = new Element("test");
314         root.addAttribute(new Attribute("xml:base",
315           "http://www.w3.org/XML/1998/namespace", base));
316         assertEquals(base, root.getBaseURI());
317     }
318
319     
320     public void testXMLBaseWithUserInfoWithXMLBaseAttribute() {
321         String JavaDoc base = "http://invited:test@www.example.com/";
322         Element root = new Element("test");
323         root.addAttribute(new Attribute("xml:base",
324           "http://www.w3.org/XML/1998/namespace", base));
325         assertEquals(base, root.getBaseURI());
326     }
327
328     
329     public void testElementWithEmptyXMLBaseAttributeHasSameBaseURIAsDocument() {
330         
331         String JavaDoc base = "http://www.example.com/";
332         Element root = new Element("test");
333         root.addAttribute(new Attribute("xml:base",
334           "http://www.w3.org/XML/1998/namespace", base));
335         
336         Element child = new Element("child");
337         root.appendChild(child);
338         child.addAttribute(new Attribute("xml:base",
339           "http://www.w3.org/XML/1998/namespace", ""));
340         Document doc = new Document(root);
341         doc.setBaseURI("http://www.cafeaulait.org/");
342         assertEquals("http://www.cafeaulait.org/", child.getBaseURI());
343         
344     }
345
346     
347     public void testBaseURIOfElementWithEmptyXMLBaseAttributeIsEmptyStringIfTheresNoActualBaseURI() {
348         
349         String JavaDoc base = "http://www.example.com/";
350         Element root = new Element("test");
351         root.addAttribute(new Attribute("xml:base",
352           "http://www.w3.org/XML/1998/namespace", base));
353         
354         Element child = new Element("child");
355         child.addAttribute(new Attribute("xml:base",
356           "http://www.w3.org/XML/1998/namespace", ""));
357         root.appendChild(child);
358         new Document(root);
359         assertEquals("", child.getBaseURI());
360         
361     }
362
363     
364     public void testXMLBaseWithUnreservedCharacters() {
365         String JavaDoc base = "http://www.example.com/()-_.!~*'";
366         Element root = new Element("test");
367         root.addAttribute(new Attribute("xml:base",
368           "http://www.w3.org/XML/1998/namespace", base));
369         assertEquals(base, root.getBaseURI());
370     }
371
372     
373     public void testXMLBaseWithNonASCIICharacters()
374       throws UnsupportedEncodingException JavaDoc {
375       
376         String JavaDoc omega = "\u03A9";
377         // In UTF-8 %ce%a9
378
String JavaDoc base = "http://www.example.com/" + omega;
379         Element root = new Element("test");
380         root.addAttribute(new Attribute("xml:base",
381           "http://www.w3.org/XML/1998/namespace", base));
382         assertEquals("http://www.example.com/%CE%A9", root.getBaseURI());
383         
384     }
385     
386     
387     public void testBaseWithNonASCIICharacters()
388       throws UnsupportedEncodingException JavaDoc {
389         
390         String JavaDoc base = "http://www.example.com/%ce%a9";
391         Element root = new Element("test");
392         root.setBaseURI(base);
393         assertEquals(base, root.getBaseURI());
394         
395     }
396     
397     
398     public void testBadIPv6Base() {
399         
400         Element root = new Element("test");
401         try {
402             root.setBaseURI(
403               "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]/test.xml#xpointer(/*[1])"
404             );
405             fail("allowed multiple brackets");
406         }
407         catch (MalformedURIException success) {
408             assertNotNull(success.getMessage());
409         }
410
411         try {
412             root.setBaseURI(
413               "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/"
414             );
415             fail("allowed mismatched brackets");
416         }
417         catch (MalformedURIException success) {
418             assertNotNull(success.getMessage());
419         }
420
421         try {
422             root.setBaseURI(
423               "http://]FEDC:BA98:7654:3210:FEDC:BA98:7654:3210[/"
424             );
425             fail("allowed right bracket before left bracket");
426         }
427         catch (MalformedURIException success) {
428             assertNotNull(success.getMessage());
429         }
430
431     }
432
433     
434     public void testAllowEmptyXMLBase() {
435         Element root = doc.getRootElement();
436         root.setBaseURI(base1);
437         root.addAttribute(new Attribute("xml:base",
438           "http://www.w3.org/XML/1998/namespace", ""));
439         assertEquals(base1, root.getBaseURI());
440     }
441
442     
443     public void testFailures() {
444         
445         Element root = doc.getRootElement();
446         
447         try {
448             root.setBaseURI("http://www.w3.org/ testing");
449             fail("Allowed URI containing space");
450         }
451         catch (MalformedURIException success) {
452             assertNotNull(success.getMessage());
453         }
454
455         try {
456             root.setBaseURI("http://www.w3.org/tes%ting");
457             fail("Allowed URI containing %");
458         }
459         catch (MalformedURIException success) {
460             assertNotNull(success.getMessage());
461         }
462
463         try {
464             root.setBaseURI("http://www.w3.org/%Atesting");
465             fail("Allowed URI containing half percent");
466         }
467         catch (MalformedURIException success) {
468             assertNotNull(success.getMessage());
469         }
470
471         try {
472             root.setBaseURI("http://www.w3.org/%A");
473             fail("Allowed URI containing half percent at end of path");
474         }
475         catch (MalformedURIException success) {
476             assertNotNull(success.getMessage());
477         }
478
479
480         try {
481             root.setBaseURI("http://www.w3.org/^testing");
482             fail("Allowed URI containing unwise character");
483         }
484         catch (MalformedURIException success) {
485             assertNotNull(success.getMessage());
486         }
487
488         try {
489             root.setBaseURI("http://www.w3.org/<testing");
490             fail("Allowed URI containing unwise < character");
491         }
492         catch (MalformedURIException success) {
493             assertNotNull(success.getMessage());
494         }
495
496         try {
497             root.setBaseURI("http://www.w3.org/\u0000testing");
498             fail("Allowed URI containing unwise null C0 control character");
499         }
500         catch (MalformedURIException success) {
501             assertNotNull(success.getMessage());
502         }
503
504         try {
505             root.setBaseURI("http://www.w3.org/\u0007testing");
506             fail("Allowed URI containing unwise BEL C0 control character");
507         }
508         catch (MalformedURIException success) {
509             assertNotNull(success.getMessage());
510         }
511
512
513     }
514
515     
516     // Note that the xml:base attribute can contain an IRI,
517
// not a URI, so this is a little different than the failures
518
// on setBaseURI
519
public void testXMLBaseFailures() {
520         
521         Attribute base = new Attribute("xml:base",
522           "http://www.w3.org/XML/1998/namespace", "base.html");
523         Element test = new Element("test");
524         test.addAttribute(base);
525         
526         base.setValue("http://www.w3.org/tes%ting");
527         assertEquals("", test.getBaseURI());
528
529         base.setValue("http://www.w3.org/%Atesting");
530         assertEquals("", test.getBaseURI());
531
532         base.setValue("http://www.w3.org/%A");
533         assertEquals("", test.getBaseURI());
534         
535         base.setValue("http://www.w3.org/%0testing");
536         assertEquals("", test.getBaseURI());
537         
538         base.setValue("http://www.w3.org/%7testing");
539         assertEquals("", test.getBaseURI());
540
541     }
542     
543  
544     public void testSyntacticallyIllegalXMLBaseValuesAreIgnored() {
545         
546         Attribute base = new Attribute("xml:base",
547           "http://www.w3.org/XML/1998/namespace", "base.html");
548         Element test = new Element("test");
549         test.setBaseURI("http://www.example.com/");
550         test.addAttribute(base);
551         
552         base.setValue("http://www.w3.org/tes%ting");
553         assertEquals("http://www.example.com/", test.getBaseURI());
554
555     }
556     
557     
558     // Note that the xml:base attribute can contain an IRI,
559
// not a URI. It may also contain unescaped characters that
560
// need to be escaped. This tests for unescaped values.
561
public void testValuesLegalInXMLBaseButNotInAURI() {
562         
563         Element element = new Element("test");
564         Attribute base = new Attribute("xml:base",
565           "http://www.w3.org/XML/1998/namespace", "base.html");
566         element.addAttribute(base);
567         
568         base.setValue("http://www.w3.org/ testing");
569         assertEquals("http://www.w3.org/%20testing", element.getBaseURI());
570
571         base.setValue("http://www.w3.org/^testing");
572         assertEquals("http://www.w3.org/%5Etesting", element.getBaseURI());
573
574         base.setValue("http://www.w3.org/<testing");
575         assertEquals("http://www.w3.org/%3Ctesting", element.getBaseURI());
576
577     }
578     
579     
580     public void testXMLBaseValuesCanContainPercentEscapes() {
581         
582         Attribute base = new Attribute("xml:base",
583           "http://www.w3.org/XML/1998/namespace", "base.html");
584         Element e = new Element("test");
585         e.addAttribute(base);
586         base.setValue("http://www.w3.org/%20testing");
587         String JavaDoc baseURI = e.getBaseURI();
588         assertEquals("http://www.w3.org/%20testing", baseURI);
589         
590     }
591     
592     
593     public void testInheritBaseFromDoc() {
594         assertEquals(base1, doc.getRootElement().getBaseURI());
595     }
596
597     
598     public void testLoadElementFromDifferentEntity() {
599         assertEquals(base2,
600           doc.getRootElement().getChild(0).getBaseURI());
601     }
602
603     
604     public void testLeafNode() {
605         assertEquals(
606           doc.getRootElement().getChild(0).getBaseURI(),
607           doc.getRootElement().getChild(0).getChild(0).getBaseURI()
608         );
609     }
610
611     
612     public void testLoadElementFromSameEntity() {
613         assertEquals(
614           base1,
615           doc.getRootElement().getFirstChildElement("child2").getBaseURI()
616         );
617     }
618
619     
620     public void testXMLBaseAbsolute() {
621         assertEquals(
622           base2,
623           doc.getRootElement().getFirstChildElement("child3").getBaseURI()
624         );
625     }
626
627     
628     public void testXMLBaseRelative() {
629         Element e = doc.getRootElement().getFirstChildElement("child4");
630         String JavaDoc u = e.getBaseURI();
631         assertEquals("http://www.base1.com/base3.html", u);
632     }
633
634     
635     public void testXMLBaseRelativeWithNoRoot() {
636         
637         Element element = new Element("test");
638         element.addAttribute(new Attribute("xml:base",
639           "http://www.w3.org/XML/1998/namespace", "base.html"));
640         assertEquals("", element.getBaseURI());
641         
642     }
643     
644     
645     public void testRelativeBaseURIsNotAllowed() {
646         
647         Element element = new Element("test");
648         try {
649             element.setBaseURI("base.html");
650             fail("Allowed relative base URI");
651         }
652         catch (MalformedURIException success) {
653             assertTrue(success.getMessage().toLowerCase().indexOf("absolute") >= 0);
654         }
655         
656     }
657
658     
659     public void testRelativeURIResolutionAgainstARedirectedBase()
660       throws IOException JavaDoc, ParsingException {
661         
662         Builder builder = new Builder();
663         Document doc = builder.build(
664           "http://www.ibiblio.org/xml/redirecttest.xml");
665         assertEquals(
666           "http://www.ibiblio.org/xml/redirected/target.xml",
667           doc.getBaseURI()
668         );
669         
670     }
671    
672     
673     public void testParentlessNodesHaveEmptyBaseURIs() {
674         Text t = new Text("data");
675         assertEquals("", t.getBaseURI());
676         
677         Element e = new Element("a");
678         assertEquals("", e.getBaseURI());
679     }
680    
681
682     // Don't use the parent to resolve the relative base URI
683
// when parent and child come from different entities
684
public void testElementsFromDifferentActualBases() {
685         Element parent = new Element("parent");
686         parent.setBaseURI("http://www.cafeconleche.org/");
687         Element child = new Element("child");
688         child.setBaseURI("http://www.example.com/");
689         parent.appendChild(child);
690         child.addAttribute(new Attribute("xml:base",
691           "http://www.w3.org/XML/1998/namespace", "/test/data/"));
692         String JavaDoc base = child.getBaseURI();
693         assertEquals("http://www.example.com/test/data/", base);
694     }
695     
696     
697     public void testBadURIInElementsFromDifferentActualBases() {
698         
699         Element parent = new Element("parent");
700         parent.setBaseURI("http://www.cafeconleche.org/");
701         Element child = new Element("child");
702         parent.appendChild(child);
703         child.addAttribute(new Attribute("xml:base",
704           "http://www.w3.org/XML/1998/namespace",
705           "%GF.html"));
706         String JavaDoc base = child.getBaseURI();
707         assertEquals("http://www.cafeconleche.org/", base);
708         
709     }
710     
711     
712     public void testBadURIInElementsFromSameActualBases() {
713         
714         Element parent = new Element("parent");
715         parent.setBaseURI("http://www.cafeconleche.org/");
716         Element child = new Element("child");
717         child.setBaseURI("http://www.cafeconleche.org/");
718         parent.appendChild(child);
719         child.addAttribute(new Attribute("xml:base",
720           "http://www.w3.org/XML/1998/namespace",
721           "http://www.example.com/%5.html"));
722         assertEquals("http://www.cafeconleche.org/", child.getBaseURI());
723         
724     }
725     
726     
727     public void testBadURIInBaseAttributeWithParent() {
728         
729         Element parent = new Element("parent");
730         parent.setBaseURI("http://www.cafeconleche.org/");
731         Element child = new Element("child");
732         child.setBaseURI("http://www.cafeconleche.org/");
733         parent.appendChild(child);
734         child.addAttribute(new Attribute("xml:base",
735           "http://www.w3.org/XML/1998/namespace",
736           "%TR.html"));
737         assertEquals("http://www.cafeconleche.org/", child.getBaseURI());
738         
739     }
740     
741     
742     public void testHierarchicalURIsWithoutProtocolHandlers() {
743         
744         String JavaDoc[] urls = {
745           "gopher://gopher.uminn.edu/", "GOPHER://gopher.uminn.edu/",
746           "gopher://gopher.uminn.edu", "GOPHER://gopher.uminn.edu",
747           "wais://wais.example.com:78/database", "WAIS://wais.example.com:78/database",
748           "file://vms.host.edu/disk$user/my/notes/note12345.txt",
749           "FILE://vms.host.edu/disk$user/my/notes/note12345.txt",
750           "z39.50s://melvyl.ucop.edu/cat", "Z39.50S://melvyl.ucop.edu/cat",
751           "z39.50r://melvyl.ucop.edu/mags?elecworld.v30.n19",
752           "Z39.50R://melvyl.ucop.edu/mags?elecworld.v30.n19",
753           "z39.50r://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
754           "Z39.50R://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
755           "vemmi://zeus.mctel.fr/demo", "VEMMI://zeus.mctel.fr/demo",
756           "vemmi://mctel.fr/demo;$USERDATA=smith;account=1234",
757           "xmlrpc.beeps://stateserver.example.com/NumberToName",
758           "XMLRPC.BEEPS://stateserver.example.com/NumberToName",
759           "tn3270://login.example.com/"
760         };
761         for (int i = 0; i < urls.length; i++) {
762             Element e = new Element("test");
763             e.addAttribute(new Attribute("xml:base",
764               "http://www.w3.org/XML/1998/namespace",
765               urls[i]));
766             Element child = new Element("child");
767             child.addAttribute(new Attribute("xml:base",
768               "http://www.w3.org/XML/1998/namespace",
769               "TR.html"));
770             e.appendChild(child);
771             String JavaDoc base = child.getBaseURI();
772             assertTrue(urls[i] + " " + base, base.endsWith("/TR.html"));
773             assertTrue(base.indexOf("://") >= 4 );
774         }
775
776     }
777     
778     
779     public void testOpaqueURIs() {
780         
781         String JavaDoc[] urls = {
782           "MAILTO:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
783           "mailto:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
784           "telnet:namespaces.ibiblio.org", "TELNET:namespaces.ibiblio.org",
785           "uri:urn:nwalsh:namespaces", "URI:urn:nwalsh:namespaces",
786           "news:comp.lang.xml", "NEWS:comp.lang.xml",
787           "mid:960830.1639@XIson.com/partA.960830.1639@XIson.com",
788           "MID:960830.1639@XIson.com/partA.960830.1639@XIson.com",
789           "cid:foo4*foo1@bar.net", "CID:foo4*foo1@bar.net",
790           "opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
791           "OPAQUELOCKTOKEN:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
792           "fax:+358.555.1234567", "FAX:+358.555.1234567",
793           "modem:+3585551234567;type=v32b?7e1;type=v110",
794           "tel:0w003585551234567;phone-context=+3585551234",
795           "tel:+1234567890;phone-context=+1234;vnd.company.option=foo",
796           "h323:user@h323.example.com", "H323:user@h323.example.com",
797         };
798         for (int i = 0; i < urls.length; i++) {
799             Element e = new Element("test");
800             e.addAttribute(new Attribute("xml:base",
801               "http://www.w3.org/XML/1998/namespace",
802               urls[i]));
803             Element child = new Element("child");
804             child.addAttribute(new Attribute("xml:base",
805               "http://www.w3.org/XML/1998/namespace",
806               "TR.html"));
807             e.appendChild(child);
808             String JavaDoc base = child.getBaseURI();
809             assertEquals("", base);
810         }
811
812     }
813
814     
815     public void testXMLBaseUsedToResolveHref()
816       throws ParsingException, IOException JavaDoc {
817       
818         File JavaDoc input = new File JavaDoc("data");
819         input = new File JavaDoc(input, "xmlbasetest.xml");
820         Document doc = builder.build(input);
821         Element root = doc.getRootElement();
822         String JavaDoc base = root.getBaseURI();
823         // This constructor only works if we have an absolute URI.
824
// If the test fails an aexception is thrown here. I can't
825
// assert equality with the expected absolute URI because
826
// that varies from one installation to the next
827
new URL JavaDoc(base);
828         assertTrue(base.startsWith("file:/"));
829                 
830     }
831     
832     
833     public void testBuildElementFromSeveralEntities()
834       throws ParsingException, IOException JavaDoc {
835       
836         File JavaDoc input = new File JavaDoc("data");
837         input = new File JavaDoc(input, "BaseURIWithEntitiesTest.xml");
838         Document doc = builder.build(input);
839         Element root = doc.getRootElement();
840         String JavaDoc rootBase = root.getBaseURI();
841         String JavaDoc childBase = root.getChild(0).getBaseURI();
842         assertFalse(rootBase.equals(childBase));
843         assertTrue(childBase.indexOf("entities") > 0);
844                 
845     }
846     
847     
848     public void testReplacedRootRetainsBaseURI() {
849      
850         Element root = new Element("root");
851         Document doc = new Document(root);
852         doc.setBaseURI("http://www.example.com");
853         doc.setRootElement(new Element("data"));
854         assertEquals("http://www.example.com", root.getBaseURI());
855         
856     }
857     
858     
859     public void testDetachedElementRetainsBaseURI() {
860      
861         Element root = new Element("root");
862         Document doc = new Document(root);
863         doc.setBaseURI("http://www.example.com");
864         Element child = new Element("child");
865         root.appendChild(child);
866         child.detach();
867         assertEquals("http://www.example.com", child.getBaseURI());
868         
869     }
870     
871     
872     public void testCopiedElementRetainsBaseURI() {
873      
874         Element root = new Element("root");
875         Document doc = new Document(root);
876         doc.setBaseURI("http://www.example.com");
877         Element child = new Element("child");
878         root.appendChild(child);
879         Node copy = child.copy();
880         assertEquals("http://www.example.com", copy.getBaseURI());
881         
882     }
883     
884     
885     public void testElementRemovedByIndexRetainsBaseURI() {
886      
887         Element root = new Element("root");
888         Document doc = new Document(root);
889         doc.setBaseURI("http://www.example.com");
890         Element child = new Element("child");
891         root.appendChild(child);
892         root.removeChild(0);
893         assertEquals("http://www.example.com", child.getBaseURI());
894         
895     }
896     
897     
898     public void testElementRemovedByReferenceRetainsBaseURI() {
899      
900         Element root = new Element("root");
901         Document doc = new Document(root);
902         doc.setBaseURI("http://www.example.com");
903         Element child = new Element("child");
904         root.appendChild(child);
905         root.removeChild(child);
906         assertEquals("http://www.example.com", child.getBaseURI());
907         
908     }
909     
910     
911     public void testRemovedChildrenRetainBaseURI() {
912      
913         Element root = new Element("root");
914         Document doc = new Document(root);
915         doc.setBaseURI("http://www.example.com");
916         Element child = new Element("child");
917         root.appendChild(child);
918         root.removeChildren();
919         assertEquals("http://www.example.com", child.getBaseURI());
920         
921     }
922     
923     
924     public void testXMLBaseAttributesAreOnlyUsedIfTheyreInTheSameEntity() {
925      
926         Element top = new Element("top");
927         top.addAttribute(new Attribute("xml:base",
928           "http://www.w3.org/XML/1998/namespace",
929           "http://www.example.com/"));
930         top.setBaseURI("http://www.w3.org");
931         Element bottom = new Element("bottom");
932         bottom.setBaseURI("http://www.example.net");
933         top.appendChild(bottom);
934         assertEquals("http://www.example.net", bottom.getBaseURI());
935         
936         top.setBaseURI(null);
937         assertEquals("http://www.example.net", bottom.getBaseURI());
938         
939     }
940     
941     
942     public void testXMLBaseAttributesInTheSameEntityOverrideActualBaseURI() {
943      
944         Element top = new Element("top");
945         top.addAttribute(new Attribute("xml:base",
946           "http://www.w3.org/XML/1998/namespace",
947           "http://www.example.com/"));
948         top.setBaseURI("http://www.w3.org");
949         Element bottom = new Element("bottom");
950         bottom.setBaseURI("http://www.w3.org");
951         top.appendChild(bottom);
952         assertEquals("http://www.example.com/", bottom.getBaseURI());
953         
954     }
955     
956     
957     public void testRelativeBaseURIResolution() {
958      
959         Element root = new Element("root");
960         Attribute baseAttribute = new Attribute("xml:base",
961           "http://www.w3.org/XML/1998/namespace", "http://www.example.com/data/limit/test.xml");
962         root.addAttribute(baseAttribute);
963         Element child = new Element ("child");
964         child.addAttribute(new Attribute("xml:base",
965           "http://www.w3.org/XML/1998/namespace", "child.xml"));
966         root.appendChild(child);
967         assertEquals("http://www.example.com/data/limit/child.xml", child.getBaseURI());
968         
969     }
970     
971     
972     // tests from RFC2396bis
973
public void testRFC2396NormalExamples() {
974         
975         String JavaDoc[] RFC2396bisCases = {
976            "g:h", "g:h",
977            "g", "http://a/b/c/g",
978            "./g", "http://a/b/c/g",
979            "g/", "http://a/b/c/g/",
980            "/g", "http://a/g",
981            "//g", "http://g",
982            "?y", "http://a/b/c/d;p?y",
983            "g?y", "http://a/b/c/g?y",
984            "#s", "http://a/b/c/d;p?q#s",
985            "g#s", "http://a/b/c/g#s",
986            "g?y#s", "http://a/b/c/g?y#s",
987            ";x", "http://a/b/c/;x",
988            "g;x", "http://a/b/c/g;x",
989            "g;x?y#s", "http://a/b/c/g;x?y#s",
990            "", "http://a/b/c/d;p?q",
991            ".", "http://a/b/c/",
992            "./", "http://a/b/c/",
993            "..", "http://a/b/",
994            "../", "http://a/b/",
995            "../g", "http://a/b/g",
996            "../..", "http://a/",
997            "../../", "http://a/",
998            "../../g", "http://a/g"
999         };
1000    
1001        Element root = new Element("root");
1002        Document doc = new Document(root);
1003        doc.setBaseURI("http://a/b/c/d;p?q");
1004        Attribute base = new Attribute("xml:base", "http://www.w3.org/XML/1998/namespace", "g");
1005        root.addAttribute(base);
1006        for (int i = 0; i < RFC2396bisCases.length; i += 2) {
1007            base.setValue(RFC2396bisCases[i]);
1008            assertEquals(RFC2396bisCases[i], RFC2396bisCases[i+1], root.getBaseURI());
1009        }
1010        
1011    }
1012    
1013 
1014    public void testRFC2396AbnormalExamples() {
1015        
1016        String JavaDoc[] RFC2396bisCases = {
1017            "../../../g", "http://a/g",
1018            "../../../../g", "http://a/g",
1019            "/./g", "http://a/g",
1020            "/../g", "http://a/g",
1021            "g.", "http://a/b/c/g.",
1022            ".g", "http://a/b/c/.g",
1023            "g..", "http://a/b/c/g..",
1024            "..g", "http://a/b/c/..g",
1025            "./../g", "http://a/b/g",
1026            "./g/.", "http://a/b/c/g/",
1027            "g/./h", "http://a/b/c/g/h",
1028            "g/../h", "http://a/b/c/h",
1029            "g;x=1/./y", "http://a/b/c/g;x=1/y",
1030            "g;x=1/../y", "http://a/b/c/y",
1031            "g?y/./x", "http://a/b/c/g?y/./x",
1032            "g?y/../x", "http://a/b/c/g?y/../x",
1033            "g#s/./x", "http://a/b/c/g#s/./x",
1034            "g#s/../x", "http://a/b/c/g#s/../x",
1035            "http:g", "http:g"
1036        };
1037    
1038        Element root = new Element("root");
1039        Document doc = new Document(root);
1040        doc.setBaseURI("http://a/b/c/d;p?q");
1041        Attribute base = new Attribute("xml:base", "http://www.w3.org/XML/1998/namespace", "g");
1042        root.addAttribute(base);
1043        for (int i = 0; i < RFC2396bisCases.length; i += 2) {
1044            base.setValue(RFC2396bisCases[i]);
1045            assertEquals(RFC2396bisCases[i], RFC2396bisCases[i+1], root.getBaseURI());
1046        }
1047        
1048    }
1049    
1050}
Popular Tags