Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/objectweb/jtests/jms/conform/message/MessageTypeTest.java


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 junit.framework.Test;
29  import junit.framework.TestSuite;
30  import javax.jms.*;
31  import java.util.Vector;
32  import java.util.Enumeration;
33  
34  /**
35   * Test the different types of messages provided by JMS.
36   * <br />
37   * JMS provides 6 types of messages which differs by the type of their body:
38   * <ol>
39   *   <li><code>Message</code> which doesn't have a body</li>
40   *   <li><code>TextMessage</code> with a <code>String</code> as body</li>
41   *   <li><code>ObjectMessage</code> with any <code>Object</code> as body</li>
42   *   <li><code>BytesMessage</code> with a body made of <code>bytes</code></li>
43   *   <li><code>MapMessage</code> with name-value pairs of Java primitives in its body</li>
44   *   <li><code>StreamMessage</code> with a stream of Java primitives as body</li>
45   *  </ol>
46   * <br />
47   * For each of this type of message, we test that a message can be sent and received
48   * with an empty body or not.
49   * 
50   * @author Jeff Mesnil (jmesnil@inrialpes.fr)
51   * @version $Id: MessageTypeTest.java,v 1.1 2002/04/21 21:15:18 chirino Exp $
52   */
53  public class MessageTypeTest extends PTPTestCase {
54  
55     /**
56      * Send a <code>StreamMessage</code> with 2 Java primitives in its body (a <code>
57      * String</code> and a <code>double</code>).
58      * <br />
59      * Receive it and test that the values of the primitives of the body are correct
60      */
61     public void testStreamMessage_2() {
62       try {
63         StreamMessage message = senderSession.createStreamMessage();
64         message.writeString("pi");
65         message.writeDouble(3.14159);
66         sender.send(message);
67  
68         Message m = receiver.receive();
69         assertTrue("The message should be an instance of StreamMessage.\n",
70         m instanceof StreamMessage);
71         StreamMessage msg = (StreamMessage)m;
72         assertEquals("pi", msg.readString());
73         assertEquals(3.14159, msg.readDouble(), 0);
74       } catch (JMSException e) {
75         fail(e);
76       }
77     }
78  
79     /**
80      * Send a <code>StreamMessage</code> with an empty body.
81      * <br />
82      * Receive it and test if the message is effectively an instance of 
83      * <code>StreamMessage</code>
84      */
85     public void testStreamMessage_1() {
86       try {
87         StreamMessage message = senderSession.createStreamMessage();
88         sender.send(message);
89  
90         Message msg = receiver.receive();
91         assertTrue("The message should be an instance of StreamMessage.\n",
92         msg instanceof StreamMessage);
93       } catch (JMSException e) {
94         fail(e);
95       }
96     }
97  
98     /**
99      * Test in MapMessage the conversion between <code>getObject("foo")</code> and
100     * <code>getDouble("foo")</code> (the later returning a java.lang.Double and the former a double)
101     */
102    public void testMapMessageConversion() {
103      try {
104        MapMessage message = senderSession.createMapMessage();
105        message.setDouble("pi", 3.14159);
106        sender.send(message);
107 
108        Message m = receiver.receive();
109        assertTrue("The message should be an instance of MapMessage.\n",
110        m instanceof MapMessage);
111        MapMessage msg = (MapMessage)m;
112        assertTrue(msg.getObject("pi") instanceof Double);
113        assertEquals(3.14159, ((Double)msg.getObject("pi")).doubleValue(), 0);
114        assertEquals(3.14159, msg.getDouble("pi"), 0);
115      } catch (JMSException e) {
116        fail(e);
117      }
118    }
119 
120    /**
121     * Test that the <code>MapMessage.getMapNames()</code> method returns an
122     * empty <code>Enumeration</code> when no map has been defined before.
123     * <br />
124     * Also test that the same method returns the correct names of the map.
125     */
126    public void testgetMapNames() {
127      try {
128        MapMessage message = senderSession.createMapMessage();
129        Enumeration e = message.getMapNames();
130        assertTrue("No map yet defined.\n",
131        !e.hasMoreElements());
132        message.setDouble("pi", 3.14159);
133        e = message.getMapNames();
134        assertEquals("pi", (String)(e.nextElement()));
135      } catch (JMSException e) {
136        fail(e);
137      }
138    }
139 
140    /**
141     * Send a <code>MapMessage</code> with 2 Java primitives in its body (a <code>
142     * String</code> and a <code>double</code>).
143     * <br />
144     * Receive it and test that the values of the primitives of the body are correct
145     */
146    public void testMapMessage_2() {
147      try {
148        MapMessage message = senderSession.createMapMessage();
149        message.setString("name", "pi");
150        message.setDouble("value",3.14159);
151        sender.send(message);
152 
153        Message m = receiver.receive();
154        assertTrue("The message should be an instance of MapMessage.\n",
155          m instanceof MapMessage);
156        MapMessage msg = (MapMessage)m;
157        assertEquals("pi", msg.getString("name"));
158        assertEquals(3.14159, msg.getDouble("value"), 0);
159      } catch (JMSException e) {
160        fail(e);
161      }
162    }
163 
164    /**
165     * Send a <code>MapMessage</code> with an empty body.
166     * <br />
167     * Receive it and test if the message is effectively an instance of 
168     * <code>MapMessage</code>
169     */
170    public void testMapMessage_1() {
171      try {
172        MapMessage message = senderSession.createMapMessage();
173        sender.send(message);
174 
175        Message msg = receiver.receive();
176        assertTrue("The message should be an instance of MapMessage.\n",
177       msg instanceof MapMessage);
178      } catch (JMSException e) {
179        fail(e);
180      }
181    }
182 
183    /**
184     * Send an <code>ObjectMessage</code> with a <code>Vector</code> (composed of a <code>
185     * String</code> and a <code>double</code>) in its body.
186     * <br />
187     * Receive it and test that the values of the primitives of the body are correct
188     */
189    public void testObjectMessage_2() {
190      try {    
191        Vector vector = new Vector();
192        vector.add("pi");
193        vector.add(new Double(3.14159));
194 
195        ObjectMessage message = senderSession.createObjectMessage();
196        message.setObject(vector);
197        sender.send(message);
198 
199        Message m = receiver.receive();
200        assertTrue("The message should be an instance of ObjectMessage.\n",
201        m instanceof ObjectMessage);
202        ObjectMessage msg = (ObjectMessage)m;
203        assertEquals(vector, msg.getObject());
204      } catch (JMSException e) {
205        fail(e);
206      }
207    }
208 
209    /**
210     * Send a <code>ObjectMessage</code> with an empty body.
211     * <br />
212     * Receive it and test if the message is effectively an instance of 
213     * <code>ObjectMessage</code>
214     */
215    public void testObjectMessage_1() {
216      try {
217        ObjectMessage message = senderSession.createObjectMessage();
218        sender.send(message);
219 
220        Message msg = receiver.receive();
221        assertTrue("The message should be an instance of ObjectMessage.\n",
222        msg instanceof ObjectMessage);
223      } catch (JMSException e) {
224        fail(e);
225      }
226    }
227 
228    /**
229     * Send a <code>BytesMessage</code> with 2 Java primitives in its body (a <code>
230     * String</code> and a <code>double</code>).
231     * <br />
232     * Receive it and test that the values of the primitives of the body are correct
233     */
234    public void testBytesMessage_2() {
235      try {
236        byte[] bytes = new String("pi").getBytes();
237        BytesMessage message = senderSession.createBytesMessage();
238        message.writeBytes(bytes);
239        message.writeDouble(3.14159);
240        sender.send(message);
241 
242        Message m = receiver.receive();
243        assertTrue("The message should be an instance of BytesMessage.\n",
244        m instanceof BytesMessage);
245        BytesMessage msg = (BytesMessage)m;
246        byte[] receivedBytes = new byte[bytes.length];
247        msg.readBytes(receivedBytes);
248        assertEquals(new String(bytes), new String(receivedBytes));
249        assertEquals(3.14159, msg.readDouble(), 0);  
250      } catch (JMSException e) {
251        fail(e);
252      }
253    }
254 
255    /**
256     * Send a <code>BytesMessage</code> with an empty body.
257     * <br />
258     * Receive it and test if the message is effectively an instance of 
259     * <code>BytesMessage</code>
260     */
261    public void testBytesMessage_1() {
262      try {
263        BytesMessage message = senderSession.createBytesMessage();
264        sender.send(message);
265 
266        Message msg = receiver.receive();
267        assertTrue("The message should be an instance of BytesMessage.\n",
268       msg instanceof BytesMessage);
269      } catch (JMSException e) {
270        fail(e);
271      }
272    }
273 
274    /**
275     * Send a <code>TextMessage</code> with a <code>String</code> in its body.
276     * <br />
277     * Receive it and test that the received <code>String</code> corresponds to
278     * the sent one.
279     */
280    public void testTextMessage_2() {
281      try {
282        TextMessage message = senderSession.createTextMessage();
283        message.setText("testTextMessage_2");
284        sender.send(message);
285 
286        Message m = receiver.receiveNoWait();
287        assertTrue("The message should be an instance of TextMessage.\n",
288        m instanceof TextMessage);
289        TextMessage msg = (TextMessage)m;
290        assertEquals("testTextMessage_2", msg.getText());
291      } catch (JMSException e) {
292        fail(e);
293      }
294    }
295 
296   /**
297    * Send a <code>TextMessage</code> with an empty body.
298    * <br />
299    * Receive it and test if the message is effectively an instance of 
300    * <code>TextMessage</code>
301    */
302   public void testTextMessage_1() {
303     try {
304       TextMessage message = senderSession.createTextMessage();
305       sender.send(message);
306 
307       Message msg = receiver.receive();
308       assertTrue("The message should be an instance of TextMessage.\n",
309         msg instanceof TextMessage);
310     } catch (JMSException e) {
311       fail(e);
312     }
313   }
314 
315   /** 
316    * Method to use this class in a Test suite
317    */
318   public static Test suite() {
319     return new TestSuite(MessageTypeTest.class);
320   }
321   
322   public MessageTypeTest(String name) {
323     super(name);
324   }
325 }