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.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023:
024: import javax.xml.bind.JAXBContext;
025: import javax.xml.transform.OutputKeys;
026: import javax.xml.transform.Source;
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.stream.StreamResult;
030: import javax.xml.transform.stream.StreamSource;
031: import javax.xml.ws.LogicalMessage;
032: import javax.xml.ws.handler.LogicalMessageContext;
033:
034: import junit.framework.TestCase;
035:
036: import org.apache.axis2.jaxws.context.factory.MessageContextFactory;
037: import org.apache.axis2.jaxws.core.MessageContext;
038: import org.apache.axis2.jaxws.handler.MEPContext;
039: import org.apache.axis2.jaxws.message.Block;
040: import org.apache.axis2.jaxws.message.Message;
041: import org.apache.axis2.jaxws.message.Protocol;
042: import org.apache.axis2.jaxws.message.XMLFault;
043: import org.apache.axis2.jaxws.message.XMLFaultCode;
044: import org.apache.axis2.jaxws.message.XMLFaultReason;
045: import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
046: import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
047: import org.apache.axis2.jaxws.message.factory.MessageFactory;
048: import org.apache.axis2.jaxws.registry.FactoryRegistry;
049:
050: import test.EchoString;
051: import test.ObjectFactory;
052:
053: /**
054: * Unit tests for the creation and usage of the LogicalMessageContext that is
055: * used for handler processing.
056: */
057: public class LogicalMessageContextTests extends TestCase {
058:
059: private final String INPUT = "sample input";
060: private final String FAULT_INPUT = "sample fault input";
061:
062: private final String sampleSOAP11FaultPayload = "<soapenv:Fault xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
063: + "<faultcode>soapenv:Server</faultcode>"
064: + "<faultstring>"
065: + FAULT_INPUT + "</faultstring>" + "</soapenv:Fault>";
066:
067: public LogicalMessageContextTests(String name) {
068: super (name);
069: }
070:
071: /**
072: * Test the javax.xml.transform.Source based APIs on the LogicalMessage interface.
073: * @throws Exception
074: */
075: public void testGetPayloadAsSource() throws Exception {
076: LogicalMessageContext lmc = createSampleContext();
077:
078: LogicalMessage msg = lmc.getMessage();
079: assertTrue("The returned LogicalMessage was null", msg != null);
080:
081: Source payload = msg.getPayload();
082: assertTrue("The returned payload (Source) was null",
083: payload != null);
084:
085: String resultContent = _getStringFromSource(payload);
086: assertTrue("The content returned was null",
087: resultContent != null);
088: assertTrue(
089: "The content returned was incomplete, unexpected element",
090: resultContent.indexOf("echoString") > -1);
091: assertTrue(
092: "The content returned was incomplete, unexpected content",
093: resultContent.indexOf(INPUT) > -1);
094: }
095:
096: /**
097: * Tests the setting of the payload and make sure we don't cache improperly.
098: * @throws Exception
099: */
100: public void testGetAndSetPayloadAsSource() throws Exception {
101: LogicalMessageContext lmc = createSampleContext();
102:
103: LogicalMessage msg = lmc.getMessage();
104: assertTrue("The returned LogicalMessage was null", msg != null);
105:
106: Source payload = msg.getPayload();
107: assertTrue("The returned payload (Source) was null",
108: payload != null);
109:
110: String resultContent = _getStringFromSource(payload);
111: assertTrue("The content returned was null",
112: resultContent != null);
113: assertTrue("The content returned was incorrect", resultContent
114: .indexOf(INPUT) > 0);
115:
116: // Now manipluate the content and set it back on the message.
117: int start = resultContent.indexOf(INPUT);
118: int end = start + INPUT.length();
119:
120: String newInput = "new content goes here";
121: String newContent = resultContent.substring(0, start)
122: + newInput + resultContent.substring(end);
123:
124: ByteArrayInputStream bais = new ByteArrayInputStream(newContent
125: .getBytes());
126: StreamSource newPayload = new StreamSource(bais);
127:
128: msg.setPayload(newPayload);
129:
130: // Check the payload to make sure the new content that we added
131: // was insterted correctly.
132: Source payload2 = msg.getPayload();
133: assertTrue("The returned payload (Source) was null",
134: payload2 != null);
135:
136: String resultContent2 = _getStringFromSource(payload2);
137: assertTrue("The updated content returned was null",
138: resultContent2 != null);
139: assertTrue(
140: "The updated content returned was incorrect, old content found",
141: resultContent2.indexOf(INPUT) < 0);
142: assertTrue(
143: "The updated content returned was incorrect, new content not found",
144: resultContent2.indexOf(newInput) > -1);
145: }
146:
147: /**
148: * Test to make sure we can get the payload multiple times from the same LogicalMessage.
149: * @throws Exception
150: */
151: public void testGetMultiplePayloadsAsSource() throws Exception {
152: LogicalMessageContext lmc = createSampleContext();
153:
154: LogicalMessage msg = lmc.getMessage();
155: assertTrue("The returned LogicalMessage was null", msg != null);
156:
157: int loopCount = 3;
158: for (int i = 0; i < loopCount; ++i) {
159: Source payload = msg.getPayload();
160: assertTrue("Attempt number " + i
161: + " to get the payload (Source) was null",
162: payload != null);
163:
164: String resultContent = _getStringFromSource(payload);
165: assertTrue("The content returned in loop " + i
166: + " was null", resultContent != null);
167: assertTrue("The content returned in loop " + i
168: + " was incomplete, unexpected element",
169: resultContent.indexOf("echoString") > -1);
170: assertTrue("The content returned in loop " + i
171: + " was incomplete, unexpected content",
172: resultContent.indexOf(INPUT) > -1);
173: }
174: }
175:
176: /**
177: * Tests the setting of the payload when the original content is a fault.
178: * @throws Exception
179: */
180: public void testGetAndSetFaultPayloadAsSource() throws Exception {
181: LogicalMessageContext lmc = createSampleFaultContext();
182:
183: LogicalMessage msg = lmc.getMessage();
184: assertTrue("The returned LogicalMessage was null", msg != null);
185:
186: Source payload = msg.getPayload();
187: assertTrue("The returned payload (Source) was null",
188: payload != null);
189:
190: String resultContent = _getStringFromSource(payload);
191: assertTrue("The content returned was null",
192: resultContent != null);
193: assertTrue("The content returned was incorrect", resultContent
194: .indexOf(FAULT_INPUT) > 0);
195: assertTrue(
196: "The content returned was incorrect, no fault found",
197: resultContent.indexOf("Fault") > 0);
198:
199: // Now manipluate the content and set it back on the message.
200: int start = resultContent.indexOf(FAULT_INPUT);
201: int end = start + FAULT_INPUT.length();
202:
203: String newFaultInput = "new fault content goes here";
204: String newContent = resultContent.substring(0, start)
205: + newFaultInput + resultContent.substring(end);
206:
207: ByteArrayInputStream bais = new ByteArrayInputStream(newContent
208: .getBytes());
209: StreamSource newPayload = new StreamSource(bais);
210:
211: msg.setPayload(newPayload);
212:
213: // Check the payload to make sure the new content that we added
214: // was insterted correctly.
215: Source payload2 = msg.getPayload();
216: assertTrue("The returned payload (Source) was null",
217: payload2 != null);
218:
219: String resultContent2 = _getStringFromSource(payload2);
220: assertTrue("The updated content returned was null",
221: resultContent2 != null);
222: assertTrue(
223: "The updated content returned was incorrect, old content found",
224: resultContent2.indexOf(FAULT_INPUT) < 0);
225: assertTrue(
226: "The updated content returned was incorrect, no fault found",
227: resultContent.indexOf("Fault") > 0);
228: assertTrue(
229: "The updated content returned was incorrect, new content not found",
230: resultContent2.indexOf(newFaultInput) > -1);
231: }
232:
233: /**
234: * Test the JAXB based APIs on the LogicalMessage interface.
235: * @throws Exception
236: */
237: public void testGetPayloadAsJAXB() throws Exception {
238: LogicalMessageContext lmc = createSampleContext();
239:
240: LogicalMessage msg = lmc.getMessage();
241: assertTrue("The returned LogicalMessage was null", msg != null);
242:
243: JAXBContext jbc = JAXBContext.newInstance("test");
244:
245: Object obj = msg.getPayload(jbc);
246: assertTrue("The returned payload (Object) was null",
247: obj != null);
248: assertTrue(
249: "The returned payload (Object) was of the wrong type: "
250: + obj.getClass().getName(), obj.getClass()
251: .equals(EchoString.class));
252:
253: EchoString echo = (EchoString) obj;
254: assertTrue("The EchoString object had null input", echo
255: .getInput() != null);
256: assertTrue("The EchoString object had bad input: "
257: + echo.getInput(), echo.getInput().equals(INPUT));
258: }
259:
260: public void testConvertMessageToFault() throws Exception {
261: LogicalMessageContext lmc = createSampleContext();
262:
263: LogicalMessage msg = lmc.getMessage();
264: assertTrue("The returned LogicalMessage was null", msg != null);
265:
266: Source payload = msg.getPayload();
267: assertTrue("The returned payload (Source) was null",
268: payload != null);
269:
270: String resultContent = _getStringFromSource(payload);
271: assertTrue("The content returned was null",
272: resultContent != null);
273:
274: ByteArrayInputStream bais = new ByteArrayInputStream(
275: sampleSOAP11FaultPayload.getBytes());
276: StreamSource faultSource = new StreamSource(bais);
277:
278: msg.setPayload(faultSource);
279:
280: Source newFaultSource = msg.getPayload();
281: assertTrue("The new fault content returned was null",
282: faultSource != null);
283:
284: String newFaultContent = _getStringFromSource(newFaultSource);
285: assertTrue("The new fault content returned was invalid",
286: newFaultContent.equals(sampleSOAP11FaultPayload));
287: }
288:
289: private LogicalMessageContext createSampleContext()
290: throws Exception {
291: MessageFactory factory = (MessageFactory) FactoryRegistry
292: .getFactory(MessageFactory.class);
293: Message msg = factory.create(Protocol.soap11);
294:
295: // Create a jaxb object
296: ObjectFactory objFactory = new ObjectFactory();
297: EchoString echo = objFactory.createEchoString();
298: echo.setInput(INPUT);
299:
300: // Create the necessary JAXBContext
301: JAXBContext jbc = JAXBContext.newInstance("test");
302: JAXBBlockContext blockCtx = new JAXBBlockContext(jbc);
303:
304: // Create the Block
305: JAXBBlockFactory blockFactory = (JAXBBlockFactory) FactoryRegistry
306: .getFactory(JAXBBlockFactory.class);
307: Block block = blockFactory.createFrom(echo, blockCtx, null);
308:
309: msg.setBodyBlock(block);
310:
311: MessageContext mc = new MessageContext();
312: mc.setMEPContext(new MEPContext(mc));
313: mc.setMessage(msg);
314:
315: LogicalMessageContext lmc = MessageContextFactory
316: .createLogicalMessageContext(mc);
317:
318: return lmc;
319: }
320:
321: private LogicalMessageContext createSampleFaultContext()
322: throws Exception {
323: MessageFactory factory = (MessageFactory) FactoryRegistry
324: .getFactory(MessageFactory.class);
325: Message msg = factory.create(Protocol.soap11);
326:
327: XMLFaultReason reason = new XMLFaultReason(FAULT_INPUT);
328: XMLFault fault = new XMLFault(XMLFaultCode.SENDER, reason);
329: msg.setXMLFault(fault);
330:
331: MessageContext mc = new MessageContext();
332: mc.setMEPContext(new MEPContext(mc));
333: mc.setMessage(msg);
334:
335: LogicalMessageContext lmc = MessageContextFactory
336: .createLogicalMessageContext(mc);
337:
338: return lmc;
339: }
340:
341: private String _getStringFromSource(Source source) throws Exception {
342: TransformerFactory factory = TransformerFactory.newInstance();
343: Transformer trans = factory.newTransformer();
344:
345: ByteArrayOutputStream baos = new ByteArrayOutputStream();
346: StreamResult result = new StreamResult(baos);
347:
348: trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
349: trans.transform(source, result);
350:
351: String content = new String(baos.toByteArray());
352:
353: return content;
354: }
355: }
|