KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > faults > TestSOAPFaultException


1 /*
2  * Copyright 2002-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
17 package test.faults;
18
19 import junit.framework.TestCase;
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.apache.axis.*;
24 import org.apache.axis.AxisEngine;
25 import org.apache.axis.AxisFault;
26 import org.apache.axis.Constants;
27 import org.apache.axis.EngineConfiguration;
28 import org.apache.axis.configuration.SimpleProvider;
29 import org.apache.axis.deployment.wsdd.WSDDConstants;
30 import org.apache.axis.deployment.wsdd.WSDDDeployment;
31 import org.apache.axis.deployment.wsdd.WSDDService;
32 import org.apache.axis.handlers.soap.SOAPService;
33 import org.apache.axis.message.RPCElement;
34 import org.apache.axis.message.SOAPBody;
35 import org.apache.axis.message.SOAPEnvelope;
36 import org.apache.axis.message.SOAPFault;
37 import org.apache.axis.providers.java.RPCProvider;
38 import org.apache.axis.server.AxisServer;
39 import org.apache.axis.soap.SOAP11Constants;
40 import org.apache.axis.soap.SOAPConstants;
41 import org.apache.axis.transport.local.LocalTransport;
42 import org.apache.axis.utils.XMLUtils;
43 import org.w3c.dom.Element JavaDoc;
44 import org.w3c.dom.NodeList JavaDoc;
45 import org.w3c.dom.Text JavaDoc;
46
47 import javax.xml.namespace.QName JavaDoc;
48 import javax.xml.rpc.soap.SOAPFaultException JavaDoc;
49 import javax.xml.soap.Detail JavaDoc;
50 import javax.xml.soap.SOAPException JavaDoc;
51 import javax.xml.soap.SOAPFactory JavaDoc;
52
53 /**
54  * Tests for the conversion of a JAX-RPC SOAPFaultException to an AxisFault
55  *
56  * @author Thomas Bayer (bayer@oio.de)
57  */

