You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by ng...@apache.org on 2007/04/16 17:58:02 UTC
svn commit: r529300 -
/webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java
Author: ngallardo
Date: Mon Apr 16 08:58:01 2007
New Revision: 529300
URL: http://svn.apache.org/viewvc?view=rev&rev=529300
Log:
AXIS2-2532
More LogicalMessageContext tests.
Modified:
webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java
Modified: webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java?view=diff&rev=529300&r1=529299&r2=529300
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java Mon Apr 16 08:58:01 2007
@@ -18,6 +18,7 @@
*/
package org.apache.axis2.jaxws.handler.context;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.bind.JAXBContext;
@@ -26,6 +27,7 @@
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.LogicalMessage;
import javax.xml.ws.handler.LogicalMessageContext;
@@ -36,6 +38,9 @@
import org.apache.axis2.jaxws.message.Block;
import org.apache.axis2.jaxws.message.Message;
import org.apache.axis2.jaxws.message.Protocol;
+import org.apache.axis2.jaxws.message.XMLFault;
+import org.apache.axis2.jaxws.message.XMLFaultCode;
+import org.apache.axis2.jaxws.message.XMLFaultReason;
import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
import org.apache.axis2.jaxws.message.factory.MessageFactory;
@@ -51,6 +56,7 @@
public class LogicalMessageContextTests extends TestCase {
private final String INPUT = "sample input";
+ private final String FAULT_INPUT = "sample fault input";
public LogicalMessageContextTests(String name) {
super(name);
@@ -60,9 +66,8 @@
* Test the javax.xml.transform.Source based APIs on the LogicalMessage interface.
* @throws Exception
*/
- public void testLogicalMessageContextWithSource() throws Exception {
- MessageContext mc = createSampleMessageContext();
- LogicalMessageContext lmc = MessageContextFactory.createLogicalMessageContext(mc);
+ public void testGetPayloadAsSource() throws Exception {
+ LogicalMessageContext lmc = createSampleContext();
LogicalMessage msg = lmc.getMessage();
assertTrue("The returned LogicalMessage was null", msg != null);
@@ -70,44 +75,116 @@
Source payload = msg.getPayload();
assertTrue("The returned payload (Source) was null", payload != null);
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer trans = factory.newTransformer();
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- StreamResult result = new StreamResult(baos);
-
- trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- trans.transform(payload, result);
-
- String resultContent = new String(baos.toByteArray());
+ String resultContent = _getStringFromSource(payload);
assertTrue("The content returned was null", resultContent != null);
assertTrue("The content returned was incomplete, unexpected element", resultContent.indexOf("echoString") > -1);
assertTrue("The content returned was incomplete, unexpected content", resultContent.indexOf(INPUT) > -1);
}
/**
- * Test the JAXB based APIs on the LogicalMessage interface.
+ * Tests the setting of the payload and make sure we don't cache improperly.
* @throws Exception
*/
- public void testLogicalMessageContextWithJAXB() throws Exception {
- MessageContext mc = createSampleMessageContext();
- LogicalMessageContext lmc = MessageContextFactory.createLogicalMessageContext(mc);
+ public void testGetAndSetPayloadAsSource() throws Exception {
+ LogicalMessageContext lmc = createSampleContext();
+
+ LogicalMessage msg = lmc.getMessage();
+ assertTrue("The returned LogicalMessage was null", msg != null);
+
+ Source payload = msg.getPayload();
+ assertTrue("The returned payload (Source) was null", payload != null);
+
+ String resultContent = _getStringFromSource(payload);
+ assertTrue("The content returned was null", resultContent != null);
+ assertTrue("The content returned was incorrect", resultContent.indexOf(INPUT) > 0);
+
+ // Now manipluate the content and set it back on the message.
+ int start = resultContent.indexOf(INPUT);
+ int end = start + INPUT.length();
+
+ String newInput = "new content goes here";
+ String newContent = resultContent.substring(0, start) + newInput + resultContent.substring(end);
+
+ ByteArrayInputStream bais = new ByteArrayInputStream(newContent.getBytes());
+ StreamSource newPayload = new StreamSource(bais);
+
+ msg.setPayload(newPayload);
+
+ // Check the payload to make sure the new content that we added
+ // was insterted correctly.
+ Source payload2 = msg.getPayload();
+ assertTrue("The returned payload (Source) was null", payload2 != null);
+
+ String resultContent2 = _getStringFromSource(payload2);
+ assertTrue("The updated content returned was null", resultContent2 != null);
+ assertTrue("The updated content returned was incorrect, old content found", resultContent2.indexOf(INPUT) < 0);
+ assertTrue("The updated content returned was incorrect, new content not found", resultContent2.indexOf(newInput) > -1);
+ }
+
+ /**
+ * Tests the setting of the payload when the original content is a fault.
+ * @throws Exception
+ */
+ public void testGetAndSetFaultPayloadAsSource() throws Exception {
+ LogicalMessageContext lmc = createSampleFaultContext();
LogicalMessage msg = lmc.getMessage();
assertTrue("The returned LogicalMessage was null", msg != null);
+ Source payload = msg.getPayload();
+ assertTrue("The returned payload (Source) was null", payload != null);
+
+ String resultContent = _getStringFromSource(payload);
+ assertTrue("The content returned was null", resultContent != null);
+ assertTrue("The content returned was incorrect", resultContent.indexOf(FAULT_INPUT) > 0);
+ assertTrue("The content returned was incorrect, no fault found", resultContent.indexOf("Fault") > 0);
+
+ // Now manipluate the content and set it back on the message.
+ int start = resultContent.indexOf(FAULT_INPUT);
+ int end = start + FAULT_INPUT.length();
+
+ String newFaultInput = "new fault content goes here";
+ String newContent = resultContent.substring(0, start) + newFaultInput + resultContent.substring(end);
+
+ ByteArrayInputStream bais = new ByteArrayInputStream(newContent.getBytes());
+ StreamSource newPayload = new StreamSource(bais);
+
+ msg.setPayload(newPayload);
+
+ // Check the payload to make sure the new content that we added
+ // was insterted correctly.
+ Source payload2 = msg.getPayload();
+ assertTrue("The returned payload (Source) was null", payload2 != null);
+
+ String resultContent2 = _getStringFromSource(payload2);
+ assertTrue("The updated content returned was null", resultContent2 != null);
+ assertTrue("The updated content returned was incorrect, old content found", resultContent2.indexOf(FAULT_INPUT) < 0);
+ assertTrue("The updated content returned was incorrect, no fault found", resultContent.indexOf("Fault") > 0);
+ assertTrue("The updated content returned was incorrect, new content not found", resultContent2.indexOf(newFaultInput) > -1);
+ }
+
+ /**
+ * Test the JAXB based APIs on the LogicalMessage interface.
+ * @throws Exception
+ */
+ public void testGetPayloadAsJAXB() throws Exception {
+ LogicalMessageContext lmc = createSampleContext();
+
+ LogicalMessage msg = lmc.getMessage();
+ assertTrue("The returned LogicalMessage was null", msg != null);
+
JAXBContext jbc = JAXBContext.newInstance("test");
Object obj = msg.getPayload(jbc);
assertTrue("The returned payload (Object) was null", obj != null);
assertTrue("The returned payload (Object) was of the wrong type: " + obj.getClass().getName(), obj.getClass().equals(EchoString.class));
-
+
EchoString echo = (EchoString) obj;
assertTrue("The EchoString object had null input", echo.getInput() != null);
assertTrue("The EchoString object had bad input: " + echo.getInput(), echo.getInput().equals(INPUT));
}
- private MessageContext createSampleMessageContext() throws Exception {
+ private LogicalMessageContext createSampleContext() throws Exception {
MessageFactory factory = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class);
Message msg = factory.create(Protocol.soap11);
@@ -129,6 +206,39 @@
MessageContext mc = new MessageContext();
mc.setMessage(msg);
- return mc;
+ LogicalMessageContext lmc = MessageContextFactory.createLogicalMessageContext(mc);
+
+ return lmc;
+ }
+
+ private LogicalMessageContext createSampleFaultContext() throws Exception {
+ MessageFactory factory = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class);
+ Message msg = factory.create(Protocol.soap11);
+
+ XMLFaultReason reason = new XMLFaultReason(FAULT_INPUT);
+ XMLFault fault = new XMLFault(XMLFaultCode.SENDER, reason);
+ msg.setXMLFault(fault);
+
+ MessageContext mc = new MessageContext();
+ mc.setMessage(msg);
+
+ LogicalMessageContext lmc = MessageContextFactory.createLogicalMessageContext(mc);
+
+ return lmc;
+ }
+
+ private String _getStringFromSource(Source source) throws Exception {
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer trans = factory.newTransformer();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ StreamResult result = new StreamResult(baos);
+
+ trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ trans.transform(source, result);
+
+ String content = new String(baos.toByteArray());
+
+ return content;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org