1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package groovy.util;
48
49
50 import groovy.util.GroovyTestCase;
51 import groovy.util.Node;
52 import groovy.xml.QName;
53
54 import java.util.ArrayList;
55 import java.util.HashMap;
56 import java.util.List;
57 import java.util.Map;
58
59
60 /***
61 * Tests the use of the structured Attribute type
62 *
63 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
64 * @version $Revision: 1.4 $
65 */
66 public class NodeTest extends GroovyTestCase {
67
68 public void testSimpleAttribute() {
69 Node attribute = new Node(null, "transactional");
70 assertEquals("name", "transactional", attribute.name());
71 assertEquals("attributes", 0, attribute.attributes().size());
72 assertEquals("value", 0, attribute.children().size());
73 assertEquals("text", "", attribute.text());
74
75 dump(attribute);
76 }
77
78 public void testAttributeWithAttributes() {
79 Map attributes = new HashMap();
80 attributes.put("a", "xyz");
81
82 Node attribute = new Node(null, "foo", attributes);
83 assertEquals("name", "foo", attribute.name());
84 assertEquals("attributes", 1, attribute.attributes().size());
85 assertEquals("value", 0, attribute.children().size());
86 assertEquals("text", "", attribute.text());
87
88 dump(attribute);
89 }
90
91 public void testAttributeWithText() {
92 Node attribute = new Node(null, "foo", "the text");
93 assertEquals("name", "foo", attribute.name());
94 assertEquals("attributes", 0, attribute.attributes().size());
95 assertEquals("value", 1, attribute.children().size());
96 assertEquals("text", "the text", attribute.text());
97
98 dump(attribute);
99 }
100
101 public void testAttributeWithAttributesAndChildren() {
102 Map attributes = new HashMap();
103 attributes.put("a", "xyz");
104
105 List children = new ArrayList();
106 children.add(new Node(null, "person", "James"));
107 children.add(new Node(null, "person", "Bob"));
108 children.add("someText");
109
110 Node attribute = new Node(null, "foo", attributes, children);
111 assertEquals("name", "foo", attribute.name());
112 assertEquals("attributes", 1, attribute.attributes().size());
113 assertEquals("value", 3, attribute.children().size());
114 assertEquals("text", "someText", attribute.text());
115
116 dump(attribute);
117 }
118
119 public void testAttributeWithAttributesAndChildrenWithMixedText() {
120 Map attributes = new HashMap();
121 attributes.put("a", "xyz");
122
123 List children = new ArrayList();
124 children.add("someText");
125 Node node1 = new Node(null, "person", "James");
126 children.add(node1);
127 children.add("moreText");
128 Node node2 = new Node(null, "person", "Bob");
129 children.add(node2);
130 children.add("moreText");
131
132 Node attribute = new Node(null, "foo", attributes, children);
133 assertEquals("name", "foo", attribute.name());
134 assertEquals("attributes", 1, attribute.attributes().size());
135 assertEquals("value", 5, attribute.children().size());
136 assertEquals("text", "someTextmoreTextmoreText", attribute.text());
137
138
139
140 List list = (List) attribute.get("person");
141 assertEquals("Expected list size: " + list, 2, list.size());
142
143 assertEquals("Node1", node1, list.get(0));
144 assertEquals("Node2", node2, list.get(1));
145
146 dump(attribute);
147 }
148
149 public void testNavigationUsingQNames() throws Exception {
150 QName name1 = new QName("http://something", "foo", "f");
151
152 Node node = new Node(null, null, new ArrayList());
153 Node child = new Node(null, new QName("http://something", "foo", "f"), new HashMap(), new ArrayList());
154 child.attributes().put("cheese", "Edam");
155 Node grandChild = new Node(null, new QName("http://something", "bar", "f"), new HashMap(), new ArrayList());
156 grandChild.attributes().put("drink", "Beer");
157 grandChild.children().add("I am a youngling");
158 child.children().add(grandChild);
159
160 node.children().add(child);
161
162
163 Object value = node.getAt(name1);
164 assertTrue("Should return a list: " + value, value instanceof NodeList);
165 NodeList list = (NodeList) value;
166 assertEquals("Size", 1, list.size());
167
168 Node answer = (Node) list.get(0);
169 assertNotNull("Node is null!", answer);
170
171 System.out.println("Found node: " + answer);
172
173
174 NodeList gc = list.getAt(new QName("http://something", "bar"));
175 assertEquals("grand children size", 1, gc.size());
176
177 System.out.println("Found grandChild: " + gc);
178
179 String text= gc.text();
180 assertEquals("text of grandchild", "I am a youngling", text);
181 }
182
183 protected void dump(Node node) {
184 NodePrinter printer = new NodePrinter();
185 printer.print(node);
186 }
187
188 }