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.jaxws.handler.context;
020:
021: import java.io.ByteArrayOutputStream;
022:
023: import javax.xml.soap.SOAPMessage;
024: import javax.xml.transform.Source;
025: import javax.xml.transform.Transformer;
026: import javax.xml.transform.TransformerFactory;
027: import javax.xml.transform.stream.StreamResult;
028: import javax.xml.ws.LogicalMessage;
029: import javax.xml.ws.handler.LogicalMessageContext;
030:
031: import junit.framework.TestCase;
032:
033: import org.apache.axis2.jaxws.context.factory.MessageContextFactory;
034: import org.apache.axis2.jaxws.core.MessageContext;
035: import org.apache.axis2.jaxws.handler.MEPContext;
036: import org.apache.axis2.jaxws.handler.SoapMessageContext;
037: import org.apache.axis2.jaxws.message.Message;
038: import org.apache.axis2.jaxws.message.Protocol;
039: import org.apache.axis2.jaxws.message.XMLFault;
040: import org.apache.axis2.jaxws.message.XMLFaultCode;
041: import org.apache.axis2.jaxws.message.XMLFaultReason;
042: import org.apache.axis2.jaxws.message.factory.MessageFactory;
043: import org.apache.axis2.jaxws.registry.FactoryRegistry;
044:
045: /**
046: * A test suite for scenarios where the internal MessageContext is converted
047: * between handler context formats (SOAPMessageContext and LogicalMessageContext).
048: *
049: * The flows that need to be tested here are:
050: *
051: * 1) INBOUND: The MessageContext will be converted from a LogicalMessageContext
052: * to a SOAPMessageContext. This drives converting the OM message
053: * to an SAAJ SOAPMessage among other things.
054: *
055: * specific tests:
056: * - Normal message
057: * - Fault message
058: *
059: * 2) OUTBOUND: The MessageContext will be converted from a SOAPMessageContext
060: * to a LogicalMessageContext. This will drive conversion from
061: * an SAAJ SOAPMessage back to an OM (there are some very tricky
062: * pieces of code contained within that scenario).
063: *
064: * specific test:
065: * - Normal message
066: * - Fault message
067: */
068: public class CompositeMessageContextTests extends TestCase {
069:
070: private final String FAULT_INPUT = "sample fault input";
071:
072: /**
073: * A test that mimics the inbound flow through a handler chain.
074: */
075: public void testInboundFaultFlow() throws Exception {
076: MessageContext mc = createSampleFaultMessageContext();
077:
078: LogicalMessageContext lmc = MessageContextFactory
079: .createLogicalMessageContext(mc);
080: LogicalMessage lm = lmc.getMessage();
081: Source payload = lm.getPayload();
082: assertTrue("The returned payload (Source) was null",
083: payload != null);
084:
085: ByteArrayOutputStream baos = new ByteArrayOutputStream();
086: StreamResult result = new StreamResult(baos);
087: Transformer t = TransformerFactory.newInstance()
088: .newTransformer();
089: t.transform(payload, result);
090:
091: String content = new String(baos.toByteArray());
092: assertTrue(
093: "The returned content (String) from the payload was null",
094: content != null);
095: assertTrue("The <faultcode> element was not found", content
096: .indexOf("faultcode") > -1);
097: assertTrue("The <faultstring> element was not found", content
098: .indexOf("faultstring") > -1);
099: assertTrue(
100: "The fault did not contain the expected fault string",
101: content.indexOf(FAULT_INPUT) > -1);
102:
103: SoapMessageContext smc = MessageContextFactory
104: .createSoapMessageContext(mc);
105: SOAPMessage sm = smc.getMessage();
106: assertTrue("The returned SOAPMessage was null", sm != null);
107: assertTrue("The SOAPMessage did not contain a SOAPBody", sm
108: .getSOAPBody() != null);
109: assertTrue("The SOAPBody did not contain a SOAPFault", sm
110: .getSOAPBody().getFault() != null);
111: }
112:
113: /**
114: * A test that mimics the outbound flow through a handler chain.
115: */
116: public void testOutboundFaultFlow() throws Exception {
117: MessageContext mc = createSampleFaultMessageContext();
118:
119: SoapMessageContext smc = MessageContextFactory
120: .createSoapMessageContext(mc);
121: SOAPMessage sm = smc.getMessage();
122: assertTrue("The returned SOAPMessage was null", sm != null);
123: assertTrue("The SOAPMessage did not contain a SOAPBody", sm
124: .getSOAPBody() != null);
125: assertTrue("The SOAPBody did not contain a SOAPFault", sm
126: .getSOAPBody().getFault() != null);
127:
128: LogicalMessageContext lmc = MessageContextFactory
129: .createLogicalMessageContext(mc);
130: LogicalMessage lm = lmc.getMessage();
131: Source payload = lm.getPayload();
132: assertTrue("The returned payload (Source) was null",
133: payload != null);
134:
135: ByteArrayOutputStream baos = new ByteArrayOutputStream();
136: StreamResult result = new StreamResult(baos);
137: Transformer t = TransformerFactory.newInstance()
138: .newTransformer();
139: t.transform(payload, result);
140:
141: String content = new String(baos.toByteArray());
142: assertTrue(
143: "The returned content (String) from the payload was null",
144: content != null);
145: assertTrue("The <faultcode> element was not found", content
146: .indexOf("faultcode") > -1);
147: assertTrue("The <faultstring> element was not found", content
148: .indexOf("faultstring") > -1);
149: assertTrue(
150: "The fault did not contain the expected fault string",
151: content.indexOf(FAULT_INPUT) > -1);
152: }
153:
154: private MessageContext createSampleFaultMessageContext()
155: throws Exception {
156: MessageFactory factory = (MessageFactory) FactoryRegistry
157: .getFactory(MessageFactory.class);
158: Message msg = factory.create(Protocol.soap11);
159:
160: XMLFaultReason reason = new XMLFaultReason(FAULT_INPUT);
161: XMLFault fault = new XMLFault(XMLFaultCode.SENDER, reason);
162: msg.setXMLFault(fault);
163:
164: MessageContext mc = new MessageContext();
165: mc.setMEPContext(new MEPContext(mc));
166: mc.setMessage(msg);
167:
168: return mc;
169: }
170: }
|