Title: [701] trunk/core/src/test/java/org/servicemix/examples: SM-122 : Message content not delivered in cluster flow

Diff

Modified: trunk/core/src/main/java/org/servicemix/jbi/jaxp/BytesSource.java (700 => 701)

--- trunk/core/src/main/java/org/servicemix/jbi/jaxp/BytesSource.java	2005-10-28 15:03:55 UTC (rev 700)
+++ trunk/core/src/main/java/org/servicemix/jbi/jaxp/BytesSource.java	2005-10-28 17:42:44 UTC (rev 701)
@@ -50,4 +50,8 @@
     public Reader getReader() {
         return new InputStreamReader(getInputStream());
     }
+
+    public byte[] getData() {
+        return data;
+    }
 }

Modified: trunk/core/src/main/java/org/servicemix/jbi/jaxp/SourceTransformer.java (700 => 701)

--- trunk/core/src/main/java/org/servicemix/jbi/jaxp/SourceTransformer.java	2005-10-28 15:03:55 UTC (rev 700)
+++ trunk/core/src/main/java/org/servicemix/jbi/jaxp/SourceTransformer.java	2005-10-28 17:42:44 UTC (rev 701)
@@ -93,10 +93,15 @@
     public String toString(Source source) throws TransformerException {
         if (source == null) {
             return null;
+        } else if (source instanceof StringSource) {
+            return ((StringSource) source).getText();
+        } else if (source instanceof BytesSource) {
+            return new String(((BytesSource) source).getData());
+        } else {
+            StringWriter buffer = new StringWriter();
+            toResult(source, new StreamResult(buffer));
+            return buffer.toString();
         }
-        StringWriter buffer = new StringWriter();
-        toResult(source, new StreamResult(buffer));
-        return buffer.toString();
     }
 
     /**
@@ -113,7 +118,7 @@
      * @throws ParserConfigurationException 
      */
     public String contentToString(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
-        return toString(toDOMNode(message));
+        return toString(message.getContent());
     }
 
     /**

Modified: trunk/core/src/main/java/org/servicemix/jbi/jaxp/StringSource.java (700 => 701)

--- trunk/core/src/main/java/org/servicemix/jbi/jaxp/StringSource.java	2005-10-28 15:03:55 UTC (rev 700)
+++ trunk/core/src/main/java/org/servicemix/jbi/jaxp/StringSource.java	2005-10-28 17:42:44 UTC (rev 701)
@@ -55,4 +55,8 @@
     	return "StringSource[" + text + "]";
     }
 
+    public String getText() {
+        return text;
+    }
+
 }

Modified: trunk/core/src/main/java/org/servicemix/jbi/messaging/NormalizedMessageImpl.java (700 => 701)

--- trunk/core/src/main/java/org/servicemix/jbi/messaging/NormalizedMessageImpl.java	2005-10-28 15:03:55 UTC (rev 700)
+++ trunk/core/src/main/java/org/servicemix/jbi/messaging/NormalizedMessageImpl.java	2005-10-28 17:42:44 UTC (rev 701)
@@ -21,17 +21,26 @@
 
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 
+import org.servicemix.jbi.framework.ComponentNameSpace;
 import org.servicemix.jbi.jaxp.SourceTransformer;
 import org.servicemix.jbi.jaxp.StringSource;
+import org.servicemix.jbi.servicedesc.ServiceEndpointImpl;
 
 import javax.activation.DataHandler;
+import javax.jbi.messaging.ExchangeStatus;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
 import javax.security.auth.Subject;
+import javax.xml.namespace.QName;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.io.Serializable;
+import java.net.URI;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.Map;
@@ -42,7 +51,7 @@
  *
  * @version $Revision$
  */
-public class NormalizedMessageImpl implements NormalizedMessage, Serializable{
+public class NormalizedMessageImpl implements NormalizedMessage, Externalizable {
     
     private static final long serialVersionUID = 9179194301410526549L;
     private transient MessageExchangeImpl exchange;
@@ -258,4 +267,38 @@
     protected Map createAttachmentsMap() {
         return new ConcurrentHashMap();
     }
+
+    /**
+     * Write to a Stream
+     * @param out
+     * @throws IOException
+     */
+    public void writeExternal(ObjectOutput out) throws IOException {
+        try {
+            out.writeObject(attachments);
+            out.writeObject(properties);
+            String src = ""
+            out.writeObject(src);
+        } catch (TransformerException e) {
+            throw (IOException) new IOException("Could not transform content to string").initCause(e);
+        }
+    }
+
+    /**
+     * Read from a stream
+     * 
+     * @param in
+     * @throws IOException
+     * @throws ClassNotFoundException
+     */
+    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        attachments = (Map) in.readObject();
+        properties = (Map) in.readObject();
+        String src = "" in.readObject();
+        if (src != null) {
+            content = new StringSource(src);
+        }
+    }
+
 }
