KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jtests > jms > conform > message > MessageTypeTest


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2002 INRIA
4  * Contact: joram-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Jeff Mesnil (jmesnil@inrialpes.fr)
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.jtests.jms.conform.message;
26
27 import org.objectweb.jtests.jms.framework.PTPTestCase;
28 import org.objectweb.jtests.jms.framework.TestConfig;
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31 import javax.jms.*;
32 import java.util.Vector JavaDoc;
33 import java.util.Enumeration JavaDoc;
34
35 /**
36  * Test the different types of messages provided by JMS.
37  * <br />
38  * JMS provides 6 types of messages which differs by the type of their body:
39  * <ol>
40  * <li><code>Message</code> which doesn't have a body</li>
41  * <li><code>TextMessage</code> with a <code>String</code> as body</li>
42  * <li><code>ObjectMessage</code> with any <code>Object</code> as body</li>
43  * <li><code>BytesMessage</code> with a body made of <code>bytes</code></li>
44  * <li><code>MapMessage</code> with name-value pairs of Java primitives in its body</li>
45  * <li><code>StreamMessage</code> with a stream of Java primitives as body</li>
46  * </ol>
47  * <br />
48  * For each of this type of message, we test that a message can be sent and received
49  * with an empty body or not.
50  *
51  * @author Jeff Mesnil (jmesnil@inrialpes.fr)
52  * @version $Id: MessageTypeTest.java,v 1.5 2002/06/19 15:32:44 jmesnil Exp $
53  */

