1 package test.soap12; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.axis.AxisFault; 7 import org.apache.axis.Message; 8 import org.apache.axis.MessageContext; 9 import org.apache.axis.Constants; 10 import org.apache.axis.encoding.SerializationContext; 11 import org.apache.axis.encoding.SerializationContext; 12 import org.apache.axis.message.SOAPBodyElement; 13 import org.apache.axis.message.SOAPEnvelope; 14 import org.apache.axis.message.SOAPFault; 15 import org.apache.axis.server.AxisServer; 16 import org.apache.axis.soap.SOAPConstants; 17 18 import javax.xml.namespace.QName ; 19 import java.io.StringWriter ; 20 import java.io.Writer ; 21 22 27 public class TestFault extends TestCase { 28 public static Test suite() { 29 return new TestSuite(TestFault.class); 30 } 31 32 public TestFault(String name) { 33 super(name); 34 } 35 public void setUp() throws Exception { 36 } 37 38 public static final QName FAULTCODE = new QName ("http://c","faultcode_c"); 39 public static final QName FAULTSUBCODE[] = { new QName ("http://a","subcode_a"), 40 new QName ("http://b","subcode_b") }; 41 public static final String FAULTREASON = "reason"; 42 public static final String FAULTROLE = "role"; 43 public static final String FAULTNODE = "node"; 44 45 public void testFault() throws Exception 46 { 47 MessageContext msgContext = new MessageContext(new AxisServer()); 49 msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS); 50 msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE); 51 52 SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS); 53 54 SOAPFault fault = new SOAPFault(new AxisFault(FAULTCODE, FAULTSUBCODE, 55 FAULTREASON, FAULTROLE, 56 FAULTNODE, null)); 57 58 msg.addBodyElement(fault); 59 Writer stringWriter = new StringWriter (); 60 SerializationContext context = 61 new SerializationContext(stringWriter, msgContext); 62 context.setDoMultiRefs(false); 63 msg.output(context); 64 String msgString = stringWriter.toString(); 65 66 AxisServer server = new AxisServer(); 68 Message message = new Message(msgString); 69 message.setMessageContext(new MessageContext(server)); 70 71 SOAPEnvelope envelope = message.getSOAPEnvelope(); 72 assertNotNull("envelope should not be null", envelope); 73 74 SOAPBodyElement respBody = envelope.getFirstBody(); 75 assertTrue("respBody should be a SOAPFaultElement", respBody 76 instanceof SOAPFault); 77 AxisFault aFault = ((SOAPFault) respBody).getFault(); 78 79 assertNotNull("Fault should not be null", aFault); 80 81 assertEquals(FAULTCODE, aFault.getFaultCode()); 82 assertEquals(FAULTREASON, aFault.getFaultReason()); 83 assertEquals(FAULTROLE, aFault.getFaultRole()); 84 assertEquals(FAULTNODE, aFault.getFaultNode()); 85 QName q[] = aFault.getFaultSubCodes(); 86 for (int i = 0; i < q.length; i++) 87 assertEquals(FAULTSUBCODE[i], q[i]); 88 } 89 90 } 91 | Popular Tags |