KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestSOAPFaultDetail


1
2 package test.saaj;
3
4 import org.apache.axis.Message;
5 import org.apache.axis.MessageContext;
6 import org.apache.axis.server.AxisServer;
7 import org.apache.axis.message.SOAPBodyElement;
8 import org.apache.axis.message.SOAPFault;
9 import org.apache.axis.message.SOAPEnvelope;
10 import org.apache.axis.encoding.DeserializationContext;
11 import org.xml.sax.InputSource JavaDoc;
12
13 import javax.xml.soap.DetailEntry JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 public class TestSOAPFaultDetail extends junit.framework.TestCase
19 {
20     private MessageContext _msgContext;
21
22     public TestSOAPFaultDetail(String JavaDoc name)
23     {
24         super(name);
25         _msgContext = new MessageContext(new AxisServer());
26     }
27
28     String JavaDoc xmlString =
29 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
30 "<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\">" +
31 " <soapenv:Body>" +
32 " <soapenv:Fault>" +
33 " <faultcode>soapenv:Server.generalException</faultcode>" +
34 " <faultstring></faultstring>" +
35 " <detail>" +
36 " <tickerSymbol xsi:type=\"xsd:string\">MACR</tickerSymbol>" +
37 " <ns1:exceptionName xmlns:ns1=\"http://xml.apache.org/axis/\">test.wsdl.faults.InvalidTickerFaultMessage</ns1:exceptionName>" +
38 " </detail>" +
39 " </soapenv:Fault>" +
40 " </soapenv:Body>" +
41 "</soapenv:Envelope>";
42
43
44     public void testDetails() throws Exception JavaDoc
45     {
46         Reader JavaDoc reader = new StringReader JavaDoc(xmlString);
47         InputSource JavaDoc src = new InputSource JavaDoc(reader);
48         SOAPBodyElement bodyItem = getFirstBody(src);
49         assertTrue("The SOAPBodyElement I got was not a SOAPFault, it was a " +
50                 bodyItem.getClass().getName(), bodyItem instanceof SOAPFault);
51         SOAPFault flt = (SOAPFault)bodyItem;
52         flt.addDetail();
53         javax.xml.soap.Detail JavaDoc d = flt.getDetail();
54         Iterator JavaDoc i = d.getDetailEntries();
55         while (i.hasNext())
56         {
57             DetailEntry JavaDoc entry = (DetailEntry JavaDoc) i.next();
58             String JavaDoc name = entry.getElementName().getLocalName();
59             if ("tickerSymbol".equals(name)) {
60                 assertEquals("the value of the tickerSymbol element didn't match",
61                         "MACR", entry.getValue());
62             } else if ("exceptionName".equals(name)) {
63                 assertEquals("the value of the exceptionName element didn't match",
64                         "test.wsdl.faults.InvalidTickerFaultMessage", entry.getValue());
65             } else {
66                 assertTrue("Expecting details element name of 'tickerSymbol' or 'expceptionName' - I found :" + name, false);
67             }
68         }
69         assertTrue(d != null);
70     }
71
72     private SOAPBodyElement getFirstBody(InputSource JavaDoc msgSource)
73             throws Exception JavaDoc
74     {
75         DeserializationContext dser = new DeserializationContext(
76                 msgSource, _msgContext, Message.RESPONSE);
77         dser.parse();
78         SOAPEnvelope env = dser.getEnvelope();
79
80         return env.getFirstBody();
81     }
82
83     /**
84      * Main
85      */

86     public static void main(String JavaDoc[] args)
87             throws Exception JavaDoc
88     {
89         TestSOAPFaultDetail detailTest = new TestSOAPFaultDetail("faultdetails");
90         detailTest.testDetails();
91     }
92
93 }
94
Popular Tags