KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestIterator


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: TestIterator.java,v 1.1 2003/07/07 10:30:29 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
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21
22 /** A test harness to test the Iterator API in DOM4J
23   *
24   * @author <a HREF="mailto:jdoughty@jdoughty@cs.gmu.edu">Jonathan Doughty</a>
25   * @version $Revision: 1.1 $
26   */

27 public class TestIterator extends AbstractTestCase {
28
29     protected Document iterDocument;
30     private static final int NUMELE = 10;
31
32     public static void main( String JavaDoc[] args ) {
33         TestRunner.run( suite() );
34     }
35
36     public static Test suite() {
37         return new TestSuite( TestIterator.class );
38     }
39
40     public TestIterator(String JavaDoc name) {
41         super(name);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45         iterDocument = createDocument();
46
47         Element root = iterDocument.addElement( "root" );
48
49         for (int i = 0; i < NUMELE; i++) {
50             root.addElement( "iterator test")
51                 .addAttribute( "instance", Integer.toString(i));
52         }
53     }
54
55     // Test case(s)
56
//-------------------------------------------------------------------------
57

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

206
Popular Tags