1 17 package org.apache.servicemix.jbi.messaging; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.jbi.jaxp.BytesSource; 22 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 23 import org.apache.servicemix.jbi.jaxp.StringSource; 24 import org.apache.servicemix.jbi.messaging.InOnlyImpl; 25 import org.apache.servicemix.jbi.util.StreamDataSource; 26 27 import javax.activation.DataHandler ; 28 import javax.jbi.messaging.MessageExchange; 29 import javax.jbi.messaging.NormalizedMessage; 30 import javax.xml.namespace.QName ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.stream.StreamSource ; 33 34 import java.io.ByteArrayInputStream ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.ObjectInputStream ; 37 import java.io.ObjectOutputStream ; 38 39 import junit.framework.TestCase; 40 41 public class MessageExchangeImplTest extends TestCase { 42 43 private static final Log log = LogFactory.getLog(MessageExchangeImplTest.class); 44 45 protected void testSerializeDeserialize(Source src) throws Exception { 46 MessageExchange me = new InOnlyImpl("exchangeId"); 47 me.setOperation(new QName ("uri", "op")); 48 me.setProperty("myProp", "myValue"); 49 NormalizedMessage msg = me.createMessage(); 50 msg.setProperty("myMsgProp", "myMsgValue"); 51 msg.setContent(src); 52 msg.addAttachment("myAttachment", new DataHandler (new StreamDataSource(new ByteArrayInputStream ("hello".getBytes())))); 53 me.setMessage(msg, "in"); 54 assertNotNull(((NormalizedMessageImpl) msg).getBody()); 55 56 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 57 ObjectOutputStream oos = new ObjectOutputStream (baos); 58 oos.writeObject(me); 59 oos.close(); 60 61 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 62 ObjectInputStream ois = new ObjectInputStream (bais); 63 Object out = ois.readObject(); 64 65 assertNotNull(out); 66 assertTrue(out instanceof MessageExchange); 67 MessageExchange meOut = (MessageExchange) out; 68 assertEquals(new QName ("uri", "op"), meOut.getOperation()); 69 assertEquals("myValue", meOut.getProperty("myProp")); 70 NormalizedMessage msgOut = meOut.getMessage("in"); 71 assertNotNull(msgOut); 72 assertEquals("myMsgValue", msgOut.getProperty("myMsgProp")); 73 Source outSrc = msgOut.getContent(); 74 assertNotNull(outSrc); 75 String outStr = new SourceTransformer().toString(outSrc); 76 assertNotNull(outStr); 77 assertNotNull(((NormalizedMessageImpl) msgOut).getBody()); 78 log.info(outStr); 79 assertNotNull(msgOut.getAttachment("myAttachment")); 80 } 81 82 83 public void testSerializeDeserializeWithStringSource() throws Exception { 84 Source src = new StringSource("<hello>world</hello>"); 85 testSerializeDeserialize(src); 86 } 87 88 public void testSerializeDeserializeWithBytesSource() throws Exception { 89 Source src = new BytesSource("<hello>world</hello>".getBytes()); 90 testSerializeDeserialize(src); 91 } 92 93 public void testSerializeDeserializeWithStreamSource() throws Exception { 94 Source src = new StreamSource (new ByteArrayInputStream ("<hello>world</hello>".getBytes())); 95 testSerializeDeserialize(src); 96 } 97 98 public void testSerializeDeserializeWithDomSource() throws Exception { 99 Source src = new SourceTransformer().toDOMSource(new StringSource("<hello>world</hello>")); 100 testSerializeDeserialize(src); 101 } 102 103 public void testSerializeDeserializeWithSaxSource() throws Exception { 104 Source src = new SourceTransformer().toSAXSource(new StringSource("<hello>world</hello>")); 105 testSerializeDeserialize(src); 106 } 107 108 } 109 | Popular Tags |