1 17 package org.apache.servicemix.soap.marshalers; 18 19 import java.io.ByteArrayOutputStream ; 20 21 import javax.xml.namespace.QName ; 22 23 import junit.framework.TestCase; 24 25 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 26 import org.apache.servicemix.jbi.jaxp.StringSource; 27 import org.apache.servicemix.jbi.util.DOMUtil; 28 import org.apache.servicemix.soap.SoapFault; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 33 public class FaultTest extends TestCase { 34 35 private SourceTransformer sourceTransformer = new SourceTransformer(); 36 37 protected void testSoap11(boolean useDom) throws Exception { 38 SoapMarshaler marshaler = new SoapMarshaler(true, useDom); 39 try { 40 marshaler.createReader().read(getClass().getResourceAsStream("fault-1.1-ok.xml")); 41 } catch (SoapFault fault) { 42 assertEquals(SoapMarshaler.SOAP_11_CODE_SERVER, fault.getCode()); 43 assertNull(fault.getSubcode()); 44 assertEquals("Server Error", fault.getReason()); 45 assertNotNull(fault.getDetails()); 46 Node node = sourceTransformer.toDOMNode(fault.getDetails()); 47 Element e = node instanceof Document ? ((Document ) node).getDocumentElement() : (Element ) node; 48 assertEquals(new QName ("Some-URI", "myfaultdetails"), DOMUtil.getQName(e)); 49 } 50 } 51 52 public void testReadSoap11UsingDom() throws Exception { 53 testSoap11(true); 54 } 55 56 public void testReadSoap11UsingStax() throws Exception { 57 testSoap11(false); 58 } 59 60 protected void testSoap12(boolean useDom) throws Exception { 61 SoapMarshaler marshaler = new SoapMarshaler(true, useDom); 62 try { 63 marshaler.createReader().read(getClass().getResourceAsStream("fault-1.2-ok.xml")); 64 } catch (SoapFault fault) { 65 assertEquals(SoapMarshaler.SOAP_12_CODE_SENDER, fault.getCode()); 66 assertEquals(new QName ("http://www.example.org/timeouts", "MessageTimeout"), fault.getSubcode()); 67 assertEquals("Sender Timeout", fault.getReason()); 68 assertNotNull(fault.getDetails()); 69 Node node = sourceTransformer.toDOMNode(fault.getDetails()); 70 Element e = node instanceof Document ? ((Document ) node).getDocumentElement() : (Element ) node; 71 assertEquals(new QName ("http://www.example.org/timeouts", "MaxTime"), DOMUtil.getQName(e)); 72 } 73 } 74 75 public void testReadSoap12UsingDom() throws Exception { 76 testSoap12(true); 77 } 78 79 public void testReadSoap12UsingStax() throws Exception { 80 testSoap12(false); 81 } 82 83 public void testWriteSoap11UsingDom() throws Exception { 84 testWriteSoap11(true); 85 } 86 87 public void testWriteSoap11UsingStax() throws Exception { 88 testWriteSoap11(false); 89 } 90 91 protected void testWriteSoap11(boolean useDom) throws Exception { 92 SoapMarshaler marshaler = new SoapMarshaler(true, useDom); 93 marshaler.setSoapUri(SoapMarshaler.SOAP_11_URI); 94 SoapMessage msg = new SoapMessage(); 95 SoapFault fault = new SoapFault(new QName ("my:urn", "code"), null, "My reason", null, null, new StringSource("<ns1:hello xmlns:ns1='my:urn'>world</ns1:hello>")); 96 msg.setFault(fault); 97 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 98 marshaler.createWriter(msg).write(baos); 99 System.err.println(baos.toString()); 100 101 Node node = sourceTransformer.toDOMNode(new StringSource(baos.toString())); 102 Element e = ((Document ) node).getDocumentElement(); 103 assertEquals(new QName (SoapMarshaler.SOAP_11_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e)); 104 e = DOMUtil.getFirstChildElement(e); 105 assertEquals(new QName (SoapMarshaler.SOAP_11_URI, SoapMarshaler.BODY), DOMUtil.getQName(e)); 106 Element elFault = DOMUtil.getFirstChildElement(e); 107 assertEquals(new QName (SoapMarshaler.SOAP_11_URI, SoapMarshaler.FAULT), DOMUtil.getQName(elFault)); 108 Element elCode = DOMUtil.getFirstChildElement(elFault); 109 assertEquals(SoapMarshaler.SOAP_11_FAULTCODE, DOMUtil.getQName(elCode)); 110 assertEquals(new QName ("my:urn", "code"), DOMUtil.createQName(elCode, DOMUtil.getElementText(elCode))); 111 Element elReason = DOMUtil.getNextSiblingElement(elCode); 112 assertEquals(SoapMarshaler.SOAP_11_FAULTSTRING, DOMUtil.getQName(elReason)); 113 Element elDetail = DOMUtil.getNextSiblingElement(elReason); 114 assertEquals(SoapMarshaler.SOAP_11_FAULTDETAIL, DOMUtil.getQName(elDetail)); 115 e = DOMUtil.getFirstChildElement(elDetail); 116 assertEquals(new QName ("my:urn", "hello"), DOMUtil.getQName(e)); 117 } 118 119 public void testWriteSoap12UsingDom() throws Exception { 120 testWriteSoap12(true); 121 } 122 123 public void testWriteSoap12UsingStax() throws Exception { 124 testWriteSoap12(false); 125 } 126 127 protected void testWriteSoap12(boolean useDom) throws Exception { 128 SoapMarshaler marshaler = new SoapMarshaler(true, useDom); 129 marshaler.setSoapUri(SoapMarshaler.SOAP_12_URI); 130 SoapMessage msg = new SoapMessage(); 131 SoapFault fault = new SoapFault(new QName ("my:urn", "code"), null, "My reason", null, null, 132 new StringSource("<ns1:hello xmlns:ns1='my:urn'>world</ns1:hello>")); 133 msg.setFault(fault); 134 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 135 marshaler.createWriter(msg).write(baos); 136 System.err.println(baos.toString()); 137 138 Node node = sourceTransformer.toDOMNode(new StringSource(baos.toString())); 139 Element e = ((Document ) node).getDocumentElement(); 140 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e)); 141 e = DOMUtil.getFirstChildElement(e); 142 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.BODY), DOMUtil.getQName(e)); 143 Element elFault = DOMUtil.getFirstChildElement(e); 144 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.FAULT), DOMUtil.getQName(elFault)); 145 Element elCode = DOMUtil.getFirstChildElement(elFault); 146 assertEquals(SoapMarshaler.SOAP_12_FAULTCODE, DOMUtil.getQName(elCode)); 147 e = DOMUtil.getFirstChildElement(elCode); 148 assertEquals(SoapMarshaler.SOAP_12_FAULTVALUE, DOMUtil.getQName(e)); 149 e = DOMUtil.getNextSiblingElement(e); 150 assertEquals(SoapMarshaler.SOAP_12_FAULTSUBCODE, DOMUtil.getQName(e)); 151 e = DOMUtil.getFirstChildElement(e); 152 assertEquals(SoapMarshaler.SOAP_12_FAULTVALUE, DOMUtil.getQName(e)); 153 assertEquals(new QName ("my:urn", "code"), DOMUtil.createQName(e, DOMUtil.getElementText(e))); 154 Element elReason = DOMUtil.getNextSiblingElement(elCode); 155 assertEquals(SoapMarshaler.SOAP_12_FAULTREASON, DOMUtil.getQName(elReason)); 156 Element elDetail = DOMUtil.getNextSiblingElement(elReason); 157 assertEquals(SoapMarshaler.SOAP_12_FAULTDETAIL, DOMUtil.getQName(elDetail)); 158 e = DOMUtil.getFirstChildElement(elDetail); 159 assertEquals(new QName ("my:urn", "hello"), DOMUtil.getQName(e)); 160 } 161 162 } 163 | Popular Tags |