0001: /* Copyright 2002-2005 Elliotte Rusty Harold
0002:
0003: This library is free software; you can redistribute it and/or modify
0004: it under the terms of version 2.1 of the GNU Lesser General Public
0005: License as published by the Free Software Foundation.
0006:
0007: This library is distributed in the hope that it will be useful,
0008: but WITHOUT ANY WARRANTY; without even the implied warranty of
0009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0010: GNU Lesser General Public License for more details.
0011:
0012: You should have received a copy of the GNU Lesser General Public
0013: License along with this library; if not, write to the
0014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
0015: Boston, MA 02111-1307 USA
0016:
0017: You can contact Elliotte Rusty Harold by sending e-mail to
0018: elharo@metalab.unc.edu. Please include the word "XOM" in the
0019: subject line. The XOM home page is located at http://www.xom.nu/
0020: */
0021:
0022: package nu.xom.tests;
0023:
0024: import java.io.File;
0025: import java.io.IOException;
0026: import java.io.UnsupportedEncodingException;
0027: import java.net.URL;
0028: import java.util.Locale;
0029:
0030: import nu.xom.Attribute;
0031: import nu.xom.Builder;
0032: import nu.xom.Comment;
0033: import nu.xom.Document;
0034: import nu.xom.Element;
0035: import nu.xom.MalformedURIException;
0036: import nu.xom.Namespace;
0037: import nu.xom.Node;
0038: import nu.xom.ParsingException;
0039: import nu.xom.Text;
0040:
0041: /**
0042: * <p>
0043: * Tests the getting and setting of base URI information
0044: * on nodes. It's important to note that
0045: * this is really a URI, not an IRI. The <code>xml:base</code>
0046: * attribute may contain an unescaped URI; i.e. an IRI. However,
0047: * the base URI is determined after this is converted to a
0048: * real URI with all percent escapes in place. See the <a
0049: * href="http://www.w3.org/TR/2001/REC-xmlbase-20010627/">XML
0050: * Base specification</a> for elucidation of this point.
0051: * </p>
0052: *
0053: * @author Elliotte Rusty Harold
0054: * @version 1.1b4
0055: *
0056: */
0057: public class BaseURITest extends XOMTestCase {
0058:
0059: public BaseURITest(String name) {
0060: super (name);
0061: }
0062:
0063: private Document doc;
0064: private String base1 = "http://www.base1.com/";
0065: private String base2 = "http://www.base2.com/";
0066: private String base3 = "base3.html";
0067: private Builder builder = new Builder();
0068:
0069: protected void setUp() {
0070:
0071: Element root = new Element("root");
0072: doc = new Document(root);
0073: doc.setBaseURI(base1);
0074: Element child = new Element("child");
0075: root.appendChild(child);
0076: child.setBaseURI(base2);
0077: child.appendChild(new Comment("here I am"));
0078:
0079: Element child2 = new Element("child2");
0080: root.appendChild(child2);
0081:
0082: Element child3 = new Element("child3");
0083: root.appendChild(child3);
0084: child3.addAttribute(new Attribute("xml:base",
0085: Namespace.XML_NAMESPACE, base2));
0086:
0087: Element child4 = new Element("child4");
0088: root.appendChild(child4);
0089: child4.addAttribute(new Attribute("xml:base",
0090: Namespace.XML_NAMESPACE, base3));
0091:
0092: }
0093:
0094: public void testDocBase() {
0095: assertEquals(base1, doc.getBaseURI());
0096: }
0097:
0098: public void testUnsetBase() {
0099: Element root = new Element("test");
0100: root.setBaseURI(base1);
0101: root.setBaseURI(null);
0102: assertEquals("", root.getBaseURI());
0103: }
0104:
0105: public void testInheritBaseFromDocument() {
0106: Element root = doc.getRootElement();
0107: root.setBaseURI("");
0108: assertEquals(doc.getBaseURI(), root.getBaseURI());
0109: }
0110:
0111: public void testAllowEmptyBase() {
0112: Element root = new Element("test");
0113: root.setBaseURI(base1);
0114: root.setBaseURI("");
0115: assertEquals("", root.getBaseURI());
0116: }
0117:
0118: public void testIPv6Base() {
0119: String ipv6 = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/test.xml";
0120: Element root = new Element("test");
0121: root.setBaseURI(ipv6);
0122: assertEquals(ipv6, root.getBaseURI());
0123: }
0124:
0125: public void testBaseWithNonASCIICharacter() {
0126:
0127: String uri = "http://www.w3.org/\u00A9testing";
0128: Element root = new Element("test");
0129: try {
0130: root.setBaseURI(uri);
0131: fail("Allowed base URI containing non-ASCII character");
0132: } catch (MalformedURIException success) {
0133: assertNotNull(success.getMessage());
0134: }
0135:
0136: root.setBaseURI("http://www.example.org/D%C3%BCrst");
0137: assertEquals("http://www.example.org/D%C3%BCrst", root
0138: .getBaseURI());
0139:
0140: }
0141:
0142: public void testDocumentBaseWithNonASCIICharacter() {
0143:
0144: String uri = "http://www.w3.org/\u00A9testing";
0145: Element root = new Element("test");
0146: Document doc = new Document(root);
0147: try {
0148: doc.setBaseURI(uri);
0149: fail("Allowed base URI containing non-ASCII character");
0150: } catch (MalformedURIException success) {
0151: assertNotNull(success.getMessage());
0152: }
0153:
0154: doc.setBaseURI("http://www.example.org/D%C3%BCrst");
0155: assertEquals("http://www.example.org/D%C3%BCrst", doc
0156: .getBaseURI());
0157:
0158: }
0159:
0160: public void testUppercaseBase() {
0161: String base = "HTTP://WWW.EXAMPLE.COM/TEST.XML";
0162: Element root = new Element("test");
0163: root.setBaseURI(base);
0164: assertEquals(base, root.getBaseURI());
0165: }
0166:
0167: public void testASCIILettersWithXMLBaseAttribute() {
0168:
0169: String alphabet = "abcdefghijklmnopqrstuvwxyz";
0170:
0171: String base = "HTTP://WWW.EXAMPLE.COM/" + alphabet;
0172: Element root = new Element("test");
0173: root.addAttribute(new Attribute("xml:base",
0174: Namespace.XML_NAMESPACE, base));
0175: assertEquals(base, root.getBaseURI());
0176:
0177: base = "HTTP://WWW.EXAMPLE.COM/"
0178: + alphabet.toUpperCase(Locale.ENGLISH);
0179: root.addAttribute(new Attribute("xml:base",
0180: Namespace.XML_NAMESPACE, base));
0181: assertEquals(base, root.getBaseURI());
0182:
0183: }
0184:
0185: public void testXMLBaseWithParameters() {
0186: String base = "scheme://authority/data/name;v=1.1/test.db";
0187: Element root = new Element("test");
0188: root.addAttribute(new Attribute("xml:base",
0189: Namespace.XML_NAMESPACE, base));
0190: assertEquals(base, root.getBaseURI());
0191: }
0192:
0193: public void testXMLBaseWithCommaParameter() {
0194:
0195: String base = "scheme://authority/data/name,1.1/test.db";
0196: Element root = new Element("test");
0197: root.addAttribute(new Attribute("xml:base",
0198: Namespace.XML_NAMESPACE, base));
0199: assertEquals(base, root.getBaseURI());
0200:
0201: }
0202:
0203: // This one appears to be mostly theoretical
0204: public void testXMLBaseWithDollarSign() {
0205:
0206: String base = "scheme://authority/data$important";
0207: Element root = new Element("test");
0208: root.addAttribute(new Attribute("xml:base",
0209: Namespace.XML_NAMESPACE, base));
0210: assertEquals(base, root.getBaseURI());
0211:
0212: }
0213:
0214: public void testFragmentIDWithXMLBaseAttribute() {
0215:
0216: String base = "HTTP://WWW.EXAMPLE.COM/#test";
0217: Element root = new Element("test");
0218: root.addAttribute(new Attribute("xml:base",
0219: Namespace.XML_NAMESPACE, base));
0220: assertEquals(base, root.getBaseURI());
0221:
0222: }
0223:
0224: public void testQueryString() {
0225:
0226: String base = "http://www.example.com/test?name=value&data=important";
0227: Element root = new Element("test");
0228: root.addAttribute(new Attribute("xml:base",
0229: Namespace.XML_NAMESPACE, base));
0230: assertEquals(base, root.getBaseURI());
0231:
0232: }
0233:
0234: // -" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
0235: public void testUnreserved() {
0236:
0237: String unreserved = "-.!~*'()";
0238: String base = "http://www.example.com/" + unreserved;
0239: Element root = new Element("test");
0240: root.addAttribute(new Attribute("xml:base",
0241: Namespace.XML_NAMESPACE, base));
0242: assertEquals(base, root.getBaseURI());
0243:
0244: }
0245:
0246: public void testDelims() {
0247:
0248: String[] delims = { "<", ">", "\"" };
0249: for (int i = 0; i < delims.length; i++) {
0250: String base = "http://www.example.com/" + delims[i] + "/";
0251: Element root = new Element("test");
0252: root.addAttribute(new Attribute("xml:base",
0253: Namespace.XML_NAMESPACE, base));
0254: assertEquals("http://www.example.com/%"
0255: + Integer.toHexString(delims[i].charAt(0))
0256: .toUpperCase() + "/", root.getBaseURI());
0257: }
0258:
0259: }
0260:
0261: public void testUnwise() {
0262:
0263: char[] unwise = { '{', '}', '|', '\\', '^', '`' };
0264: for (int i = 0; i < unwise.length; i++) {
0265: String base = "http://www.example.com/" + unwise[i] + "/";
0266: Element root = new Element("test");
0267: root.addAttribute(new Attribute("xml:base",
0268: Namespace.XML_NAMESPACE, base));
0269: assertEquals("http://www.example.com/%"
0270: + Integer.toHexString(unwise[i]).toUpperCase()
0271: + "/", root.getBaseURI());
0272: }
0273:
0274: }
0275:
0276: public void testBaseWithUnusualParts() {
0277: String base = "HTTP://user@WWW.EXAMPLE.COM:65130/TEST-2+final.XML?name=value&name2=value2";
0278: Element root = new Element("test");
0279: root.setBaseURI(base);
0280: assertEquals(base, root.getBaseURI());
0281: }
0282:
0283: public void testBaseWithEscapedParts() {
0284: String base = "http://www.example.com/test%20test";
0285: Element root = new Element("test");
0286: root.setBaseURI(base);
0287: assertEquals(base, root.getBaseURI());
0288: }
0289:
0290: public void testXMLBaseWithPlus() {
0291: String base = "http://www.example.com/test+test";
0292: Element root = new Element("test");
0293: root.addAttribute(new Attribute("xml:base",
0294: Namespace.XML_NAMESPACE, base));
0295: assertEquals(base, root.getBaseURI());
0296: }
0297:
0298: public void testXMLBaseWithUserInfoWithXMLBaseAttribute() {
0299: String base = "http://invited:test@www.example.com/";
0300: Element root = new Element("test");
0301: root.addAttribute(new Attribute("xml:base",
0302: Namespace.XML_NAMESPACE, base));
0303: assertEquals(base, root.getBaseURI());
0304: }
0305:
0306: public void testElementWithEmptyXMLBaseAttributeHasSameBaseURIAsDocument() {
0307:
0308: String base = "http://www.example.com/";
0309: Element root = new Element("test");
0310: root.addAttribute(new Attribute("xml:base",
0311: Namespace.XML_NAMESPACE, base));
0312:
0313: Element child = new Element("child");
0314: root.appendChild(child);
0315: child.addAttribute(new Attribute("xml:base",
0316: Namespace.XML_NAMESPACE, ""));
0317: Document doc = new Document(root);
0318: doc.setBaseURI("http://www.cafeaulait.org/");
0319: assertEquals("http://www.cafeaulait.org/", child.getBaseURI());
0320:
0321: }
0322:
0323: public void testBaseURIOfElementWithEmptyXMLBaseAttributeIsEmptyStringIfTheresNoActualBaseURI() {
0324:
0325: String base = "http://www.example.com/";
0326: Element root = new Element("test");
0327: root.addAttribute(new Attribute("xml:base",
0328: Namespace.XML_NAMESPACE, base));
0329:
0330: Element child = new Element("child");
0331: child.addAttribute(new Attribute("xml:base",
0332: Namespace.XML_NAMESPACE, ""));
0333: root.appendChild(child);
0334: new Document(root);
0335: assertEquals("", child.getBaseURI());
0336:
0337: }
0338:
0339: public void testXMLBaseWithUnreservedCharacters() {
0340: String base = "http://www.example.com/()-_.!~*'";
0341: Element root = new Element("test");
0342: root.addAttribute(new Attribute("xml:base",
0343: Namespace.XML_NAMESPACE, base));
0344: assertEquals(base, root.getBaseURI());
0345: }
0346:
0347: public void testXMLBaseWithNonASCIICharacters()
0348: throws UnsupportedEncodingException {
0349:
0350: String omega = "\u03A9";
0351: // In UTF-8 %ce%a9
0352: String base = "http://www.example.com/" + omega;
0353: Element root = new Element("test");
0354: root.addAttribute(new Attribute("xml:base",
0355: Namespace.XML_NAMESPACE, base));
0356: assertEquals("http://www.example.com/%CE%A9", root.getBaseURI());
0357:
0358: }
0359:
0360: public void testBaseWithNonASCIICharacters()
0361: throws UnsupportedEncodingException {
0362:
0363: String base = "http://www.example.com/%ce%a9";
0364: Element root = new Element("test");
0365: root.setBaseURI(base);
0366: assertEquals(base, root.getBaseURI());
0367:
0368: }
0369:
0370: public void testBadIPv6Base() {
0371:
0372: Element root = new Element("test");
0373: try {
0374: root
0375: .setBaseURI("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]/test.xml#xpointer(/*[1])");
0376: fail("allowed multiple brackets");
0377: } catch (MalformedURIException success) {
0378: assertNotNull(success.getMessage());
0379: }
0380:
0381: try {
0382: root
0383: .setBaseURI("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/");
0384: fail("allowed mismatched brackets");
0385: } catch (MalformedURIException success) {
0386: assertNotNull(success.getMessage());
0387: }
0388:
0389: try {
0390: root
0391: .setBaseURI("http://]FEDC:BA98:7654:3210:FEDC:BA98:7654:3210[/");
0392: fail("allowed right bracket before left bracket");
0393: } catch (MalformedURIException success) {
0394: assertNotNull(success.getMessage());
0395: }
0396:
0397: }
0398:
0399: public void testAllowEmptyXMLBase() {
0400: Element root = doc.getRootElement();
0401: root.setBaseURI(base1);
0402: root.addAttribute(new Attribute("xml:base",
0403: Namespace.XML_NAMESPACE, ""));
0404: assertEquals(base1, root.getBaseURI());
0405: }
0406:
0407: public void testFailures() {
0408:
0409: Element root = doc.getRootElement();
0410:
0411: try {
0412: root.setBaseURI("http://www.w3.org/ testing");
0413: fail("Allowed URI containing space");
0414: } catch (MalformedURIException success) {
0415: assertNotNull(success.getMessage());
0416: }
0417:
0418: try {
0419: root.setBaseURI("http://www.w3.org/tes%ting");
0420: fail("Allowed URI containing %");
0421: } catch (MalformedURIException success) {
0422: assertNotNull(success.getMessage());
0423: }
0424:
0425: try {
0426: root.setBaseURI("http://www.w3.org/%Atesting");
0427: fail("Allowed URI containing half percent");
0428: } catch (MalformedURIException success) {
0429: assertNotNull(success.getMessage());
0430: }
0431:
0432: try {
0433: root.setBaseURI("http://www.w3.org/%A");
0434: fail("Allowed URI containing half percent at end of path");
0435: } catch (MalformedURIException success) {
0436: assertNotNull(success.getMessage());
0437: }
0438:
0439: try {
0440: root.setBaseURI("http://www.w3.org/^testing");
0441: fail("Allowed URI containing unwise character");
0442: } catch (MalformedURIException success) {
0443: assertNotNull(success.getMessage());
0444: }
0445:
0446: try {
0447: root.setBaseURI("http://www.w3.org/<testing");
0448: fail("Allowed URI containing unwise < character");
0449: } catch (MalformedURIException success) {
0450: assertNotNull(success.getMessage());
0451: }
0452:
0453: try {
0454: root.setBaseURI("http://www.w3.org/\u0000testing");
0455: fail("Allowed URI containing unwise null C0 control character");
0456: } catch (MalformedURIException success) {
0457: assertNotNull(success.getMessage());
0458: }
0459:
0460: try {
0461: root.setBaseURI("http://www.w3.org/\u0007testing");
0462: fail("Allowed URI containing unwise BEL C0 control character");
0463: } catch (MalformedURIException success) {
0464: assertNotNull(success.getMessage());
0465: }
0466:
0467: }
0468:
0469: // Note that the xml:base attribute can contain an IRI,
0470: // not a URI, so this is a little different than the failures
0471: // on setBaseURI
0472: public void testXMLBaseFailures() {
0473:
0474: Attribute base = new Attribute("xml:base",
0475: Namespace.XML_NAMESPACE, "base.html");
0476: Element test = new Element("test");
0477: test.addAttribute(base);
0478:
0479: base.setValue("http://www.w3.org/tes%ting");
0480: assertEquals("", test.getBaseURI());
0481:
0482: base.setValue("http://www.w3.org/%Atesting");
0483: assertEquals("", test.getBaseURI());
0484:
0485: base.setValue("http://www.w3.org/%A");
0486: assertEquals("", test.getBaseURI());
0487:
0488: base.setValue("http://www.w3.org/%0testing");
0489: assertEquals("", test.getBaseURI());
0490:
0491: base.setValue("http://www.w3.org/%7testing");
0492: assertEquals("", test.getBaseURI());
0493:
0494: }
0495:
0496: public void testSyntacticallyIllegalXMLBaseValuesAreIgnored() {
0497:
0498: Attribute base = new Attribute("xml:base",
0499: Namespace.XML_NAMESPACE, "base.html");
0500: Element test = new Element("test");
0501: test.setBaseURI("http://www.example.com/");
0502: test.addAttribute(base);
0503:
0504: base.setValue("http://www.w3.org/tes%ting");
0505: assertEquals("http://www.example.com/", test.getBaseURI());
0506:
0507: }
0508:
0509: // Note that the xml:base attribute can contain an IRI,
0510: // not a URI. It may also contain unescaped characters that
0511: // need to be escaped. This tests for unescaped values.
0512: public void testValuesLegalInXMLBaseButNotInAURI() {
0513:
0514: Element element = new Element("test");
0515: Attribute base = new Attribute("xml:base",
0516: Namespace.XML_NAMESPACE, "base.html");
0517: element.addAttribute(base);
0518:
0519: base.setValue("http://www.w3.org/ testing");
0520: assertEquals("http://www.w3.org/%20testing", element
0521: .getBaseURI());
0522:
0523: base.setValue("http://www.w3.org/^testing");
0524: assertEquals("http://www.w3.org/%5Etesting", element
0525: .getBaseURI());
0526:
0527: base.setValue("http://www.w3.org/<testing");
0528: assertEquals("http://www.w3.org/%3Ctesting", element
0529: .getBaseURI());
0530:
0531: }
0532:
0533: public void testXMLBaseValuesCanContainPercentEscapes() {
0534:
0535: Attribute base = new Attribute("xml:base",
0536: Namespace.XML_NAMESPACE, "base.html");
0537: Element e = new Element("test");
0538: e.addAttribute(base);
0539: base.setValue("http://www.w3.org/%20testing");
0540: String baseURI = e.getBaseURI();
0541: assertEquals("http://www.w3.org/%20testing", baseURI);
0542:
0543: }
0544:
0545: public void testInheritBaseFromDoc() {
0546: assertEquals(base1, doc.getRootElement().getBaseURI());
0547: }
0548:
0549: public void testLoadElementFromDifferentEntity() {
0550: assertEquals(base2, doc.getRootElement().getChild(0)
0551: .getBaseURI());
0552: }
0553:
0554: public void testLeafNode() {
0555: assertEquals(doc.getRootElement().getChild(0).getBaseURI(), doc
0556: .getRootElement().getChild(0).getChild(0).getBaseURI());
0557: }
0558:
0559: public void testLoadElementFromSameEntity() {
0560: assertEquals(base1, doc.getRootElement().getFirstChildElement(
0561: "child2").getBaseURI());
0562: }
0563:
0564: public void testXMLBaseAbsolute() {
0565: assertEquals(base2, doc.getRootElement().getFirstChildElement(
0566: "child3").getBaseURI());
0567: }
0568:
0569: public void testXMLBaseRelative() {
0570: Element e = doc.getRootElement().getFirstChildElement("child4");
0571: String u = e.getBaseURI();
0572: assertEquals("http://www.base1.com/base3.html", u);
0573: }
0574:
0575: public void testXMLBaseRelativeWithNoRoot() {
0576:
0577: Element element = new Element("test");
0578: element.addAttribute(new Attribute("xml:base",
0579: Namespace.XML_NAMESPACE, "base.html"));
0580: assertEquals("", element.getBaseURI());
0581:
0582: }
0583:
0584: public void testRelativeBaseURIsNotAllowed() {
0585:
0586: Element element = new Element("test");
0587: try {
0588: element.setBaseURI("base.html");
0589: fail("Allowed relative base URI");
0590: } catch (MalformedURIException success) {
0591: assertTrue(success.getMessage().toLowerCase().indexOf(
0592: "absolute") >= 0);
0593: }
0594:
0595: }
0596:
0597: public void testRelativeURIResolutionAgainstARedirectedBase()
0598: throws IOException, ParsingException {
0599:
0600: Builder builder = new Builder();
0601: Document doc = builder
0602: .build("http://www.ibiblio.org/xml/redirecttest.xml");
0603: assertEquals(
0604: "http://www.ibiblio.org/xml/redirected/target.xml", doc
0605: .getBaseURI());
0606:
0607: }
0608:
0609: public void testParentlessNodesHaveEmptyBaseURIs() {
0610: Text t = new Text("data");
0611: assertEquals("", t.getBaseURI());
0612:
0613: Element e = new Element("a");
0614: assertEquals("", e.getBaseURI());
0615: }
0616:
0617: // Don't use the parent to resolve the relative base URI
0618: // when parent and child come from different entities
0619: public void testElementsFromDifferentActualBases() {
0620: Element parent = new Element("parent");
0621: parent.setBaseURI("http://www.cafeconleche.org/");
0622: Element child = new Element("child");
0623: child.setBaseURI("http://www.example.com/");
0624: parent.appendChild(child);
0625: child.addAttribute(new Attribute("xml:base",
0626: Namespace.XML_NAMESPACE, "/test/data/"));
0627: String base = child.getBaseURI();
0628: assertEquals("http://www.example.com/test/data/", base);
0629: }
0630:
0631: public void testBadURIInElementsFromDifferentActualBases() {
0632:
0633: Element parent = new Element("parent");
0634: parent.setBaseURI("http://www.cafeconleche.org/");
0635: Element child = new Element("child");
0636: parent.appendChild(child);
0637: child.addAttribute(new Attribute("xml:base",
0638: Namespace.XML_NAMESPACE, "%GF.html"));
0639: String base = child.getBaseURI();
0640: assertEquals("http://www.cafeconleche.org/", base);
0641:
0642: }
0643:
0644: public void testBadURIInElementsFromSameActualBases() {
0645:
0646: Element parent = new Element("parent");
0647: parent.setBaseURI("http://www.cafeconleche.org/");
0648: Element child = new Element("child");
0649: child.setBaseURI("http://www.cafeconleche.org/");
0650: parent.appendChild(child);
0651: child.addAttribute(new Attribute("xml:base",
0652: Namespace.XML_NAMESPACE,
0653: "http://www.example.com/%5.html"));
0654: assertEquals("http://www.cafeconleche.org/", child.getBaseURI());
0655:
0656: }
0657:
0658: public void testBadURIInBaseAttributeWithParent() {
0659:
0660: Element parent = new Element("parent");
0661: parent.setBaseURI("http://www.cafeconleche.org/");
0662: Element child = new Element("child");
0663: child.setBaseURI("http://www.cafeconleche.org/");
0664: parent.appendChild(child);
0665: child.addAttribute(new Attribute("xml:base",
0666: Namespace.XML_NAMESPACE, "%TR.html"));
0667: assertEquals("http://www.cafeconleche.org/", child.getBaseURI());
0668:
0669: }
0670:
0671: public void testHierarchicalURIsWithoutProtocolHandlers() {
0672:
0673: String[] urls = {
0674: "gopher://gopher.uminn.edu/",
0675: "GOPHER://gopher.uminn.edu/",
0676: "gopher://gopher.uminn.edu",
0677: "GOPHER://gopher.uminn.edu",
0678: "wais://wais.example.com:78/database",
0679: "WAIS://wais.example.com:78/database",
0680: "file://vms.host.edu/disk$user/my/notes/note12345.txt",
0681: "FILE://vms.host.edu/disk$user/my/notes/note12345.txt",
0682: "z39.50s://melvyl.ucop.edu/cat",
0683: "Z39.50S://melvyl.ucop.edu/cat",
0684: "z39.50r://melvyl.ucop.edu/mags?elecworld.v30.n19",
0685: "Z39.50R://melvyl.ucop.edu/mags?elecworld.v30.n19",
0686: "z39.50r://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
0687: "Z39.50R://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
0688: "vemmi://zeus.mctel.fr/demo",
0689: "VEMMI://zeus.mctel.fr/demo",
0690: "vemmi://mctel.fr/demo;$USERDATA=smith;account=1234",
0691: "xmlrpc.beeps://stateserver.example.com/NumberToName",
0692: "XMLRPC.BEEPS://stateserver.example.com/NumberToName",
0693: "tn3270://login.example.com/" };
0694: for (int i = 0; i < urls.length; i++) {
0695: Element e = new Element("test");
0696: e.addAttribute(new Attribute("xml:base",
0697: Namespace.XML_NAMESPACE, urls[i]));
0698: Element child = new Element("child");
0699: child.addAttribute(new Attribute("xml:base",
0700: Namespace.XML_NAMESPACE, "TR.html"));
0701: e.appendChild(child);
0702: String base = child.getBaseURI();
0703: assertTrue(urls[i] + " " + base, base.endsWith("/TR.html"));
0704: assertTrue(base.indexOf("://") >= 4);
0705: }
0706:
0707: }
0708:
0709: public void testOpaqueURIs() {
0710:
0711: String[] urls = {
0712: "MAILTO:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
0713: "mailto:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
0714: "telnet:namespaces.ibiblio.org",
0715: "TELNET:namespaces.ibiblio.org",
0716: "uri:urn:nwalsh:namespaces",
0717: "URI:urn:nwalsh:namespaces",
0718: "news:comp.lang.xml",
0719: "NEWS:comp.lang.xml",
0720: "mid:960830.1639@XIson.com/partA.960830.1639@XIson.com",
0721: "MID:960830.1639@XIson.com/partA.960830.1639@XIson.com",
0722: "cid:foo4*foo1@bar.net",
0723: "CID:foo4*foo1@bar.net",
0724: "opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
0725: "OPAQUELOCKTOKEN:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
0726: "fax:+358.555.1234567",
0727: "FAX:+358.555.1234567",
0728: "modem:+3585551234567;type=v32b?7e1;type=v110",
0729: "tel:0w003585551234567;phone-context=+3585551234",
0730: "tel:+1234567890;phone-context=+1234;vnd.company.option=foo",
0731: "h323:user@h323.example.com",
0732: "H323:user@h323.example.com", };
0733: for (int i = 0; i < urls.length; i++) {
0734: Element e = new Element("test");
0735: e.addAttribute(new Attribute("xml:base",
0736: Namespace.XML_NAMESPACE, urls[i]));
0737: Element child = new Element("child");
0738: child.addAttribute(new Attribute("xml:base",
0739: Namespace.XML_NAMESPACE, "TR.html"));
0740: e.appendChild(child);
0741: String base = child.getBaseURI();
0742: assertEquals("", base);
0743: }
0744:
0745: }
0746:
0747: public void testURIStartsWithColon() {
0748:
0749: String url = ":elharo@metalab.unc.edu?Subject=XOM%20Namespace";
0750: Element e = new Element("test");
0751: e.addAttribute(new Attribute("xml:base",
0752: Namespace.XML_NAMESPACE, url));
0753: Element child = new Element("child");
0754: child.addAttribute(new Attribute("xml:base",
0755: Namespace.XML_NAMESPACE, "TR.html"));
0756: e.appendChild(child);
0757: String base = child.getBaseURI();
0758: assertEquals("", base);
0759:
0760: }
0761:
0762: public void testURIStartsWithNumber() {
0763:
0764: String url = "7aelharo@metalab.unc.edu?Subject=XOM%20Namespace";
0765: Element e = new Element("test");
0766: e.addAttribute(new Attribute("xml:base",
0767: Namespace.XML_NAMESPACE, url));
0768: Element child = new Element("child");
0769: child.addAttribute(new Attribute("xml:base",
0770: Namespace.XML_NAMESPACE, "TR.html"));
0771: e.appendChild(child);
0772: String base = child.getBaseURI();
0773: assertEquals("", base);
0774:
0775: }
0776:
0777: public void testURISchemeContainsComma() {
0778:
0779: String url = "foo,foo:elharo@metalab.unc.edu?Subject=XOM%20Namespace";
0780: Element e = new Element("test");
0781: e.addAttribute(new Attribute("xml:base",
0782: Namespace.XML_NAMESPACE, url));
0783: Element child = new Element("child");
0784: child.addAttribute(new Attribute("xml:base",
0785: Namespace.XML_NAMESPACE, "TR.html"));
0786: e.appendChild(child);
0787: String base = child.getBaseURI();
0788: assertEquals("", base);
0789:
0790: }
0791:
0792: public void testXMLBaseUsedToResolveHref() throws ParsingException,
0793: IOException {
0794:
0795: File input = new File("data");
0796: input = new File(input, "xmlbasetest.xml");
0797: Document doc = builder.build(input);
0798: Element root = doc.getRootElement();
0799: String base = root.getBaseURI();
0800: // This constructor only works if we have an absolute URI.
0801: // If the test fails an aexception is thrown here. I can't
0802: // assert equality with the expected absolute URI because
0803: // that varies from one installation to the next
0804: new URL(base);
0805: assertTrue(base.startsWith("file:/"));
0806:
0807: }
0808:
0809: public void testBuildElementFromSeveralEntities()
0810: throws ParsingException, IOException {
0811:
0812: File input = new File("data");
0813: input = new File(input, "BaseURIWithEntitiesTest.xml");
0814: Document doc = builder.build(input);
0815: Element root = doc.getRootElement();
0816: String rootBase = root.getBaseURI();
0817: String childBase = root.getChild(0).getBaseURI();
0818: assertFalse(rootBase.equals(childBase));
0819: assertTrue(childBase.indexOf("entities") > 0);
0820:
0821: }
0822:
0823: public void testReplacedRootRetainsBaseURI() {
0824:
0825: Element root = new Element("root");
0826: Document doc = new Document(root);
0827: doc.setBaseURI("http://www.example.com");
0828: doc.setRootElement(new Element("data"));
0829: assertEquals("http://www.example.com", root.getBaseURI());
0830:
0831: }
0832:
0833: public void testDetachedElementRetainsBaseURI() {
0834:
0835: Element root = new Element("root");
0836: Document doc = new Document(root);
0837: doc.setBaseURI("http://www.example.com");
0838: Element child = new Element("child");
0839: root.appendChild(child);
0840: child.detach();
0841: assertEquals("http://www.example.com", child.getBaseURI());
0842:
0843: }
0844:
0845: public void testCopiedElementRetainsBaseURI() {
0846:
0847: Element root = new Element("root");
0848: Document doc = new Document(root);
0849: doc.setBaseURI("http://www.example.com");
0850: Element child = new Element("child");
0851: root.appendChild(child);
0852: Node copy = child.copy();
0853: assertEquals("http://www.example.com", copy.getBaseURI());
0854:
0855: }
0856:
0857: public void testElementRemovedByIndexRetainsBaseURI() {
0858:
0859: Element root = new Element("root");
0860: Document doc = new Document(root);
0861: doc.setBaseURI("http://www.example.com");
0862: Element child = new Element("child");
0863: root.appendChild(child);
0864: root.removeChild(0);
0865: assertEquals("http://www.example.com", child.getBaseURI());
0866:
0867: }
0868:
0869: public void testElementRemovedByReferenceRetainsBaseURI() {
0870:
0871: Element root = new Element("root");
0872: Document doc = new Document(root);
0873: doc.setBaseURI("http://www.example.com");
0874: Element child = new Element("child");
0875: root.appendChild(child);
0876: root.removeChild(child);
0877: assertEquals("http://www.example.com", child.getBaseURI());
0878:
0879: }
0880:
0881: public void testRemovedChildrenRetainBaseURI() {
0882:
0883: Element root = new Element("root");
0884: Document doc = new Document(root);
0885: doc.setBaseURI("http://www.example.com");
0886: Element child = new Element("child");
0887: root.appendChild(child);
0888: root.removeChildren();
0889: assertEquals("http://www.example.com", child.getBaseURI());
0890:
0891: }
0892:
0893: public void testXMLBaseAttributesAreOnlyUsedIfTheyreInTheSameEntity() {
0894:
0895: Element top = new Element("top");
0896: top.addAttribute(new Attribute("xml:base",
0897: Namespace.XML_NAMESPACE, "http://www.example.com/"));
0898: top.setBaseURI("http://www.w3.org");
0899: Element bottom = new Element("bottom");
0900: bottom.setBaseURI("http://www.example.net");
0901: top.appendChild(bottom);
0902: assertEquals("http://www.example.net", bottom.getBaseURI());
0903:
0904: top.setBaseURI(null);
0905: assertEquals("http://www.example.net", bottom.getBaseURI());
0906:
0907: }
0908:
0909: public void testXMLBaseAttributesInTheSameEntityOverrideActualBaseURI() {
0910:
0911: Element top = new Element("top");
0912: top.addAttribute(new Attribute("xml:base",
0913: Namespace.XML_NAMESPACE, "http://www.example.com/"));
0914: top.setBaseURI("http://www.w3.org");
0915: Element bottom = new Element("bottom");
0916: bottom.setBaseURI("http://www.w3.org");
0917: top.appendChild(bottom);
0918: assertEquals("http://www.example.com/", bottom.getBaseURI());
0919:
0920: }
0921:
0922: public void testRelativeBaseURIResolution() {
0923:
0924: Element root = new Element("root");
0925: Attribute baseAttribute = new Attribute("xml:base",
0926: Namespace.XML_NAMESPACE,
0927: "http://www.example.com/data/limit/test.xml");
0928: root.addAttribute(baseAttribute);
0929: Element child = new Element("child");
0930: child.addAttribute(new Attribute("xml:base",
0931: Namespace.XML_NAMESPACE, "child.xml"));
0932: root.appendChild(child);
0933: assertEquals("http://www.example.com/data/limit/child.xml",
0934: child.getBaseURI());
0935:
0936: }
0937:
0938: public void testRelativeBaseURIResolutionWithNumbers() {
0939:
0940: Element root = new Element("root");
0941: Attribute baseAttribute = new Attribute("xml:base",
0942: Namespace.XML_NAMESPACE,
0943: "http://www.example.com/data/limit/test.xml");
0944: root.addAttribute(baseAttribute);
0945: Element child = new Element("child");
0946: child.addAttribute(new Attribute("xml:base",
0947: Namespace.XML_NAMESPACE, "01test.xml"));
0948: root.appendChild(child);
0949: assertEquals("http://www.example.com/data/limit/01test.xml",
0950: child.getBaseURI());
0951:
0952: }
0953:
0954: public void testRelativeBaseURIStartsWithDotSlash() {
0955:
0956: Element root = new Element("root");
0957: Attribute baseAttribute = new Attribute("xml:base",
0958: Namespace.XML_NAMESPACE,
0959: "http://www.example.com/data/limit/test.xml");
0960: root.addAttribute(baseAttribute);
0961: Element child = new Element("child");
0962: child.addAttribute(new Attribute("xml:base",
0963: Namespace.XML_NAMESPACE, "./test.xml"));
0964: root.appendChild(child);
0965: assertEquals("http://www.example.com/data/limit/test.xml",
0966: child.getBaseURI());
0967:
0968: }
0969:
0970: public void testRelativeBaseURIIsDot() {
0971:
0972: Element root = new Element("root");
0973: Attribute baseAttribute = new Attribute("xml:base",
0974: Namespace.XML_NAMESPACE,
0975: "http://www.example.com/data/limit/test.xml");
0976: root.addAttribute(baseAttribute);
0977: Element child = new Element("child");
0978: child.addAttribute(new Attribute("xml:base",
0979: Namespace.XML_NAMESPACE, "."));
0980: root.appendChild(child);
0981: assertEquals("http://www.example.com/data/limit/", child
0982: .getBaseURI());
0983:
0984: }
0985:
0986: public void testRelativeBaseURIIsDotDot() {
0987:
0988: Element root = new Element("root");
0989: Attribute baseAttribute = new Attribute("xml:base",
0990: Namespace.XML_NAMESPACE,
0991: "http://www.example.com/data/limit/test.xml");
0992: root.addAttribute(baseAttribute);
0993: Element child = new Element("child");
0994: child.addAttribute(new Attribute("xml:base",
0995: Namespace.XML_NAMESPACE, ".."));
0996: root.appendChild(child);
0997: assertEquals("http://www.example.com/data/", child.getBaseURI());
0998:
0999: }
1000:
1001: public void testRelativeBaseURIStartsWithDotDotSlash() {
1002:
1003: Element root = new Element("root");
1004: Attribute baseAttribute = new Attribute("xml:base",
1005: Namespace.XML_NAMESPACE,
1006: "http://www.example.com/data/limit/test.xml");
1007: root.addAttribute(baseAttribute);
1008: Element child = new Element("child");
1009: child.addAttribute(new Attribute("xml:base",
1010: Namespace.XML_NAMESPACE, "../test.xml"));
1011: root.appendChild(child);
1012: assertEquals("http://www.example.com/data/test.xml", child
1013: .getBaseURI());
1014:
1015: }
1016:
1017: public void testAbsoluteBaseURIEndsWithDotDotSlash() {
1018:
1019: Element root = new Element("root");
1020: Attribute baseAttribute = new Attribute("xml:base",
1021: Namespace.XML_NAMESPACE,
1022: "http://www.example.com/data/limit/../");
1023: root.addAttribute(baseAttribute);
1024: Element child = new Element("child");
1025: child.addAttribute(new Attribute("xml:base",
1026: Namespace.XML_NAMESPACE, "test.xml"));
1027: root.appendChild(child);
1028: assertEquals("http://www.example.com/data/test.xml", child
1029: .getBaseURI());
1030:
1031: }
1032:
1033: public void testAbsoluteBaseURIEndsWithDotDotSlashDotDot() {
1034:
1035: Element root = new Element("root");
1036: Attribute baseAttribute = new Attribute("xml:base",
1037: Namespace.XML_NAMESPACE, "http://www.example.com/../..");
1038: root.addAttribute(baseAttribute);
1039: Element child = new Element("child");
1040: child.addAttribute(new Attribute("xml:base",
1041: Namespace.XML_NAMESPACE, "test.xml"));
1042: root.appendChild(child);
1043: assertEquals("http://www.example.com/test.xml", child
1044: .getBaseURI());
1045:
1046: }
1047:
1048: // I'm not sure about this one; need to check????
1049: public void testAbsoluteBaseURIEndsWithDotDot() {
1050:
1051: Element root = new Element("root");
1052: Attribute baseAttribute = new Attribute("xml:base",
1053: Namespace.XML_NAMESPACE,
1054: "http://www.example.com/data/limit/..");
1055: root.addAttribute(baseAttribute);
1056: Element child = new Element("child");
1057: child.addAttribute(new Attribute("xml:base",
1058: Namespace.XML_NAMESPACE, "test.xml"));
1059: root.appendChild(child);
1060: assertEquals("http://www.example.com/data/test.xml", child
1061: .getBaseURI());
1062:
1063: }
1064:
1065: // tests from RFC2396bis
1066: public void testRFC2396NormalExamples() {
1067:
1068: String[] RFC2396bisCases = { "g:h", "g:h", "g",
1069: "http://a/b/c/g", "./g", "http://a/b/c/g", "g/",
1070: "http://a/b/c/g/", "/g", "http://a/g", "//g",
1071: "http://g", "?y", "http://a/b/c/d;p?y", "g?y",
1072: "http://a/b/c/g?y", "#s", "http://a/b/c/d;p?q#s",
1073: "g#s", "http://a/b/c/g#s", "g?y#s",
1074: "http://a/b/c/g?y#s", ";x", "http://a/b/c/;x", "g;x",
1075: "http://a/b/c/g;x", "g;x?y#s", "http://a/b/c/g;x?y#s",
1076: "", "http://a/b/c/d;p?q", ".", "http://a/b/c/", "./",
1077: "http://a/b/c/", "..", "http://a/b/", "../",
1078: "http://a/b/", "../g", "http://a/b/g", "../..",
1079: "http://a/", "../../", "http://a/", "../../g",
1080: "http://a/g" };
1081:
1082: Element root = new Element("root");
1083: Document doc = new Document(root);
1084: doc.setBaseURI("http://a/b/c/d;p?q");
1085: Attribute base = new Attribute("xml:base",
1086: Namespace.XML_NAMESPACE, "g");
1087: root.addAttribute(base);
1088: for (int i = 0; i < RFC2396bisCases.length; i += 2) {
1089: base.setValue(RFC2396bisCases[i]);
1090: assertEquals(RFC2396bisCases[i], RFC2396bisCases[i + 1],
1091: root.getBaseURI());
1092: }
1093:
1094: }
1095:
1096: public void testRFC2396AbnormalExamples() {
1097:
1098: String[] RFC2396bisCases = { "../../../g", "http://a/g",
1099: "../../../../g", "http://a/g", "/./g", "http://a/g",
1100: "/../g", "http://a/g", "g.", "http://a/b/c/g.", ".g",
1101: "http://a/b/c/.g", "g..", "http://a/b/c/g..", "..g",
1102: "http://a/b/c/..g", "./../g", "http://a/b/g", "./g/.",
1103: "http://a/b/c/g/", "g/./h", "http://a/b/c/g/h",
1104: "g/../h", "http://a/b/c/h", "g;x=1/./y",
1105: "http://a/b/c/g;x=1/y", "g;x=1/../y", "http://a/b/c/y",
1106: "g?y/./x", "http://a/b/c/g?y/./x", "g?y/../x",
1107: "http://a/b/c/g?y/../x", "g#s/./x",
1108: "http://a/b/c/g#s/./x", "g#s/../x",
1109: "http://a/b/c/g#s/../x", "http:g", "http:g" };
1110:
1111: Element root = new Element("root");
1112: Document doc = new Document(root);
1113: doc.setBaseURI("http://a/b/c/d;p?q");
1114: Attribute base = new Attribute("xml:base",
1115: Namespace.XML_NAMESPACE, "g");
1116: root.addAttribute(base);
1117: for (int i = 0; i < RFC2396bisCases.length; i += 2) {
1118: base.setValue(RFC2396bisCases[i]);
1119: assertEquals(RFC2396bisCases[i], RFC2396bisCases[i + 1],
1120: root.getBaseURI());
1121: }
1122:
1123: }
1124:
1125: public void testSlashDotDot() {
1126:
1127: Element root = new Element("root");
1128: Document doc = new Document(root);
1129: doc.setBaseURI("http://www.example.com/");
1130: Attribute base = new Attribute("xml:base",
1131: Namespace.XML_NAMESPACE, "g");
1132: root.addAttribute(base);
1133: base.setValue("..");
1134: assertEquals("http://www.example.com/", root.getBaseURI());
1135:
1136: }
1137:
1138: public void testSlashDotDotSlashDotDot() {
1139:
1140: Element root = new Element("root");
1141: Document doc = new Document(root);
1142: doc.setBaseURI("http://www.example.com/");
1143: Attribute base = new Attribute("xml:base",
1144: Namespace.XML_NAMESPACE, "g");
1145: root.addAttribute(base);
1146: base.setValue("../..");
1147: assertEquals("http://www.example.com/", root.getBaseURI());
1148:
1149: }
1150:
1151: public void testSlashDot() {
1152:
1153: Element root = new Element("root");
1154: Document doc = new Document(root);
1155: doc.setBaseURI("http://www.example.com/");
1156: Attribute base = new Attribute("xml:base",
1157: Namespace.XML_NAMESPACE, "g");
1158: root.addAttribute(base);
1159: base.setValue(".");
1160: assertEquals("http://www.example.com/", root.getBaseURI());
1161:
1162: }
1163:
1164: }
|