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.namespace.QName;
025: import javax.xml.soap.Detail;
026: import javax.xml.soap.DetailEntry;
027: import javax.xml.soap.MessageFactory;
028: import javax.xml.soap.Name;
029: import javax.xml.soap.SOAPBody;
030: import javax.xml.soap.SOAPConstants;
031: import javax.xml.soap.SOAPElement;
032: import javax.xml.soap.SOAPEnvelope;
033: import javax.xml.soap.SOAPException;
034: import javax.xml.soap.SOAPFactory;
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 java.io.ByteArrayOutputStream;
041: import java.util.Iterator;
042: import java.util.Locale;
043:
044: public class SOAPFaultTest extends TestCase {
045:
046: public SOAPFaultTest(String name) {
047: super (name);
048: }
049:
050: public void _testSOAPFaultWithDetails() throws Exception {
051: /* We are trying to generate the following SOAPFault
052:
053: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
054: xmlns:xsd="http://www.w3.org/2001/XMLSchema"
055: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
056: xmlns:cwmp="http://cwmp.com">
057: <soapenv:Header>
058: <cwmp:ID soapenv:mustUnderstand="1">HEADERID-786767comm
059: 8</cwmp:ID>
060: </soapenv:Header>
061: <soapenv:Body>
062: <soapenv:Fault>
063: <faultcode>Client</faultcode>
064: <faultstring>CWMP fault</faultstring>
065: <faultactor>http://gizmos.com/order</faultactor>
066: <detail>
067: <cwmp:Fault>
068: <cwmp:FaultCode>This is the fault code</cwmp:FaultCode>
069: <cwmp:FaultString>Fault Message</cwmp:FaultString>
070: <cwmp:Message>This is a test fault</cwmp:FaultString>
071: </cwmp:Fault>
072: </detail>
073: </soapenv:Fault>
074: </soapenv:Body>
075: </soapenv:Envelope>
076:
077: */
078:
079: MessageFactory fac = MessageFactory.newInstance();
080:
081: //Create the response to the message
082: SOAPMessage soapMessage = fac.createMessage();
083: SOAPPart soapPart = soapMessage.getSOAPPart();
084: SOAPEnvelope envelope = soapPart.getEnvelope();
085: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
086: SOAPBody body = envelope.getBody();
087: SOAPHeader header = envelope.getHeader();
088: Name idName = envelope.createName("ID", "cwmp",
089: "http://cwmp.com");
090: SOAPHeaderElement id = header.addHeaderElement(idName);
091: id.setMustUnderstand(true);
092: id.addTextNode("HEADERID-7867678");
093:
094: //Create the SOAPFault object
095: SOAPFault fault = body.addFault();
096: fault.setFaultCode("Client");
097: fault.setFaultString("CWMP fault");
098: fault.setFaultActor("http://gizmos.com/order");
099:
100: assertEquals("Client", fault.getFaultCode());
101: assertEquals("CWMP fault", fault.getFaultString());
102: assertEquals("http://gizmos.com/order", fault.getFaultActor());
103:
104: //Add Fault Detail information
105: Detail faultDetail = fault.addDetail();
106: Name cwmpFaultName = envelope.createName("Fault", "cwmp",
107: "http://cwmp.com");
108: DetailEntry faultDetailEntry = faultDetail
109: .addDetailEntry(cwmpFaultName);
110: SOAPElement e = faultDetailEntry.addChildElement("FaultCode");
111:
112: e.addTextNode("This is the fault code");
113: SOAPElement e2 = faultDetailEntry.addChildElement(envelope
114: .createName("FaultString", "cwmp", "http://cwmp.com"));
115: e2.addTextNode("Fault Message");
116:
117: SOAPElement e3 = faultDetailEntry.addChildElement("Message");
118: e3.addTextNode("This is a test fault");
119:
120: soapMessage.saveChanges();
121:
122: // ------------------- Validate the contents -------------------------------------
123: final Detail detail = fault.getDetail();
124: final Iterator detailEntryIter = detail.getDetailEntries();
125: boolean foundFirst = false;
126: boolean foundSecond = false;
127: boolean foundThird = false;
128: while (detailEntryIter.hasNext()) {
129: final DetailEntry detailEntry = (DetailEntry) detailEntryIter
130: .next();
131: final Iterator childElementsIter = detailEntry
132: .getChildElements();
133: while (childElementsIter.hasNext()) {
134: final SOAPElement soapElement = (SOAPElement) childElementsIter
135: .next();
136: if (soapElement.getTagName().equals("FaultCode")
137: && soapElement.getValue().equals(
138: "This is the fault code")) {
139: foundFirst = true;
140: }
141: if (soapElement.getTagName().equals("cwmp:FaultString")
142: && soapElement.getValue().equals(
143: "Fault Message")) {
144: foundSecond = true;
145: }
146: if (soapElement.getTagName().equals("Message")
147: && soapElement.getValue().equals(
148: "This is a test fault")) {
149: foundThird = true;
150: }
151: }
152: }
153: assertTrue(foundFirst && foundSecond && foundThird);
154: // ------------------------------------------------------------------------------
155:
156: // Test whether the fault is being serialized properly
157: ByteArrayOutputStream baos = new ByteArrayOutputStream();
158: soapMessage.writeTo(baos);
159: String xml = new String(baos.toByteArray());
160:
161: assertTrue(xml.indexOf("<faultcode>Client</faultcode>") != -1);
162: assertTrue(xml.indexOf("<faultstring>CWMP fault</faultstring>") != -1);
163: assertTrue(xml
164: .indexOf("<faultactor>http://gizmos.com/order</faultactor>") != -1);
165: }
166:
167: public void testAddDetailsTwice() {
168: try {
169: MessageFactory fac = MessageFactory.newInstance();
170:
171: //Create the response to the message
172: SOAPMessage soapMessage = fac.createMessage();
173: SOAPPart soapPart = soapMessage.getSOAPPart();
174: SOAPEnvelope envelope = soapPart.getEnvelope();
175: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
176: SOAPBody body = envelope.getBody();
177:
178: body.addFault().addDetail();
179: try {
180: body.getFault().addDetail();
181: fail("Expected Exception did not occur");
182: } catch (SOAPException e) {
183: assertTrue(true);
184: }
185:
186: } catch (SOAPException e) {
187: fail("Unexpected Exception Occurred : " + e);
188: }
189: }
190:
191: public void _testQuick() throws Exception {
192: MessageFactory msgfactory = MessageFactory.newInstance();
193: SOAPFactory factory = SOAPFactory.newInstance();
194: SOAPMessage outputmsg = msgfactory.createMessage();
195: String valueCode = "faultcode";
196: String valueString = "faultString";
197: SOAPFault fault = outputmsg.getSOAPPart().getEnvelope()
198: .getBody().addFault();
199: fault.setFaultCode(valueCode);
200: fault.setFaultString(valueString);
201: Detail detail = fault.addDetail();
202: detail.addDetailEntry(factory.createName("Hello"));
203: ByteArrayOutputStream baos = new ByteArrayOutputStream();
204: if (outputmsg.saveRequired()) {
205: outputmsg.saveChanges();
206: }
207: outputmsg.writeTo(baos);
208: String xml = new String(baos.toByteArray());
209: assertTrue(xml.indexOf("Hello") != -1);
210: }
211:
212: public void testFaults() {
213: try {
214: MessageFactory messageFactory = MessageFactory
215: .newInstance();
216: SOAPFactory soapFactory = SOAPFactory.newInstance();
217: SOAPMessage message = messageFactory.createMessage();
218: SOAPBody body = message.getSOAPBody();
219: SOAPFault fault = body.addFault();
220:
221: Name faultName = soapFactory.createName("Client", "",
222: SOAPConstants.URI_NS_SOAP_ENVELOPE);
223: fault.setFaultCode(faultName);
224:
225: fault
226: .setFaultString("Message does not have necessary info");
227: fault.setFaultActor("http://gizmos.com/order");
228:
229: Detail detail = fault.addDetail();
230:
231: Name entryName = soapFactory.createName("order", "PO",
232: "http://gizmos.com/orders/");
233: DetailEntry entry = detail.addDetailEntry(entryName);
234: entry.addTextNode("Quantity element does not have a value");
235:
236: Name entryName2 = soapFactory.createName("confirmation",
237: "PO", "http://gizmos.com/confirm");
238: DetailEntry entry2 = detail.addDetailEntry(entryName2);
239: entry2.addTextNode("Incomplete address: " + "no zip code");
240:
241: message.saveChanges();
242: //message.writeTo(System.out);
243:
244: // Now retrieve the SOAPFault object and
245: // its contents, after checking to see that
246: // there is one
247: if (body.hasFault()) {
248: SOAPFault newFault = body.getFault();
249:
250: // Get the qualified name of the fault code
251: assertNotNull(newFault.getFaultCodeAsName());
252: assertNotNull(newFault.getFaultString());
253: assertNotNull(newFault.getFaultActor());
254: Detail newDetail = newFault.getDetail();
255:
256: if (newDetail != null) {
257: Iterator entries = newDetail.getDetailEntries();
258:
259: while (entries.hasNext()) {
260: DetailEntry newEntry = (DetailEntry) entries
261: .next();
262: String value = newEntry.getValue();
263: assertNotNull(value);
264: }
265: }
266: }
267: } catch (Exception e) {
268: fail("Unexpected Exception : " + e);
269: }
270: }
271:
272: public void testGetFaultActor() {
273: try {
274: SOAPMessage msg = MessageFactory.newInstance()
275: .createMessage();
276: SOAPFault sf = msg.getSOAPBody().addFault();
277: sf.setFaultActor("/faultActorURI");
278: sf.setFaultActor("/faultActorURI2");
279: String result = sf.getFaultActor();
280:
281: if (!result.equals("/faultActorURI2")) {
282: fail("Fault Actor not properly set");
283: }
284: } catch (Exception e) {
285: fail("Unexpected Exception : " + e);
286: }
287: }
288:
289: public void testGetFaultString() {
290: try {
291: SOAPMessage msg = MessageFactory.newInstance()
292: .createMessage();
293: SOAPFault sf = msg.getSOAPBody().addFault();
294:
295: sf.setFaultString("1st Fault String");
296: sf.setFaultString("2nd Fault String");
297: String result = sf.getFaultString();
298:
299: if (!result.equals("2nd Fault String")) {
300: fail("Fault String not properly set");
301: }
302: } catch (Exception e) {
303: fail("Unexpected Exception : " + e);
304: }
305: }
306:
307: public void testAppendSubCode() throws Exception {
308: MessageFactory fac = MessageFactory
309: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
310: SOAPMessage soapMessage = fac.createMessage();
311: SOAPPart soapPart = soapMessage.getSOAPPart();
312: SOAPEnvelope envelope = soapPart.getEnvelope();
313: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
314: SOAPBody body = envelope.getBody();
315: SOAPFault soapFault = body.addFault();
316: QName qname = new QName("http://example.com", "myfault1",
317: "flt1");
318: soapFault.appendFaultSubcode(qname);
319:
320: QName qname2 = new QName("http://example2.com", "myfault2",
321: "flt2");
322: soapFault.appendFaultSubcode(qname2);
323:
324: QName qname3 = new QName("http://example3.com", "myfault3",
325: "flt3");
326: soapFault.appendFaultSubcode(qname3);
327:
328: soapMessage.saveChanges();
329:
330: Iterator faultSubCodes = soapFault.getFaultSubcodes();
331: assertNotNull(faultSubCodes);
332: }
333:
334: public void testAppendFaultSubCode() throws Exception {
335: MessageFactory fac = MessageFactory
336: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
337: SOAPMessage soapMessage = fac.createMessage();
338: SOAPPart soapPart = soapMessage.getSOAPPart();
339: SOAPEnvelope envelope = soapPart.getEnvelope();
340: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
341: SOAPBody body = envelope.getBody();
342: SOAPFault sf = body.addFault();
343:
344: QName expected1 = new QName("http://example.com", "myfault1",
345: "flt1");
346: QName expected2 = new QName("http://example.com", "myfault2",
347: "flt2");
348: boolean found1 = false;
349: boolean found2 = false;
350:
351: //Appending fault Subcode
352: sf.appendFaultSubcode(expected1);
353: //Appending a second fault Subcode
354: sf.appendFaultSubcode(expected2);
355:
356: //Getting FaultSubCodes from SOAPFault
357: Iterator i = sf.getFaultSubcodes();
358: int j = 0;
359: while (i.hasNext()) {
360: Object o = i.next();
361: if (o != null && o instanceof QName) {
362: QName actual = (QName) o;
363: if (actual.equals(expected1)) {
364: if (!found1) {
365: found1 = true;
366: //System.out.println("Subcode= '"+actual+"'");
367: } else {
368: //System.out.println("Received a duplicate Subcode :'"+actual+"'");
369: }
370: } else if (actual.equals(expected2)) {
371: if (!found2) {
372: found2 = true;
373: //System.out.println("Subcode= '"+actual+"'");
374: } else {
375: //System.out.println("Received a duplicate Subcode :'"+actual+"'");
376: }
377: }
378: }
379: j++;
380: }
381: if (j < 1) {
382: fail("No Subcode was returned");
383: }
384: if (j > 2) {
385: fail("More than two Subcodes were returned");
386: }
387: if (!found1) {
388: fail("The following Subcode was not received: '"
389: + expected1 + "'");
390: }
391: if (!found2) {
392: fail("The following Subcode was not received: '"
393: + expected2 + "'");
394: }
395: }
396:
397: public void _testGetFaultReasonTexts() {
398: try {
399: MessageFactory fac = MessageFactory
400: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
401:
402: SOAPMessage soapMessage = fac.createMessage();
403: SOAPPart soapPart = soapMessage.getSOAPPart();
404:
405: SOAPEnvelope envelope = soapPart.getEnvelope();
406: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
407: SOAPBody body = envelope.getBody();
408: SOAPFault soapFault = body.addFault();
409: soapFault.addFaultReasonText("myReason", new Locale("en"));
410: soapFault.addFaultReasonText("de-myReason",
411: new Locale("de"));
412: soapFault.addFaultReasonText("si-myReason",
413: new Locale("si"));
414: soapMessage.saveChanges();
415: Iterator reasonTexts = soapFault.getFaultReasonTexts();
416: while (reasonTexts.hasNext()) {
417: String reasonText = (String) reasonTexts.next();
418: assertNotNull(reasonText);
419: }
420:
421: } catch (Exception e) {
422: fail("Unexpected Exception : " + e);
423: }
424: }
425:
426: public void _testAddFaultReasonText1() {
427: try {
428: MessageFactory fac = MessageFactory.newInstance();
429: SOAPMessage soapMessage = fac.createMessage();
430: SOAPPart soapPart = soapMessage.getSOAPPart();
431:
432: SOAPEnvelope envelope = soapPart.getEnvelope();
433: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
434: SOAPBody body = envelope.getBody();
435: SOAPFault soapFault = body.addFault();
436: soapFault.addFaultReasonText("myReason", Locale.ENGLISH);
437: soapMessage.saveChanges();
438:
439: } catch (SOAPException e) {
440: fail("Unexpected Exception Occurred : " + e);
441: }
442: }
443:
444: public void testAddFaultReasonText2() {
445: try {
446: MessageFactory fac = MessageFactory
447: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
448: SOAPMessage soapMessage = fac.createMessage();
449: SOAPPart soapPart = soapMessage.getSOAPPart();
450:
451: SOAPEnvelope envelope = soapPart.getEnvelope();
452: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
453: SOAPBody body = envelope.getBody();
454: SOAPFault soapFault = body.addFault();
455: soapFault.addFaultReasonText("myReason", new Locale("en"));
456: soapFault.addFaultReasonText("de-myReason",
457: new Locale("de"));
458: soapFault.addFaultReasonText("si-myReason",
459: new Locale("si"));
460: soapMessage.saveChanges();
461:
462: } catch (SOAPException e) {
463: fail("Unexpected Exception Occurred : " + e);
464: }
465: }
466:
467: public void testAddFaultReasonText3() {
468: try {
469: MessageFactory fac = MessageFactory
470: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
471: SOAPMessage soapMessage = fac.createMessage();
472: SOAPPart soapPart = soapMessage.getSOAPPart();
473:
474: SOAPEnvelope envelope = soapPart.getEnvelope();
475: SOAPBody body = envelope.getBody();
476: SOAPFault sf = body.addFault();
477:
478: String expected = "Its my fault again";
479: boolean found = false;
480: sf.addFaultReasonText("Its my fault", Locale.ENGLISH);
481: sf.addFaultReasonText(expected, Locale.ENGLISH);
482: Iterator i = sf.getFaultReasonTexts();
483: int j = 0;
484: while (i.hasNext()) {
485: Object o = i.next();
486: if (o != null && o instanceof String) {
487: String actual = (String) o;
488: if (actual.equals(expected)) {
489: if (!found) {
490: found = true;
491: }
492: }
493: }
494: j++;
495: }
496: if (j < 1) {
497: fail("No reason text was returned");
498: }
499: if (j > 1) {
500: fail("More than one reason text was returned");
501: }
502: if (!found) {
503: fail("The following Reason text was not received: '"
504: + expected + "'");
505: }
506: } catch (SOAPException e) {
507: fail("Unexpected Exception Occurred : " + e);
508: }
509: }
510:
511: public void testAddFaultReasonText4() throws Exception {
512: MessageFactory fac = MessageFactory
513: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
514: SOAPMessage soapMessage = fac.createMessage();
515: SOAPPart soapPart = soapMessage.getSOAPPart();
516: SOAPEnvelope envelope = soapPart.getEnvelope();
517: SOAPBody body = envelope.getBody();
518: SOAPFault sf = body.addFault();
519:
520: String expected1 = "Its my fault";
521: String expected2 = "Its my fault again";
522:
523: boolean found1 = false;
524: boolean found2 = false;
525: sf.addFaultReasonText(expected1, Locale.UK);
526: sf.addFaultReasonText(expected2, Locale.ENGLISH);
527: Iterator i = sf.getFaultReasonTexts();
528: int j = 0;
529: while (i.hasNext()) {
530: Object o = i.next();
531: if (o != null && o instanceof String) {
532: String actual = (String) o;
533: if (actual.equals(expected1)) {
534: if (!found1) {
535: found1 = true;
536: }
537: } else if (actual.equals(expected2)) {
538: if (!found2) {
539: found2 = true;
540: }
541: }
542: }
543: j++;
544: }
545: if (j < 1) {
546: fail("No reason text was returned");
547: }
548: if (j > 2) {
549: fail("More than two reason texts were returned");
550: }
551: if (!found1) {
552: fail("The following Reason text was not received: '"
553: + expected1 + "'");
554: }
555: if (!found2) {
556: fail("The following Reason text was not received: '"
557: + expected2 + "'");
558: }
559: }
560:
561: public void testSetFaultRole() {
562: try {
563: MessageFactory fac = MessageFactory
564: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
565:
566: SOAPMessage soapMessage = fac.createMessage();
567: SOAPPart soapPart = soapMessage.getSOAPPart();
568:
569: SOAPEnvelope envelope = soapPart.getEnvelope();
570: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
571: SOAPBody body = envelope.getBody();
572: SOAPFault soapFault = body.addFault();
573: soapFault.setFaultRole("test");
574: soapMessage.saveChanges();
575:
576: } catch (SOAPException e) {
577: fail("Unexpected Exception Occurred : " + e);
578: }
579: }
580:
581: public void testSetFaultNode() {
582: try {
583: MessageFactory fac = MessageFactory
584: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
585:
586: SOAPMessage soapMessage = fac.createMessage();
587: SOAPPart soapPart = soapMessage.getSOAPPart();
588:
589: SOAPEnvelope envelope = soapPart.getEnvelope();
590: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
591: SOAPBody body = envelope.getBody();
592: SOAPFault soapFault = body.addFault();
593: soapFault.setFaultNode("test");
594: soapMessage.saveChanges();
595:
596: } catch (SOAPException e) {
597: fail("Unexpected Exception Occurred : " + e);
598: }
599: }
600:
601: public void _testGetFaultReasonText() {
602: try {
603: MessageFactory fac = MessageFactory
604: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
605:
606: SOAPMessage soapMessage = fac.createMessage();
607: SOAPPart soapPart = soapMessage.getSOAPPart();
608:
609: SOAPEnvelope envelope = soapPart.getEnvelope();
610: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
611: SOAPBody body = envelope.getBody();
612: SOAPFault soapFault = body.addFault();
613: soapFault.addFaultReasonText("myReason", new Locale("en"));
614: soapFault.addFaultReasonText("de-myReason",
615: new Locale("de"));
616: soapFault.addFaultReasonText("si-myReason",
617: new Locale("si"));
618: soapMessage.saveChanges();
619:
620: String faultReasonText = soapFault
621: .getFaultReasonText(new Locale("si"));
622: assertNotNull(faultReasonText);
623: faultReasonText = soapFault.getFaultReasonText(new Locale(
624: "ja"));
625: assertNull(faultReasonText);
626:
627: } catch (Exception e) {
628: fail("Unexpected Exception : " + e);
629: }
630: }
631:
632: public void _testGetFaultCodeAsQName() {
633: try {
634: //MessageFactory fac = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
635: MessageFactory fac = MessageFactory.newInstance();
636:
637: SOAPMessage soapMessage = fac.createMessage();
638: SOAPPart soapPart = soapMessage.getSOAPPart();
639:
640: SOAPEnvelope envelope = soapPart.getEnvelope();
641: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
642: SOAPBody body = envelope.getBody();
643: SOAPFault soapFault = body.addFault();
644: soapFault.addFaultReasonText("myReason", new Locale("en"));
645: soapFault.setFaultCode("mycode");
646: soapMessage.saveChanges();
647:
648: QName qname = soapFault.getFaultCodeAsQName();
649: assertNotNull(qname);
650:
651: } catch (Exception e) {
652: fail("Unexpected Exception : " + e);
653: }
654: }
655:
656: public void testHasDetail() {
657: try {
658: MessageFactory fac = MessageFactory
659: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
660: //MessageFactory fac = MessageFactory.newInstance();
661:
662: SOAPMessage soapMessage = fac.createMessage();
663: SOAPPart soapPart = soapMessage.getSOAPPart();
664:
665: SOAPEnvelope envelope = soapPart.getEnvelope();
666: envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
667: SOAPBody body = envelope.getBody();
668: SOAPFault soapFault = body.addFault();
669: Detail detail = soapFault.addDetail();
670: detail.setAttribute("test", "myvalue");
671: soapMessage.saveChanges();
672: } catch (Exception e) {
673: fail("Unexpected Exception : " + e);
674: }
675: }
676:
677: public void testFaultReasonLocales() {
678: try {
679: MessageFactory fac = MessageFactory
680: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
681: SOAPMessage soapMessage = fac.createMessage();
682: SOAPPart soapPart = soapMessage.getSOAPPart();
683: SOAPEnvelope envelope = soapPart.getEnvelope();
684: SOAPBody body = envelope.getBody();
685: //SOAPFault sf = body.addFault();
686:
687: Locale expected1 = Locale.ENGLISH;
688: Locale expected2 = Locale.UK;
689: Locale expected3 = Locale.GERMAN;
690: boolean found1 = false;
691: boolean found2 = false;
692: boolean found3 = false;
693:
694: SOAPFault sf = body.addFault(
695: SOAPConstants.SOAP_RECEIVER_FAULT, "Its my fault1",
696: expected1);
697: sf.addFaultReasonText("Its my fault1", expected1);
698: sf.addFaultReasonText("Its my fault2", expected2);
699: sf.addFaultReasonText("Its my fault3", expected3);
700: Iterator i = sf.getFaultReasonLocales();
701:
702: int localeCount = 0;
703: while (i.hasNext()) {
704: localeCount++;
705: i.next();
706: }
707:
708: i = sf.getFaultReasonLocales();
709: int j = 0;
710: while (i.hasNext()) {
711: Object o = i.next();
712: if (o instanceof Locale) {
713: Locale actual = (Locale) o;
714: if (actual != null) {
715: if (actual.equals(expected1)) {
716: if (!found1) {
717: found1 = true;
718: }
719: } else if (actual.equals(expected2)) {
720: if (!found2) {
721: found2 = true;
722: }
723: } else if (actual.equals(expected3)) {
724: if (!found3) {
725: found3 = true;
726: }
727: }
728: }
729: }
730: j++;
731: }
732: if (j < 1) {
733: fail("No reason text was returned");
734: }
735: if (j > 3) {
736: fail("More than 3 Locales were returned");
737: }
738: if (!found1) {
739: fail("The following Locale was not received: '"
740: + expected1 + "'");
741: }
742: if (!found2) {
743: fail("The following Locale was not received: '"
744: + expected2 + "'");
745: }
746: if (!found3) {
747: fail("The following Locale was not received: '"
748: + expected3 + "'");
749: }
750: } catch (Exception e) {
751: fail("Exception: " + e);
752: }
753: }
754:
755: public void testFaultStringLocale() {
756: try {
757: MessageFactory fac = MessageFactory
758: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
759: //MessageFactory fac = MessageFactory.newInstance();
760: SOAPMessage soapMessage = fac.createMessage();
761: SOAPPart soapPart = soapMessage.getSOAPPart();
762: SOAPEnvelope envelope = soapPart.getEnvelope();
763: SOAPBody body = envelope.getBody();
764: SOAPFault sf = body.addFault();
765:
766: //Setting fault string with no Locale
767: sf.setFaultString("this is the fault string");
768: Locale result = sf.getFaultStringLocale();
769: assertNotNull(result);
770: } catch (Exception e) {
771: fail("Exception: " + e);
772: }
773: }
774:
775: public void testFaultStringLocale2() {
776: try {
777: MessageFactory fac = MessageFactory
778: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
779: //MessageFactory fac = MessageFactory.newInstance();
780: SOAPMessage soapMessage = fac.createMessage();
781: SOAPPart soapPart = soapMessage.getSOAPPart();
782: SOAPEnvelope envelope = soapPart.getEnvelope();
783: SOAPBody body = envelope.getBody();
784: SOAPFault sf = body.addFault();
785:
786: sf.setFaultString("this is the fault string");
787: Locale result = sf.getFaultStringLocale();
788: assertNotNull(result);
789: assertTrue(result.equals(Locale.getDefault()));
790: } catch (Exception e) {
791: fail("Exception: " + e);
792: }
793: }
794:
795: public void testSetFaultStringLocale() {
796: try {
797: MessageFactory fac = MessageFactory
798: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
799: //MessageFactory fac = MessageFactory.newInstance();
800: SOAPMessage soapMessage = fac.createMessage();
801: SOAPPart soapPart = soapMessage.getSOAPPart();
802: SOAPEnvelope envelope = soapPart.getEnvelope();
803: SOAPBody body = envelope.getBody();
804: SOAPFault sf = body.addFault();
805:
806: Locale expected = Locale.ENGLISH;
807: sf.setFaultString("this is the fault string", expected);
808: Locale result = sf.getFaultStringLocale();
809: assertNotNull(result);
810: assertTrue(result.equals(expected));
811: } catch (Exception e) {
812: fail("Exception: " + e);
813: }
814: }
815:
816: public void testFaultCodeWithPrefix1() {
817: try {
818: MessageFactory fac = MessageFactory.newInstance();
819: SOAPMessage soapMessage = fac.createMessage();
820: SOAPPart soapPart = soapMessage.getSOAPPart();
821: SOAPEnvelope envelope = soapPart.getEnvelope();
822: SOAPBody body = envelope.getBody();
823: SOAPFault sf = body.addFault();
824:
825: String prefix = "wso2";
826: sf.setFaultCode(prefix + ":Server");
827: String result = sf.getFaultCode();
828:
829: assertNotNull(result);
830: assertEquals(prefix + ":Server", result);
831: } catch (Exception e) {
832: fail(e.getMessage());
833: }
834: }
835:
836: public void testFaultCodeWithPrefix2() {
837: try {
838: MessageFactory fac = MessageFactory
839: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
840: SOAPMessage soapMessage = fac.createMessage();
841: SOAPPart soapPart = soapMessage.getSOAPPart();
842: SOAPEnvelope envelope = soapPart.getEnvelope();
843: SOAPBody body = envelope.getBody();
844: SOAPFault sf = body.addFault();
845:
846: String prefix = "wso2";
847: sf.setFaultCode(prefix + ":Server");
848: String result = sf.getFaultCode();
849:
850: assertNotNull(result);
851: assertEquals(prefix + ":Server", result);
852: } catch (Exception e) {
853: fail(e.getMessage());
854: }
855: }
856:
857: public void testSetGetFaultCodeAsName1() {
858: try {
859: SOAPFactory fac = SOAPFactory
860: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
861: SOAPFault sf = fac.createFault();
862:
863: Name name = fac.createName("myfault", "flt",
864: "http://example.com");
865: sf.setFaultCode(name);
866:
867: Name name2 = sf.getFaultCodeAsName();
868: assertNotNull(name2);
869: assertEquals(name.getLocalName(), name2.getLocalName());
870: assertEquals(name.getPrefix(), name2.getPrefix());
871: assertEquals(name.getURI(), name2.getURI());
872:
873: QName name3 = sf.getFaultCodeAsQName();
874: assertNotNull(name3);
875: assertEquals(name.getLocalName(), name3.getLocalPart());
876: assertEquals(name.getPrefix(), name3.getPrefix());
877: assertEquals(name.getURI(), name3.getNamespaceURI());
878:
879: } catch (Exception e) {
880: fail(e.getMessage());
881: }
882: }
883:
884: public void testSetGetFaultCodeAsName2() {
885: try {
886: QName qname = SOAPConstants.SOAP_SENDER_FAULT;
887: SOAPFactory fac = SOAPFactory
888: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
889: Name name = fac.createName(qname.getLocalPart(), qname
890: .getPrefix(), qname.getNamespaceURI());
891: SOAPFault sf = fac.createFault();
892: sf.setFaultCode(name);
893:
894: Name name2 = sf.getFaultCodeAsName();
895: assertNotNull(name2);
896: assertEquals(name.getLocalName(), name2.getLocalName());
897: assertEquals(name.getPrefix(), name2.getPrefix());
898: assertEquals(name.getURI(), name2.getURI());
899:
900: QName name3 = sf.getFaultCodeAsQName();
901: assertNotNull(name3);
902: assertEquals(name.getLocalName(), name3.getLocalPart());
903: assertEquals(name.getPrefix(), name3.getPrefix());
904: assertEquals(name.getURI(), name3.getNamespaceURI());
905:
906: } catch (Exception e) {
907: fail(e.getMessage());
908: }
909:
910: }
911:
912: public void testSetGetFaultCodeAsQName1() throws Exception {
913: SOAPFactory fac = SOAPFactory
914: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
915: SOAPFault sf = fac.createFault();
916:
917: QName name = new QName("http://example.com", "myfault", "flt");
918: sf.setFaultCode(name);
919:
920: QName name2 = sf.getFaultCodeAsQName();
921: assertNotNull(name2);
922: assertEquals(name.getLocalPart(), name2.getLocalPart());
923: assertEquals(name.getPrefix(), name2.getPrefix());
924: assertEquals(name.getNamespaceURI(), name2.getNamespaceURI());
925:
926: Name name3 = sf.getFaultCodeAsName();
927: assertNotNull(name3);
928: assertEquals(name.getLocalPart(), name3.getLocalName());
929: assertEquals(name.getPrefix(), name3.getPrefix());
930: assertEquals(name.getNamespaceURI(), name3.getURI());
931: }
932:
933: public void testSetGetFaultCodeAsQName2() {
934: try {
935: QName name = SOAPConstants.SOAP_SENDER_FAULT;
936: SOAPFactory fac = SOAPFactory
937: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
938: SOAPFault sf = fac.createFault();
939: sf.setFaultCode(name);
940:
941: QName name2 = sf.getFaultCodeAsQName();
942: assertNotNull(name2);
943: assertEquals(name.getLocalPart(), name2.getLocalPart());
944: assertEquals(name.getPrefix(), name2.getPrefix());
945: assertEquals(name.getNamespaceURI(), name2
946: .getNamespaceURI());
947:
948: Name name3 = sf.getFaultCodeAsName();
949: assertNotNull(name3);
950: assertEquals(name.getLocalPart(), name3.getLocalName());
951: assertEquals(name.getPrefix(), name3.getPrefix());
952: assertEquals(name.getNamespaceURI(), name3.getURI());
953: } catch (Exception e) {
954: fail(e.getMessage());
955: }
956: }
957:
958: public void testFault12Defaults() {
959: try {
960: MessageFactory messageFactory = MessageFactory
961: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
962: SOAPMessage message = messageFactory.createMessage();
963: SOAPBody body = message.getSOAPBody();
964: SOAPFault fault = body.addFault();
965: assertNotNull(fault.getFaultCodeAsQName());
966: assertNotNull(fault.getFaultString());
967: } catch (Exception e) {
968: fail(e.getMessage());
969: }
970: }
971:
972: public void testFault11Defaults() {
973: try {
974: MessageFactory messageFactory = MessageFactory
975: .newInstance();
976: SOAPMessage message = messageFactory.createMessage();
977: SOAPBody body = message.getSOAPBody();
978: SOAPFault fault = body.addFault();
979:
980: assertNotNull(fault.getFaultCodeAsQName());
981: assertNotNull(fault.getFaultString());
982: } catch (Exception e) {
983: fail(e.getMessage());
984: }
985: }
986:
987: }
|