1   /*
2    $Id: NodeTest.java,v 1.4 2005/09/02 16:47:31 jstrachan Exp $
3   
4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
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         // lets test get
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         // lets look up by QName
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         // now lets navigate the list
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 }