KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jtests > jms > conform > message > headers > MessageHeaderTest


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): Andreas Mueller <am@iit.de>.
23  */

24
25 package org.objectweb.jtests.jms.conform.message.headers;
26
27 import org.objectweb.jtests.jms.framework.PTPTestCase;
28 import org.objectweb.jtests.jms.framework.TestConfig;
29 import javax.jms.*;
30 import junit.framework.*;
31 import javax.naming.* ;
32
33 /**
34  * Test the headers of a message
35  *
36  * @author Jeff Mesnil (jmesnil@inrialpes.fr)
37  * @version $Id: MessageHeaderTest.java,v 1.8 2002/05/16 10:37:56 jmesnil Exp $
38  */

39 public class MessageHeaderTest extends PTPTestCase {
40
41     /**
42      * Test that the <code>MessageProducer.setPriority()</code> changes effectively
43      * priority of the message.
44      */

45     public void testJMSPriority_2() {
46         try {
47             Message message= senderSession.createMessage();
48             sender.send(message);
49             sender.setPriority(9);
50             sender.send(message);
51             assertEquals("3.4.9 After completion of the send it holds the value specified by the " +
52                          "method sending the message.\n",
53                          9, message.getJMSPriority());
54       
55             Message msg = receiver.receive(TestConfig.TIMEOUT);
56         } catch (JMSException e) {
57             fail(e);
58         }
59     }
60
61     /**
62      * Test that the priority set by <code>Message.setJMSPriority()</code> is ignored when a
63      * message is sent and that it holds the value specified when sending the message (i.e.
64      * <code>Message.DEFAULT_PRIORITY</code> in this test).
65      */

66     public void testJMSPriority_1() {
67         try {
68             Message message= senderSession.createMessage();
69             message.setJMSPriority(0);
70             sender.send(message);
71             assertTrue("3.4.9 When a message is sent this value is ignored.\n",
72                        message.getJMSPriority() != 0);
73             assertEquals("3.4.9 After completion of the send it holds the value specified by the " +
74                          "method sending the message.\n",
75                          Message.DEFAULT_PRIORITY, message.getJMSPriority());
76       
77             Message msg = receiver.receive(TestConfig.TIMEOUT);
78         } catch (JMSException e) {
79             fail(e);
80         }
81     }
82     
83     /**
84      * Test that the value of the <code>JMSExpiration<code> header field is the same
85      * for the sent message and the received one.
86      */

87     public void testJMSExpiration() {
88         try {
89             Message message= senderSession.createMessage();
90             sender.send(message);
91
92             Message msg = receiver.receive(TestConfig.TIMEOUT);
93             assertEquals("3.4.9 When a message is received its JMSExpiration header field contains this same " +
94                          "value [i.e. set on return of the send method].\n",
95                          message.getJMSExpiration(), msg.getJMSExpiration());
96         } catch (JMSException e) {
97             fail(e);
98         }
99     }
100
101     /**
102      * Test that the <code>JMSMessageID</code> is set by the provider when the <code>send</code> method returns
103      * and that it starts with <code>"ID:"</code>.
104      */

105     public void testJMSMessageID_2() {
106         try {
107             Message message = senderSession.createMessage();
108             sender.send(message);
109             assertTrue("3.4.3 When the send method returns it contains a provider-assigned value.\n",
110                        message.getJMSMessageID() != null);
111             assertTrue("3.4.3 All JMSMessageID values must start with the prefix 'ID:'.\n",
112                        message.getJMSMessageID().startsWith("ID:"));
113       
114             Message msg = receiver.receive(TestConfig.TIMEOUT);
115             assertTrue("3.4.3 All JMSMessageID values must start with the prefix 'ID:'.\n",
116                        msg.getJMSMessageID().startsWith("ID:"));
117         } catch (JMSException e) {
118             fail(e);
119         }
120     }
121
122     /**
123      * Test that the <code>JMSMessageID</code> header field value is
124      * ignored when the message is sent.
125      */

126     public void testJMSMessageID_1() {
127         try {
128             Message message = senderSession.createMessage();
129             message.setJMSMessageID("ID:foo");
130             sender.send(message);
131             assertTrue("3.4.3 When a message is sent this value is ignored.\n",
132                        message.getJMSMessageID() != "ID:foo");
133             Message msg = receiver.receive(TestConfig.TIMEOUT);
134         } catch (JMSException e) {
135             fail(e);
136         }
137     }
138   
139     /**
140      * Test that the <code>JMSDeliveryMode</code> header field value is ignored
141      * when the message is sent and that it holds the value specified by the sending
142      * method (i.e. <code>Message.DEFAULT_DELIVERY_MODE</code> in this test when the message is received.
143      */

144     public void testJMSDeliveryMode() {
145         try {
146             // sender has been created with the DEFAULT_DELIVERY_MODE which is PERSISTENT
147
assertEquals(DeliveryMode.PERSISTENT, sender.getDeliveryMode());
148             Message message = senderSession.createMessage();
149             // send a message specfiying NON_PERSISTENT for the JMSDeliveryMode header field
150
message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
151             sender.send(message);
152             assertTrue("3.4.2 When a message is sent this value is ignored",
153                        message.getJMSDeliveryMode() != DeliveryMode.NON_PERSISTENT);
154             assertEquals("3.4.2 After completion of the send it holds the delivery mode specified " +
155                          "by the sending method (persistent by default).\n",
156                          Message.DEFAULT_DELIVERY_MODE, message.getJMSDeliveryMode());
157
158             Message msg = receiver.receive(TestConfig.TIMEOUT);
159         } catch (JMSException e) {
160             fail(e);
161         }
162     }
163
164     /**
165      * Test that the <code>JMSDestination</code> header field value is ignored when the message
166      * is sent and that after completion of the sending method, it holds the <code>Destination</code>
167      * specified by the sending method.
168      * Also test that the value of the header on the received message is the same that on the sent message.
169      */

170     public void testJMSDestination() {
171         try {
172             admin.createQueue("anotherQueue");
173             Context ctx = admin.createInitialContext();
174             Queue anotherQueue = (Queue)ctx.lookup("anotherQueue");
175             assertTrue(anotherQueue != senderQueue);
176
177             // set the JMSDestination header field to the anotherQueue Destination
178
Message message = senderSession.createMessage();
179             message.setJMSDestination(anotherQueue);
180             sender.send(message);
181             assertTrue("3.4.1 When a message is sent this value is ignored.\n",
182                        message.getJMSDestination() != anotherQueue);
183             assertEquals("3.4.1 After completion of the send it holds the destination object specified " +
184                          "by the sending method.\n",
185                          senderQueue, message.getJMSDestination());
186
187             Message msg = receiver.receive(TestConfig.TIMEOUT);
188             assertEquals("3.4.1 When a message is received, its destination value must be equivalent " +
189                          " to the value assigned when it was sent.\n",
190                          ((Queue)message.getJMSDestination()).getQueueName(),
191                          ((Queue)msg.getJMSDestination()).getQueueName());
192
193             admin.deleteQueue("anotherQueue");
194         } catch (JMSException e) {
195             fail(e);
196         } catch (NamingException e) {
197             fail(e.getMessage());
198         }
199     }
200
201     /**
202      * Test that a <code>Destination</code> set by the <code>setJMSReplyTo()</code>
203      * method on a sended message corresponds to the <code>Destination</code> get by
204      * the </code>getJMSReplyTo()</code> method.
205      */

206     public void testJMSReplyTo_1() {
207         try {
208             Message message = senderSession.createMessage();
209             message.setJMSReplyTo(senderQueue);
210             sender.send(message);
211
212             Message msg = receiver.receive(TestConfig.TIMEOUT);
213             Destination dest = msg.getJMSReplyTo();
214             assertTrue("JMS ReplyTo header field should be a Queue",
215                        dest instanceof Queue);
216             Queue replyTo = (Queue) dest;
217             assertEquals("JMS ReplyTo header field should be equals to the sender queue",
218                          ((Queue)replyTo).getQueueName(), ((Queue)senderQueue).getQueueName());
219         } catch (JMSException e) {
220             fail(e);
221         }
222     }
223
224     /**
225      * Test that if the JMS ReplyTo header field has been set as a <code>TemporaryQueue</code>,
226      * it will be rightly get also as a <code>TemporaryQueue</code>
227      * (and not only as a <code>Queue</code>).
228      */

229     public void testJMSReplyTo_2() {
230         try {
231             TemporaryQueue tempQueue = senderSession.createTemporaryQueue();
232             Message message = senderSession.createMessage();
233             message.setJMSReplyTo(tempQueue);
234             sender.send(message);
235             
236             Message msg = receiver.receive(TestConfig.TIMEOUT);
237             Destination dest = msg.getJMSReplyTo();
238             assertTrue("JMS ReplyTo header field should be a TemporaryQueue",
239                        dest instanceof TemporaryQueue);
240             Queue replyTo = (Queue) dest;
241             assertEquals("JMS ReplyTo header field should be equals to the temporary queue",
242                          ((Queue)replyTo).getQueueName(), ((Queue)tempQueue).getQueueName());
243         } catch (JMSException e) {
244             fail(e);
245         }
246     }
247         
248     /**
249      * Method to use this class in a Test suite
250      */

251     public static Test suite() {
252         return new TestSuite(MessageHeaderTest.class);
253     }
254   
255     public MessageHeaderTest(String JavaDoc name) {
256         super(name);
257     }
258 }
259
260
Popular Tags