KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > IteratorTester


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om;
17
18 import org.apache.axis2.soap.SOAPEnvelope;
19 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder;
20
21 import javax.xml.stream.XMLInputFactory;
22 import java.io.FileReader JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 public class IteratorTester extends AbstractTestCase {
26     private SOAPEnvelope envelope = null;
27
28     public IteratorTester(String JavaDoc testName) {
29         super(testName);
30     }
31
32     protected void setUp() throws Exception JavaDoc {
33         envelope = new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(new FileReader JavaDoc(getTestResourceFile("soap/soapmessage1.xml")))).getSOAPEnvelope();
34     }
35
36     protected void tearDown() throws Exception JavaDoc {
37         envelope = null;
38     }
39
40     public void testIterator() {
41         OMElement elt = envelope;
42         Iterator JavaDoc iter = elt.getChildren();
43         while (iter.hasNext()) {
44             assertNotNull(iter.next());
45         }
46
47     }
48
49     /**
50      * test the remove exception behavior
51      */

52     public void testIteratorRemove1() {
53         OMElement elt = envelope;
54         Iterator JavaDoc iter = elt.getChildren();
55
56         //this is supposed to throw an illegal state exception
57
try {
58             iter.remove();
59             fail("remove should throw an exception");
60         } catch (IllegalStateException JavaDoc e) {
61             //ok. this is what should happen
62
}
63
64     }
65
66     /**
67      * test the remove exception behavior, consecutive remove calls
68      */

69     public void testIteratorRemove2() {
70         OMElement elt = envelope;
71         Iterator JavaDoc iter = elt.getChildren();
72         if (iter.hasNext()) {
73             iter.next();
74         }
75         iter.remove();
76
77         //this call must generate an exception
78
try {
79             iter.remove();
80             fail("calling remove twice without a call to next is prohibited");
81         } catch (IllegalStateException JavaDoc e) {
82             //ok if we come here :)
83
}
84
85     }
86
87     /**
88      * Remove all!
89      */

90     public void testIteratorRemove3() {
91         OMElement elt = envelope;
92         Iterator JavaDoc iter = elt.getChildren();
93         while (iter.hasNext()) {
94             iter.next();
95             iter.remove();
96         }
97         iter = elt.getChildren();
98         if (iter.hasNext()) {
99             fail("No children should remain after removing all!");
100         }
101
102
103     }
104
105     /**
106      * test whether the children count reduces.
107      */

108     public void testIteratorRemove4() {
109         OMElement elt = envelope;
110         Iterator JavaDoc iter = elt.getChildren();
111         int firstChildrenCount = 0;
112         int secondChildrenCount = 0;
113         while (iter.hasNext()) {
114             assertNotNull((OMNode) iter.next());
115             firstChildrenCount++;
116         }
117
118         //this should remove the last node
119
iter.remove();
120
121         //reloop and check the count
122
//Note- here we should get a fresh iterator since there is no method to
123
//reset the iterator
124
iter = elt.getChildren(); //reset the iterator
125
while (iter.hasNext()) {
126             assertNotNull((OMNode) iter.next());
127             secondChildrenCount++;
128         }
129         assertEquals("children count must reduce from 1", firstChildrenCount - 1, secondChildrenCount);
130
131     }
132
133
134 }
135
Popular Tags