1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 /***
13 * A test harness to test the DocumentHelper.makeElement() methodt
14 *
15 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
16 * @version $Revision: 1.3 $
17 */
18 public class MakeElementTest extends AbstractTestCase {
19 public static void main(String[] args) {
20 TestRunner.run(MakeElementTest.class);
21 }
22
23
24
25 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116