KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestContent


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: TestContent.java,v 1.4 2003/12/06 20:59:44 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Document;
16 import org.dom4j.Element;
17 import org.dom4j.Node;
18 import org.dom4j.ProcessingInstruction;
19 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 /** A test harness to test the content API in DOM4J
25   *
26   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
27   * @version $Revision: 1.4 $
28   */

29 public class TestContent extends AbstractTestCase {
30
31     public static void main( String JavaDoc[] args ) {
32         TestRunner.run( suite() );
33     }
34
35     public static Test suite() {
36         return new TestSuite( TestContent.class );
37     }
38
39     public TestContent(String JavaDoc name) {
40         super(name);
41     }
42
43     // Test case(s)
44
//-------------------------------------------------------------------------
45
public void testRoot() throws Exception JavaDoc {
46         Element root = document.getRootElement();
47         assertTrue( "Has root element", root != null );
48
49         List JavaDoc authors = root.elements( "author" );
50         assertTrue( "Root has children", authors != null && authors.size() == 2 );
51
52         Element author1 = (Element) authors.get(0);
53         Element author2 = (Element) authors.get(1);
54
55         assertTrue( "Author1 is James", author1.attributeValue( "name" ).equals( "James" ) );
56         assertTrue( "Author2 is Bob", author2.attributeValue( "name" ).equals( "Bob" ) );
57
58         testGetAttributes(author1);
59         testGetAttributes(author2);
60     }
61
62     public void testContent() throws Exception JavaDoc {
63         Element root = document.getRootElement();
64         assertTrue( "Has root element", root != null );
65
66         List JavaDoc content = root.content();
67         assertTrue( "Root has content", content != null && content.size() >= 2 );
68
69         boolean iterated = false;
70         for ( Iterator JavaDoc iter = content.iterator(); iter.hasNext(); ) {
71             Object JavaDoc object = iter.next();
72             assertTrue( "Content object is a node", object instanceof Node );
73             iterated = true;
74         }
75
76         assertTrue( "Iteration completed", iterated );
77     }
78
79     public void testGetNode() throws Exception JavaDoc {
80         Element root = document.getRootElement();
81         assertTrue( "Has root element", root != null );
82
83         int count = root.nodeCount();
84         assertTrue( "Root has correct node count", count == 2 );
85
86         boolean iterated = false;
87         for ( int i = 0; i < count; i++ ) {
88             Node node = root.node(i);
89             assertTrue( "Valid node returned from node()", node != null );
90             iterated = true;
91         }
92
93         assertTrue( "Iteration completed", iterated );
94     }
95
96     public void testGetXPathNode() throws Exception JavaDoc {
97         Element root = document.getRootElement();
98         assertTrue( "Has root element", root != null );
99
100         int count = root.nodeCount();
101         assertTrue( "Root has correct node count", count == 2 );
102
103         boolean iterated = false;
104         for ( int i = 0; i < count; i++ ) {
105             Node node = root.getXPathResult(i);
106             assertTrue( "Valid node returned from node()", node != null );
107             assertTrue( "Node supports the parent relationship", node.supportsParent() );
108             iterated = true;
109         }
110
111         assertTrue( "Iteration completed", iterated );
112     }
113
114     public void testOrderOfPI() throws Exception JavaDoc {
115         Document document = nodeFactory.createDocument();
116         document.addProcessingInstruction( "xml-stylesheet", "type=\"text/xsl\" HREF=\"...\"" );
117         document.addElement( "root" );
118
119         List JavaDoc list = document.content();
120         Object JavaDoc pi = list.get(0);
121         Object JavaDoc root = list.get(1);
122
123         assertTrue( "First element is a PI", pi instanceof ProcessingInstruction );
124         assertTrue( "Second element is an element", root instanceof Element );
125
126         document = O3DocumentHelper.parseText(
127             "<?xml version=\"1.0\" ?>\n"
128             + "<?xml-stylesheet type=\"text/xsl\" HREF=\"foo\" ?>\n"
129             + "<root/>"
130         );
131
132         list = document.content();
133         pi = list.get(0);
134         root = list.get(1);
135
136         assertTrue( "First element is a PI", pi instanceof ProcessingInstruction );
137         assertTrue( "Second element is an element", root instanceof Element );
138
139     }
140
141     public void testAddingInTheMiddle() throws Exception JavaDoc {
142         Document doc = nodeFactory.createDocument();
143         Element root = doc.addElement( "html" );
144         Element header = root.addElement( "header" );
145         Element footer = root.addElement( "footer" );
146
147         // now lets add <foo> in between header & footer
148
List JavaDoc list = root.content();
149         Element foo = nodeFactory.createElement( "foo" );
150         list.add( 1, foo );
151
152         // assertions
153
assertTrue( list.size() == 3 );
154         assertTrue( list.get(0) == header );
155         assertTrue( list.get(1) == foo );
156         assertTrue( list.get(2) == footer );
157     }
158
159     public void testAddAtIndex() throws Exception JavaDoc {
160       System.out.println("testAddAtIndex");
161         Document doc = nodeFactory.createDocument();
162       System.out.println("document is " + doc);
163         Element root = doc.addElement( "html" );
164       System.out.println("root is " + root);
165         Element header = root.addElement( "header" );
166       System.out.println("header is " + header);
167         Element body = root.addElement( "body" );
168       System.out.println("body is " + body);
169
170         Element foo = nodeFactory.createElement( "foo" );
171         System.out.println("created element " + foo);
172         Element bar = nodeFactory.createElement( "bar" );
173         System.out.println("created element " + bar);
174
175         List JavaDoc content = header.content();
176       System.out.println("got content " + content.getClass().getName());
177         content.add(0, foo);
178         content.add(0, bar);
179       System.out.println("content size is now " + content.size());
180
181       System.out.println("checking header node 1 " + header.node(1));
182         assertNotNull("checking header node 1: " + header.node(1));
183         assertEquals( "foo", header.node(1).getName() );
184       System.out.println("checking header node 0");
185         assertNotNull("checking header node 0: " + header.node(0));
186         assertEquals( "bar", header.node(0).getName() );
187
188         foo = nodeFactory.createElement( "foo" );
189         bar = nodeFactory.createElement( "bar" );
190
191         content = body.content();
192       System.out.println("Adding foo to body");
193         content.add(0, foo);
194       System.out.println("Adding bar to body");
195         content.add(1, bar);
196
197         assertNotNull("checking body node 0: " + body.node(0));
198         assertEquals( "foo", body.node(0).getName() );
199         assertNotNull("checking body node 1: " + body.node(1));
200         assertEquals( "bar", body.node(1).getName() );
201     }
202
203
204
205     // Implementation methods
206
//-------------------------------------------------------------------------
207
protected void testGetAttributes(Element author) throws Exception JavaDoc {
208
209         String JavaDoc definedName = "name";
210         String JavaDoc undefinedName = "undefined-attribute-name";
211         String JavaDoc defaultValue = "** Default Value **";
212
213         String JavaDoc value = author.attributeValue( definedName, defaultValue );
214         assertTrue( "Defined value doesn't return specified default value", value != defaultValue );
215
216         value = author.attributeValue( undefinedName, defaultValue );
217         assertTrue( "Undefined value returns specified default value", value == defaultValue );
218     }
219
220 }
221
222
223
224
225 /*
226  * Redistribution and use of this software and associated documentation
227  * ("Software"), with or without modification, are permitted provided
228  * that the following conditions are met:
229  *
230  * 1. Redistributions of source code must retain copyright
231  * statements and notices. Redistributions must also contain a
232  * copy of this document.
233  *
234  * 2. Redistributions in binary form must reproduce the
235  * above copyright notice, this list of conditions and the
236  * following disclaimer in the documentation and/or other
237  * materials provided with the distribution.
238  *
239  * 3. The name "DOM4J" must not be used to endorse or promote
240  * products derived from this Software without prior written
241  * permission of MetaStuff, Ltd. For written permission,
242  * please contact dom4j-info@metastuff.com.
243  *
244  * 4. Products derived from this Software may not be called "DOM4J"
245  * nor may "DOM4J" appear in their names without prior written
246  * permission of MetaStuff, Ltd. DOM4J is a registered
247  * trademark of MetaStuff, Ltd.
248  *
249  * 5. Due credit should be given to the DOM4J Project
250  * (http://dom4j.org/).
251  *
252  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
253  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
254  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
255  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
256  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
257  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
258  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
259  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
261  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
262  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
263  * OF THE POSSIBILITY OF SUCH DAMAGE.
264  *
265  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
266  *
267  * $Id: TestContent.java,v 1.4 2003/12/06 20:59:44 per_nyfelt Exp $
268  */

269
Popular Tags