1 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 ; 30 import org.w3c.dom.Text ; 31 32 import javax.xml.namespace.QName ; 33 34 40 41 public class FaultDecode extends TestCase { 42 public static final String FAULT_CODE = "Some.FaultCode"; 43 public static final String FAULT_STRING = "This caused a fault"; 44 public static final String DETAIL_ENTRY_TEXT = 45 "This was a really bad thing"; 46 47 public FaultDecode(String name) { 48 super(name); 49 } 51 public static Test suite() { 52 return new TestSuite(FaultDecode.class); 53 } 54 55 public void testFault() throws Exception { 56 String 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 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 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 text = (Text )el.getFirstChild(); 104 assertEquals("detail entry string should match", 105 text.getData(), DETAIL_ENTRY_TEXT); 106 107 } 109 public static void main(String [] args) throws Exception { 110 FaultDecode tester = new FaultDecode("test"); 111 TestRunner runner = new TestRunner(); 112 runner.doRun(tester.suite(), false); 113 } 114 } 115 | Popular Tags |