001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.soap.interceptor;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022:
023: import javax.xml.namespace.QName;
024: import javax.xml.stream.XMLStreamReader;
025: import javax.xml.stream.XMLStreamWriter;
026:
027: import org.w3c.dom.Document;
028:
029: import org.apache.cxf.binding.soap.Soap11;
030: import org.apache.cxf.binding.soap.Soap12;
031: import org.apache.cxf.binding.soap.SoapFault;
032: import org.apache.cxf.binding.soap.SoapMessage;
033: import org.apache.cxf.helpers.DOMUtils;
034: import org.apache.cxf.interceptor.Fault;
035: import org.apache.cxf.message.ExchangeImpl;
036: import org.apache.cxf.message.MessageImpl;
037: import org.apache.cxf.staxutils.StaxUtils;
038: import org.apache.cxf.test.AbstractCXFTest;
039: import org.junit.Test;
040:
041: public class SoapFaultSerializerTest extends AbstractCXFTest {
042:
043: @Test
044: public void testSoap11Out() throws Exception {
045: String faultString = "Hadrian caused this Fault!";
046: SoapFault fault = new SoapFault(faultString, Soap11
047: .getInstance().getSender());
048:
049: SoapMessage m = new SoapMessage(new MessageImpl());
050: m.setExchange(new ExchangeImpl());
051: m.setContent(Exception.class, fault);
052:
053: ByteArrayOutputStream out = new ByteArrayOutputStream();
054: XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
055: writer.writeStartDocument();
056: writer.writeStartElement("Body");
057:
058: m.setContent(XMLStreamWriter.class, writer);
059:
060: Soap11FaultOutInterceptor interceptor = new Soap11FaultOutInterceptor();
061: interceptor.handleMessage(m);
062:
063: writer.writeEndElement();
064: writer.writeEndDocument();
065: writer.close();
066:
067: Document faultDoc = DOMUtils.readXml(new ByteArrayInputStream(
068: out.toByteArray()));
069: assertValid("//s:Fault/faultcode[text()='ns1:Client']",
070: faultDoc);
071: assertValid("//s:Fault/faultstring[text()='" + faultString
072: + "']", faultDoc);
073:
074: XMLStreamReader reader = StaxUtils
075: .createXMLStreamReader(new ByteArrayInputStream(out
076: .toByteArray()));
077: m.setContent(XMLStreamReader.class, reader);
078:
079: reader.nextTag();
080:
081: Soap11FaultInInterceptor inInterceptor = new Soap11FaultInInterceptor();
082: inInterceptor.handleMessage(m);
083:
084: SoapFault fault2 = (SoapFault) m.getContent(Exception.class);
085: assertNotNull(fault2);
086: assertEquals(fault.getMessage(), fault2.getMessage());
087: assertEquals(Soap11.getInstance().getSender(), fault2
088: .getFaultCode());
089: }
090:
091: @Test
092: public void testSoap12Out() throws Exception {
093: String faultString = "Hadrian caused this Fault!";
094: SoapFault fault = new SoapFault(faultString, Soap12
095: .getInstance().getSender());
096:
097: QName qname = new QName("http://cxf.apache.org/soap/fault",
098: "invalidsoap", "cxffaultcode");
099: fault.setSubCode(qname);
100:
101: SoapMessage m = new SoapMessage(new MessageImpl());
102: m.setVersion(Soap12.getInstance());
103:
104: m.setContent(Exception.class, fault);
105:
106: ByteArrayOutputStream out = new ByteArrayOutputStream();
107: XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
108: writer.writeStartDocument();
109: writer.writeStartElement("Body");
110:
111: m.setContent(XMLStreamWriter.class, writer);
112:
113: Soap12FaultOutInterceptor interceptor = new Soap12FaultOutInterceptor();
114: interceptor.handleMessage(m);
115:
116: writer.writeEndElement();
117: writer.writeEndDocument();
118: writer.close();
119:
120: Document faultDoc = DOMUtils.readXml(new ByteArrayInputStream(
121: out.toByteArray()));
122:
123: assertValid(
124: "//soap12env:Fault/soap12env:Code/soap12env:Value[text()='ns1:Sender']",
125: faultDoc);
126: assertValid(
127: "//soap12env:Fault/soap12env:Code/soap12env:Subcode[text()='ns2:invalidsoap']",
128: faultDoc);
129: assertValid(
130: "//soap12env:Fault/soap12env:Reason/soap12env:Text[@xml:lang='en']",
131: faultDoc);
132: assertValid(
133: "//soap12env:Fault/soap12env:Reason/soap12env:Text[text()='"
134: + faultString + "']", faultDoc);
135:
136: XMLStreamReader reader = StaxUtils
137: .createXMLStreamReader(new ByteArrayInputStream(out
138: .toByteArray()));
139: m.setContent(XMLStreamReader.class, reader);
140:
141: reader.nextTag();
142:
143: Soap12FaultInInterceptor inInterceptor = new Soap12FaultInInterceptor();
144: inInterceptor.handleMessage(m);
145:
146: SoapFault fault2 = (SoapFault) m.getContent(Exception.class);
147: assertNotNull(fault2);
148: assertEquals(Soap12.getInstance().getSender(), fault2
149: .getFaultCode());
150: assertEquals(fault.getMessage(), fault2.getMessage());
151: }
152:
153: @Test
154: public void testFaultToSoapFault() throws Exception {
155: Exception ex = new Exception();
156: Fault fault = new Fault(ex, Fault.FAULT_CODE_CLIENT);
157:
158: SoapFault sf = SoapFault.createFault(fault, Soap11
159: .getInstance());
160: assertEquals(Soap11.getInstance().getSender(), sf
161: .getFaultCode());
162:
163: sf = SoapFault.createFault(fault, Soap12.getInstance());
164: assertEquals(Soap12.getInstance().getSender(), sf
165: .getFaultCode());
166:
167: fault = new Fault(ex, Fault.FAULT_CODE_SERVER);
168: sf = SoapFault.createFault(fault, Soap11.getInstance());
169: assertEquals(Soap11.getInstance().getReceiver(), sf
170: .getFaultCode());
171:
172: sf = SoapFault.createFault(fault, Soap12.getInstance());
173: assertEquals(Soap12.getInstance().getReceiver(), sf
174: .getFaultCode());
175:
176: }
177: }
|