KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > DomNodeTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Map;
45
46 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
47 import com.gargoylesoftware.htmlunit.WebTestCase;
48
49 /**
50  * Tests for DomNode
51  *
52  * @version $Revision: 1.5 $
53  * @author Chris Erskine
54  */

55 public class DomNodeTest extends WebTestCase {
56
57     /**
58      * Create an instance
59      *
60      * @param name Name of the test
61      */

62     public DomNodeTest( final String name ) {
63         super( name );
64     }
65
66     /**
67      * @throws Exception if the test fails
68      */

69     public void testRemoveAllChildren() throws Exception {
70         final String content
71             = "<html><head></head><body>\n"
72             + "<p id='tag'><table>\n"
73             + "<tr><td>row 1</td></tr>\n"
74             + "<tr><td>row 2</td></tr>\n"
75             + "</table></p></body></html>\n";
76         final List collectedAlerts = new ArrayList();
77         final HtmlPage page = loadPage(content, collectedAlerts);
78
79         final DomNode node = page.getDocumentElement().getHtmlElementById("tag");
80         node.removeAllChildren();
81         assertEquals("Did not remove all nodes", null, node.getFirstChild());
82     }
83
84     /**
85      * @throws Exception if the test fails
86      */

87     public void testReplace() throws Exception {
88         final String content
89             = "<html><head></head><body>\n"
90             + "<br><div id='tag'/><br></body></html>\n";
91         final HtmlPage page = loadPage(content);
92
93         final DomNode node = page.getDocumentElement().getHtmlElementById("tag");
94         
95         final DomNode previousSibling = node.getPreviousSibling();
96         final DomNode nextSibling = node.getNextSibling();
97         final DomNode parent = node.getParentNode();
98         
99         // position among parent's children
100
final int position = readPositionAmongParentChildren(node);
101
102         final DomNode newNode = new DomText(page, "test");
103         node.replace(newNode);
104         assertSame("previous sibling", previousSibling, newNode.getPreviousSibling());
105         assertSame("next sibling", nextSibling, newNode.getNextSibling());
106         assertSame("parent", parent, newNode.getParentNode());
107         assertSame(newNode, previousSibling.getNextSibling());
108         assertSame(newNode, nextSibling.getPreviousSibling());
109         assertEquals(position, readPositionAmongParentChildren(newNode));
110     }
111
112     /**
113      * @throws Exception if the test fails
114      */

115     public void testGetNewNodeById() throws Exception {
116         final String content
117             = "<html><head></head><body>\n"
118             + "<br><div id='tag'/></body></html>\n";
119         final HtmlPage page = loadPage(content);
120
121         final DomNode node = page.getDocumentElement().getHtmlElementById("tag");
122         
123         final Map attributes = new HashMap();
124         attributes.put("id", "newElt");
125         final DomNode newNode = new HtmlDivision(page, attributes);
126         try {
127             page.getHtmlElementById("newElt");
128             fail("Element should not exist yet");
129         }
130         catch (final ElementNotFoundException e) {
131             // nothing to do, it's ok
132
}
133
134         node.replace(newNode);
135
136         page.getHtmlElementById("newElt");
137         try {
138             page.getHtmlElementById("tag");
139             fail("Element should not exist anymore");
140         }
141         catch (final ElementNotFoundException e) {
142             // nothing to do, it's ok
143
}
144         
145         newNode.insertBefore(node);
146         page.getHtmlElementById("tag");
147     }
148
149     /**
150      * @throws Exception if the test fails
151      */

152     public void testAppendChild() throws Exception {
153         final String content
154             = "<html><head></head><body>\n"
155             + "<br><div><div id='tag'/></div><br></body></html>\n";
156         final HtmlPage page = loadPage(content);
157
158         final DomNode node = page.getDocumentElement().getHtmlElementById("tag");
159         
160         final DomNode parent = node.getParentNode();
161         
162         // position among parent's children
163
final int position = readPositionAmongParentChildren(node);
164
165         final DomNode newNode = new DomText(page, "test");
166         parent.appendChild(newNode);
167         assertSame("new node previous sibling", node, newNode.getPreviousSibling());
168         assertSame("new node next sibling", null, newNode.getNextSibling());
169         assertSame("next sibling", newNode, node.getNextSibling());
170         assertSame("parent", parent, newNode.getParentNode());
171         assertEquals(position+1, readPositionAmongParentChildren(newNode));
172
173         final DomNode newNode2 = new DomText(page, "test2");
174         parent.appendChild(newNode2);
175         page.getHtmlElementById("tag");
176     }
177
178     /**
179      * @throws Exception if the test fails
180      */

181     public void testInsertBefore() throws Exception {
182         final String content
183             = "<html><head></head><body>\n"
184             + "<br><div id='tag'/><br></body></html>\n";
185         final HtmlPage page = loadPage(content);
186
187         final DomNode node = page.getDocumentElement().getHtmlElementById("tag");
188         
189         final DomNode previousSibling = node.getPreviousSibling();
190         final DomNode nextSibling = node.getNextSibling();
191         final DomNode parent = node.getParentNode();
192         
193         // position among parent's children
194
final int position = readPositionAmongParentChildren(node);
195
196         final DomNode newNode = new DomText(page, "test");
197         node.insertBefore(newNode);
198         assertSame("new node previous sibling", previousSibling, newNode.getPreviousSibling());
199         assertSame("previous sibling", newNode, node.getPreviousSibling());
200         assertSame("new node next sibling", node, newNode.getNextSibling());
201         assertSame("next sibling", nextSibling, node.getNextSibling());
202         assertSame("parent", parent, newNode.getParentNode());
203         assertSame(newNode, previousSibling.getNextSibling());
204         assertSame(node, nextSibling.getPreviousSibling());
205         assertEquals(position, readPositionAmongParentChildren(newNode));
206     }
207
208     /**
209      * Reads the position of the node amongs the children of its parent
210      * @param node the node to look at
211      * @return the position
212      */

213     private int readPositionAmongParentChildren(final DomNode node) {
214         int i = 0;
215         for (final Iterator iter = node.getParentNode().getChildIterator(); iter.hasNext();) {
216             final DomNode child = (DomNode) iter.next();
217             if (child == node) {
218                 return i;
219             }
220             ++i;
221         }
222
223         return -1;
224     }
225 }
226
Popular Tags