KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > faults > FaultDecode


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package test.faults;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22 import org.apache.axis.AxisFault;
23 import org.apache.axis.Message;
24 import org.apache.axis.MessageContext;
25 import org.apache.axis.message.SOAPBodyElement;
26 import org.apache.axis.message.SOAPEnvelope;
27 import org.apache.axis.message.SOAPFault;
28 import org.apache.axis.server.AxisServer;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Text JavaDoc;
31
32 import javax.xml.namespace.QName JavaDoc;
33
34 /**
35  * This class tests Fault deserialization.
36  *
37  * @author Mark Roder <mroder@wamnet.com>
38  * @author Glen Daniels (gdaniels@apache.org)
39  */

40
41 public class FaultDecode extends TestCase {
42     public static final String JavaDoc FAULT_CODE = "Some.FaultCode";
43     public static final String JavaDoc FAULT_STRING = "This caused a fault";
44     public static final String JavaDoc DETAIL_ENTRY_TEXT =
45         "This was a really bad thing";
46     
47     public FaultDecode(String JavaDoc name) {
48         super(name);
49     } // ctor
50

51     public static Test suite() {
52         return new TestSuite(FaultDecode.class);
53     }
54
55     public void testFault() throws Exception JavaDoc {
56         String JavaDoc messageText = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
57             + "<soap:Header>"
58             + ""
59             + "</soap:Header> "
60             + "<soap:Body> "
61             + " <soap:Fault>"
62             + " <faultcode>" + FAULT_CODE + "</faultcode>"
63             + " <faultstring>" + FAULT_STRING + "</faultstring>"
64             + " <detail><d1>" + DETAIL_ENTRY_TEXT + "</d1></detail>"
65             + " </soap:Fault>"
66             + "</soap:Body>"
67             + "</soap:Envelope>";
68
69         AxisServer server = new AxisServer();
70         Message message = new Message(messageText);
71         message.setMessageContext(new MessageContext(server));
72
73         SOAPEnvelope envelope = (SOAPEnvelope) message.getSOAPEnvelope();
74         assertNotNull("envelope", envelope);
75
76         SOAPBodyElement respBody = envelope.getFirstBody();
77         assertTrue("respBody should be a SOAPFaultElement", respBody
78                         instanceof SOAPFault);
79         AxisFault aFault = ((SOAPFault) respBody).getFault();
80
81         assertNotNull("Fault should not be null", aFault);
82         
83         QName JavaDoc faultCode = aFault.getFaultCode();
84         assertNotNull("faultCode should not be null", faultCode);
85         assertEquals("faultCode should match",
86                      faultCode.getLocalPart(),
87                      "Some.FaultCode");
88         
89         String JavaDoc faultString = aFault.getFaultString();
90         assertNotNull("faultString should not be null", faultString);
91         assertEquals("faultString should match", faultString,
92                      FAULT_STRING);
93         
94         Element [] details = aFault.getFaultDetails();
95         assertNotNull("faultDetails should not be null", details);
96         assertEquals("details should have exactly one element", details.length,
97                      1);
98         
99         Element el = details[0];
100         assertEquals("detail entry tag name should match",
101                      el.getLocalName(), "d1");
102         
103         Text JavaDoc text = (Text JavaDoc)el.getFirstChild();
104         assertEquals("detail entry string should match",
105                      text.getData(), DETAIL_ENTRY_TEXT);
106         
107     } // testFault
108

109     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
110         FaultDecode tester = new FaultDecode("test");
111         TestRunner runner = new TestRunner();
112         runner.doRun(tester.suite(), false);
113     }
114 }
115
Popular Tags