+

Modified: trunk/core/src/test/java/org/servicemix/examples/ReceiverComponent.java (700 => 701)

--- trunk/core/src/test/java/org/servicemix/examples/ReceiverComponent.java	2005-10-28 15:03:55 UTC (rev 700)
+++ trunk/core/src/test/java/org/servicemix/examples/ReceiverComponent.java	2005-10-28 17:42:44 UTC (rev 701)
@@ -19,6 +19,7 @@
 
 import org.servicemix.MessageExchangeListener;
 import org.servicemix.components.util.ComponentSupport;
+import org.servicemix.jbi.jaxp.SourceTransformer;
 
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
@@ -46,6 +47,11 @@
         if (inMessage == null) {
             throw new MessagingException("Null in message delivered!");
         }
+        try {
+            System.out.println(new SourceTransformer().toString(inMessage.getContent()));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
         messageList.addMessage(inMessage);
         done(exchange);
     }

Added: trunk/core/src/test/java/org/servicemix/jbi/messaging/MessageExchangeImplTest.java (700 => 701)

--- trunk/core/src/test/java/org/servicemix/jbi/messaging/MessageExchangeImplTest.java	2005-10-28 15:03:55 UTC (rev 700)
+++ trunk/core/src/test/java/org/servicemix/jbi/messaging/MessageExchangeImplTest.java	2005-10-28 17:42:44 UTC (rev 701)
@@ -0,0 +1,96 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); 
+ * you may not use this file except in compliance with the License. 
+ * You may obtain a copy of the License at 
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License. 
+ * 
+ **/
+package org.servicemix.jbi.messaging;
+
+import org.servicemix.jbi.jaxp.BytesSource;
+import org.servicemix.jbi.jaxp.SourceTransformer;
+import org.servicemix.jbi.jaxp.StringSource;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import junit.framework.TestCase;
+
+public class MessageExchangeImplTest extends TestCase {
+    
+    protected void testSerializeDeserialize(Source src) throws Exception {
+        MessageExchange me = new InOnlyImpl("exchangeId");
+        me.setProperty("myProp", "myValue");
+        NormalizedMessage msg = me.createMessage();
+        msg.setProperty("myMsgProp", "myMsgValue");
+        msg.setContent(src);
+        //msg.addAttachment("myAttachment", null);
+        me.setMessage(msg, "in");
+        
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(me);
+        oos.close();
+        
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        ObjectInputStream ois = new ObjectInputStream(bais);
+        Object out = ois.readObject();
+        
+        assertNotNull(out);
+        assertTrue(out instanceof MessageExchange);
+        MessageExchange meOut = (MessageExchange) out;
+        assertEquals("myValue", meOut.getProperty("myProp"));
+        NormalizedMessage msgOut = meOut.getMessage("in");
+        assertNotNull(msgOut);
+        assertEquals("myMsgValue", msgOut.getProperty("myMsgProp"));
+        Source outSrc = msgOut.getContent();
+        assertNotNull(outSrc);
+        String outStr = new SourceTransformer().toString(outSrc);
+        assertNotNull(outStr);
+        System.out.println(outStr);
+    }
+
+    
+    public void testSerializeDeserializeWithStringSource() throws Exception {
+        Source src = "" StringSource("<hello>world</hello>");
+        testSerializeDeserialize(src);
+    }
+
+    public void testSerializeDeserializeWithBytesSource() throws Exception {
+        Source src = "" BytesSource("<hello>world</hello>".getBytes());
+        testSerializeDeserialize(src);
+    }
+
+    public void testSerializeDeserializeWithStreamSource() throws Exception {
+        Source src = "" StreamSource(new ByteArrayInputStream("<hello>world</hello>".getBytes()));
+        testSerializeDeserialize(src);
+    }
+
+    public void testSerializeDeserializeWithDomSource() throws Exception {
+        Source src = "" SourceTransformer().toDOMSource(new StringSource("<hello>world</hello>"));
+        testSerializeDeserialize(src);
+    }
+
+    public void testSerializeDeserializeWithSaxSource() throws Exception {
+        Source src = "" SourceTransformer().toSAXSource(new StringSource("<hello>world</hello>"));
+        testSerializeDeserialize(src);
+    }
+
+}

Reply via email to