KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > messaging > MessageExchangeImplTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
28 import javax.jbi.messaging.MessageExchange;
29 import javax.jbi.messaging.NormalizedMessage;
30 import javax.xml.namespace.QName JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37 import java.io.ObjectOutputStream JavaDoc;
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 JavaDoc {
46         MessageExchange me = new InOnlyImpl("exchangeId");
47         me.setOperation(new QName JavaDoc("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 JavaDoc(new StreamDataSource(new ByteArrayInputStream JavaDoc("hello".getBytes()))));
53         me.setMessage(msg, "in");
54         assertNotNull(((NormalizedMessageImpl) msg).getBody());
55         
56         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
57         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
58         oos.writeObject(me);
59         oos.close();
60         
61         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
62         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
63         Object JavaDoc out = ois.readObject();
64         
65         assertNotNull(out);
66         assertTrue(out instanceof MessageExchange);
67         MessageExchange meOut = (MessageExchange) out;
68         assertEquals(new QName JavaDoc("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 JavaDoc 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 JavaDoc {
84         Source src = new StringSource("<hello>world</hello>");
85         testSerializeDeserialize(src);
86     }
87
88     public void testSerializeDeserializeWithBytesSource() throws Exception JavaDoc {
89         Source src = new BytesSource("<hello>world</hello>".getBytes());
90         testSerializeDeserialize(src);
91     }
92
93     public void testSerializeDeserializeWithStreamSource() throws Exception JavaDoc {
94         Source src = new StreamSource JavaDoc(new ByteArrayInputStream JavaDoc("<hello>world</hello>".getBytes()));
95         testSerializeDeserialize(src);
96     }
97
98     public void testSerializeDeserializeWithDomSource() throws Exception JavaDoc {
99         Source src = new SourceTransformer().toDOMSource(new StringSource("<hello>world</hello>"));
100         testSerializeDeserialize(src);
101     }
102
103     public void testSerializeDeserializeWithSaxSource() throws Exception JavaDoc {
104         Source src = new SourceTransformer().toSAXSource(new StringSource("<hello>world</hello>"));
105         testSerializeDeserialize(src);
106     }
107
108 }
109
Popular Tags