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:
020: package org.apache.axis2.saaj;
021:
022: import junit.framework.TestCase;
023:
024: import javax.xml.soap.Detail;
025: import javax.xml.soap.DetailEntry;
026: import javax.xml.soap.MessageFactory;
027: import javax.xml.soap.MimeHeaders;
028: import javax.xml.soap.Name;
029: import javax.xml.soap.Node;
030: import javax.xml.soap.SOAPBody;
031: import javax.xml.soap.SOAPConstants;
032: import javax.xml.soap.SOAPElement;
033: import javax.xml.soap.SOAPEnvelope;
034: import javax.xml.soap.SOAPException;
035: import javax.xml.soap.SOAPFault;
036: import javax.xml.soap.SOAPHeader;
037: import javax.xml.soap.SOAPHeaderElement;
038: import javax.xml.soap.SOAPMessage;
039: import javax.xml.soap.SOAPPart;
040: import javax.xml.soap.Text;
041: import java.io.ByteArrayInputStream;
042: import java.util.Iterator;
043:
044: public class SOAPEnvelopeTest extends TestCase {
045:
046: private static final String XML_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
047: + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
048: + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
049: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
050: + " <soapenv:Header>\n"
051: + " <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n"
052: + " <shw:Myname>Tony</shw:Myname>\n"
053: + " </shw:Hello>\n"
054: + " </soapenv:Header>\n"
055: + " <soapenv:Body>\n"
056: + "<shw:Address shw:t='test' xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n"
057: + "<shw:City>GENT</shw:City>\n"
058: + "</shw:Address>\n"
059: + "</soapenv:Body>\n" + "</soapenv:Envelope>";
060:
061: public SOAPEnvelopeTest(String name) {
062: super (name);
063: }
064:
065: public void testEnvelope() throws Exception {
066: MessageFactory mf = MessageFactory.newInstance();
067: SOAPMessage smsg = mf.createMessage(new MimeHeaders(),
068: new ByteArrayInputStream(XML_STRING.getBytes()));
069: SOAPPart sp = smsg.getSOAPPart();
070: SOAPEnvelope se = sp.getEnvelope();
071: assertTrue(se != null);
072:
073: // validate the body
074: final SOAPBody body = sp.getEnvelope().getBody();
075: validateBody(body.getChildElements());
076: }
077:
078: public void testDetachHeader() throws Exception {
079: MessageFactory mf = MessageFactory.newInstance();
080: SOAPMessage smsg = mf.createMessage(new MimeHeaders(),
081: new ByteArrayInputStream(XML_STRING.getBytes()));
082: SOAPPart sp = smsg.getSOAPPart();
083: SOAPEnvelope se = sp.getEnvelope();
084: assertTrue(se != null);
085: SOAPHeader header = se.getHeader();
086: assertNotNull(header);
087: header.detachNode();
088: assertNull(se.getHeader());
089: assertNull(smsg.getSOAPHeader());
090: }
091:
092: public void testDetachBody() {
093: try {
094: MessageFactory mf = MessageFactory.newInstance();
095: SOAPMessage smsg = mf.createMessage(new MimeHeaders(),
096: new ByteArrayInputStream(XML_STRING.getBytes()));
097: SOAPPart sp = smsg.getSOAPPart();
098: SOAPEnvelope se = sp.getEnvelope();
099:
100: try {
101: se.addBody();
102: fail("Expected Exception did not occur");
103: } catch (SOAPException e) {
104: assertTrue(true);
105: }
106:
107: se.getBody().detachNode();
108: assertNull(se.getBody());
109: try {
110: se.addBody();
111: } catch (SOAPException e) {
112: e.printStackTrace();
113: fail("Unexpected Exception occurred.");
114: }
115: } catch (Exception e) {
116: e.printStackTrace();
117: fail("Unexpected Exception : " + e);
118: }
119: }
120:
121: public void testEnvelope2() throws Exception {
122: MessageFactory mf = MessageFactory.newInstance();
123: final ByteArrayInputStream baIS = new ByteArrayInputStream(
124: XML_STRING.getBytes());
125: final MimeHeaders mimeheaders = new MimeHeaders();
126: mimeheaders.addHeader("Content-Type", "text/xml");
127: SOAPMessage smsg = mf.createMessage(mimeheaders, baIS);
128:
129: SOAPEnvelope envelope = smsg.getSOAPPart().getEnvelope();
130: SOAPBody body = envelope.getBody();
131: assertTrue(body != null);
132: }
133:
134: // TODO: This test fails due to some issues in OM. Needs to be added to the test suite
135: // that issue is fixed
136: public void _testEnvelopeWithLeadingComment() throws Exception {
137: String soapMessageWithLeadingComment = "<?xml version='1.0' encoding='UTF-8'?>"
138: + "<!-- Comment -->"
139: + "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>"
140: + "<env:Body><echo><arg0>Hello</arg0></echo></env:Body>"
141: + "</env:Envelope>";
142:
143: MessageFactory factory = MessageFactory.newInstance();
144: SOAPMessage message = factory.createMessage(new MimeHeaders(),
145: new ByteArrayInputStream(soapMessageWithLeadingComment
146: .getBytes()));
147: SOAPPart part = message.getSOAPPart();
148: SOAPEnvelope envelope = part.getEnvelope();
149: message.writeTo(System.out);
150: assertTrue(envelope != null);
151: assertTrue(envelope.getBody() != null);
152: }
153:
154: public void testEnvelopeWithCommentInEnvelope() throws Exception {
155:
156: String soapMessageWithLeadingComment = "<?xml version='1.0' encoding='UTF-8'?>\n"
157: + "<soapenv:Envelope xmlns='http://somewhere.com/html'\n"
158: + " xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'\n"
159: + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'\n"
160: + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n"
161: + "<!-- Comment -->"
162: + " <soapenv:Body>\n"
163: + " <echo><arg0>Hello</arg0></echo>" +
164: // " <t:echo xmlns:t='http://test.org/Test'><t:arg0>Hello</t:arg0></t:echo>" +
165: " </soapenv:Body>\n" + "</soapenv:Envelope>";
166:
167: MessageFactory factory = MessageFactory.newInstance();
168: SOAPMessage message = factory.createMessage(new MimeHeaders(),
169: new ByteArrayInputStream(soapMessageWithLeadingComment
170: .getBytes()));
171: SOAPPart part = message.getSOAPPart();
172: SOAPEnvelope envelope = part.getEnvelope();
173: assertTrue(envelope != null);
174: assertTrue(envelope.getBody() != null);
175: }
176:
177: public void testEnvelopeWithCommentInBody() throws Exception {
178:
179: String soapMessageWithLeadingComment = "<?xml version='1.0' encoding='UTF-8'?>\n"
180: + "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'\n"
181: + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'\n"
182: + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n"
183: + " <soapenv:Body>\n"
184: + "<!-- Comment -->"
185: +
186: // " <echo><arg0>Hello</arg0></echo>" +
187: " <t:echo xmlns:t='http://test.org/Test'><t:arg0>Hello</t:arg0></t:echo>"
188: + " </soapenv:Body>\n" + "</soapenv:Envelope>";
189:
190: MessageFactory factory = MessageFactory.newInstance();
191: SOAPMessage message = factory.createMessage(new MimeHeaders(),
192: new ByteArrayInputStream(soapMessageWithLeadingComment
193: .getBytes()));
194: SOAPPart part = message.getSOAPPart();
195: SOAPEnvelope envelope = part.getEnvelope();
196: assertTrue(envelope != null);
197: assertTrue(envelope.getBody() != null);
198: }
199:
200: public void testEnvelopeWithComments() throws Exception {
201:
202: String soapMessageWithLeadingComment = "<?xml version='1.0' encoding='UTF-8'?>\n"
203: + "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'\n"
204: + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'\n"
205: + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n"
206: + " <soapenv:Header>\n"
207: + "<!-- Comment -->"
208: + " <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n"
209: + "<!-- Comment -->"
210: + " <shw:Myname><!-- Comment -->Tony</shw:Myname>\n"
211: + " </shw:Hello>\n"
212: + " </soapenv:Header>\n"
213: + " <soapenv:Body>\n"
214: + "<!-- Comment -->"
215: + " <t:echo xmlns:t='http://test.org/Test'><t:arg0>Hello</t:arg0></t:echo>"
216: + " </soapenv:Body>\n" + "</soapenv:Envelope>";
217:
218: MessageFactory factory = MessageFactory.newInstance();
219: SOAPMessage message = factory.createMessage(new MimeHeaders(),
220: new ByteArrayInputStream(soapMessageWithLeadingComment
221: .getBytes()));
222: SOAPPart part = message.getSOAPPart();
223: SOAPEnvelope envelope = part.getEnvelope();
224: assertTrue(envelope != null);
225: assertTrue(envelope.getBody() != null);
226: }
227:
228: public void _testFaults() throws Exception {
229: SOAPEnvelope envelope = getSOAPEnvelope();
230: SOAPBody body = envelope.getBody();
231:
232: assertFalse(body.hasFault());
233: SOAPFault soapFault = body.addFault();
234: soapFault.setFaultString("myFault");
235: soapFault.setFaultCode("CODE");
236:
237: assertTrue(body.hasFault());
238: assertNotNull(body.getFault());
239: assertSame(soapFault, body.getFault());
240:
241: assertEquals("myFault", soapFault.getFaultString());
242: assertEquals("CODE", soapFault.getFaultCode());
243: }
244:
245: public void _testFaults2() throws Exception {
246: SOAPEnvelope envelope = getSOAPEnvelope();
247: SOAPBody body = envelope.getBody();
248: SOAPFault fault = body.addFault();
249:
250: assertTrue(body.getFault() != null);
251:
252: Detail d1 = fault.addDetail();
253: Name name = envelope.createName("GetLastTradePrice", "WOMBAT",
254: "http://www.wombat.org/trader");
255: d1.addDetailEntry(name);
256:
257: Detail d2 = fault.getDetail();
258: assertTrue(d2 != null);
259: Iterator i = d2.getDetailEntries();
260: assertTrue(getIteratorCount(i) == 1);
261: i = d2.getDetailEntries();
262: while (i.hasNext()) {
263: DetailEntry de = (DetailEntry) i.next();
264: assertEquals(de.getElementName(), name);
265: }
266: }
267:
268: public void testHeaderElements() throws Exception {
269: SOAPEnvelope envelope = getSOAPEnvelope();
270: SOAPHeader header = envelope.getHeader();
271:
272: SOAPHeaderElement headerEle = header.addHeaderElement(envelope
273: .createName("foo1", "f1", "foo1-URI"));
274: headerEle.setActor("actor-URI");
275: headerEle.setMustUnderstand(true);
276:
277: Iterator iterator = header.extractHeaderElements("actor-URI");
278: int cnt = 0;
279: while (iterator.hasNext()) {
280: cnt++;
281: SOAPHeaderElement resultHeaderEle = (SOAPHeaderElement) iterator
282: .next();
283:
284: assertEquals(headerEle.getActor(), resultHeaderEle
285: .getActor());
286: assertEquals(resultHeaderEle.getMustUnderstand(), headerEle
287: .getMustUnderstand());
288: }
289: assertTrue(cnt == 1);
290: iterator = header.extractHeaderElements("actor-URI");
291: assertTrue(!iterator.hasNext());
292: }
293:
294: public void testText() throws Exception {
295: SOAPEnvelope envelope = getSOAPEnvelope();
296: SOAPBody body = envelope.getBody();
297: Iterator iStart = body.getChildElements();
298: int countStart = getIteratorCount(iStart);
299:
300: final String bodyText = "This is the body text";
301:
302: SOAPElement se = body.addChildElement("Child");
303: assertTrue(se != null);
304: SOAPElement soapElement = se.addTextNode(bodyText);
305: assertEquals(bodyText, soapElement.getValue());
306:
307: Iterator i = body.getChildElements();
308: int count = getIteratorCount(i);
309: assertTrue(count == countStart + 1);
310: }
311:
312: public void testNonCommentText() throws Exception {
313: SOAPEnvelope envelope = getSOAPEnvelope();
314: SOAPBody body = envelope.getBody();
315: SOAPElement se = body.addChildElement("Child");
316: se.addTextNode("This is text");
317: Iterator iterator = se.getChildElements();
318: Object o = null;
319: while (iterator.hasNext()) {
320: o = iterator.next();
321: if (o instanceof Text) {
322: break;
323: }
324: }
325: assertTrue(o instanceof Text);
326: Text t = (Text) o;
327: assertTrue(!t.isComment());
328: }
329:
330: public void testCommentText() throws Exception {
331: SOAPEnvelope envelope = getSOAPEnvelope();
332: SOAPBody body = envelope.getBody();
333: SOAPElement se = body.addChildElement("Child");
334: se.addTextNode("<!-- This is a comment -->");
335: Iterator iterator = se.getChildElements();
336: Node n = null;
337: while (iterator.hasNext()) {
338: n = (Node) iterator.next();
339: if (n instanceof Text) {
340: break;
341: }
342: }
343: assertTrue(n instanceof Text);
344: Text t = (Text) n;
345: assertTrue(t.isComment());
346: }
347:
348: public void testAttributes() throws Exception {
349: SOAPEnvelope envelope = getSOAPEnvelope();
350: SOAPBody body = envelope.getBody();
351:
352: Name name1 = envelope.createName("MyAttr1");
353: String value1 = "MyValue1";
354:
355: Name name2 = envelope.createName("MyAttr2");
356: String value2 = "MyValue2";
357:
358: Name name3 = envelope.createName("MyAttr3");
359: String value3 = "MyValue3";
360:
361: body.addAttribute(name1, value1);
362: body.addAttribute(name2, value2);
363: body.addAttribute(name3, value3);
364:
365: Iterator iterator = body.getAllAttributes();
366: assertTrue(getIteratorCount(iterator) == 3);
367: iterator = body.getAllAttributes();
368:
369: boolean foundName1 = false;
370: boolean foundName2 = false;
371: boolean foundName3 = false;
372: while (iterator.hasNext()) {
373: Name name = (Name) iterator.next();
374: if (name.equals(name1)) {
375: foundName1 = true;
376: assertEquals(value1, body.getAttributeValue(name));
377: } else if (name.equals(name2)) {
378: foundName2 = true;
379: assertEquals(value2, body.getAttributeValue(name));
380: } else if (name.equals(name3)) {
381: foundName3 = true;
382: assertEquals(value3, body.getAttributeValue(name));
383: }
384: }
385: assertTrue(foundName1 && foundName2 && foundName3);
386: }
387:
388: public void testAttributes2() throws Exception {
389: SOAPEnvelope envelope = getSOAPEnvelope();
390: SOAPBody body = envelope.getBody();
391:
392: Name name1 = envelope.createName("MyAttr1", "att",
393: "http://test.com/Attr");
394: String value1 = "MyValue1";
395:
396: Name name2 = envelope.createName("MyAttr2");
397: String value2 = "MyValue2";
398:
399: Name name3 = envelope.createName("MyAttr3");
400: String value3 = "MyValue3";
401:
402: body.addAttribute(name1, value1);
403: body.addAttribute(name2, value2);
404: body.addAttribute(name3, value3);
405:
406: Iterator iterator = body.getAllAttributes();
407: assertTrue(getIteratorCount(iterator) == 3);
408: iterator = body.getAllAttributes();
409:
410: boolean foundName1 = false;
411: boolean foundName2 = false;
412: boolean foundName3 = false;
413: while (iterator.hasNext()) {
414: Name name = (Name) iterator.next();
415: if (name.equals(name1)) {
416: foundName1 = true;
417: assertEquals(value1, body.getAttributeValue(name));
418: } else if (name.equals(name2)) {
419: foundName2 = true;
420: assertEquals(value2, body.getAttributeValue(name));
421: } else if (name.equals(name3)) {
422: foundName3 = true;
423: assertEquals(value3, body.getAttributeValue(name));
424: }
425: }
426: assertTrue(foundName1 && foundName2 && foundName3);
427: }
428:
429: public void testAttributes3() throws Exception {
430: SOAPEnvelope envelope = getSOAPEnvelope();
431: SOAPBody body = envelope.getBody();
432:
433: Name name1 = envelope.createName("MyAttr1", "att",
434: "http://test.com/Attr");
435: String value1 = "MyValue1";
436:
437: Name name2 = envelope.createName("MyAttr2", "att",
438: "http://test.com/Attr");
439: String value2 = "MyValue2";
440:
441: Name name3 = envelope.createName("MyAttr3", "att",
442: "http://test.com/Attr");
443: String value3 = "MyValue3";
444:
445: body.addAttribute(name1, value1);
446: body.addAttribute(name2, value2);
447: body.addAttribute(name3, value3);
448:
449: Iterator iterator = body.getAllAttributes();
450: assertTrue(getIteratorCount(iterator) == 3);
451: iterator = body.getAllAttributes();
452:
453: boolean foundName1 = false;
454: boolean foundName2 = false;
455: boolean foundName3 = false;
456: while (iterator.hasNext()) {
457: Name name = (Name) iterator.next();
458: if (name.equals(name1)) {
459: foundName1 = true;
460: assertEquals(value1, body.getAttributeValue(name));
461: } else if (name.equals(name2)) {
462: foundName2 = true;
463: assertEquals(value2, body.getAttributeValue(name));
464: } else if (name.equals(name3)) {
465: foundName3 = true;
466: assertEquals(value3, body.getAttributeValue(name));
467: }
468: }
469: assertTrue(foundName1 && foundName2 && foundName3);
470: }
471:
472: public void testAddHeader() {
473: try {
474: SOAPEnvelope envelope = getSOAPEnvelope();
475: try {
476: envelope.addHeader();
477: fail("Did not get expected SOAPException");
478: } catch (SOAPException e) {
479: assertTrue("Got expected SOAPException", true);
480: }
481: envelope.getHeader().detachNode();
482: assertNull(envelope.getHeader());
483: SOAPHeader myhdr;
484:
485: try {
486: myhdr = envelope.addHeader();
487: assertNotNull("SOAPHeader return value is null", myhdr);
488: } catch (SOAPException e) {
489: fail("Unexpected SOAPException : " + e);
490: }
491: } catch (Exception e) {
492: e.printStackTrace();
493: fail("Unexpected Exception : " + e);
494: }
495: }
496:
497: private SOAPEnvelope getSOAPEnvelope() throws Exception {
498: MessageFactory factory = MessageFactory.newInstance();
499: SOAPMessage message = factory.createMessage();
500: return message.getSOAPPart().getEnvelope();
501: }
502:
503: private int getIteratorCount(java.util.Iterator i) {
504: int count = 0;
505: while (i.hasNext()) {
506: count++;
507: i.next();
508: }
509: return count;
510: }
511:
512: private void validateBody(Iterator iter) {
513: while (iter.hasNext()) {
514: final Object obj = iter.next();
515: if (obj instanceof Text) {
516: final String data = ((Text) obj).getData();
517: assertTrue("\n".equals(data) || "GENT".equals(data));
518: } else {
519: final SOAPElement soapElement = (SOAPElement) obj;
520: final Iterator attIter = soapElement.getAllAttributes();
521: while (attIter.hasNext()) {
522: final Object o = attIter.next();
523: assertEquals("test", soapElement
524: .getAttributeValue((Name) o));
525: }
526:
527: final Iterator childElementIter = soapElement
528: .getChildElements();
529: if (childElementIter == null) {
530: return;
531: }
532: validateBody(childElementIter);
533: }
534: }
535: }
536:
537: public void testSetEncodingStyle() throws Exception {
538: SOAPEnvelope envelope = getSOAPEnvelope();
539: envelope.setEncodingStyle("http://example.com/MyEncodings");
540: assertNotNull(envelope.getEncodingStyle());
541: assertEquals("http://example.com/MyEncodings", envelope
542: .getEncodingStyle());
543: }
544:
545: public void testElementAfterBody() throws Exception {
546: MessageFactory factory = MessageFactory
547: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
548: SOAPMessage message = factory.createMessage();
549: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
550:
551: try {
552: //SOAP1.2 does not allow trailing blocks after the Body
553: //Call SOAPEnvelope.addChildElement() and (expect SOAPException)
554: Name elementAfterBody = envelope.createName("AfterBody",
555: "e", "some-uri");
556: envelope.addChildElement(elementAfterBody);
557: fail("Did not throw expected SOAPException");
558: } catch (SOAPException e) {
559: //Did throw expected SOAPException"
560: } catch (Exception e) {
561: fail("Unexpected Exception: " + e.getMessage());
562: }
563: }
564: }
|