KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > support > SimpleMessageConverterTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.support;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.jms.BytesMessage JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.MapMessage JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.jms.ObjectMessage JavaDoc;
29 import javax.jms.Session JavaDoc;
30 import javax.jms.TextMessage JavaDoc;
31
32 import junit.framework.TestCase;
33 import org.easymock.ArgumentsMatcher;
34 import org.easymock.MockControl;
35
36 import org.springframework.jms.support.converter.SimpleMessageConverter;
37 import org.springframework.jms.support.converter.SimpleMessageConverter102;
38
39 /**
40  * @author Juergen Hoeller
41  * @since 18.09.2004
42  */

43 public class SimpleMessageConverterTests extends TestCase {
44
45     public void testStringConversion() throws JMSException JavaDoc {
46         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
47         Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
48         MockControl messageControl = MockControl.createControl(TextMessage JavaDoc.class);
49         TextMessage JavaDoc message = (TextMessage JavaDoc) messageControl.getMock();
50
51         String JavaDoc content = "test";
52
53         session.createTextMessage(content);
54         sessionControl.setReturnValue(message, 1);
55         message.getText();
56         messageControl.setReturnValue(content, 1);
57         sessionControl.replay();
58         messageControl.replay();
59
60         SimpleMessageConverter converter = new SimpleMessageConverter();
61         Message JavaDoc msg = converter.toMessage(content, session);
62         assertEquals(content, converter.fromMessage(msg));
63
64         sessionControl.verify();
65         messageControl.verify();
66     }
67
68     public void testByteArrayConversion() throws JMSException JavaDoc {
69         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
70         Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
71         MockControl messageControl = MockControl.createControl(BytesMessage JavaDoc.class);
72         BytesMessage JavaDoc message = (BytesMessage JavaDoc) messageControl.getMock();
73
74         byte[] content = "test".getBytes();
75
76         session.createBytesMessage();
77         sessionControl.setReturnValue(message, 1);
78         message.writeBytes(content);
79         messageControl.setVoidCallable(1);
80         message.getBodyLength();
81         messageControl.setReturnValue(content.length, 1);
82         message.readBytes(new byte[content.length]);
83         messageControl.setMatcher(new ArgumentsMatcher() {
84             public boolean matches(Object JavaDoc[] arg0, Object JavaDoc[] arg1) {
85                 byte[] one = (byte[]) arg0[0];
86                 byte[] two = (byte[]) arg1[0];
87                 return Arrays.equals(one, two);
88             }
89             public String JavaDoc toString(Object JavaDoc[] arg0) {
90                 return "bla";
91             }
92         });
93         messageControl.setReturnValue(content.length, 1);
94         sessionControl.replay();
95         messageControl.replay();
96
97         SimpleMessageConverter converter = new SimpleMessageConverter();
98         Message JavaDoc msg = converter.toMessage(content, session);
99         assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length);
100
101         sessionControl.verify();
102         messageControl.verify();
103     }
104
105     public void testMapConversion(MapMessage JavaDoc message) throws JMSException JavaDoc {
106         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
107         Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
108         MockControl messageControl = MockControl.createControl(MapMessage JavaDoc.class);
109
110         Map JavaDoc content = new HashMap JavaDoc();
111         content.put("key1", "value1");
112         content.put("key2", "value2");
113
114         session.createMapMessage();
115         sessionControl.setReturnValue(message, 1);
116         message.setObject("key1", "value1");
117         messageControl.setVoidCallable(1);
118         message.setObject("key2", "value2");
119         messageControl.setVoidCallable(1);
120         message.getMapNames();
121         messageControl.setReturnValue(Collections.enumeration(content.keySet()));
122         message.getObject("key1");
123         messageControl.setReturnValue("value1", 1);
124         message.getObject("key2");
125         messageControl.setReturnValue("value2", 1);
126         sessionControl.replay();
127         messageControl.replay();
128
129         SimpleMessageConverter converter = new SimpleMessageConverter();
130         Message JavaDoc msg = converter.toMessage(content, session);
131         assertEquals(content, converter.fromMessage(msg));
132
133         sessionControl.verify();
134         messageControl.verify();
135     }
136
137     public void testSerializableConversion() throws JMSException JavaDoc {
138         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
139         Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
140         MockControl messageControl = MockControl.createControl(ObjectMessage JavaDoc.class);
141         ObjectMessage JavaDoc message = (ObjectMessage JavaDoc) messageControl.getMock();
142
143         Integer JavaDoc content = new Integer JavaDoc(5);
144
145         session.createObjectMessage(content);
146         sessionControl.setReturnValue(message, 1);
147         message.getObject();
148         messageControl.setReturnValue(content, 1);
149         sessionControl.replay();
150         messageControl.replay();
151
152         SimpleMessageConverter converter = new SimpleMessageConverter();
153         Message JavaDoc msg = converter.toMessage(content, session);
154         assertEquals(content, converter.fromMessage(msg));
155
156         sessionControl.verify();
157         messageControl.verify();
158     }
159
160     public void testByteArrayConversion102() throws JMSException JavaDoc {
161         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
162         Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
163         MockControl messageControl = MockControl.createControl(BytesMessage JavaDoc.class);
164         BytesMessage JavaDoc message = (BytesMessage JavaDoc) messageControl.getMock();
165
166         byte[] content = new byte[5000];
167
168         session.createBytesMessage();
169         sessionControl.setReturnValue(message, 1);
170         message.writeBytes(content);
171         messageControl.setVoidCallable(1);
172         message.readBytes(new byte[SimpleMessageConverter102.BUFFER_SIZE]);
173         messageControl.setMatcher(new ArgumentsMatcher() {
174             public boolean matches(Object JavaDoc[] arg0, Object JavaDoc[] arg1) {
175                 byte[] one = (byte[]) arg0[0];
176                 byte[] two = (byte[]) arg1[0];
177                 return Arrays.equals(one, two);
178             }
179             public String JavaDoc toString(Object JavaDoc[] arg0) {
180                 return "bla";
181             }
182         });
183         messageControl.setReturnValue(SimpleMessageConverter102.BUFFER_SIZE, 1);
184         message.readBytes(new byte[SimpleMessageConverter102.BUFFER_SIZE]);
185         messageControl.setReturnValue(5000 - SimpleMessageConverter102.BUFFER_SIZE, 1);
186         sessionControl.replay();
187         messageControl.replay();
188
189         SimpleMessageConverter102 converter = new SimpleMessageConverter102();
190         Message JavaDoc msg = converter.toMessage(content, session);
191         assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length);
192
193         sessionControl.verify();
194         messageControl.verify();
195     }
196
197 }
198
Popular Tags