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: */
019: package org.apache.axis2.faults;
020:
021: import junit.framework.TestCase;
022: import org.apache.axiom.soap.SOAPFactory;
023: import org.apache.axiom.soap.SOAPFaultCode;
024: import org.apache.axiom.soap.SOAPFaultValue;
025: import org.apache.axiom.soap.SOAP12Constants;
026: import org.apache.axiom.soap.SOAPFaultReason;
027: import org.apache.axiom.soap.SOAPFaultText;
028: import org.apache.axiom.soap.SOAPFaultDetail;
029: import org.apache.axiom.om.OMAbstractFactory;
030: import org.apache.axiom.om.OMElement;
031: import org.apache.axis2.AxisFault;
032: import org.apache.axis2.transport.TransportUtils;
033: import org.apache.axis2.context.MessageContext;
034: import org.apache.axis2.context.ConfigurationContextFactory;
035: import org.apache.axis2.context.ConfigurationContext;
036: import org.apache.axis2.util.MessageContextBuilder;
037: import org.apache.axis2.util.Utils;
038:
039: import javax.xml.namespace.QName;
040: import java.io.ByteArrayOutputStream;
041:
042: public class FaultSerializationTest extends TestCase {
043: public void testFaultSerialization() throws Exception {
044: final String REASON = "ReasonValue";
045:
046: SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
047: SOAPFaultCode soapFaultCode = soapFactory.createSOAPFaultCode();
048: SOAPFaultValue soapFaultValue = soapFactory
049: .createSOAPFaultValue(soapFaultCode);
050: soapFaultValue.setText(new QName(
051: SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI, "Sender"));
052:
053: SOAPFaultReason soapFaultReason = soapFactory
054: .createSOAPFaultReason();
055: SOAPFaultText soapFaultText = soapFactory
056: .createSOAPFaultText(soapFaultReason);
057: soapFaultText.setText(REASON);
058:
059: SOAPFaultDetail soapFaultDetail = soapFactory
060: .createSOAPFaultDetail();
061: QName qName = new QName("http://mycompany.com",
062: "FaultException", "ex");
063: OMElement exception = soapFactory.createOMElement(qName,
064: soapFaultDetail);
065: exception.setText("Detail text");
066: AxisFault fault = new AxisFault(soapFaultCode, soapFaultReason,
067: null, null, soapFaultDetail);
068:
069: ConfigurationContext cc = ConfigurationContextFactory
070: .createDefaultConfigurationContext();
071: MessageContext ctx = cc.createMessageContext();
072: SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
073: ctx.setEnvelope(fac.getDefaultEnvelope());
074: MessageContext faultCtx = MessageContextBuilder
075: .createFaultMessageContext(ctx, fault);
076:
077: ByteArrayOutputStream bos = new ByteArrayOutputStream();
078: TransportUtils.writeMessage(faultCtx, bos);
079:
080: String result = new String(bos.toByteArray());
081:
082: // For right now, just making sure we have a test for AXIS2-2752
083: // Confirm reason was correctly processed
084: assertTrue("Incorrect or missing reason!", result
085: .indexOf(REASON) > -1);
086: }
087:
088: // test for https://issues.apache.org/jira/browse/AXIS2-1703
089: public void testFaultReason() throws Exception {
090: SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
091: OMElement response = soapFactory.createOMElement(new QName(
092: "testNs", "test"));
093: String faultReason = "myFaultReason";
094: AxisFault fault = new AxisFault(new QName("myQname"),
095: faultReason, "myFaultNode", "myFaultRole", response);
096:
097: ConfigurationContext cc = ConfigurationContextFactory
098: .createDefaultConfigurationContext();
099: MessageContext ctx = cc.createMessageContext();
100: SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
101: ctx.setEnvelope(fac.getDefaultEnvelope());
102: MessageContext faultCtx = MessageContextBuilder
103: .createFaultMessageContext(ctx, fault);
104:
105: assertEquals(faultReason, Utils
106: .getInboundFaultFromMessageContext(faultCtx)
107: .getReason());
108: }
109: }
|