1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 18 public class MakeElementTest extends AbstractTestCase { 19 public static void main(String [] args) { 20 TestRunner.run(MakeElementTest.class); 21 } 22 23 public void testMakeElement() throws Exception { 26 Document doc = DocumentHelper.createDocument(); 27 28 Element c = DocumentHelper.makeElement(doc, "a/b/c"); 29 assertTrue("Should return a valid element", c != null); 30 31 Element c2 = DocumentHelper.makeElement(doc, "a/b/c"); 32 33 assertTrue("Found same element again", c == c2); 34 35 c.addAttribute("x", "123"); 36 37 Node found = doc.selectSingleNode("/a/b/c[@x='123']"); 38 39 assertEquals("Found same node via XPath", c, found); 40 41 Element b = c.getParent(); 42 43 Element e = DocumentHelper.makeElement(b, "c/d/e"); 44 45 assertTrue("Should return a valid element", e != null); 46 47 Element e2 = DocumentHelper.makeElement(b, "c/d/e"); 48 49 assertTrue("Found same element again", e == e2); 50 51 e.addAttribute("y", "456"); 52 53 found = b.selectSingleNode("c/d/e[@y='456']"); 54 55 assertEquals("Found same node via XPath", e, found); 56 } 57 58 public void testMakeQualifiedElement() throws Exception { 59 Document doc = DocumentHelper.createDocument(); 60 Element root = doc.addElement("root"); 61 root.addNamespace("", "defaultURI"); 62 root.addNamespace("foo", "fooURI"); 63 root.addNamespace("bar", "barURI"); 64 65 Element c = DocumentHelper.makeElement(doc, "root/foo:b/bar:c"); 66 assertTrue("Should return a valid element", c != null); 67 68 assertEquals("c has a valid namespace", "barURI", c.getNamespaceURI()); 69 70 Element b = c.getParent(); 71 72 assertEquals("b has a valid namespace", "fooURI", b.getNamespaceURI()); 73 74 log("Created: " + c); 75 76 Element c2 = DocumentHelper.makeElement(doc, "root/foo:b/bar:c"); 77 assertTrue("Found same element again", c == c2); 78 } 79 } 80 81 117 | Popular Tags |