KickJava   Java API By Example, From Geeks To Geeks.

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


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.om.impl.llom.traverse.OMChildrenQNameIterator;
19 import org.apache.axis2.soap.SOAPEnvelope;
20 import org.apache.axis2.soap.impl.llom.SOAPConstants;
21 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder;
22
23 import javax.xml.namespace.QName JavaDoc;
24 import javax.xml.stream.XMLInputFactory;
25 import java.io.FileReader JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 public class OMChildrenQNameIteratorTest extends AbstractTestCase {
29     private SOAPEnvelope envelope = null;
30     StAXSOAPModelBuilder stAXSOAPModelBuilder;
31
32     public OMChildrenQNameIteratorTest(String JavaDoc testName) {
33         super(testName);
34     }
35
36     OMChildrenQNameIterator omChildrenQNameIterator;
37
38     protected void setUp() throws Exception JavaDoc {
39         stAXSOAPModelBuilder = new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(new FileReader JavaDoc(getTestResourceFile("soap/soapmessage1.xml"))));
40         envelope = stAXSOAPModelBuilder.getSOAPEnvelope();
41     }
42
43     protected void tearDown() throws Exception JavaDoc {
44         envelope = null;
45     }
46
47     private QName JavaDoc getBodyQname() {
48
49         return new QName JavaDoc(stAXSOAPModelBuilder.getEnvelopeNamespace().getName(),
50                 SOAPConstants.BODY_LOCAL_NAME,
51                 stAXSOAPModelBuilder.getEnvelopeNamespace().getPrefix());
52     }
53
54     public void testIterator() {
55         OMElement elt = envelope;
56         Iterator iter = elt.getChildrenWithName(getBodyQname());
57         while (iter.hasNext()) {
58             assertNotNull(iter.next());
59         }
60
61     }
62
63     /**
64      * test the remove exception behavior
65      */

66     public void testIteratorRemove1() {
67         OMElement elt = envelope;
68         Iterator iter = elt.getChildrenWithName(getBodyQname());
69
70         //this is supposed to throw an illegal state exception
71
try {
72             iter.remove();
73             fail("remove should throw an exception");
74         } catch (IllegalStateException JavaDoc e) {
75             //ok. this is what should happen
76
}
77
78     }
79
80     /**
81      * test the remove exception behavior, consecutive remove calls
82      */

83     public void testIteratorRemove2() {
84         OMElement elt = envelope;
85         Iterator iter = elt.getChildrenWithName(getBodyQname());
86         if (iter.hasNext()) {
87             iter.next();
88         }
89         iter.remove();
90
91         //this call must generate an exception
92
try {
93             iter.remove();
94             fail("calling remove twice without a call to next is prohibited");
95         } catch (IllegalStateException JavaDoc e) {
96             //ok if we come here :)
97
}
98
99     }
100
101     /**
102      * Remove all!
103      */

104     public void testIteratorRemove3() {
105         OMElement elt = envelope;
106         Iterator iter = elt.getChildrenWithName(getBodyQname());
107         while (iter.hasNext()) {
108             iter.next();
109             iter.remove();
110         }
111         iter = elt.getChildrenWithName(getBodyQname());
112         if (iter.hasNext()) {
113             fail("No children should remain after removing all!");
114         }
115
116
117     }
118
119     /**
120      * test whether the children count reduces.
121      */

122     public void testIteratorRemove4() {
123         OMElement elt = envelope;
124         Iterator iter = elt.getChildrenWithName(getBodyQname());
125         int firstChildrenCount = 0;
126         int secondChildrenCount = 0;
127         while (iter.hasNext()) {
128             assertNotNull((OMNode) iter.next());
129             firstChildrenCount++;
130         }
131
132         //this should remove the last node
133
iter.remove();
134
135         //reloop and check the count
136
//Note- here we should get a fresh iterator since there is no method to
137
//reset the iterator
138
iter = elt.getChildrenWithName(getBodyQname()); //reset the iterator
139
while (iter.hasNext()) {
140             assertNotNull((OMNode) iter.next());
141             secondChildrenCount++;
142         }
143         assertEquals("children count must reduce from 1", firstChildrenCount - 1, secondChildrenCount);
144
145     }
146
147 }
Popular Tags