KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestSOAPFaults


1 package test.saaj;
2
3 import test.AxisTestBase;
4
5 import javax.xml.soap.Detail JavaDoc;
6 import javax.xml.soap.MessageFactory JavaDoc;
7 import javax.xml.soap.SOAPFactory JavaDoc;
8 import javax.xml.soap.SOAPFault JavaDoc;
9 import javax.xml.soap.SOAPMessage JavaDoc;
10 import javax.xml.soap.SOAPElement JavaDoc;
11 import javax.xml.soap.DetailEntry JavaDoc;
12 import javax.xml.soap.Name JavaDoc;
13 import javax.xml.soap.SOAPPart JavaDoc;
14 import javax.xml.soap.SOAPEnvelope JavaDoc;
15 import javax.xml.soap.SOAPBody JavaDoc;
16 import javax.xml.soap.SOAPHeader JavaDoc;
17 import javax.xml.soap.SOAPHeaderElement JavaDoc;
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 public class TestSOAPFaults extends AxisTestBase {
22     public TestSOAPFaults(String JavaDoc name) {
23         super(name);
24     }
25
26     public void testQuick() throws Exception JavaDoc {
27         MessageFactory JavaDoc msgfactory = MessageFactory.newInstance();
28         SOAPFactory JavaDoc factory = SOAPFactory.newInstance();
29         SOAPMessage JavaDoc outputmsg = msgfactory.createMessage();
30         String JavaDoc valueCode = "faultcode";
31         String JavaDoc valueString = "faultString";
32         SOAPFault JavaDoc fault = outputmsg.getSOAPPart().getEnvelope().getBody().addFault();
33         fault.setFaultCode(valueCode);
34         fault.setFaultString(valueString);
35         Detail JavaDoc d;
36         d = fault.addDetail();
37         d.addDetailEntry(factory.createName("Hello"));
38         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
39         if (outputmsg != null) {
40             if (outputmsg.saveRequired()) {
41                 outputmsg.saveChanges();
42             }
43             outputmsg.writeTo(baos);
44         }
45         String JavaDoc xml = new String JavaDoc(baos.toByteArray());
46         assertTrue(xml.indexOf("Hello")!=-1);
47     }
48     
49     public void testSOAPFaultSaveChanges() throws Exception JavaDoc {
50         MessageFactory JavaDoc msgFactory =
51                 MessageFactory.newInstance();
52         SOAPMessage JavaDoc msg = msgFactory.createMessage();
53         SOAPEnvelope JavaDoc envelope =
54                 msg.getSOAPPart().getEnvelope();
55         SOAPBody JavaDoc body = envelope.getBody();
56         SOAPFault JavaDoc fault = body.addFault();
57
58         fault.setFaultCode("Client");
59         fault.setFaultString(
60                 "Message does not have necessary info");
61         fault.setFaultActor("http://gizmos.com/order");
62
63         Detail JavaDoc detail = fault.addDetail();
64
65         Name JavaDoc entryName = envelope.createName("order", "PO",
66                 "http://gizmos.com/orders/");
67         DetailEntry JavaDoc entry = detail.addDetailEntry(entryName);
68         entry.addTextNode(
69                 "quantity element does not have a value");
70
71         Name JavaDoc entryName2 = envelope.createName("confirmation",
72                 "PO", "http://gizmos.com/confirm");
73         DetailEntry JavaDoc entry2 = detail.addDetailEntry(entryName2);
74         entry2.addTextNode("Incomplete address: no zip code");
75
76         msg.saveChanges();
77
78         // Now retrieve the SOAPFault object and its contents
79
//after checking to see that there is one
80

81         if (body.hasFault()) {
82             fault = body.getFault();
83             String JavaDoc code = fault.getFaultCode();
84             String JavaDoc string = fault.getFaultString();
85             String JavaDoc actor = fault.getFaultActor();
86
87             System.out.println("SOAP fault contains: ");
88             System.out.println(" fault code = " + code);
89             System.out.println(" fault string = " + string);
90             if (actor != null) {
91                 System.out.println(" fault actor = " + actor);
92             }
93
94             detail = fault.getDetail();
95             if (detail != null) {
96                 Iterator JavaDoc it = detail.getDetailEntries();
97                 while (it.hasNext()) {
98                     entry = (DetailEntry JavaDoc) it.next();
99                     String JavaDoc value = entry.getValue();
100                     System.out.println(" Detail entry = " + value);
101                 }
102             }
103         }
104         
105     }
106
107     public void testAxis1432() throws Exception JavaDoc {
108         String JavaDoc xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:cwmp=\"http://cwmp.com\">\n" +
109                 " <soapenv:Header>\n" +
110                 " <cwmp:ID soapenv:mustUnderstand=\"1\">HEADERID-7867678</cwmp:ID>\n" +
111                 " </soapenv:Header>\n" +
112                 " <soapenv:Body>\n" +
113                 " <soapenv:Fault>\n" +
114                 " <faultcode>soapenv:Client</faultcode>\n" +
115                 " <faultstring>CWMP fault</faultstring>\n" +
116                 " <detail>\n" +
117                 " <cwmp:Fault>\n" +
118                 " <cwmp:FaultCode>This is the fault code</cwmp:FaultCode>\n" +
119                 " <cwmp:FaultString>Fault Message</cwmp:FaultString>\n" +
120                 " </cwmp:Fault>\n" +
121                 " </detail>\n" +
122                 " </soapenv:Fault>\n" +
123                 " </soapenv:Body>\n" +
124                 "</soapenv:Envelope>";
125         
126         MessageFactory JavaDoc fac = MessageFactory.newInstance();
127         SOAPMessage JavaDoc faultMessage = fac.createMessage();
128
129         // Create the response to the message
130
faultMessage = fac.createMessage();
131         SOAPPart JavaDoc part = faultMessage.getSOAPPart();
132         SOAPEnvelope JavaDoc envelope = part.getEnvelope();
133         envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com");
134         SOAPBody JavaDoc body = envelope.getBody();
135         SOAPHeader JavaDoc header = envelope.getHeader();
136         Name JavaDoc idName = envelope.createName("ID", "cwmp", "http://cwmp.com");
137         SOAPHeaderElement JavaDoc id = header.addHeaderElement(idName);
138         id.setMustUnderstand(true);
139         id.addTextNode("HEADERID-7867678");
140         id.setActor(null);
141
142         // Create the SOAPFault object
143
SOAPFault JavaDoc fault = body.addFault();
144         fault.setFaultCode("Client");
145         fault.setFaultString("CWMP fault");
146         fault.setFaultActor(null);
147
148         // Add Fault Detail information
149
Detail JavaDoc faultDetail = fault.addDetail();
150         Name JavaDoc cwmpFaultName = envelope.createName("Fault", "cwmp",
151                 "http://cwmp.com");
152         DetailEntry JavaDoc cwmpFaultDetail =
153                 faultDetail.addDetailEntry(cwmpFaultName);
154         SOAPElement JavaDoc e = cwmpFaultDetail.addChildElement("FaultCode");
155
156         e.addTextNode("This is the fault code");
157         SOAPElement JavaDoc e2 = cwmpFaultDetail.addChildElement(envelope.createName("FaultString", "cwmp", "http://cwmp.com"));
158         e2.addTextNode("Fault Message");
159         faultMessage.saveChanges();
160
161         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
162         faultMessage.writeTo(baos);
163         String JavaDoc xml2 = new String JavaDoc(baos.toByteArray());
164         assertXMLEqual(xml,xml2);
165     }
166
167     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
168         TestSOAPFaults detailTest = new TestSOAPFaults("TestSOAPFaults");
169         detailTest.testQuick();
170     }
171 }
172
Popular Tags