1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15
16 /*** A test harness to test the DocumentHelper.makeElement() methodt
17 *
18 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
19 * @version $Revision: 1.5 $
20 */
21 public class TestMakeElement extends AbstractTestCase {
22
23 public static void main( String[] args ) {
24 TestRunner.run( suite() );
25 }
26
27 public static Test suite() {
28 return new TestSuite( TestMakeElement.class );
29 }
30
31 public TestMakeElement(String name) {
32 super(name);
33 }
34
35
36
37 public void testMakeElement() throws Exception {
38 Document doc = DocumentHelper.createDocument();
39
40 Element c = DocumentHelper.makeElement( doc, "a/b/c" );
41 assertTrue( "Should return a valid element", c != null );
42
43 Element c2 = DocumentHelper.makeElement( doc, "a/b/c" );
44
45 assertTrue( "Found same element again", c == c2 );
46
47 c.addAttribute( "x", "123" );
48
49 Node found = doc.selectSingleNode( "/a/b/c[@x='123']" );
50
51 assertEquals( "Found same node via XPath", c, found );
52
53 Element b = c.getParent();
54
55 Element e = DocumentHelper.makeElement( b, "c/d/e" );
56
57 assertTrue( "Should return a valid element", e != null );
58
59 Element e2 = DocumentHelper.makeElement( b, "c/d/e" );
60
61 assertTrue( "Found same element again", e == e2 );
62
63 e.addAttribute( "y", "456" );
64
65 found = b.selectSingleNode( "c/d/e[@y='456']" );
66
67 assertEquals( "Found same node via XPath", e, found );
68 }
69
70 public void testMakeQualifiedElement() throws Exception {
71 Document doc = DocumentHelper.createDocument();
72 Element root = doc.addElement( "root" );
73 root.addNamespace( "", "defaultURI" );
74 root.addNamespace( "foo", "fooURI" );
75 root.addNamespace( "bar", "barURI" );
76
77 Element c = DocumentHelper.makeElement( doc, "root/foo:b/bar:c" );
78 assertTrue( "Should return a valid element", c != null );
79
80 assertEquals( "c has a valid namespace", "barURI", c.getNamespaceURI() );
81
82 Element b = c.getParent();
83
84 assertEquals( "b has a valid namespace", "fooURI", b.getNamespaceURI() );
85
86 log( "Created: " + c );
87
88 Element c2 = DocumentHelper.makeElement( doc, "root/foo:b/bar:c" );
89 assertTrue( "Found same element again", c == c2 );
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140