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: TestIterator.java,v 1.5 2004/06/25 08:03:47 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  
20  /*** A test harness to test the Iterator API in DOM4J
21    *
22    * @author <a href="mailto:jdoughty@jdoughty@cs.gmu.edu">Jonathan Doughty</a>
23    * @version $Revision: 1.5 $
24    */
25  public class TestIterator extends AbstractTestCase {
26  
27      protected Document iterDocument;
28      private static final int NUMELE = 10;
29  
30      public static void main( String[] args ) {
31          TestRunner.run( suite() );
32      }
33      
34      public static Test suite() {
35          return new TestSuite( TestIterator.class );
36      }
37      
38      public TestIterator(String name) {
39          super(name);
40      }
41  
42      protected void setUp() throws Exception {
43          iterDocument = createDocument();
44          
45          Element root = iterDocument.addElement( "root" );
46  
47          for (int i = 0; i < NUMELE; i++) {
48              root.addElement( "iterator test")
49                  .addAttribute( "instance", Integer.toString(i));
50          }
51      }
52  
53      // Test case(s)    
54      //-------------------------------------------------------------------------                   
55  
56      public void testElementCount() throws Exception {
57          Element root = iterDocument.getRootElement();
58          assertTrue( "Has root element", root != null );
59          
60          List elements = root.elements( "iterator test" );
61          int elementSize = elements.size();
62          assertTrue( "Root has " + elementSize + " children",
63                      elements != null && elementSize == NUMELE );
64          
65      }
66  
67      public void testPlainIteration() throws Exception {
68          Element root = iterDocument.getRootElement();
69          List elements = root.elements( "iterator test" );
70          Iterator iter = root.elementIterator( "iterator test" );
71          int elementSize = elements.size();
72          
73          int count = 0;
74          for (; iter.hasNext();) {
75              Element e = (Element) iter.next();
76              assertTrue("instance " + e.attribute("instance").getValue() +
77                         " equals "+ count,
78                         e.attribute("instance").getValue().equals(
79                             Integer.toString(count)));
80              count++;
81          }
82          
83          assertTrue( elementSize + " elements iterated", count == elementSize );
84      }
85  
86      public void testSkipAlternates() throws Exception {
87          Element root = iterDocument.getRootElement();
88          List elements = root.elements( "iterator test" );
89          Iterator iter = root.elementIterator( "iterator test" );
90          int elementSize = elements.size();
91          int count = 0;
92          for (; iter.hasNext();) {
93              Element e = (Element) iter.next();
94              assertTrue("instance " + e.attribute("instance").getValue() +
95                         " equals "+ count*2,
96                         e.attribute("instance").getValue().equals(
97                             Integer.toString(count*2)));
98              iter.next();
99              count++;
100         }
101         assertTrue( (elementSize/2) + " alternate elements iterated",
102                     count == (elementSize/2) );
103     }
104 
105     public void testNoHasNext() throws Exception {
106         Element root = iterDocument.getRootElement();
107         List elements = root.elements( "iterator test" );
108         Iterator iter = root.elementIterator( "iterator test" );
109         int elementSize = elements.size();
110         int count = 0;
111         Element e = null;
112         for (; count < elementSize; ) {
113             e = (Element) iter.next();
114             assertTrue("instance " + e.attribute("instance").getValue() +
115                        " equals "+ count,
116                        e.attribute("instance").getValue().equals(
117                            Integer.toString(count)));
118             System.out.println("instance " +
119                                e.attribute("instance").getValue() +
120                                " equals "+ count);
121             count++;
122         }
123         try {
124             e = (Element) iter.next();
125             if (e != null) {
126                 // Real Iterators wouldn't get here
127                 assertTrue( "no more elements,value instead is " +
128                             e.attribute("instance").getValue(), e == null);
129             }
130         }
131         catch (Exception exp) {
132             assertTrue( "Real iterators throw NoSuchElementException",
133                         exp instanceof java.util.NoSuchElementException);
134         }
135     }
136 
137     public void testExtraHasNexts() throws Exception {
138         Element root = iterDocument.getRootElement();
139         List elements = root.elements( "iterator test" );
140         Iterator iter = root.elementIterator( "iterator test" );
141         int elementSize = elements.size();
142         int count = 0;
143         for (; iter.hasNext();) {
144             Element e = (Element) iter.next();
145             assertTrue("instance " + e.attribute("instance").getValue() +
146                        " equals "+ count,
147                        e.attribute("instance").getValue().equals(
148                            Integer.toString(count)));
149             iter.hasNext();
150             count++;
151         }
152         assertTrue( elementSize + " elements iterated with extra hasNexts",
153                     count == elementSize );
154     }
155 }
156 
157 
158 
159 
160 /*
161  * Redistribution and use of this software and associated documentation
162  * ("Software"), with or without modification, are permitted provided
163  * that the following conditions are met:
164  *
165  * 1. Redistributions of source code must retain copyright
166  *    statements and notices.  Redistributions must also contain a
167  *    copy of this document.
168  *
169  * 2. Redistributions in binary form must reproduce the
170  *    above copyright notice, this list of conditions and the
171  *    following disclaimer in the documentation and/or other
172  *    materials provided with the distribution.
173  *
174  * 3. The name "DOM4J" must not be used to endorse or promote
175  *    products derived from this Software without prior written
176  *    permission of MetaStuff, Ltd.  For written permission,
177  *    please contact dom4j-info@metastuff.com.
178  *
179  * 4. Products derived from this Software may not be called "DOM4J"
180  *    nor may "DOM4J" appear in their names without prior written
181  *    permission of MetaStuff, Ltd. DOM4J is a registered
182  *    trademark of MetaStuff, Ltd.
183  *
184  * 5. Due credit should be given to the DOM4J Project - 
185  *    http://www.dom4j.org
186  *
187  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
188  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
189  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
190  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
191  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
192  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
193  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
194  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
195  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
196  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
197  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
198  * OF THE POSSIBILITY OF SUCH DAMAGE.
199  *
200  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
201  *
202  * $Id: TestIterator.java,v 1.5 2004/06/25 08:03:47 maartenc Exp $
203  */