01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.util;
04:
05: import junit.framework.TestCase;
06: import org.w3c.dom.*;
07:
08: public class XmlUtilTest extends TestCase {
09: public void testCreateXMLDocumentFromString() throws Exception {
10: Document doc = XmlUtil.newDocument("<test>test</test>");
11: NodeList elements = doc.getElementsByTagName("test");
12: assertEquals(1, elements.getLength());
13: }
14:
15: public void testGetLocalElementByTagName() throws Exception {
16: Document doc = XmlUtil.newDocument("<level1>" + " <target1/>"
17: + " <level2>" + " <target2/>" + " </level2>"
18: + "</level1>");
19: Element level1 = doc.getDocumentElement();
20: Element level2 = XmlUtil.getElementByTagName(level1, "level2");
21:
22: Element target1 = XmlUtil.getLocalElementByTagName(level1,
23: "target1");
24: assertNotNull(target1);
25:
26: Element target2 = XmlUtil.getLocalElementByTagName(level1,
27: "target2");
28: assertNull(target2);
29:
30: target2 = XmlUtil.getLocalElementByTagName(level2, "target2");
31: assertNotNull(target2);
32: }
33:
34: public void testAddCdataElement() throws Exception {
35: Document doc = XmlUtil.newDocument();
36: Element root = doc.createElement("root");
37: doc.appendChild(root);
38:
39: XmlUtil.addCdataNode(doc, root, "mydata", "<>&#;");
40:
41: Element myDataElement = XmlUtil.getElementByTagName(root,
42: "mydata");
43: assertNotNull(myDataElement);
44: Node childNode = myDataElement.getChildNodes().item(0);
45: assertTrue(childNode instanceof CDATASection);
46: CDATASection cData = (CDATASection) childNode;
47: assertEquals("<>&#;", cData.getNodeValue());
48: }
49: }
|