54 public class MessageTypeTest extends PTPTestCase {
55
56    /**
57     * Send a <code>StreamMessage</code> with 2 Java primitives in its body (a <code>
58     * String</code> and a <code>double</code>).
59     * <br />
60     * Receive it and test that the values of the primitives of the body are correct
61     */

62    public void testStreamMessage_2() {
63      try {
64        StreamMessage message = senderSession.createStreamMessage();
65        message.writeString("pi");
66        message.writeDouble(3.14159);
67        sender.send(message);
68        
69        Message m = receiver.receive(TestConfig.TIMEOUT);
70        assertTrue("The message should be an instance of StreamMessage.\n",
71          m instanceof StreamMessage);
72        StreamMessage msg = (StreamMessage)m;
73        assertEquals("pi", msg.readString());
74        assertEquals(3.14159, msg.readDouble(), 0);
75      } catch (JMSException e) {
76        fail(e);
77      }
78    }
79
80    /**
81     * Send a <code>StreamMessage</code> with an empty body.
82     * <br />
83     * Receive it and test if the message is effectively an instance of
84     * <code>StreamMessage</code>
85     */

86    public void testStreamMessage_1() {
87      try {
88        StreamMessage message = senderSession.createStreamMessage();
89        sender.send(message);
90
91        Message msg = receiver.receive(TestConfig.TIMEOUT);
92        assertTrue("The message should be an instance of StreamMessage.\n",
93          msg instanceof StreamMessage);
94      } catch (JMSException e) {
95        fail(e);
96      }
97    }
98
99    /**
100     * Test in MapMessage the conversion between <code>getObject("foo")</code> and
101     * <code>getDouble("foo")</code> (the later returning a java.lang.Double and the former a double)
102     */

103    public void testMapMessageConversion() {
104      try {
105        MapMessage message = senderSession.createMapMessage();
106        message.setDouble("pi", 3.14159);
107        sender.send(message);
108
109        Message m = receiver.receive(TestConfig.TIMEOUT);
110        assertTrue("The message should be an instance of MapMessage.\n",
111          m instanceof MapMessage);
112        MapMessage msg = (MapMessage)m;
113        assertTrue(msg.getObject("pi") instanceof Double JavaDoc);
114        assertEquals(3.14159, ((Double JavaDoc)msg.getObject("pi")).doubleValue(), 0);
115        assertEquals(3.14159, msg.getDouble("pi"), 0);
116      } catch (JMSException e) {
117        fail(e);
118      }
119    }
120
121    /**
122     * Test that the if the name parameter of the set methods of a <code>MapMessage</code> is <code>null</code>,
123     * the method must throw the error <code>java.lang.IllegalArgumentException</code>.
124     * <br />
125     * @since JMS 1.1
126     */

127     public void testNullInSetMethodsForMapMessage() {
128      try {
129        MapMessage message = senderSession.createMapMessage();
130        message.setBoolean(null, true);
131        fail("Should throw an IllegalArgumentException");
132      } catch (IllegalArgumentException JavaDoc e) {
133      } catch (JMSException e) {
134          fail("Should throw an IllegalArgumentException, not a" +e);
135      }
136    }
137
138    /**
139     * Test that the if the name parameter of the set methods of a <code>MapMessage</code> is an empty String,
140     * the method must throw the error <code>java.lang.IllegalArgumentException</code>.
141     * <br />
142     * @since JMS 1.1
143     */

144     public void testEmptyStringInSetMethodsForMapMessage() {
145      try {
146        MapMessage message = senderSession.createMapMessage();
147        message.setBoolean("", true);
148        fail("Should throw an IllegalArgumentException");
149      } catch (IllegalArgumentException JavaDoc e) {
150      } catch (JMSException e) {
151          fail("Should throw an IllegalArgumentException, not a" +e);
152      }
153    }
154
155    /**
156     * Test that the <code>MapMessage.getMapNames()</code> method returns an
157     * empty <code>Enumeration</code> when no map has been defined before.
158     * <br />
159     * Also test that the same method returns the correct names of the map.
160     */

161    public void testgetMapNames() {
162      try {
163        MapMessage message = senderSession.createMapMessage();
164        Enumeration JavaDoc e = message.getMapNames();
165        assertTrue("No map yet defined.\n",
166          !e.hasMoreElements());
167        message.setDouble("pi", 3.14159);
168        e = message.getMapNames();
169        assertEquals("pi", (String JavaDoc)(e.nextElement()));
170      } catch (JMSException e) {
171        fail(e);
172      }
173    }
174
175    /**
176     * Send a <code>MapMessage</code> with 2 Java primitives in its body (a <code>
177     * String</code> and a <code>double</code>).
178     * <br />
179     * Receive it and test that the values of the primitives of the body are correct
180     */

181    public void testMapMessage_2() {
182      try {
183        MapMessage message = senderSession.createMapMessage();
184        message.setString("name", "pi");
185        message.setDouble("value",3.14159);
186        sender.send(message);
187
188        Message m = receiver.receive(TestConfig.TIMEOUT);
189        assertTrue("The message should be an instance of MapMessage.\n",
190              m instanceof MapMessage);
191        MapMessage msg = (MapMessage)m;
192        assertEquals("pi", msg.getString("name"));
193        assertEquals(3.14159, msg.getDouble("value"), 0);
194      } catch (JMSException e) {
195        fail(e);
196      }
197    }
198
199    /**
200     * Send a <code>MapMessage</code> with an empty body.
201     * <br />
202     * Receive it and test if the message is effectively an instance of
203     * <code>MapMessage</code>
204     */

205    public void testMapMessage_1() {
206      try {
207        MapMessage message = senderSession.createMapMessage();
208        sender.send(message);
209
210        Message msg = receiver.receive(TestConfig.TIMEOUT);
211        assertTrue("The message should be an instance of MapMessage.\n",
212          msg instanceof MapMessage);
213      } catch (JMSException e) {
214        fail(e);
215      }
216    }
217
218    /**
219     * Send an <code>ObjectMessage</code> with a <code>Vector</code> (composed of a <code>
220     * String</code> and a <code>double</code>) in its body.
221     * <br />
222     * Receive it and test that the values of the primitives of the body are correct
223     */

224    public void testObjectMessage_2() {
225      try {
226        Vector JavaDoc vector = new Vector JavaDoc();
227        vector.add("pi");
228        vector.add(new Double JavaDoc(3.14159));
229
230        ObjectMessage message = senderSession.createObjectMessage();
231        message.setObject(vector);
232        sender.send(message);
233
234        Message m = receiver.receive(TestConfig.TIMEOUT);
235        assertTrue("The message should be an instance of ObjectMessage.\n",
236          m instanceof ObjectMessage);
237        ObjectMessage msg = (ObjectMessage)m;
238        assertEquals(vector, msg.getObject());
239      } catch (JMSException e) {
240        fail(e);
241      }
242    }
243
244    /**
245     * Send a <code>ObjectMessage</code> with an empty body.
246     * <br />
247     * Receive it and test if the message is effectively an instance of
248     * <code>ObjectMessage</code>
249     */

250    public void testObjectMessage_1() {
251      try {
252        ObjectMessage message = senderSession.createObjectMessage();
253        sender.send(message);
254
255        Message msg = receiver.receive(TestConfig.TIMEOUT);
256        assertTrue("The message should be an instance of ObjectMessage.\n",
257          msg instanceof ObjectMessage);
258      } catch (JMSException e) {
259        fail(e);
260      }
261    }
262
263    /**
264     * Send a <code>BytesMessage</code> with 2 Java primitives in its body (a <code>
265     * String</code> and a <code>double</code>).
266     * <br />
267     * Receive it and test that the values of the primitives of the body are correct
268     */

269    public void testBytesMessage_2() {
270      try {
271        byte[] bytes = new String JavaDoc("pi").getBytes();
272        BytesMessage message = senderSession.createBytesMessage();
273        message.writeBytes(bytes);
274        message.writeDouble(3.14159);
275        sender.send(message);
276
277        Message m = receiver.receive(TestConfig.TIMEOUT);
278        assertTrue("The message should be an instance of BytesMessage.\n",
279          m instanceof BytesMessage);
280        BytesMessage msg = (BytesMessage)m;
281        byte[] receivedBytes = new byte[bytes.length];
282        msg.readBytes(receivedBytes);
283        assertEquals(new String JavaDoc(bytes), new String JavaDoc(receivedBytes));
284        assertEquals(3.14159, msg.readDouble(), 0);
285      } catch (JMSException e) {
286        fail(e);
287      }
288    }
289
290    /**
291     * Send a <code>BytesMessage</code> with an empty body.
292     * <br />
293     * Receive it and test if the message is effectively an instance of
294     * <code>BytesMessage</code>
295     */

296    public void testBytesMessage_1() {
297      try {
298        BytesMessage message = senderSession.createBytesMessage();
299        sender.send(message);
300
301        Message msg = receiver.receive(TestConfig.TIMEOUT);
302        assertTrue("The message should be an instance of BytesMessage.\n",
303          msg instanceof BytesMessage);
304      } catch (JMSException e) {
305        fail(e);
306      }
307    }
308
309    /**
310     * Send a <code>TextMessage</code> with a <code>String</code> in its body.
311     * <br />
312     * Receive it and test that the received <code>String</code> corresponds to
313     * the sent one.
314     */

315    public void testTextMessage_2() {
316      try {
317        TextMessage message = senderSession.createTextMessage();
318        message.setText("testTextMessage_2");
319        sender.send(message);
320
321        Message m = receiver.receive(TestConfig.TIMEOUT);
322        assertTrue("The message should be an instance of TextMessage.\n",
323          m instanceof TextMessage);
324        TextMessage msg = (TextMessage)m;
325        assertEquals("testTextMessage_2", msg.getText());
326      } catch (JMSException e) {
327        fail(e);
328      }
329    }
330
331   /**
332    * Send a <code>TextMessage</code> with an empty body.
333    * <br />
334    * Receive it and test if the message is effectively an instance of
335    * <code>TextMessage</code>
336    */

337   public void testTextMessage_1() {
338     try {
339       TextMessage message = senderSession.createTextMessage();
340       sender.send(message);
341
342       Message msg = receiver.receive(TestConfig.TIMEOUT);
343       assertTrue("The message should be an instance of TextMessage.\n",
344          msg instanceof TextMessage);
345     } catch (JMSException e) {
346       fail(e);
347     }
348   }
349
350   /**
351    * Method to use this class in a Test suite
352    */

353   public static Test suite() {
354     return new TestSuite(MessageTypeTest.class);
355   }
356   
357   public MessageTypeTest(String JavaDoc name) {
358     super(name);
359   }
360 }
361
Popular Tags