1 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 ; 44 import org.w3c.dom.NodeList ; 45 import org.w3c.dom.Text ; 46 47 import javax.xml.namespace.QName ; 48 import javax.xml.rpc.soap.SOAPFaultException ; 49 import javax.xml.soap.Detail ; 50 import javax.xml.soap.SOAPException ; 51 import javax.xml.soap.SOAPFactory ; 52 53 58 public class TestSOAPFaultException extends TestCase { 59 60 private static final SOAPConstants soapConsts = new SOAP11Constants(); 61 private static final QName QNAME_FAULT_SERVER_USER = new QName (soapConsts.getEnvelopeURI(), Constants.FAULT_SERVER_USER); 62 private static final String SERVICE_NAME = "FailingService"; 63 64 68 public TestSOAPFaultException() { 69 super("TestSOAPFaultException"); 70 } 71 72 public TestSOAPFaultException(String s) { 73 super(s); 74 } 75 76 public static Test suite() { 77 return new TestSuite(TestSOAPFaultException.class); 78 } 79 80 83 public void testDefaults() { 84 SOAPFaultException soapFaultException = new SOAPFaultException (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 95 public void testMakeFaultOutOfSOAPFaultException() { 96 97 QName faultcode = new QName (soapConsts.getEnvelopeURI(), "Server.MySubClass"); 98 99 SOAPFaultException soapFaultException = new SOAPFaultException (faultcode, "MyFaultString", "http://myactor", null); 100 101 AxisFault axisFault = AxisFault.makeFault(soapFaultException); 102 103 assertEquals(faultcode, axisFault.getFaultCode()); 104 } 105 106 109 public void testDetails() { 110 SOAPFaultException soapFaultException = new SOAPFaultException (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 125 public void testThrowingSOAPFaultExceptionFromServiceMethod() throws Exception { 126 127 WSDDDeployment conf = new WSDDDeployment(); 128 129 WSDDService service = new WSDDService(); 130 service.setName(SERVICE_NAME); 131 service.setProviderQName(new QName (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 MessageContext mc = new MessageContext(engine); 143 mc.setService((SOAPService) service.getInstance(conf)); 144 mc.setProperty(MessageContext.TRANS_URL, "local"); 145 146 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 try { 155 engine.invoke(mc); 156 } catch (AxisFault af) { 157 checkDetailAgainstTestDetail(af); 158 } 159 } 160 161 167 public void doSth() throws Exception { 168 throw new SOAPFaultException (QNAME_FAULT_SERVER_USER, "MyFaultString", "http://myactor", getTestDetail()); 169 } 170 171 175 private Detail getTestDetail() { 176 Detail detail = null; 177 try { 178 detail = SOAPFactory.newInstance().createDetail(); 179 detail.addChildElement("MyDetails").addTextNode("hossa"); 180 detail.addChildElement("foo").addChildElement("baz"); 181 } catch (SOAPException e) { 182 fail("Can't create detail"); 183 } 184 return detail; 185 } 186 187 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 ) details[0].getChildNodes().item(0)).getData()); 197 assertEquals("wrong name for foo element", "foo", details[1].getNodeName()); 198 199 boolean found = false; 200 NodeList 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 |