KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > IteratorTest


1 /*
2  * Copyright 2001-2005 (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
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * A test harness to test the Iterator API in DOM4J
17  *
18  * @author <a HREF="mailto:jdoughty@jdoughty@cs.gmu.edu">Jonathan Doughty </a>
19  * @version $Revision: 1.4 $
20  */

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

182
Free Books   Free Magazines  
Popular Tags