0001: /* Copyright 2002-2004 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:
0027: import nu.xom.Attribute;
0028: import nu.xom.Builder;
0029: import nu.xom.Comment;
0030: import nu.xom.DocType;
0031: import nu.xom.Document;
0032: import nu.xom.Element;
0033: import nu.xom.Elements;
0034: import nu.xom.IllegalAddException;
0035: import nu.xom.IllegalNameException;
0036: import nu.xom.MalformedURIException;
0037: import nu.xom.MultipleParentException;
0038: import nu.xom.NamespaceConflictException;
0039: import nu.xom.NoSuchAttributeException;
0040: import nu.xom.Node;
0041: import nu.xom.Nodes;
0042: import nu.xom.ParsingException;
0043: import nu.xom.ProcessingInstruction;
0044: import nu.xom.Text;
0045:
0046: /**
0047: * <p>
0048: * Tests for the <code>Element</code> class.
0049: * </p>
0050: *
0051: * @author Elliotte Rusty Harold
0052: * @version 1.1b2
0053: *
0054: */
0055: public class ElementTest extends XOMTestCase {
0056:
0057: private Element element;
0058: private Element child1;
0059: private Text child2;
0060: private Comment child3;
0061: private Element child4;
0062: private Element child5;
0063: private String[] legal = { "http://www.is.edu/sakdsk#sjadh",
0064: "http://www.is.edu/sakdsk?name=value&name=head",
0065: "uri:isbn:0832473864", "http://www.examples.com:80",
0066: "http://www.examples.com:80/",
0067: "http://www.is.edu/%20sakdsk#sjadh" };
0068:
0069: private String[] illegal = { "http://www.is.edu/%sakdsk#sjadh",
0070: "http://www.is.edu/k\u0245kakdsk#sjadh", "!@#$%^&*()",
0071: "fred", "#fred", "/fred" };
0072:
0073: protected void setUp() {
0074:
0075: child1 = new Element("test");
0076: child2 = new Text("test2");
0077: child3 = new Comment("test3");
0078: child4 = new Element("pre:test", "http://www.example.com");
0079: child5 = new Element("test", "http://www.example.com");
0080: element = new Element("name");
0081:
0082: element.appendChild(child1);
0083: element.appendChild(child2);
0084: element.appendChild(child3);
0085: element.appendChild(child4);
0086: element.appendChild(child5);
0087: element.appendChild(" \r\n");
0088:
0089: }
0090:
0091: public ElementTest(String name) {
0092: super (name);
0093: }
0094:
0095: public void testGetChildElementsNull() {
0096:
0097: Elements elements = element.getChildElements("",
0098: "http://www.example.com");
0099: assertEquals(2, elements.size());
0100: elements = element.getChildElements("", "");
0101: assertEquals(1, elements.size());
0102: elements = element.getChildElements(null,
0103: "http://www.example.com");
0104: assertEquals(2, elements.size());
0105: elements = element.getChildElements("", null);
0106: assertEquals(1, elements.size());
0107:
0108: }
0109:
0110: public void testGetFirstChildElement() {
0111:
0112: Element first = element.getFirstChildElement("test");
0113: assertEquals(child1, first);
0114:
0115: first = element.getFirstChildElement("test",
0116: "http://www.example.com");
0117: assertEquals(child4, first);
0118:
0119: assertNull(element.getFirstChildElement("nonesuch"));
0120: assertNull(element.getFirstChildElement("pre:test"));
0121: assertNull(element.getFirstChildElement("nonesuch",
0122: "http://www.example.com"));
0123:
0124: }
0125:
0126: public void testElementNamedXMLNS() {
0127:
0128: String name = "xmlns";
0129: Element e = new Element(name);
0130:
0131: assertEquals(name, e.getLocalName());
0132: assertEquals(name, e.getQualifiedName());
0133: assertEquals("", e.getNamespacePrefix());
0134: assertEquals("", e.getNamespaceURI());
0135:
0136: }
0137:
0138: public void testElementWithPrefixXMLNS() {
0139:
0140: try {
0141: new Element("xmlns:foo", "http://www.example.org/");
0142: fail("Allowed xmlns prefix on element");
0143: } catch (NamespaceConflictException success) {
0144: assertNotNull(success.getMessage());
0145: }
0146:
0147: }
0148:
0149: public void testConstructor1() {
0150:
0151: String name = "Jethro";
0152: Element e = new Element(name);
0153:
0154: assertEquals(name, e.getLocalName());
0155: assertEquals(name, e.getQualifiedName());
0156: assertEquals("", e.getNamespacePrefix());
0157: assertEquals("", e.getNamespaceURI());
0158: }
0159:
0160: public void testConstructor2() {
0161: String name = "sakjdhjhd";
0162: String uri = "http://www.something.com/";
0163: Element e = new Element(name, uri);
0164:
0165: assertEquals(name, e.getLocalName());
0166: assertEquals(name, e.getQualifiedName());
0167: assertEquals("", e.getNamespacePrefix());
0168: assertEquals(uri, e.getNamespaceURI());
0169:
0170: }
0171:
0172: public void testConstructor3() {
0173:
0174: String name = "red:sakjdhjhd";
0175: String uri = "http://www.something.com/";
0176: Element e = new Element(name, uri);
0177:
0178: assertEquals("sakjdhjhd", e.getLocalName());
0179: assertEquals(name, e.getQualifiedName());
0180: assertEquals("red", e.getNamespacePrefix());
0181: assertEquals(uri, e.getNamespaceURI());
0182:
0183: }
0184:
0185: public void testCopyConstructorWithAdditionalNamespaces() {
0186:
0187: Element original = new Element("red");
0188: original.addNamespaceDeclaration("pre",
0189: "http://www.example.org");
0190: Element copy = new Element(original);
0191: assertEquals("http://www.example.org", copy
0192: .getNamespaceURI("pre"));
0193:
0194: }
0195:
0196: public void testAllowEmptyNamespace() {
0197:
0198: String name = "sakjdhjhd";
0199: String uri = "http://www.something.com/";
0200: Element e = new Element(name, uri);
0201:
0202: e.setNamespaceURI("");
0203:
0204: assertEquals("", e.getNamespaceURI());
0205:
0206: }
0207:
0208: public void testToString() {
0209:
0210: String name = "sakjdhjhd";
0211: String uri = "http://www.something.com/";
0212: Element e = new Element(name, uri);
0213:
0214: String s = e.toString();
0215: assertTrue(s.startsWith("[nu.xom.Element: "));
0216: assertTrue(s.endsWith("]"));
0217: assertTrue(s.indexOf(name) != -1);
0218:
0219: }
0220:
0221: public void testToXML() {
0222:
0223: String name = "sakjdhjhd";
0224: String uri = "http://www.something.com/";
0225: Element e = new Element(name, uri);
0226:
0227: String s = e.toXML();
0228: assertTrue(s.endsWith("/>"));
0229: assertTrue(s.startsWith("<" + name));
0230: assertTrue(s.indexOf(uri) != -1);
0231: assertTrue(s.indexOf("xmlns=") != -1);
0232:
0233: }
0234:
0235: public void testToXML2() throws ParsingException, IOException {
0236:
0237: Builder builder = new Builder();
0238: File f = new File("data");
0239: f = new File(f, "soapresponse.xml");
0240: Document doc = builder.build(f);
0241: Element root = doc.getRootElement();
0242: String form = root.toXML();
0243: Document doc2 = builder.build(form, f.toURL().toExternalForm());
0244: Element root2 = doc2.getRootElement();
0245: assertEquals(root, root2);
0246:
0247: }
0248:
0249: public void testToXMLWithXMLLangAttribute() {
0250: Element e = new Element("e");
0251: e.addAttribute(new Attribute("xml:lang",
0252: "http://www.w3.org/XML/1998/namespace", "en"));
0253: assertEquals("<e xml:lang=\"en\" />", e.toXML());
0254: }
0255:
0256: public void testAllowNullNamespace() {
0257: String name = "sakjdhjhd";
0258: String uri = "http://www.something.com/";
0259: Element e = new Element(name, uri);
0260:
0261: e.setNamespaceURI(null);
0262:
0263: assertEquals("", e.getNamespaceURI());
0264:
0265: }
0266:
0267: public void testCantInsertDoctype() {
0268: String name = "sakjdhjhd";
0269: String uri = "http://www.something.com/";
0270: Element e = new Element(name, uri);
0271:
0272: try {
0273: e.appendChild(new DocType(name));
0274: fail("Appended a document type declaration to an element");
0275: } catch (IllegalAddException success) {
0276: assertNotNull(success.getMessage());
0277: }
0278:
0279: }
0280:
0281: public void testXMLNamespace() {
0282:
0283: String name = "red:sakjdhjhd";
0284: String uri = "http://www.red.com/";
0285: Element e = new Element(name, uri);
0286:
0287: String xmlNamespace = "http://www.w3.org/XML/1998/namespace";
0288:
0289: assertEquals(xmlNamespace, e.getNamespaceURI("xml"));
0290:
0291: try {
0292: e.addNamespaceDeclaration("xml", "http://www.yahoo.com/");
0293: fail("remapped xml prefix!");
0294: } catch (NamespaceConflictException success) {
0295: assertNotNull(success.getMessage());
0296: }
0297: assertEquals(e.getNamespaceURI("xml"), xmlNamespace);
0298:
0299: // This should succeed
0300: e.addNamespaceDeclaration("xml", xmlNamespace);
0301: assertEquals(xmlNamespace, e.getNamespaceURI("xml"));
0302:
0303: }
0304:
0305: public void testUndeclareDefaultNamespace() {
0306:
0307: String name = "red:sakjdhjhd";
0308: String uri = "http://www.red.com/";
0309: Element child = new Element(name, uri);
0310: Element parent = new Element("parent", "http://www.example.com");
0311:
0312: assertEquals("http://www.example.com", parent
0313: .getNamespaceURI(""));
0314:
0315: parent.appendChild(child);
0316: child.addNamespaceDeclaration("", "");
0317:
0318: assertEquals("", child.getNamespaceURI(""));
0319: assertEquals("http://www.example.com", parent
0320: .getNamespaceURI(""));
0321:
0322: Element child2 = new Element("name", "http://www.default.com");
0323: parent.appendChild(child2);
0324:
0325: // should not be able to undeclare default namespace on child2
0326: try {
0327: child2.addNamespaceDeclaration("", "");
0328: fail("Illegally undeclared default namespace");
0329: } catch (NamespaceConflictException success) {
0330: assertNotNull(success.getMessage());
0331: }
0332:
0333: }
0334:
0335: public void testUnsetDefaultNamespaceWithAttribute() {
0336:
0337: String name = "sakjdhjhd";
0338: String uri = "http://www.red.com/";
0339: Element element = new Element(name, uri);
0340: element.addAttribute(new Attribute("test", "test"));
0341: element.setNamespaceURI("");
0342: assertEquals("", element.getNamespaceURI(""));
0343:
0344: }
0345:
0346: public void testSetNamespaceWithAttribute() {
0347:
0348: String name = "red:sakjdhjhd";
0349: String uri = "http://www.red.com/";
0350: Element element = new Element(name, uri);
0351: element
0352: .addAttribute(new Attribute("red:attribute", uri,
0353: "test"));
0354: element.setNamespaceURI(uri);
0355: assertEquals(uri, element.getNamespaceURI());
0356: assertEquals(uri, element.getNamespaceURI("red"));
0357:
0358: }
0359:
0360: public void testAddNamespaceToElementWithAttribute() {
0361:
0362: String name = "a";
0363: String uri = "http://www.w3.org/1999/xhtml";
0364: Element element = new Element(name);
0365: element.addAttribute(new Attribute("href",
0366: "http://www.elharo.com"));
0367: element.setNamespaceURI(uri);
0368: assertEquals(uri, element.getNamespaceURI());
0369:
0370: }
0371:
0372: public void testSameNamespaceForElementAndAttribute() {
0373:
0374: String name = "a";
0375: String uri = "http://www.w3.org/1999/xhtml";
0376: Element element = new Element(name);
0377: element.addAttribute(new Attribute("html:href", uri,
0378: "http://www.elharo.com"));
0379: element.setNamespaceURI("http://www.example.com");
0380: element.setNamespacePrefix("pre");
0381: element.setNamespaceURI(uri);
0382: element.setNamespacePrefix("html");
0383: assertEquals(uri, element.getNamespaceURI());
0384: assertEquals("html", element.getNamespacePrefix());
0385:
0386: }
0387:
0388: public void testToXMLWithXMLAttributes() {
0389: Element e = new Element("test");
0390: e.addAttribute(new Attribute("xml:space",
0391: "http://www.w3.org/XML/1998/namespace", "preserve"));
0392: e.addAttribute(new Attribute("zzz:zzz",
0393: "http://www.example.org", "preserve"));
0394: String result = e.toXML();
0395: assertEquals(
0396: "<test xmlns:zzz=\"http://www.example.org\" xml:space=\"preserve\" zzz:zzz=\"preserve\" />",
0397: result);
0398: }
0399:
0400: public void testGetNamespacePrefixInt() {
0401: Element e = new Element("test");
0402: e.addAttribute(new Attribute("xml:space",
0403: "http://www.w3.org/XML/1998/namespace", "preserve"));
0404: e.addAttribute(new Attribute("zzz:zzz",
0405: "http://www.example.org", "preserve"));
0406: assertEquals(2, e.getNamespaceDeclarationCount());
0407: try {
0408: e.getNamespacePrefix(2);
0409: fail("Got prefix beyond bounds");
0410: } catch (IndexOutOfBoundsException success) {
0411: assertNotNull(success.getMessage());
0412: }
0413: }
0414:
0415: // a very weird case but legal
0416: public void testXMLPrefixOnElement() {
0417: Element e = new Element("xml:test",
0418: "http://www.w3.org/XML/1998/namespace");
0419: assertEquals(0, e.getNamespaceDeclarationCount());
0420: assertEquals("<xml:test />", e.toXML());
0421: try {
0422: e.getNamespacePrefix(0);
0423: fail("Got prefix beyond bounds");
0424: } catch (IndexOutOfBoundsException success) {
0425: assertNotNull(success.getMessage());
0426: }
0427: }
0428:
0429: public void testConflictingDefaultNamespace() {
0430:
0431: Element e = new Element("foo", "http://www.foo.com/");
0432: try {
0433: e.addNamespaceDeclaration("", "http://www.bar.com/");
0434: fail("Added conflicting default namespace");
0435: } catch (NamespaceConflictException success) {
0436: String message = success.getMessage();
0437: assertTrue(message.indexOf("default namespace") > 0);
0438: assertTrue(message.indexOf("http://www.foo.com") > 0);
0439: assertTrue(message.indexOf("http://www.bar.com/") > 0);
0440: }
0441:
0442: }
0443:
0444: public void testNamespaceMappings() {
0445:
0446: String name = "red:sakjdhjhd";
0447: String uri = "http://www.red.com/";
0448: Element e = new Element(name, uri);
0449: e.addNamespaceDeclaration("blue", "http://www.blue.com/");
0450: e.addNamespaceDeclaration("green", "http://www.green.com/");
0451: Attribute a1 = new Attribute("test", "test");
0452: Attribute a2 = new Attribute("pre1:green",
0453: "http://www.green.com/", "data");
0454: Attribute a3 = new Attribute("yellow:sfsdadf",
0455: "http://www.yellow.com/", "data");
0456: e.addAttribute(a1);
0457: e.addAttribute(a2);
0458: e.addAttribute(a3);
0459:
0460: assertEquals("http://www.red.com/", e.getNamespaceURI("red"));
0461: assertEquals("http://www.green.com/", e
0462: .getNamespaceURI("green"));
0463: assertEquals("http://www.blue.com/", e.getNamespaceURI("blue"));
0464: assertEquals("http://www.green.com/", e.getNamespaceURI("pre1"));
0465: assertEquals("http://www.yellow.com/", e
0466: .getNamespaceURI("yellow"));
0467:
0468: Element e2 = new Element("mauve:child", "http://www.mauve.com");
0469: e.appendChild(e2);
0470: assertEquals("http://www.red.com/", e2.getNamespaceURI("red"));
0471: assertEquals("http://www.blue.com/", e2.getNamespaceURI("blue"));
0472: assertEquals("http://www.green.com/", e2
0473: .getNamespaceURI("green"));
0474: assertEquals("http://www.green.com/", e2
0475: .getNamespaceURI("pre1"));
0476: assertEquals("http://www.yellow.com/", e2
0477: .getNamespaceURI("yellow"));
0478: assertNull(e2.getNamespaceURI("head"));
0479:
0480: try {
0481: e.addNamespaceDeclaration("pre1", "http://www.blue2.com");
0482: fail("Added conflicting namespace");
0483: } catch (NamespaceConflictException success) {
0484: String message = success.getMessage();
0485: assertTrue(message.indexOf("http://www.blue2.com") > 0);
0486: assertTrue(message.indexOf("pre1") > 0);
0487: }
0488:
0489: try {
0490: Attribute a4 = new Attribute("pre1:mauve",
0491: "http://www.sadas.com/", "data");
0492: e.addAttribute(a4);
0493: fail("Added conflicting namespace");
0494: } catch (NamespaceConflictException success) {
0495: assertNotNull(success.getMessage());
0496: }
0497:
0498: // can't add conflicting attribute from
0499: // different namespace even with identical QName
0500: try {
0501: Attribute a4 = new Attribute("pre1:green",
0502: "http://www.example.com/", "data");
0503: e.addAttribute(a4);
0504: } catch (NamespaceConflictException success) {
0505: assertNotNull(success.getMessage());
0506: }
0507:
0508: e.removeNamespaceDeclaration("green");
0509: assertNull(e.getNamespaceURI("green"));
0510: e.addNamespaceDeclaration("green", "http://www.green2.com/");
0511: assertEquals("http://www.green2.com/", e
0512: .getNamespaceURI("green"));
0513:
0514: }
0515:
0516: public void testAttributes() {
0517:
0518: String name = "red:sakjdhjhd";
0519: String uri = "http://www.red.com/";
0520: Element e = new Element(name, uri);
0521:
0522: Attribute a1 = new Attribute("name", "simple");
0523: Attribute a2 = new Attribute("pre1:green",
0524: "http://www.green.com/", "data");
0525:
0526: e.addAttribute(a1);
0527: e.addAttribute(a2);
0528:
0529: assertEquals(a2, e.getAttribute("green",
0530: "http://www.green.com/"));
0531: assertEquals(a1, e.getAttribute("name"));
0532: assertEquals(a1, e.getAttribute("name", ""));
0533: assertEquals(e, a1.getParent());
0534: assertEquals("simple", e.getAttribute("name").getValue());
0535:
0536: a1.detach();
0537: assertNull(a1.getParent());
0538: assertNull(e.getAttribute("name"));
0539:
0540: Attribute removed = e.removeAttribute(a2);
0541: assertNull(a2.getParent());
0542: assertEquals(a2, removed);
0543: assertNull(e.getAttribute("green", "http://www.green.com/"));
0544:
0545: }
0546:
0547: public void testRemoveNullAttribute() {
0548:
0549: String name = "red:sakjdhjhd";
0550: String uri = "http://www.red.com/";
0551: Element e = new Element(name, uri);
0552:
0553: Attribute a1 = new Attribute("name", "simple");
0554: Attribute a2 = new Attribute("pre1:green",
0555: "http://www.green.com/", "data");
0556:
0557: e.addAttribute(a1);
0558: e.addAttribute(a2);
0559:
0560: try {
0561: e.removeAttribute(null);
0562: fail("Removed Null Attribute");
0563: } catch (NullPointerException success) {
0564: assertNotNull(success.getMessage());
0565: }
0566:
0567: }
0568:
0569: public void testRemoveAttributeFromElementWithNoAttributes() {
0570:
0571: String name = "red:sakjdhjhd";
0572: String uri = "http://www.red.com/";
0573: Element e = new Element(name, uri);
0574:
0575: Attribute a1 = new Attribute("name", "simple");
0576:
0577: try {
0578: e.removeAttribute(a1);
0579: fail("Removed Attribute that didn't belong");
0580: } catch (NoSuchAttributeException success) {
0581: assertTrue(success.getMessage().indexOf(
0582: a1.getQualifiedName()) > 0);
0583: }
0584:
0585: }
0586:
0587: public void testRemoveAttributeFromElementWithDifferentAttributes() {
0588:
0589: String name = "red:sakjdhjhd";
0590: String uri = "http://www.red.com/";
0591: Element e = new Element(name, uri);
0592: e.addAttribute(new Attribute("name", "value"));
0593: Attribute a1 = new Attribute("name", "simple");
0594:
0595: try {
0596: e.removeAttribute(a1);
0597: fail("Removed Attribute that didn't belong");
0598: } catch (NoSuchAttributeException success) {
0599: assertTrue(success.getMessage().indexOf(
0600: a1.getQualifiedName()) > 0);
0601: }
0602:
0603: }
0604:
0605: public void testGetValue() {
0606:
0607: String name = "red:sakjdhjhd";
0608: String uri = "http://www.red.com/";
0609: Element e = new Element(name, uri);
0610:
0611: assertEquals(e.getValue(), "");
0612:
0613: e.appendChild(new Text("data"));
0614: assertEquals(e.getValue(), "data");
0615: e.appendChild(new Text(" moredata"));
0616: assertEquals(e.getValue(), "data moredata");
0617: e.appendChild(new Comment(" moredata"));
0618: assertEquals(e.getValue(), "data moredata");
0619: e.appendChild(new ProcessingInstruction("target", "moredata"));
0620: assertEquals(e.getValue(), "data moredata");
0621:
0622: Element e2 = new Element("child");
0623: e.appendChild(e2);
0624: assertEquals("data moredata", e.getValue());
0625: e2.appendChild(new Text("something"));
0626: assertEquals("data moredatasomething", e.getValue());
0627:
0628: }
0629:
0630: public void testSetLocalName() {
0631:
0632: String name = "red:sakjdhjhd";
0633: String uri = "http://www.red.com/";
0634: Element e = new Element(name, uri);
0635:
0636: assertEquals("sakjdhjhd", e.getLocalName());
0637:
0638: e.setLocalName("dude");
0639: assertEquals("dude", e.getLocalName());
0640: e.setLocalName("digits__");
0641: assertEquals("digits__", e.getLocalName());
0642: e.setLocalName("digits1234");
0643: assertEquals("digits1234", e.getLocalName());
0644: e.setLocalName("digits-z");
0645: assertEquals("digits-z", e.getLocalName());
0646:
0647: try {
0648: e.setLocalName("spaces ");
0649: fail("Local name allowed to contain spaces");
0650: } catch (IllegalNameException success) {
0651: assertNotNull(success.getMessage());
0652: assertEquals("spaces ", success.getData());
0653: }
0654:
0655: try {
0656: e.setLocalName("digits:test");
0657: fail("Local name allowed to contain colon");
0658: } catch (IllegalNameException success) {
0659: assertNotNull(success.getMessage());
0660: }
0661:
0662: try {
0663: e.setLocalName("digits!test");
0664: fail("Local name allowed to contain bang");
0665: } catch (IllegalNameException success) {
0666: assertNotNull(success.getMessage());
0667: assertEquals("digits!test", success.getData());
0668: }
0669:
0670: try {
0671: e.setLocalName("digits\u0000test");
0672: fail("Local name allowed to contain null");
0673: } catch (IllegalNameException success) {
0674: assertNotNull(success.getMessage());
0675: assertEquals("digits\u0000test", success.getData());
0676: }
0677:
0678: }
0679:
0680: public void testSetNamespacePrefix() {
0681:
0682: String name = "red:sakjdhjhd";
0683: String uri = "http://www.red.com/";
0684: String prefix = "red";
0685: Element e = new Element(name, uri);
0686:
0687: assertEquals(prefix, e.getNamespacePrefix());
0688:
0689: e.setNamespacePrefix("dude");
0690: assertEquals("dude", e.getNamespacePrefix());
0691: e.setNamespacePrefix("digits__");
0692: assertEquals("digits__", e.getNamespacePrefix());
0693: e.setNamespacePrefix("digits1234");
0694: assertEquals("digits1234", e.getNamespacePrefix());
0695: e.setNamespacePrefix("digits-z");
0696: assertEquals("digits-z", e.getNamespacePrefix());
0697: e.setNamespacePrefix("");
0698: assertEquals("", e.getNamespacePrefix());
0699: e.setNamespacePrefix(null);
0700: assertEquals("", e.getNamespacePrefix());
0701:
0702: try {
0703: e.setNamespacePrefix("spaces ");
0704: fail("namespace prefix allowed to contain spaces");
0705: } catch (IllegalNameException success) {
0706: assertNotNull(success.getMessage());
0707: assertEquals("spaces ", success.getData());
0708: }
0709:
0710: try {
0711: e.setNamespacePrefix("digits:test");
0712: fail("namespace prefix allowed to contain colon");
0713: } catch (IllegalNameException success) {
0714: assertNotNull(success.getMessage());
0715: }
0716:
0717: try {
0718: e.setNamespacePrefix("digits!test");
0719: fail("namespace prefix allowed to contain bang");
0720: } catch (IllegalNameException success) {
0721: assertNotNull(success.getMessage());
0722: assertEquals("digits!test", success.getData());
0723: }
0724:
0725: try {
0726: e.setNamespacePrefix("digits\u0000test");
0727: fail("namespace prefix allowed to contain null");
0728: } catch (IllegalNameException success) {
0729: assertNotNull(success.getMessage());
0730: assertEquals("digits\u0000test", success.getData());
0731: }
0732:
0733: }
0734:
0735: public void testSetNamespaceURI() {
0736:
0737: String name = "red:sakjdhjhd";
0738: String uri = "http://www.red.com/";
0739: Element e = new Element(name, uri);
0740:
0741: assertEquals(e.getNamespaceURI(), uri);
0742:
0743: for (int i = 0; i < legal.length; i++) {
0744: e.setNamespaceURI(legal[i]);
0745: assertEquals(legal[i], e.getNamespaceURI());
0746: }
0747:
0748: for (int i = 0; i < illegal.length; i++) {
0749: try {
0750: e.setNamespaceURI(illegal[i]);
0751: fail("illegal namespace URI allowed");
0752: } catch (MalformedURIException success) {
0753: assertNotNull(success.getMessage());
0754: }
0755: }
0756:
0757: }
0758:
0759: public void testRFC2396LegalRFC3986IllegalNamespaceURI() {
0760:
0761: String name = "red:green";
0762: String uri = "dcp.tcp.pft://192.168.0.1:1002:3002?fec=1&crc=0'";
0763: try {
0764: new Element(name, uri);
0765: fail("Allowed RFC 2396 legal but RFC 3986 illegal URI");
0766: } catch (MalformedURIException success) {
0767: assertNotNull(success.getMessage());
0768: }
0769:
0770: }
0771:
0772: public void testSetNamespaceURIConflictsWithAdditionalNamespaceDeclaration() {
0773:
0774: String name = "red:sakjdhjhd";
0775: String uri = "http://www.red.com/";
0776: Element e = new Element(name, uri);
0777: e.addNamespaceDeclaration("red", "http://www.red.com/");
0778:
0779: try {
0780: e.setNamespaceURI("http://www.example.com");
0781: fail("illegal namespace conflict");
0782: } catch (NamespaceConflictException success) {
0783: assertNotNull(success.getMessage());
0784: }
0785:
0786: }
0787:
0788: public void testSetNamespaceURIConflictsWithAttributeNamespace() {
0789:
0790: String name = "red:sakjdhjhd";
0791: String uri = "http://www.red.com/";
0792: Element e = new Element(name, uri);
0793: e.addAttribute(new Attribute("red:test", "http://www.red.com/",
0794: "value"));
0795:
0796: try {
0797: e.setNamespaceURI("http://www.example.com");
0798: fail("illegal attribute namespace conflict");
0799: } catch (NamespaceConflictException success) {
0800: assertNotNull(success.getMessage());
0801: }
0802:
0803: }
0804:
0805: public void testChangePrefix() {
0806:
0807: String name = "red:sakjdhjhd";
0808: String uri = "http://www.red.com/";
0809: Element element = new Element(name, uri);
0810: element.addNamespaceDeclaration("blue", "http://www.foo.com/");
0811: element.addAttribute(new Attribute("green:money",
0812: "http://example.com/", "value"));
0813: element.addNamespaceDeclaration("purple", uri);
0814: element
0815: .addAttribute(new Attribute("mauve:money", uri, "value"));
0816:
0817: try {
0818: element.setNamespacePrefix("blue");
0819: fail("Conflicting prefix allowed against additional "
0820: + "namespace declaration");
0821: } catch (NamespaceConflictException success) {
0822: assertNotNull(success.getMessage());
0823: }
0824:
0825: try {
0826: element.setNamespacePrefix("green");
0827: fail("Conflicting prefix allowed against attribute");
0828: } catch (NamespaceConflictException success) {
0829: assertNotNull(success.getMessage());
0830: }
0831:
0832: element.setNamespacePrefix("purple");
0833: assertEquals("purple", element.getNamespacePrefix());
0834: element.setNamespacePrefix("mauve");
0835: assertEquals("mauve", element.getNamespacePrefix());
0836:
0837: }
0838:
0839: public void testDeclareNamespacePrefix() {
0840:
0841: String name = "red:sakjdhjhd";
0842: String uri = "http://www.red.com/";
0843: Element e = new Element(name, uri);
0844:
0845: for (int i = 0; i < legal.length; i++) {
0846: e.removeNamespaceDeclaration("prefix");
0847: e.addNamespaceDeclaration("prefix", legal[i]);
0848: assertEquals(legal[i], e.getNamespaceURI("prefix"));
0849: }
0850:
0851: for (int i = 0; i < illegal.length; i++) {
0852: try {
0853: e.addNamespaceDeclaration("prefix", illegal[i]);
0854: fail("illegal namespace URI allowed " + illegal[i]);
0855: } catch (MalformedURIException ex) {
0856: // success
0857: assertNotNull(ex.getMessage());
0858: }
0859: }
0860:
0861: }
0862:
0863: public void testInsertChildUsingString() {
0864:
0865: String name = "red:sakjdhjhd";
0866: String uri = "http://www.red.com/";
0867: Element e = new Element(name, uri);
0868:
0869: Element e2 = new Element("mauve:child", "http://www.mauve.com");
0870: e.insertChild(e2, 0);
0871: Element e3 = new Element("mauve:child", "http://www.mauve.com");
0872: e.insertChild(e3, 0);
0873:
0874: e.insertChild("Hello", 0);
0875: assertEquals("Hello", e.getChild(0).getValue());
0876:
0877: }
0878:
0879: public void testInsertNull() {
0880:
0881: String name = "red:sakjdhjhd";
0882: String uri = "http://www.red.com/";
0883: Element e = new Element(name, uri);
0884: String data = null;
0885: try {
0886: e.insertChild(data, 0);
0887: fail("Inserted null");
0888: } catch (NullPointerException success) {
0889: // success;
0890: }
0891: }
0892:
0893: public void appendNullChild() {
0894:
0895: String name = "red:sakjdhjhd";
0896: String uri = "http://www.red.com/";
0897: Element e = new Element(name, uri);
0898: String data = null;
0899: try {
0900: e.appendChild(data);
0901: fail("Appended null");
0902: } catch (NullPointerException success) {
0903: // success;
0904: }
0905: }
0906:
0907: public void testInsertChild() {
0908:
0909: String name = "red:sakjdhjhd";
0910: String uri = "http://www.red.com/";
0911: Element e = new Element(name, uri);
0912:
0913: Element e2 = new Element("mv:child", "http://www.mauve.com");
0914: e.insertChild(e2, 0);
0915: Element e3 = new Element("mv:child", "http://www.mauve.com");
0916: e.insertChild(e3, 0);
0917: Element e4 = new Element("mv:child", "http://www.mauve.com");
0918: e3.insertChild(e4, 0);
0919:
0920: assertEquals(e3, e.getChild(0));
0921:
0922: try {
0923: Element root = new Element("root");
0924: Document doc = new Document(root);
0925: e.insertChild(doc, 0);
0926: fail("Added document as child");
0927: } catch (IllegalAddException ex) {
0928: // success
0929: assertNotNull(ex.getMessage());
0930: }
0931: try {
0932: e.insertChild(e2, 0);
0933: fail("Added child twice");
0934: } catch (MultipleParentException ex) {
0935: // success
0936: assertNotNull(ex.getMessage());
0937: }
0938: try {
0939: e.insertChild(e4, 0);
0940: fail("Added child twice");
0941: } catch (MultipleParentException ex) {
0942: // success
0943: assertNotNull(ex.getMessage());
0944: }
0945: try {
0946: e.insertChild((Node) null, 0);
0947: fail("Added null child");
0948: } catch (NullPointerException success) {
0949: // success
0950: }
0951: try {
0952: e.insertChild(new Comment("test"), 20);
0953: fail("Added comment after end");
0954: } catch (IndexOutOfBoundsException success) {
0955: // success
0956: assertNotNull(success.getMessage());
0957: }
0958: try {
0959: e.insertChild(new Comment("test"), -20);
0960: fail("Added comment before start");
0961: } catch (IndexOutOfBoundsException success) {
0962: // success
0963: }
0964:
0965: }
0966:
0967: public void testInsertString() {
0968:
0969: Element element = new Element("test");
0970: element.insertChild("test", 0);
0971:
0972: String s = null;
0973: try {
0974: element.insertChild(s, 0);
0975: fail("Inserted string as null");
0976: } catch (NullPointerException success) {
0977: // success
0978: }
0979:
0980: element.insertChild("", 0);
0981: // empty node should be created
0982: assertEquals(2, element.getChildCount());
0983:
0984: }
0985:
0986: public void testUnsetNamespaceWhenPrefixed() {
0987:
0988: Element element = new Element("prefix:name",
0989: "http://www.foo.com/");
0990:
0991: try {
0992: element.setNamespaceURI("");
0993: fail("Unset namespace");
0994: } catch (NamespaceConflictException success) {
0995: assertNotNull(success.getMessage());
0996: }
0997: try {
0998: element.setNamespaceURI(null);
0999: fail("Unset namespace");
1000: } catch (NamespaceConflictException success) {
1001: assertNotNull(success.getMessage());
1002: }
1003:
1004: }
1005:
1006: public void testGetChildElements() {
1007:
1008: Elements children = element.getChildElements();
1009: assertEquals(3, children.size());
1010: assertEquals(child1, children.get(0));
1011: assertEquals(child4, children.get(1));
1012: assertEquals(child5, children.get(2));
1013:
1014: children = element.getChildElements("nonesuch");
1015: assertEquals(0, children.size());
1016:
1017: children = element.getChildElements("test");
1018: assertEquals(1, children.size());
1019: assertEquals(child1, children.get(0));
1020:
1021: children = element.getChildElements("test",
1022: "http://www.example.com");
1023: assertEquals(2, children.size());
1024: assertEquals(child4, children.get(0));
1025: assertEquals(child5, children.get(1));
1026:
1027: }
1028:
1029: public void testAddAttribute() {
1030:
1031: Element element = new Element("name");
1032: Attribute a1 = new Attribute("name", "value");
1033: Attribute a2 = new Attribute("xlink:type",
1034: "http://www.w3.org/TR/1999/xlink", "simple");
1035:
1036: element.addAttribute(a1);
1037: element.addAttribute(a2);
1038:
1039: assertEquals(2, element.getAttributeCount());
1040:
1041: Element element2 = new Element("name");
1042: try {
1043: element2.addAttribute(a1);
1044: fail("added attribute with existing parent");
1045: } catch (MultipleParentException success) {
1046: assertNotNull(success.getMessage());
1047: }
1048:
1049: a1.detach();
1050: element2.addAttribute(a1);
1051:
1052: a2.detach();
1053: Element funky = new Element("xlink:funky",
1054: "http://www.funky.org");
1055: try {
1056: funky.addAttribute(a2);
1057: fail("added conflicting namespace");
1058: } catch (NamespaceConflictException success) {
1059: assertNotNull(success.getMessage());
1060: }
1061:
1062: a2.detach();
1063: Element notasfunky = new Element("prefix:funky",
1064: "http://www.w3.org/TR/1999/xlink");
1065: notasfunky.addAttribute(a2);
1066:
1067: Attribute a3 = new Attribute("xlink:type",
1068: "http://www.w3.org/TR/1999/xlink", "simple");
1069: Attribute a4 = new Attribute("xlink:href",
1070: "http://www.w3.org/1998/xlink", "simple");
1071: Element test = new Element("test");
1072: try {
1073: test.addAttribute(a3);
1074: test.addAttribute(a4);
1075: fail("added conflicting attributes");
1076: } catch (NamespaceConflictException success) {
1077: assertNotNull(success.getMessage());
1078: }
1079:
1080: Attribute a5 = new Attribute("xlink:type",
1081: "http://www.w3.org/TR/1999/xlink", "simple");
1082: Attribute a6 = new Attribute("xlink:type",
1083: "http://www.w3.org/1998/xlink", "simple");
1084: Element test2 = new Element("test");
1085: try {
1086: test2.addAttribute(a5);
1087: test2.addAttribute(a6);
1088: fail("added conflicting attributes");
1089: } catch (NamespaceConflictException success) {
1090: assertNotNull(success.getMessage());
1091: }
1092:
1093: }
1094:
1095: public void testAddAttributesWithAdditionalNamespaces() {
1096:
1097: Element element = new Element("name");
1098: element.addNamespaceDeclaration("xlink",
1099: "http://www.w3.org/TR/1999/xlink");
1100: element
1101: .addNamespaceDeclaration("pre",
1102: "http://www.example.com");
1103:
1104: Attribute a1 = new Attribute("name", "value");
1105: Attribute a2 = new Attribute("xlink:type",
1106: "http://www.w3.org/TR/1999/xlink", "simple");
1107:
1108: element.addAttribute(a1);
1109: element.addAttribute(a2);
1110:
1111: assertEquals(2, element.getAttributeCount());
1112:
1113: try {
1114: element.addAttribute(new Attribute("pre:att",
1115: "ftp://example.com/", "value"));
1116: fail("added attribute that conflicts with "
1117: + "additional namespace declaration");
1118: } catch (NamespaceConflictException success) {
1119: assertNotNull(success.getMessage());
1120: }
1121:
1122: element.addAttribute(new Attribute("ok:att",
1123: "ftp://example.com/", "value"));
1124: assertEquals(3, element.getAttributeCount());
1125:
1126: try {
1127: element.addNamespaceDeclaration("ok",
1128: "http://www.example.net");
1129: fail("added namespace declaration that "
1130: + "conflicts with attribute");
1131: } catch (NamespaceConflictException ex) {
1132: assertNotNull(ex.getMessage());
1133: }
1134:
1135: assertEquals("ftp://example.com/", element
1136: .getNamespaceURI("ok"));
1137: assertEquals("http://www.w3.org/TR/1999/xlink", element
1138: .getNamespaceURI("xlink"));
1139: assertEquals("http://www.example.com", element
1140: .getNamespaceURI("pre"));
1141:
1142: }
1143:
1144: public void testTriple() throws IOException, ParsingException {
1145: String data = "<b><c1 /><c2 /></b>";
1146: Builder builder = new Builder();
1147: Document doc = builder.build(data, "http://www.example.org/");
1148: Node root = doc.getRootElement();
1149: Node rootcopy = root.copy();
1150: assertEquals(data, rootcopy.toXML());
1151: }
1152:
1153: public void testCopyChildElementWithNoChildren() {
1154:
1155: Element parent = new Element("parent");
1156: Element child = new Element("child");
1157: parent.appendChild(child);
1158: Element copy = new Element(child);
1159: assertEquals(child, copy);
1160:
1161: }
1162:
1163: public void testSimpleCopy() {
1164:
1165: Element parent = new Element("parent");
1166: Element child = new Element("child");
1167: parent.appendChild(child);
1168: Element copy = new Element(parent);
1169: assertEquals(parent, copy);
1170:
1171: }
1172:
1173: public void testCopyElementWithAttributes() {
1174:
1175: Element parent = new Element("parent");
1176: Attribute a = new Attribute("name", "value");
1177: parent.addAttribute(a);
1178: Element copy = new Element(parent);
1179: assertEquals(parent, copy);
1180: Attribute copied = copy.getAttribute(0);
1181: assertEquals(a, copied);
1182: assertEquals(copy, copied.getParent());
1183:
1184: }
1185:
1186: public void testCopyEmptyElement() {
1187:
1188: Element parent = new Element("parent");
1189: Element copy = new Element(parent);
1190: assertEquals(parent, copy);
1191:
1192: }
1193:
1194: public void testEmptyElementAsRootElementCopy() {
1195:
1196: Element root = new Element("root");
1197: Document doc = new Document(root);
1198: Node copy = doc.copy();
1199: assertEquals(doc, copy);
1200: }
1201:
1202: public void testCopy() {
1203:
1204: String name = "red:sakjdhjhd";
1205: String uri = "http://www.red.com/";
1206: String baseURI = "http://www.example.com/";
1207: Element e = new Element(name, uri);
1208:
1209: e.addNamespaceDeclaration("blue", "http://www.blue.com");
1210: e.addNamespaceDeclaration("green", "http://www.green.com");
1211: Attribute a1 = new Attribute("test", "test");
1212: Attribute a2 = new Attribute("pre1:green",
1213: "http://www.green.com/", "data");
1214: Attribute a3 = new Attribute("yellow:sfsdadf",
1215: "http://www.yellow.com/", "data");
1216: e.addAttribute(a1);
1217: e.addAttribute(a2);
1218: e.addAttribute(a3);
1219:
1220: Element e2 = new Element("mv:child", "http://www.mauve.com");
1221: e.appendChild(e2);
1222:
1223: Element e3 = new Element("mv:child", "http://www.mauve.com");
1224: e.insertChild(e3, 0);
1225: Element e4 = new Element("mv:child", "http://www.mauve.com");
1226: e3.insertChild(e4, 0);
1227: e.setBaseURI(baseURI);
1228:
1229: Element copy = (Element) e.copy();
1230:
1231: assertEquals(e.getNamespaceURI("red"), copy
1232: .getNamespaceURI("red"));
1233: assertEquals(e.getNamespaceURI("blue"), copy
1234: .getNamespaceURI("blue"));
1235: assertEquals(e.getValue(), copy.getValue());
1236:
1237: Attribute ea = e.getAttribute("test");
1238: Attribute ca = copy.getAttribute("test");
1239: assertEquals(ea.getValue(), ca.getValue());
1240: assertEquals(e.getBaseURI(), copy.getBaseURI());
1241:
1242: }
1243:
1244: public void testDeepCopy() {
1245:
1246: Element top = new Element("e");
1247: Element parent = top;
1248: for (int i = 0; i < 100; i++) {
1249: Element child = new Element("e" + i);
1250: parent.appendChild(child);
1251: parent = child;
1252: }
1253: Element copy = (Element) top.copy();
1254: assertEquals(top, copy);
1255:
1256: }
1257:
1258: public void testRemoveChildren() {
1259:
1260: String name = "red:sakjdhjhd";
1261: String uri = "http://www.red.com/";
1262: Element parent = new Element(name, uri);
1263:
1264: Attribute a1 = new Attribute("test", "test");
1265: parent.addAttribute(a1);
1266:
1267: Element child1 = new Element("mv:child", "http://www.mauve.com");
1268: parent.appendChild(child1);
1269: Element child2 = new Element("mv:child", "http://www.mauve.com");
1270: parent.appendChild(child2);
1271: Element grandchild = new Element("mv:child",
1272: "http://www.mauve.com");
1273: child2.insertChild(grandchild, 0);
1274:
1275: assertEquals(child2, grandchild.getParent());
1276: assertEquals(parent, child1.getParent());
1277: assertEquals(parent, child2.getParent());
1278:
1279: Nodes result = parent.removeChildren();
1280:
1281: assertEquals(0, parent.getChildCount());
1282: assertNull(child1.getParent());
1283: assertNull(child2.getParent());
1284: assertEquals(child2, grandchild.getParent());
1285: assertEquals(parent, a1.getParent());
1286:
1287: assertEquals(2, result.size());
1288: assertEquals(child1, result.get(0));
1289: assertEquals(child2, result.get(1));
1290: assertEquals(grandchild, child2.getChild(0));
1291: assertEquals(child2, grandchild.getParent());
1292:
1293: }
1294:
1295: public void testRemovedChildrenInheritBaseURI() {
1296:
1297: String base = "http://www.example.com/";
1298: Element parent = new Element("parent");
1299: Element child = new Element("child");
1300: parent.setBaseURI(base);
1301: parent.appendChild(child);
1302: parent.removeChildren();
1303: assertEquals(base, child.getBaseURI());
1304:
1305: }
1306:
1307: public void testRemoveNonElementChildren() {
1308:
1309: String name = "red:sakjdhjhd";
1310: String uri = "http://www.red.com/";
1311: Element parent = new Element(name, uri);
1312:
1313: Attribute a1 = new Attribute("test", "test");
1314: parent.addAttribute(a1);
1315:
1316: Node child1 = new Text("http://www.mauve.com");
1317: parent.appendChild(child1);
1318: Node child2 = new ProcessingInstruction("child",
1319: "http://www.mauve.com");
1320: parent.appendChild(child2);
1321: Node child3 = new Comment("http://www.mauve.com");
1322: parent.appendChild(child3);
1323:
1324: assertEquals(parent, child3.getParent());
1325: assertEquals(parent, child1.getParent());
1326: assertEquals(parent, child2.getParent());
1327:
1328: Nodes result = parent.removeChildren();
1329:
1330: assertEquals(0, parent.getChildCount());
1331: assertNull(child1.getParent());
1332: assertNull(child2.getParent());
1333: assertNull(child3.getParent());
1334: assertEquals(parent, a1.getParent());
1335:
1336: assertEquals(3, result.size());
1337: assertEquals(child1, result.get(0));
1338: assertEquals(child2, result.get(1));
1339: assertEquals(child3, result.get(2));
1340:
1341: }
1342:
1343: public void testGetAttributeValue() {
1344:
1345: String name = "sakjdhjhd";
1346: Element e = new Element(name);
1347:
1348: assertNull(e.getAttributeValue("test"));
1349: assertNull(e.getAttributeValue("base",
1350: "http://www.w3.org/XML/1998/namespace"));
1351:
1352: e.addAttribute(new Attribute("test", "value"));
1353: e.addAttribute(new Attribute("xml:base",
1354: "http://www.w3.org/XML/1998/namespace",
1355: "http://www.example.com/"));
1356:
1357: assertEquals("value", e.getAttributeValue("test"));
1358: assertEquals("http://www.example.com/", e.getAttributeValue(
1359: "base", "http://www.w3.org/XML/1998/namespace"));
1360: assertNull(e.getAttributeValue("xml:base"));
1361: assertNull(e.getAttributeValue("base"));
1362: assertNull(e.getAttributeValue("test",
1363: "http://www.w3.org/XML/1998/namespace"));
1364:
1365: }
1366:
1367: public void testGetAttribute() {
1368:
1369: String name = "sakjdhjhd";
1370: Element e = new Element(name);
1371:
1372: assertNull(e.getAttribute("test"));
1373: assertNull(e.getAttribute("base",
1374: "http://www.w3.org/XML/1998/namespace"));
1375:
1376: try {
1377: e.getAttribute(0);
1378: } catch (IndexOutOfBoundsException success) {
1379: // success
1380: }
1381:
1382: Attribute a1 = new Attribute("test", "value");
1383: Attribute a2 = new Attribute("xml:base",
1384: "http://www.w3.org/XML/1998/namespace",
1385: "http://www.example.com/");
1386:
1387: e.addAttribute(a1);
1388: e.addAttribute(a2);
1389:
1390: assertEquals(a1, e.getAttribute("test"));
1391: assertEquals(a2, e.getAttribute("base",
1392: "http://www.w3.org/XML/1998/namespace"));
1393:
1394: try {
1395: e.getAttribute(2);
1396: fail("Got attribute beyond bounds");
1397: } catch (IndexOutOfBoundsException success) {
1398: assertNotNull(success.getMessage());
1399: }
1400:
1401: try {
1402: e.getAttribute(-1);
1403: fail("Got attribute with negative index");
1404: } catch (IndexOutOfBoundsException success) {
1405: assertNotNull(success.getMessage());
1406: }
1407:
1408: }
1409:
1410: public void testGetNamespacePrefix() {
1411: Element html = new Element("html");
1412: assertEquals("", html.getNamespacePrefix());
1413: }
1414:
1415: public void testXMLPrefixAllowed() {
1416: Element test = new Element("xml:base",
1417: "http://www.w3.org/XML/1998/namespace");
1418: assertEquals("xml", test.getNamespacePrefix());
1419: assertEquals("http://www.w3.org/XML/1998/namespace", test
1420: .getNamespaceURI());
1421: assertEquals("xml:base", test.getQualifiedName());
1422: }
1423:
1424: public void testXMLPrefixNotAllowedWithWrongURI() {
1425: try {
1426: new Element("xml:base", "http://www.example.org/");
1427: fail("Allowed wrong namespace for xml prefix");
1428: } catch (NamespaceConflictException success) {
1429: assertNotNull(success.getMessage());
1430: }
1431:
1432: }
1433:
1434: public void testWrongPrefixNotAllowedWithXMLURI() {
1435:
1436: try {
1437: new Element("test:base",
1438: "http://www.w3.org/XML/1998/namespace");
1439: fail("Allowed XML namespace to be associated with non-xml prefix");
1440: } catch (NamespaceConflictException success) {
1441: assertNotNull(success.getMessage());
1442: }
1443:
1444: }
1445:
1446: public void testEmptyName() {
1447:
1448: try {
1449: new Element("");
1450: fail("Allowed empty string for element name");
1451: } catch (IllegalNameException success) {
1452: assertNotNull(success.getMessage());
1453: assertEquals("", success.getData());
1454: }
1455:
1456: }
1457:
1458: public void testBadNameStartCharacter() {
1459:
1460: try {
1461: new Element("1Kelvin");
1462: fail("Allowed element name to begin with digit");
1463: } catch (IllegalNameException success) {
1464: assertNotNull(success.getMessage());
1465: assertEquals("1Kelvin", success.getData());
1466: }
1467:
1468: }
1469:
1470: public void testNullName() {
1471:
1472: try {
1473: new Element((String) null);
1474: fail("Allowed null for element name");
1475: } catch (NullPointerException success) {
1476: // success
1477: }
1478:
1479: }
1480:
1481: }
|