001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.saaj;
020:
021: import junit.framework.TestCase;
022: import org.apache.axiom.om.impl.dom.NodeImpl;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.soap.MessageFactory;
026: import javax.xml.soap.Name;
027: import javax.xml.soap.Node;
028: import javax.xml.soap.SOAPBody;
029: import javax.xml.soap.SOAPConstants;
030: import javax.xml.soap.SOAPElement;
031: import javax.xml.soap.SOAPEnvelope;
032: import javax.xml.soap.SOAPException;
033: import javax.xml.soap.SOAPHeader;
034: import javax.xml.soap.SOAPMessage;
035: import javax.xml.soap.SOAPPart;
036: import javax.xml.soap.Text;
037: import java.util.Iterator;
038: import java.util.List;
039:
040: public class SOAPElementTest extends TestCase {
041:
042: private SOAPElement soapEle;
043:
044: protected void setUp() throws Exception {
045: soapEle = SOAPFactoryImpl.newInstance().createElement("Test",
046: "test", "http://test.apache.org/");
047: }
048:
049: public void testAddTextNode() {
050: assertNotNull(soapEle);
051: String value = "foo";
052: try {
053: soapEle.addTextNode(value);
054: } catch (SOAPException e) {
055: fail("Unexpected Exception " + e);
056: }
057: assertEquals(value, soapEle.getValue());
058: TextImplEx text = assertContainsText(soapEle);
059: assertEquals(value, text.getValue());
060: }
061:
062: public void testChildren() {
063: try {
064: soapEle.addTextNode("foo");
065: SOAPElement childEle1 = SOAPFactoryImpl.newInstance()
066: .createElement("Child1", "ch",
067: "http://test.apache.org/");
068: SOAPElement childEle2 = SOAPFactoryImpl.newInstance()
069: .createElement("Child2", "ch",
070: "http://test.apache.org/");
071: soapEle.addChildElement(childEle1);
072: soapEle.addChildElement(childEle2);
073: } catch (SOAPException e) {
074: fail("Unexpected Exception " + e);
075: }
076:
077: Object o = soapEle.getChildElements().next();
078: Object o2 = soapEle.getChildElements().next();
079:
080: assertSame(o, o2); // both elements should be the same SAAJ Node
081: assertEquals(((javax.xml.soap.Text) o).getValue(),
082: ((javax.xml.soap.Text) o2).getValue());
083:
084: int childrenCount = 0;
085: for (Iterator iter = soapEle.getChildElements(); iter.hasNext();) {
086: iter.next();
087: childrenCount++;
088: }
089: assertEquals(3, childrenCount);
090:
091: Object z1 = soapEle.getChildNodes().item(0);
092: Object z2 = soapEle.getFirstChild();
093:
094: assertSame(o, z1); // should be same SAAJ Node
095: assertSame(z1, z2); // should be same SAAJ Node
096:
097: assertEquals(((javax.xml.soap.Text) z1).getValue(),
098: ((javax.xml.soap.Text) z2).getValue());
099:
100: Node lastChildNode = (Node) soapEle.getLastChild();
101: SOAPElement lastChildSOAPEle = (SOAPElement) lastChildNode;
102:
103: assertEquals("Child2", lastChildSOAPEle.getLocalName());
104: assertEquals("http://test.apache.org/", lastChildSOAPEle
105: .getNamespaceURI());
106: assertEquals("ch", lastChildSOAPEle.getPrefix());
107: }
108:
109: public void testChildrenAndSiblings() {
110: try {
111: soapEle.addTextNode("foo");
112: soapEle.addChildElement("Child1", "ch",
113: "http://test.apache.org/");
114: soapEle.addChildElement("Child2", "ch",
115: "http://test.apache.org/");
116: } catch (SOAPException e) {
117: fail("Unexpected Exception " + e);
118: }
119:
120: Object o = soapEle.getChildElements().next();
121: Object o2 = soapEle.getChildElements().next();
122: assertSame(o, o2); // both elements should be the same SAAJ Node
123: assertEquals(((javax.xml.soap.Text) o).getValue(),
124: ((javax.xml.soap.Text) o2).getValue());
125:
126: int childrenCount = 0;
127: for (Iterator iter = soapEle.getChildElements(); iter.hasNext();) {
128: iter.next();
129: childrenCount++;
130: }
131: assertEquals(3, childrenCount);
132:
133: Object z1 = soapEle.getChildNodes().item(0);
134: Object z2 = soapEle.getFirstChild();
135: assertSame(o, z1); // should be same SAAJ Node
136: assertSame(z1, z2); // should be same SAAJ Node
137: assertEquals(((javax.xml.soap.Text) z1).getValue(),
138: ((javax.xml.soap.Text) z2).getValue());
139:
140: SOAPElement lastChildSOAPEle = (SOAPElement) soapEle
141: .getLastChild();
142:
143: assertEquals("Child2", lastChildSOAPEle.getLocalName());
144: assertEquals("ch:Child2", lastChildSOAPEle.getNodeName());
145: assertEquals("http://test.apache.org/", lastChildSOAPEle
146: .getNamespaceURI());
147: assertEquals("ch", lastChildSOAPEle.getPrefix());
148: assertNotNull(lastChildSOAPEle.getParentNode());
149: assertTrue(lastChildSOAPEle.getPreviousSibling() instanceof javax.xml.soap.SOAPElement);
150: assertNull(lastChildSOAPEle.getNextSibling());
151:
152: javax.xml.soap.Node firstChild = (javax.xml.soap.Node) soapEle
153: .getFirstChild();
154: javax.xml.soap.Node nextSibling = (javax.xml.soap.Node) (firstChild
155: .getNextSibling());
156: assertNull(firstChild.getPreviousSibling());
157:
158: assertTrue(firstChild instanceof javax.xml.soap.Text);
159: assertTrue(nextSibling instanceof javax.xml.soap.SOAPElement);
160: assertTrue(nextSibling.getPreviousSibling() instanceof javax.xml.soap.Text);
161: assertEquals("Child1", nextSibling.getLocalName());
162: assertEquals("ch:Child1", nextSibling.getNodeName());
163: assertEquals("http://test.apache.org/", nextSibling
164: .getNamespaceURI());
165: assertEquals("ch", nextSibling.getPrefix());
166:
167: javax.xml.soap.Node nextSibling2 = (javax.xml.soap.Node) nextSibling
168: .getNextSibling();
169: assertEquals("Child2", nextSibling2.getLocalName());
170: assertEquals("ch:Child2", nextSibling2.getNodeName());
171: assertEquals("http://test.apache.org/", lastChildSOAPEle
172: .getNamespaceURI());
173: assertEquals("ch", nextSibling2.getPrefix());
174: assertNull(nextSibling2.getNextSibling());
175: }
176:
177: public void testCommentSibling() {
178: try {
179: soapEle.addTextNode("foo");
180: soapEle.addChildElement("Child1", "ch",
181: "http://test.apache.org/");
182: soapEle.addTextNode("<!-- This is a Comment-->");
183: soapEle.addChildElement("Child2", "ch",
184: "http://test.apache.org/");
185: } catch (SOAPException e) {
186: fail("Unexpected Exception " + e);
187: }
188:
189: assertTrue(((Text) soapEle.getFirstChild().getNextSibling()
190: .getNextSibling()).isComment());
191: assertTrue(((Text) soapEle.getLastChild().getPreviousSibling())
192: .isComment());
193: }
194:
195: public void testCommentSibling2() {
196: try {
197: soapEle.addTextNode("foo");
198: soapEle.addTextNode("<!-- This is a Comment-->");
199: soapEle.addTextNode("bar");
200: soapEle.addChildElement("Child1", "ch",
201: "http://test.apache.org/");
202: soapEle.addChildElement("Child2", "ch",
203: "http://test.apache.org/");
204: } catch (SOAPException e) {
205: fail("Unexpected Exception " + e);
206: }
207:
208: assertTrue(((Text) soapEle.getFirstChild().getNextSibling())
209: .isComment());
210: assertFalse(((Text) soapEle.getLastChild().getPreviousSibling()
211: .getPreviousSibling()).isComment());
212: assertFalse(((Text) soapEle.getLastChild().getPreviousSibling()
213: .getPreviousSibling()).isComment());
214: }
215:
216: public void testAddChildElement() {
217: try {
218: String s = "MyName1";
219: String p = "MyPrefix1";
220: String u = "myURI";
221: SOAPBody body = MessageFactory.newInstance()
222: .createMessage().getSOAPBody();
223: SOAPElement myse = body.addNamespaceDeclaration(p, u);
224: SOAPElement se = body.addChildElement(s, p);
225: if (se == null) {
226: fail("SOAPElement was null");
227: } else {
228: Iterator i = body.getChildElements();
229: int count = getIteratorCount(i);
230: i = body.getChildElements();
231: if (count != 1) {
232: fail("Wrong iterator count returned of " + count
233: + ", expected 1");
234: } else {
235: SOAPElement se2 = (SOAPElement) i.next();
236: if (!se.equals(se2)) {
237: fail("Elements not equal");
238: }
239: }
240: String name = se.getElementName().getLocalName();
241: Name n = se.getElementName();
242: String prefix = se.getElementName().getPrefix();
243: if (!name.equals(s) || !prefix.equals(p)) {
244: fail("addChildElement() did not return correct local name and prefix");
245: }
246: }
247: } catch (Exception e) {
248: e.printStackTrace();
249: fail("Exception: " + e);
250: }
251: }
252:
253: public void testAddChildElement2() {
254: boolean pass = true;
255: try {
256: SOAPMessage msg = MessageFactory.newInstance(
257: SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
258: SOAPEnvelope soapEnvelope = msg.getSOAPPart().getEnvelope();
259: SOAPBody body = msg.getSOAPBody();
260:
261: Name name = soapEnvelope.createName("MyChild1");
262: //Add child element Name object with localName=MyChild1
263: SOAPElement se = body.addChildElement(name);
264: if (se == null) {
265: fail("addChildElement() did not return SOAPElement");
266: //pass = false;
267: } else {
268: //Find the child element just added
269: Iterator childs = body.getChildElements(name);
270: int count = 0;
271: while (childs.hasNext()) {
272: Object obj = (Object) childs.next();
273: count++;
274: }
275:
276: childs = body.getChildElements(name);
277: assertTrue(count == 1);
278:
279: SOAPElement se2 = (SOAPElement) childs.next();
280: assertEquals(se, se2);
281: //se = se2 (expected)
282:
283: //Retrieve the SOAPElement Name
284: Name n = se.getElementName();
285: //System.out.println("localName="+n.getLocalName()+" prefix="
286: // +n.getPrefix()+" URI="+n.getURI()+" qualifiedName="
287: // +n.getQualifiedName());
288: assertEquals(n, name);
289: //if (!n.equals(name)) {
290: //System.out.println("Name objects are not equal (unexpected)");
291: //System.out.println("addChildElement() did not return " +
292: //"correct Name object expected localName=" +
293: //name.getLocalName() + ", got localName="
294: //+ n.getLocalName());
295: //}
296:
297: //Name objects are equal (expected)
298: }
299:
300: } catch (Exception e) {
301: fail("Exception: " + e);
302: }
303: }
304:
305: public void testAddTextNode2() {
306: try {
307: SOAPMessage msg = MessageFactory.newInstance()
308: .createMessage();
309: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
310: SOAPBody body = envelope.getBody();
311: Iterator iStart = envelope.getChildElements();
312: int countStart = getIteratorCount(iStart);
313: SOAPElement se = envelope
314: .addTextNode("<txt>This is text</txt>");
315: if (se == null) {
316: fail("addTextNode() did not return SOAPElement");
317: } else if (!envelope.getValue().equals(
318: "<txt>This is text</txt>")) {
319: String s = body.getValue();
320: fail("addTextNode() did not return expected text, Returned "
321: + s + ", Expected <txt>This is text</txt>");
322: }
323: Iterator i = envelope.getChildElements();
324: int count = getIteratorCount(i);
325: i = envelope.getChildElements();
326: if (count != ++countStart) {
327: fail("Wrong iterator count returned of " + count
328: + ", expected " + countStart);
329: } else {
330: Object obj = null;
331: while (i.hasNext()) {
332: obj = i.next();
333: if (obj instanceof Text) {
334: break;
335: }
336: }
337: if (!(obj instanceof Text)) {
338: fail("obj is not instanceof Text");
339: }
340: }
341: } catch (Exception e) {
342: fail("Exception: " + e);
343: }
344: }
345:
346: public void testRemoveAttribute() {
347: try {
348: SOAPMessage msg = MessageFactory.newInstance()
349: .createMessage();
350: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
351: SOAPBody body = envelope.getBody();
352: Name name = envelope.createName("MyAttr1");
353: String value = "MyValue1";
354: body.addAttribute(name, value);
355: boolean b = body.removeAttribute(name);
356: assertTrue("removeAttribute() did not return true", b);
357: b = body.removeAttribute(name);
358: assertFalse("removeAttribute() did not return false", b);
359: assertNull(body.getAttributeValue(name));
360: } catch (Exception e) {
361: fail("Exception: " + e);
362: }
363: }
364:
365: public void testRemoveAttribute2() {
366: try {
367: SOAPMessage msg = MessageFactory.newInstance()
368: .createMessage();
369: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
370: SOAPBody body = envelope.getBody();
371:
372: QName name = new QName("MyAttr1");
373: String value = "MyValue1";
374: body.addAttribute(name, value);
375: boolean b = body.removeAttribute(name);
376: assertTrue(b);
377:
378: b = body.removeAttribute(name);
379: if (b) {
380: //removeAttribute() did not return false
381: fail();
382: }
383: //getAttributeValue should return null
384: assertNull(body.getAttributeValue(name));
385: } catch (Exception e) {
386: fail("Error : " + e);
387: }
388: }
389:
390: public void testRemoveAttributeName() {
391: try {
392: SOAPMessage msg = MessageFactory.newInstance()
393: .createMessage();
394: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
395: SOAPBody body = envelope.getBody();
396:
397: Name name = envelope.createName("MyAttr1");
398: String value = "MyValue1";
399: body.addAttribute(name, value);
400: boolean b = body.removeAttribute(name);
401: assertTrue(b);
402:
403: b = body.removeAttribute(name);
404: assertTrue(!b);
405:
406: String s = body.getAttributeValue(name);
407: assertNull(s);
408: } catch (Exception e) {
409: fail("Failed : " + e);
410: }
411: }
412:
413: public void _testRemoveAttributeQName() {
414: try {
415: SOAPMessage msg = MessageFactory.newInstance(
416: SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
417: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
418: SOAPBody body = envelope.getBody();
419:
420: QName name = new QName("MyAttr1");
421: String value = "MyValue1";
422: body.addAttribute(name, value);
423: boolean b = body.removeAttribute(name);
424: assertTrue(b);
425: b = body.removeAttribute(name);
426: assertTrue(!b);
427:
428: assertNull(body.getAttributeValue(name));
429: } catch (Exception e) {
430: fail();
431: }
432:
433: }
434:
435: public void testRemoveNamespaceDeclaration() {
436: try {
437: String prefix = "myPrefix";
438: String uri = "myURI";
439: SOAPMessage msg = MessageFactory.newInstance()
440: .createMessage();
441: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
442: SOAPBody body = envelope.getBody();
443: body.addNamespaceDeclaration(prefix, uri);
444: boolean b = body.removeNamespaceDeclaration(prefix);
445: assertTrue(
446: "removeNamespaceDeclaration() did not return true",
447: b);
448: b = body.removeNamespaceDeclaration(prefix);
449: assertFalse(
450: "removeNamespaceDeclaration() did not return false",
451: b);
452: assertNull(body.getNamespaceURI(prefix));
453: } catch (Exception e) {
454: fail("Exception: " + e);
455: }
456: }
457:
458: public void _testSetEncodingStyle() {
459: try {
460: SOAPMessage msg = MessageFactory.newInstance()
461: .createMessage();
462: SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
463: SOAPBody body = envelope.getBody();
464: body.setEncodingStyle(SOAPConstants.URI_NS_SOAP_ENCODING);
465: try {
466: body.setEncodingStyle("BOGUS");
467: fail("Expected Exception did not occur");
468: } catch (IllegalArgumentException e) {
469: assertTrue("Expected Exception occurred", true);
470: }
471: } catch (Exception e) {
472: fail("Exception: " + e);
473: }
474: }
475:
476: private int getIteratorCount(Iterator iter) {
477: int count = 0;
478: while (iter.hasNext()) {
479: iter.next();
480: count++;
481: }
482: return count;
483: }
484:
485: private TextImplEx assertContainsText(SOAPElement soapElem) {
486: assertTrue(soapElem.hasChildNodes());
487: List childElems = toList(soapElem.getChildElements());
488: assertTrue(childElems.size() == 1);
489: NodeImpl node = (NodeImpl) childElems.get(0);
490: assertTrue(node instanceof TextImplEx);
491: return (TextImplEx) node;
492: }
493:
494: private List toList(java.util.Iterator iter) {
495: List list = new java.util.ArrayList();
496: while (iter.hasNext()) {
497: list.add(iter.next());
498: }
499: return list;
500: }
501:
502: /*
503: * test for addChildElement(QName qname)
504: */
505: public void testAddChildElement3() {
506: try {
507: QName qname = new QName("http://sample.apache.org/trader",
508: "GetStockQuote", "w");
509: soapEle.addChildElement(qname);
510: assertNotNull(soapEle);
511:
512: } catch (Exception e) {
513: fail("Exception: " + e);
514: }
515: }
516:
517: public void testGetAttributeValue() {
518: assertNotNull(soapEle);
519: String value = "234.50";
520: try {
521: QName qname = new QName("http://sample.apache.org/trader",
522: "GetStockQuote", "w");
523: soapEle.addAttribute(qname, value);
524: String valueReturned = soapEle.getAttributeValue(qname);
525: assertEquals(value, valueReturned);
526:
527: } catch (SOAPException e) {
528: fail("Unexpected Exception " + e);
529: }
530: }
531:
532: public void _testGetChildElements() {
533: try {
534: SOAPElement childEle1 = SOAPFactoryImpl.newInstance()
535: .createElement("Child1", "ch",
536: "http://test.apache.org/");
537: SOAPElement childEle2 = SOAPFactoryImpl.newInstance()
538: .createElement("Child2", "ch",
539: "http://test.apache.org/");
540: childEle1.addChildElement(childEle2);
541: soapEle.addChildElement(childEle1);
542:
543: QName qname = new QName("http://test.apache.org/",
544: "Child1", "ch");
545: Iterator childElements = soapEle.getChildElements(qname);
546:
547: int childCount = 0;
548: while (childElements.hasNext()) {
549: Node node = (Node) childElements.next();
550: childCount++;
551: }
552: assertEquals(childCount, 2);
553: } catch (SOAPException e) {
554: fail("Unexpected Exception " + e);
555: }
556: }
557:
558: //TODO : check why this is failing
559: public void _testGetChildElements2() {
560: try {
561: MessageFactory fact = MessageFactory.newInstance();
562: SOAPMessage message = fact.createMessage();
563: SOAPPart soapPart = message.getSOAPPart();
564: SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
565: SOAPBody soapBody = soapEnvelope.getBody();
566:
567: Name name = soapEnvelope.createName("MyChild1");
568: SOAPElement se = soapBody.addChildElement(name);
569: Iterator childElementsCount = soapBody.getChildElements();
570: Iterator childElements = soapBody.getChildElements();
571:
572: int childCount = 0;
573: while (childElementsCount.hasNext()) {
574: Node node = (Node) childElementsCount.next();
575: childCount++;
576: }
577: assertEquals(childCount, 1);
578: SOAPElement se2 = (SOAPElement) childElements.next();
579: if (!se.equals(se2)) {
580: fail();
581: } else {
582: System.out.println("SOAPElement se = se2 (expected)");
583: }
584:
585: Name n = se.getElementName();
586: assertEquals(n, name);
587: } catch (SOAPException e) {
588: fail("Unexpected Exception " + e);
589: }
590: }
591:
592: public void testGetChildElements3() {
593: try {
594: MessageFactory fact = MessageFactory.newInstance();
595: SOAPMessage message = fact.createMessage();
596: SOAPPart soapPart = message.getSOAPPart();
597: SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
598: SOAPBody soapBody = soapEnvelope.getBody();
599:
600: //Name name = soapEnvelope.createName("MyChild1");
601: QName name = new QName("MyChild1");
602: SOAPElement se = soapBody.addChildElement(name);
603: Iterator childElementsCount = soapBody.getChildElements();
604: Iterator childElements = soapBody.getChildElements();
605:
606: int childCount = 0;
607: while (childElementsCount.hasNext()) {
608: Node node = (Node) childElementsCount.next();
609: childCount++;
610: }
611: assertEquals(childCount, 1);
612: SOAPElement se2 = (SOAPElement) childElements.next();
613: assertEquals(se, se2);
614:
615: QName n = se.getElementQName();
616: assertEquals(n, name);
617: } catch (SOAPException e) {
618: fail("Unexpected Exception " + e);
619: }
620: }
621:
622: public void _testRemoveAttribute2() {
623: try {
624: QName qname = new QName("http://child1.apache.org/",
625: "Child1", "ch");
626: String value = "MyValue1";
627: soapEle.addAttribute(qname, value);
628: boolean b = soapEle.removeAttribute(qname);
629: assertTrue("removeAttribute() did not return true", b);
630: b = soapEle.removeAttribute(qname);
631: assertFalse("removeAttribute() did not return false", b);
632: assertNull(soapEle.getAttributeValue(qname));
633: } catch (Exception e) {
634: e.printStackTrace();
635: fail("Exception: " + e);
636: }
637: }
638:
639: public void _testSetElementQName() {
640: try {
641: QName qname = new QName("http://child1.apache.org/",
642: "newName", "ch");
643: soapEle.setElementQName(qname);
644: assertNull(soapEle.getElementName().getLocalName(),
645: "newName");
646: } catch (Exception e) {
647: e.printStackTrace();
648: fail("Exception: " + e);
649: }
650: }
651:
652: public void _testCreateQName() {
653: String prefix = "";
654: try {
655: //SOAPMessage message = MessageFactory.newInstance().createMessage();
656: SOAPMessage message = MessageFactory.newInstance(
657: SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
658: SOAPPart soapPart = message.getSOAPPart();
659: SOAPEnvelope envelope = soapPart.getEnvelope();
660: SOAPBody body = envelope.getBody();
661:
662: QName qname = envelope.createQName("qname", prefix);
663: String tprefix = qname.getPrefix();
664: String turi = qname.getNamespaceURI();
665: String tname = qname.getLocalPart();
666: if (!tprefix.equals(prefix)
667: || !turi.equals(envelope.getElementName().getURI())) {
668: fail("createQName() did not create correct qname\n"
669: + "expected: <uri="
670: + envelope.getElementName().getURI()
671: + ", prefix=" + prefix + ", localpart=qname>\n"
672: + "got: <uri=" + turi + ", prefix="
673: + tprefix + ", localpart=" + tname + ">");
674: }
675: qname = body.createQName("qname", body.getElementName()
676: .getPrefix());
677: tprefix = qname.getPrefix();
678: turi = qname.getNamespaceURI();
679: tname = qname.getLocalPart();
680: if (!tprefix.equals(body.getElementName().getPrefix())
681: || !turi.equals(body.getElementName().getURI())) {
682: fail("createQName() did not create correct qname\n"
683: + "expected: <uri="
684: + body.getElementName().getURI() + ", prefix="
685: + body.getElementName().getPrefix()
686: + ", localpart=qname>\n" + "got: <uri="
687: + turi + ", prefix=" + tprefix + ", localpart="
688: + tname + ">");
689: }
690: } catch (Exception e) {
691: fail("Failed " + e);
692: }
693: }
694:
695: public void testRemoveContent() {
696: boolean pass = true;
697: try {
698: MessageFactory factory = MessageFactory.newInstance();
699: SOAPMessage message = factory.createMessage();
700: SOAPPart soapPart = message.getSOAPPart();
701: SOAPEnvelope envelope = soapPart.getEnvelope();
702: SOAPBody body = envelope.getBody();
703:
704: Name name = envelope.createName("MyChild");
705: SOAPElement se = body.addChildElement(name);
706: assertNotNull(se);
707: Iterator childs = body.getChildElements(name);
708: int childElementCount = 0;
709: for (int a = 0; childs.hasNext(); a++) {
710: childs.next();
711: childElementCount++;
712: }
713: childs = body.getChildElements(name);
714: assertEquals(childElementCount, 1);
715:
716: Name n = se.getElementName();
717: assertEquals(n, name);
718: //Child addition verified, now call removeContents to delete it
719: se.removeContents();
720: childs = se.getChildElements();
721: childElementCount = 0;
722: for (int a = 0; childs.hasNext(); a++) {
723: childs.next();
724: childElementCount++;
725: }
726: assertEquals(childElementCount, 0);
727: } catch (Exception e) {
728: fail();
729: }
730: }
731:
732: public void testSetElementQName() {
733: try {
734: MessageFactory factory = MessageFactory
735: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
736: SOAPMessage message = factory.createMessage();
737: SOAPPart soapPart = message.getSOAPPart();
738: SOAPEnvelope envelope = soapPart.getEnvelope();
739: SOAPBody body = envelope.getBody();
740:
741: QName qname1 = new QName("http://fooURI.com", "fooElement",
742: "foo");
743: QName qname2 = new QName("http://foo2URI.com",
744: "fooElement2", "foo2");
745: SOAPElement se = body.addChildElement(qname1);
746: QName qname = se.getElementQName();
747: se = se.setElementQName(qname2);
748: qname = se.getElementQName();
749:
750: if (!qname.getNamespaceURI().equals(
751: qname2.getNamespaceURI())
752: || !qname.getLocalPart().equals(
753: qname2.getLocalPart())
754: || !qname.getPrefix().equals(qname2.getPrefix())) {
755: System.out.println("setElementQName() did not reset "
756: + "element qname\nexpected: <URI="
757: + qname2.getNamespaceURI() + ", prefix="
758: + qname2.getPrefix() + ", localpart="
759: + qname2.getLocalPart() + ">\ngot: <URI="
760: + qname.getNamespaceURI() + ", prefix="
761: + qname.getPrefix() + ", localpart="
762: + qname.getLocalPart() + ">");
763: }
764: } catch (Exception e) {
765: fail("Error :" + e);
766: }
767: }
768:
769: public void testSetElementQName2() {
770: try {
771: MessageFactory factory = MessageFactory.newInstance();
772: SOAPMessage message = factory.createMessage();
773: SOAPPart soapPart = message.getSOAPPart();
774: SOAPEnvelope envelope = soapPart.getEnvelope();
775: SOAPBody body = envelope.getBody();
776: SOAPHeader header = envelope.getHeader();
777:
778: QName qname = new QName("qname");
779: //Try and change element name of SOAPEnvelope (expect SOAPException)
780: try {
781: envelope.setElementQName(qname);
782: fail("Did not throw expected SOAPException");
783: } catch (SOAPException e) {
784: //Caught expected SOAPException
785: }
786:
787: //Try and change element name of SOAPHeader (expect SOAPException)
788: try {
789: header.setElementQName(qname);
790: fail("Did not throw expected SOAPException");
791: } catch (SOAPException e) {
792: //Caught expected SOAPException
793: }
794:
795: //Try and change element name of SOAPBody (expect SOAPException)
796: try {
797: body.setElementQName(qname);
798: fail("Did not throw expected SOAPException");
799: } catch (SOAPException e) {
800: //Caught expected SOAPException
801: }
802: } catch (Exception e) {
803: fail("Error : " + e);
804: }
805: }
806: }
|