1 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 ; 24 import javax.xml.stream.XMLInputFactory; 25 import java.io.FileReader ; 26 import java.util.Iterator ; 27 28 public class OMChildrenQNameIteratorTest extends AbstractTestCase { 29 private SOAPEnvelope envelope = null; 30 StAXSOAPModelBuilder stAXSOAPModelBuilder; 31 32 public OMChildrenQNameIteratorTest(String testName) { 33 super(testName); 34 } 35 36 OMChildrenQNameIterator omChildrenQNameIterator; 37 38 protected void setUp() throws Exception { 39 stAXSOAPModelBuilder = new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(new FileReader (getTestResourceFile("soap/soapmessage1.xml")))); 40 envelope = stAXSOAPModelBuilder.getSOAPEnvelope(); 41 } 42 43 protected void tearDown() throws Exception { 44 envelope = null; 45 } 46 47 private QName getBodyQname() { 48 49 return new QName (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 66 public void testIteratorRemove1() { 67 OMElement elt = envelope; 68 Iterator iter = elt.getChildrenWithName(getBodyQname()); 69 70 try { 72 iter.remove(); 73 fail("remove should throw an exception"); 74 } catch (IllegalStateException e) { 75 } 77 78 } 79 80 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 try { 93 iter.remove(); 94 fail("calling remove twice without a call to next is prohibited"); 95 } catch (IllegalStateException e) { 96 } 98 99 } 100 101 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 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 iter.remove(); 134 135 iter = elt.getChildrenWithName(getBodyQname()); 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 |