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