KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > exception > ExceptionTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.exception;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26
27 import javax.naming.InitialContext JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.rpc.Call JavaDoc;
30 import javax.xml.rpc.Service JavaDoc;
31 import javax.xml.rpc.ServiceFactory JavaDoc;
32 import javax.xml.rpc.soap.SOAPFaultException JavaDoc;
33 import javax.xml.soap.Detail JavaDoc;
34 import javax.xml.soap.MessageFactory JavaDoc;
35 import javax.xml.soap.Name JavaDoc;
36 import javax.xml.soap.SOAPBody JavaDoc;
37 import javax.xml.soap.SOAPConnection JavaDoc;
38 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
39 import javax.xml.soap.SOAPEnvelope JavaDoc;
40 import javax.xml.soap.SOAPFault JavaDoc;
41 import javax.xml.soap.SOAPMessage JavaDoc;
42 import javax.xml.soap.SOAPPart JavaDoc;
43
44 import junit.framework.Test;
45
46 import org.jboss.test.webservice.WebserviceTestBase;
47
48 /**
49  * Test user exception propagation.
50  *
51  * @author Thomas.Diesler@jboss.org
52  * @since 23-Sep-2004
53  */

54 public class ExceptionTestCase extends WebserviceTestBase
55 {
56    private static final String JavaDoc TARGET_NAMESPACE = "http://org.jboss.webservice/exception";
57
58    public ExceptionTestCase(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    /** Deploy the test archives */
64    public static Test suite() throws Exception JavaDoc
65    {
66       return getDeploySetup(ExceptionTestCase.class, "ws4ee-exception.jar, ws4ee-exception-client.jar");
67    }
68    
69    /** Test creation of a SOAPFault */
70    public void testSOAPFault() throws Exception JavaDoc
71    {
72       MessageFactory JavaDoc msgfactory = MessageFactory.newInstance();
73       SOAPMessage JavaDoc soapMessage = msgfactory.createMessage();
74       SOAPPart JavaDoc soapPart = soapMessage.getSOAPPart();
75       SOAPEnvelope JavaDoc soapEnv = soapPart.getEnvelope();
76       SOAPBody JavaDoc soapBody = soapEnv.getBody();
77       SOAPFault JavaDoc soapFault = soapBody.addFault();
78       Detail JavaDoc detail = soapFault.addDetail();
79       Name JavaDoc name = soapEnv.createName("GetLastTradePrice", "WOMBAT", "http://www.wombat.org/trader");
80       detail.addDetailEntry(name);
81       
82       QName JavaDoc faultCode = new QName JavaDoc("http://foo.bar", "faultCode");
83       SOAPFaultException JavaDoc sfex = new SOAPFaultException JavaDoc(faultCode, "faultString", "faultActor", detail);
84       assertEquals("faultString", sfex.getFaultString());
85       assertEquals(faultCode, sfex.getFaultCode());
86       assertEquals("faultActor", sfex.getFaultActor());
87    }
88
89    /** Test simple exception propagation */
90    public void testException() throws Exception JavaDoc
91    {
92       InitialContext JavaDoc iniCtx = getClientContext();
93       ExceptionService service = (ExceptionService)iniCtx.lookup("java:comp/env/service/ExceptionService");
94       ExceptionServiceInterface port = service.getPort();
95       try
96       {
97          port.throwException();
98          fail("Should have failed with UserException");
99       }
100       catch (UserException usrex)
101       {
102          // do nothing
103
}
104       catch (Exception JavaDoc e)
105       {
106          fail("Unexpected Exception: " + e);
107       }
108    }
109
110    /** Test exception with message */
111    public void testExceptionWithMessage() throws Exception JavaDoc
112    {
113       InitialContext JavaDoc iniCtx = getClientContext();
114       ExceptionService service = (ExceptionService)iniCtx.lookup("java:comp/env/service/ExceptionService");
115       ExceptionServiceInterface port = service.getPort();
116
117       String JavaDoc message = "Don't worry it's just a test";
118       try
119       {
120          port.throwExceptionWithMessage(message);
121          fail("Should have failed with UserException");
122       }
123       catch (UserMessageException usrex)
124       {
125          assertEquals(message, usrex.getMessage());
126       }
127       catch (Exception JavaDoc e)
128       {
129          fail("Unexpected Exception: " + e);
130       }
131    }
132
133    /** Test a complex user exception */
134    public void testComplexUserException() throws Exception JavaDoc
135    {
136       InitialContext JavaDoc iniCtx = getClientContext();
137       ExceptionService service = (ExceptionService)iniCtx.lookup("java:comp/env/service/ExceptionService");
138       ExceptionServiceInterface port = service.getPort();
139
140       String JavaDoc message = "Don't worry it's just a test";
141       try
142       {
143          port.throwComplexUserException(message, 200);
144          fail("Should have failed with ComplexUserException");
145       }
146       catch (ComplexUserException usrex)
147       {
148          assertEquals(message, usrex.getMessage());
149          assertEquals(200, usrex.getErrorCode());
150       }
151       catch (Exception JavaDoc e)
152       {
153          fail("Unexpected Exception: " + e);
154       }
155    }
156
157    /** Test a complex user exception that contains an array */
158    public void testComplexUserArrayException() throws Exception JavaDoc
159    {
160       InitialContext JavaDoc iniCtx = getClientContext();
161       ExceptionService service = (ExceptionService)iniCtx.lookup("java:comp/env/service/ExceptionService");
162       ExceptionServiceInterface port = service.getPort();
163
164       String JavaDoc message = "Don't worry it's just a test";
165       try
166       {
167          port.throwComplexUserArrayException(message, new int[] { 100, 200 });
168          fail("Should have failed with ComplexUserArrayException");
169       }
170       catch (ComplexUserArrayException usrex)
171       {
172          assertEquals(message, usrex.getMessage());
173          assertEquals(100, usrex.getErrorCodes()[0]);
174          assertEquals(200, usrex.getErrorCodes()[1]);
175       }
176       catch (Exception JavaDoc e)
177       {
178          fail("Unexpected Exception: " + e);
179       }
180    }
181
182    /** Test a fault message for a non existant operation */
183    public void testNonExistantOperation() throws Exception JavaDoc
184    {
185       String JavaDoc reqEnv =
186          "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
187          " <env:Header/>" +
188          " <env:Body>" +
189          " <ns1:nonExistantOperation xmlns:ns1='http://org.jboss.webservice/exception'/>" +
190          " </env:Body>" +
191          "</env:Envelope>";
192
193       MessageFactory JavaDoc factory = MessageFactory.newInstance();
194       SOAPMessage JavaDoc reqMsg = factory.createMessage(null, new ByteArrayInputStream JavaDoc(reqEnv.getBytes()));
195
196       SOAPConnection JavaDoc con = SOAPConnectionFactory.newInstance().createConnection();
197       String JavaDoc targetAddress = "http://" + getServerHost() + ":8080/ws4ee-exception/ExceptionBean";
198       SOAPMessage JavaDoc resMsg = con.call(reqMsg, targetAddress);
199       
200       SOAPFault JavaDoc soapFault = resMsg.getSOAPBody().getFault();
201       assertNotNull("Expected SOAPFault", soapFault);
202       
203       String JavaDoc faultString = soapFault.getFaultString();
204       assertTrue("Unexpected faultString: " + faultString, faultString.indexOf("nonExistantOperation") > 0);
205    }
206
207    /** Test a fault message for a non existant operation */
208    public void testNonExistantOperationDII() throws Exception JavaDoc
209    {
210       ServiceFactory JavaDoc factory = ServiceFactory.newInstance();
211       Service JavaDoc service = factory.createService(new QName JavaDoc(TARGET_NAMESPACE, "ExceptionService"));
212
213       Call JavaDoc call = service.createCall();
214       call.setOperationName(new QName JavaDoc(TARGET_NAMESPACE, "nonExistantOperation"));
215       String JavaDoc targetAddress = "http://" + getServerHost() + ":8080/ws4ee-exception/ExceptionBean";
216       call.setTargetEndpointAddress(targetAddress);
217       
218       try
219       {
220          call.invoke(new Object JavaDoc[] {});
221          fail("Should have failed with RemoteException");
222       }
223       catch (RemoteException JavaDoc ex)
224       {
225          String JavaDoc faultString = ex.getMessage();
226          assertTrue("Unexpected faultString: " + faultString, faultString.indexOf("nonExistantOperation") > 0);
227       }
228    }
229 }
230
Popular Tags