KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > soap12 > TestFault


1 package test.soap12;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.apache.axis.AxisFault;
7 import org.apache.axis.Message;
8 import org.apache.axis.MessageContext;
9 import org.apache.axis.Constants;
10 import org.apache.axis.encoding.SerializationContext;
11 import org.apache.axis.encoding.SerializationContext;
12 import org.apache.axis.message.SOAPBodyElement;
13 import org.apache.axis.message.SOAPEnvelope;
14 import org.apache.axis.message.SOAPFault;
15 import org.apache.axis.server.AxisServer;
16 import org.apache.axis.soap.SOAPConstants;
17
18 import javax.xml.namespace.QName JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 /**
23  * A test of soap 1.2 fault serialization and deserialization.
24  *
25  * @author Andras Avar (andras.avar@nokia.com)
26  */

27 public class TestFault extends TestCase {
28     public static Test suite() {
29         return new TestSuite(TestFault.class);
30     }
31
32     public TestFault(String JavaDoc name) {
33         super(name);
34     }
35     public void setUp() throws Exception JavaDoc {
36     }
37
38     public static final QName JavaDoc FAULTCODE = new QName JavaDoc("http://c","faultcode_c");
39     public static final QName JavaDoc FAULTSUBCODE[] = { new QName JavaDoc("http://a","subcode_a"),
40                                                  new QName JavaDoc("http://b","subcode_b") };
41     public static final String JavaDoc FAULTREASON = "reason";
42     public static final String JavaDoc FAULTROLE = "role";
43     public static final String JavaDoc FAULTNODE = "node";
44
45     public void testFault() throws Exception JavaDoc
46     {
47         // Serialize
48
MessageContext msgContext = new MessageContext(new AxisServer());
49         msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
50         msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
51         
52         SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
53
54         SOAPFault fault = new SOAPFault(new AxisFault(FAULTCODE, FAULTSUBCODE,
55                                                       FAULTREASON, FAULTROLE,
56                                                       FAULTNODE, null));
57
58         msg.addBodyElement(fault);
59         Writer JavaDoc stringWriter = new StringWriter JavaDoc();
60         SerializationContext context =
61                 new SerializationContext(stringWriter, msgContext);
62         context.setDoMultiRefs(false);
63         msg.output(context);
64         String JavaDoc msgString = stringWriter.toString();
65
66         // Deserialize and check
67
AxisServer server = new AxisServer();
68         Message message = new Message(msgString);
69         message.setMessageContext(new MessageContext(server));
70
71         SOAPEnvelope envelope = message.getSOAPEnvelope();
72         assertNotNull("envelope should not be null", envelope);
73
74         SOAPBodyElement respBody = envelope.getFirstBody();
75         assertTrue("respBody should be a SOAPFaultElement", respBody
76                         instanceof SOAPFault);
77         AxisFault aFault = ((SOAPFault) respBody).getFault();
78
79         assertNotNull("Fault should not be null", aFault);
80
81         assertEquals(FAULTCODE, aFault.getFaultCode());
82         assertEquals(FAULTREASON, aFault.getFaultReason());
83         assertEquals(FAULTROLE, aFault.getFaultRole());
84         assertEquals(FAULTNODE, aFault.getFaultNode());
85         QName JavaDoc q[] = aFault.getFaultSubCodes();
86         for (int i = 0; i < q.length; i++)
87             assertEquals(FAULTSUBCODE[i], q[i]);
88     }
89
90 }
91
Popular Tags