58 public class TestSOAPFaultException extends TestCase {
59
60     private static final SOAPConstants soapConsts = new SOAP11Constants();
61     private static final QName JavaDoc QNAME_FAULT_SERVER_USER = new QName JavaDoc(soapConsts.getEnvelopeURI(), Constants.FAULT_SERVER_USER);
62     private static final String JavaDoc SERVICE_NAME = "FailingService";
63
64     /**
65      * A construktor is needed for instanciation cause this class is also
66      * used as service implementation
67      */

68     public TestSOAPFaultException() {
69         super("TestSOAPFaultException");
70     }
71
72     public TestSOAPFaultException(String JavaDoc s) {
73         super(s);
74     }
75
76     public static Test suite() {
77         return new TestSuite(TestSOAPFaultException.class);
78     }
79
80     /**
81      * Tests the defaults generated from a SOAPFaultException
82      */

83     public void testDefaults() {
84         SOAPFaultException JavaDoc soapFaultException = new SOAPFaultException JavaDoc(null, null, null, null);
85
86         AxisFault axisFault = AxisFault.makeFault(soapFaultException);
87
88         assertEquals(QNAME_FAULT_SERVER_USER, axisFault.getFaultCode());
89         assertNotNull(axisFault.getFaultString());
90     }
91
92     /**
93      * Tests if an AxisFault is initialized with the values from a SOAPFaultException
94      */

95     public void testMakeFaultOutOfSOAPFaultException() {
96
97         QName JavaDoc faultcode = new QName JavaDoc(soapConsts.getEnvelopeURI(), "Server.MySubClass");
98
99         SOAPFaultException JavaDoc soapFaultException = new SOAPFaultException JavaDoc(faultcode, "MyFaultString", "http://myactor", null);
100
101         AxisFault axisFault = AxisFault.makeFault(soapFaultException);
102
103         assertEquals(faultcode, axisFault.getFaultCode());
104     }
105
106     /**
107      * Tests if SOAP fault details are passed to AxisFault objects
108      */

109     public void testDetails() {
110         SOAPFaultException JavaDoc soapFaultException = new SOAPFaultException JavaDoc(QNAME_FAULT_SERVER_USER, "MyFaultString", "http://myactor", getTestDetail());
111
112         AxisFault axisFault = AxisFault.makeFault(soapFaultException);
113
114         assertNotNull(axisFault.getFaultDetails());
115
116         checkDetailAgainstTestDetail(axisFault);
117     }
118
119     /**
120      * Tests if a SOAPFaultException can be thrown from within a service
121      * method and the faultcode, faultstring and detail are passed to
122      * the AxisFault object.
123      * @throws Exception
124      */

125     public void testThrowingSOAPFaultExceptionFromServiceMethod() throws Exception JavaDoc {
126
127         WSDDDeployment conf = new WSDDDeployment();
128
129         WSDDService service = new WSDDService();
130         service.setName(SERVICE_NAME);
131         service.setProviderQName(new QName JavaDoc(WSDDConstants.URI_WSDD_JAVA, "RPC"));
132         service.setParameter("className", this.getClass().getName());
133         service.setParameter("allowedMethods", "doSth");
134         service.deployToRegistry(conf);
135
136         AxisServer engine = new AxisServer(conf);
137
138         LocalTransport transport = new LocalTransport(engine);
139         transport.setRemoteService(SERVICE_NAME);
140
141         // create messageContext
142
MessageContext mc = new MessageContext(engine);
143         mc.setService((SOAPService) service.getInstance(conf));
144         mc.setProperty(MessageContext.TRANS_URL, "local");
145
146         // create SOAP envelope
147
SOAPEnvelope env = new SOAPEnvelope();
148         SOAPBody body = (SOAPBody) env.getBody();
149         body.addChildElement(new RPCElement("doSth"));
150         Message reqMsg = new Message(env);
151         mc.setRequestMessage(reqMsg);
152
153         // invoke the engine and test if the fault contains everything as
154
try {
155             engine.invoke(mc);
156         } catch (AxisFault af) {
157             checkDetailAgainstTestDetail(af);
158         }
159     }
160
161     /**
162      * Service method which is called with an axis engine. Its used to test if a service
163      * can throw an SOAPFaultException and all the information as faultcode and faultactor
164      * is passed to the caller
165      * @throws Exception
166      */

167     public void doSth() throws Exception JavaDoc {
168         throw new SOAPFaultException JavaDoc(QNAME_FAULT_SERVER_USER, "MyFaultString", "http://myactor", getTestDetail());
169     }
170
171     /**
172      * Produces a Detail object filled with test data
173      * @return a Detail object filled with test data
174      */

175     private Detail JavaDoc getTestDetail() {
176         Detail JavaDoc detail = null;
177         try {
178             detail = SOAPFactory.newInstance().createDetail();
179             detail.addChildElement("MyDetails").addTextNode("hossa");
180             detail.addChildElement("foo").addChildElement("baz");
181         } catch (SOAPException JavaDoc e) {
182             fail("Can't create detail");
183         }
184         return detail;
185     }
186
187     /**
188      * Checks if the AxisFault contains the expected detail elements
189      *
190      * @param axisFault
191      */

192     private void checkDetailAgainstTestDetail(AxisFault axisFault) {
193         Element[] details = axisFault.getFaultDetails();
194
195         assertEquals("wrong name for detail element", "MyDetails", details[0].getNodeName());
196         assertEquals("wrong node value for detail element", "hossa", ((Text JavaDoc) details[0].getChildNodes().item(0)).getData());
197         assertEquals("wrong name for foo element", "foo", details[1].getNodeName());
198
199         boolean found = false;
200         NodeList JavaDoc childs = details[1].getChildNodes();
201         for (int i = 0; i < childs.getLength(); i++) {
202             if ("baz".equals(childs.item(i).getNodeName()))
203                 found = true;
204         }
205         assertTrue("subelement baz not found in details", found);
206     }
207 }
208
Popular Tags