1 22 package org.jboss.test.webservice.exception; 23 24 import java.io.ByteArrayInputStream ; 25 import java.rmi.RemoteException ; 26 27 import javax.naming.InitialContext ; 28 import javax.xml.namespace.QName ; 29 import javax.xml.rpc.Call ; 30 import javax.xml.rpc.Service ; 31 import javax.xml.rpc.ServiceFactory ; 32 import javax.xml.rpc.soap.SOAPFaultException ; 33 import javax.xml.soap.Detail ; 34 import javax.xml.soap.MessageFactory ; 35 import javax.xml.soap.Name ; 36 import javax.xml.soap.SOAPBody ; 37 import javax.xml.soap.SOAPConnection ; 38 import javax.xml.soap.SOAPConnectionFactory ; 39 import javax.xml.soap.SOAPEnvelope ; 40 import javax.xml.soap.SOAPFault ; 41 import javax.xml.soap.SOAPMessage ; 42 import javax.xml.soap.SOAPPart ; 43 44 import junit.framework.Test; 45 46 import org.jboss.test.webservice.WebserviceTestBase; 47 48 54 public class ExceptionTestCase extends WebserviceTestBase 55 { 56 private static final String TARGET_NAMESPACE = "http://org.jboss.webservice/exception"; 57 58 public ExceptionTestCase(String name) 59 { 60 super(name); 61 } 62 63 64 public static Test suite() throws Exception 65 { 66 return getDeploySetup(ExceptionTestCase.class, "ws4ee-exception.jar, ws4ee-exception-client.jar"); 67 } 68 69 70 public void testSOAPFault() throws Exception 71 { 72 MessageFactory msgfactory = MessageFactory.newInstance(); 73 SOAPMessage soapMessage = msgfactory.createMessage(); 74 SOAPPart soapPart = soapMessage.getSOAPPart(); 75 SOAPEnvelope soapEnv = soapPart.getEnvelope(); 76 SOAPBody soapBody = soapEnv.getBody(); 77 SOAPFault soapFault = soapBody.addFault(); 78 Detail detail = soapFault.addDetail(); 79 Name name = soapEnv.createName("GetLastTradePrice", "WOMBAT", "http://www.wombat.org/trader"); 80 detail.addDetailEntry(name); 81 82 QName faultCode = new QName ("http://foo.bar", "faultCode"); 83 SOAPFaultException sfex = new SOAPFaultException (faultCode, "faultString", "faultActor", detail); 84 assertEquals("faultString", sfex.getFaultString()); 85 assertEquals(faultCode, sfex.getFaultCode()); 86 assertEquals("faultActor", sfex.getFaultActor()); 87 } 88 89 90 public void testException() throws Exception 91 { 92 InitialContext 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 } 104 catch (Exception e) 105 { 106 fail("Unexpected Exception: " + e); 107 } 108 } 109 110 111 public void testExceptionWithMessage() throws Exception 112 { 113 InitialContext iniCtx = getClientContext(); 114 ExceptionService service = (ExceptionService)iniCtx.lookup("java:comp/env/service/ExceptionService"); 115 ExceptionServiceInterface port = service.getPort(); 116 117 String 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 e) 128 { 129 fail("Unexpected Exception: " + e); 130 } 131 } 132 133 134 public void testComplexUserException() throws Exception 135 { 136 InitialContext iniCtx = getClientContext(); 137 ExceptionService service = (ExceptionService)iniCtx.lookup("java:comp/env/service/ExceptionService"); 138 ExceptionServiceInterface port = service.getPort(); 139 140 String 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 e) 152 { 153 fail("Unexpected Exception: " + e); 154 } 155 } 156 157 158 public void testComplexUserArrayException() throws Exception 159 { 160 InitialContext iniCtx = getClientContext(); 161 ExceptionService service = (ExceptionService)iniCtx.lookup("java:comp/env/service/ExceptionService"); 162 ExceptionServiceInterface port = service.getPort(); 163 164 String 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 e) 177 { 178 fail("Unexpected Exception: " + e); 179 } 180 } 181 182 183 public void testNonExistantOperation() throws Exception 184 { 185 String 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 factory = MessageFactory.newInstance(); 194 SOAPMessage reqMsg = factory.createMessage(null, new ByteArrayInputStream (reqEnv.getBytes())); 195 196 SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection(); 197 String targetAddress = "http://" + getServerHost() + ":8080/ws4ee-exception/ExceptionBean"; 198 SOAPMessage resMsg = con.call(reqMsg, targetAddress); 199 200 SOAPFault soapFault = resMsg.getSOAPBody().getFault(); 201 assertNotNull("Expected SOAPFault", soapFault); 202 203 String faultString = soapFault.getFaultString(); 204 assertTrue("Unexpected faultString: " + faultString, faultString.indexOf("nonExistantOperation") > 0); 205 } 206 207 208 public void testNonExistantOperationDII() throws Exception 209 { 210 ServiceFactory factory = ServiceFactory.newInstance(); 211 Service service = factory.createService(new QName (TARGET_NAMESPACE, "ExceptionService")); 212 213 Call call = service.createCall(); 214 call.setOperationName(new QName (TARGET_NAMESPACE, "nonExistantOperation")); 215 String targetAddress = "http://" + getServerHost() + ":8080/ws4ee-exception/ExceptionBean"; 216 call.setTargetEndpointAddress(targetAddress); 217 218 try 219 { 220 call.invoke(new Object [] {}); 221 fail("Should have failed with RemoteException"); 222 } 223 catch (RemoteException ex) 224 { 225 String faultString = ex.getMessage(); 226 assertTrue("Unexpected faultString: " + faultString, faultString.indexOf("nonExistantOperation") > 0); 227 } 228 } 229 } 230 | Popular Tags |