1 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 ; 23 import java.util.Iterator ; 24 25 public class IteratorTester extends AbstractTestCase { 26 private SOAPEnvelope envelope = null; 27 28 public IteratorTester(String testName) { 29 super(testName); 30 } 31 32 protected void setUp() throws Exception { 33 envelope = new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(new FileReader (getTestResourceFile("soap/soapmessage1.xml")))).getSOAPEnvelope(); 34 } 35 36 protected void tearDown() throws Exception { 37 envelope = null; 38 } 39 40 public void testIterator() { 41 OMElement elt = envelope; 42 Iterator iter = elt.getChildren(); 43 while (iter.hasNext()) { 44 assertNotNull(iter.next()); 45 } 46 47 } 48 49 52 public void testIteratorRemove1() { 53 OMElement elt = envelope; 54 Iterator iter = elt.getChildren(); 55 56 try { 58 iter.remove(); 59 fail("remove should throw an exception"); 60 } catch (IllegalStateException e) { 61 } 63 64 } 65 66 69 public void testIteratorRemove2() { 70 OMElement elt = envelope; 71 Iterator iter = elt.getChildren(); 72 if (iter.hasNext()) { 73 iter.next(); 74 } 75 iter.remove(); 76 77 try { 79 iter.remove(); 80 fail("calling remove twice without a call to next is prohibited"); 81 } catch (IllegalStateException e) { 82 } 84 85 } 86 87 90 public void testIteratorRemove3() { 91 OMElement elt = envelope; 92 Iterator 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 108 public void testIteratorRemove4() { 109 OMElement elt = envelope; 110 Iterator 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 iter.remove(); 120 121 iter = elt.getChildren(); 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 |