KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jtests > jms > conform > selector > SelectorTest


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.selector;
26
27 import org.objectweb.jtests.jms.framework.*;
28 import junit.framework.*;
29
30 import javax.jms.*;
31
32 /**
33  * Test the message selector features of JMS
34  *
35  * @author Jeff Mesnil (jmesnil@inrialpes.fr)
36  * @version $Id: SelectorTest.java,v 1.7 2002/06/27 08:34:01 jmesnil Exp $
37  */

38 public class SelectorTest extends PTPTestCase {
39
40     /**
41      * Test that an empty string as a message selector indicates that there
42      * is no message selector for the message consumer.
43      */

44     public void testEmptyStringAsSelector() {
45         try {
46             receiverConnection.stop();
47             receiver = receiverSession.createReceiver(receiverQueue, "");
48             receiverConnection.start();
49
50             TextMessage message = senderSession.createTextMessage();
51             message.setText("testEmptyStringAsSelector");
52             sender.send(message);
53
54             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
55             assertTrue("No message was received", msg != null);
56             assertEquals("testEmptyStringAsSelector", msg.getText());
57         } catch (JMSException e) {
58             fail(e);
59         }
60     }
61
62     /**
63      * Tats that String literals are well handled by the message selector.
64      * <br />
65      * <ul>
66      * <li><code>"string = 'literal''s;"</code> is <code>true</code> for "literal's" and <code>false</code> for "literal"</li>
67      * </ul>
68      */

69
70     public void testStringLiterals() {
71         try {
72             receiverConnection.stop();
73             receiver = receiverSession.createReceiver(receiverQueue, "string = 'literal''s'");
74             receiverConnection.start();
75             
76             TextMessage dummyMessage = senderSession.createTextMessage();
77             dummyMessage.setStringProperty("string", "literal");
78             dummyMessage.setText("testStringLiterals:1");
79             sender.send(dummyMessage);
80             
81             TextMessage message = senderSession.createTextMessage();
82             message.setStringProperty("string", "literal's");
83             message.setText("testStringLiterals:2");
84             sender.send(message);
85
86             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
87             assertTrue("No message was received", msg != null);
88             assertEquals("testStringLiterals:2", msg.getText());
89         } catch (JMSException e) {
90             fail(e);
91         }
92     }
93
94     /**
95      * Test that the JMS property <code>JMSDeliveryMode</code> is treated as having the values <code>'PERSISTENT'</code>
96      * or <code>'NON_PERSISTENT'</code> when used in a message selector (chapter 3.8.1.3).
97      */

98     public void testJMSDeliveryModeInSelector() {
99         try {
100             receiverConnection.stop();
101             receiver = receiverSession.createReceiver(receiverQueue, "JMSDeliveryMode = 'PERSISTENT'");
102             receiverConnection.start();
103          
104             TextMessage dummyMessage = senderSession.createTextMessage();
105             dummyMessage.setText("testJMSDeliveryModeInSelector:1");
106             // send a dummy message in *non persistent* mode
107
sender.send(dummyMessage, DeliveryMode.NON_PERSISTENT, sender.getPriority(), sender.getTimeToLive());
108    
109             TextMessage message = senderSession.createTextMessage();
110             message.setText("testJMSDeliveryModeInSelector:2");
111             // send a message in *persistent*
112
sender.send(message, DeliveryMode.PERSISTENT, sender.getPriority(), sender.getTimeToLive());
113          
114             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
115             assertTrue("No message was received", msg != null);
116             // only the message sent in persistent mode should be received.
117
assertEquals(DeliveryMode.PERSISTENT, msg.getJMSDeliveryMode());
118             assertEquals("testJMSDeliveryModeInSelector:2", msg.getText());
119         } catch (JMSException e) {
120             fail(e);
121         }
122     }
123
124     /**
125      * Test that conversions that apply to the <code>get</code> methods for properties do not
126      * apply when a property is used in a message selector expression.
127      * Based on the example of chapter 3.8.1.1 about identifiers.
128      */

129     public void testIdentifierConversion() {
130         try {
131             receiverConnection.stop();
132             receiver = receiverSession.createReceiver(receiverQueue, "NumberOfOrders > 1");
133             receiverConnection.start();
134
135             TextMessage dummyMessage = senderSession.createTextMessage();
136             dummyMessage.setStringProperty("NumberOfOrders", "2");
137             dummyMessage.setText("testIdentifierConversion:1");
138             sender.send(dummyMessage);
139    
140             TextMessage message = senderSession.createTextMessage();
141             message.setIntProperty("NumberOfOrders", 2);
142             message.setText("testIdentifierConversion:2");
143             sender.send(message);
144
145             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
146             assertEquals("testIdentifierConversion:2", msg.getText());
147         } catch (JMSException e) {
148             fail(e);
149         }
150     }
151  
152     /**
153      * Test the message selector using the filter example provided by the JMS specifications.
154      * <br />
155      * <ul>
156      * <li><code>"JMSType = 'car' AND color = 'blue' AND weight > 2500"</code></li>
157      * </ul>
158      */

159     public void testSelectorExampleFromSpecs() {
160         try {
161             receiverConnection.stop();
162             receiver = receiverSession.createReceiver(receiverQueue, "JMSType = 'car' AND color = 'blue' AND weight > 2500");
163             receiverConnection.start();
164
165             TextMessage dummyMessage = senderSession.createTextMessage();
166             dummyMessage.setJMSType("car");
167             dummyMessage.setStringProperty("color", "red");
168             dummyMessage.setLongProperty("weight", 3000);
169             dummyMessage.setText("testSelectorExampleFromSpecs:1");
170             sender.send(dummyMessage);
171    
172             TextMessage message = senderSession.createTextMessage();
173             message.setJMSType("car");
174             message.setStringProperty("color", "blue");
175             message.setLongProperty("weight", 3000);
176             message.setText("testSelectorExampleFromSpecs:2");
177             sender.send(message);
178
179             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
180             assertEquals("testSelectorExampleFromSpecs:2", msg.getText());
181         } catch (JMSException e) {
182             fail(e);
183         }
184     }
185
186     /**
187      * Test the ">" condition in message selector.
188      * <br />
189      * <ul>
190      * <li><code>"weight > 2500"</code> is <code>true</code> for 3000 and <code>false</code> for 1000</li>
191      * </ul>
192      */

193     public void testGreaterThan() {
194         try {
195             receiverConnection.stop();
196             receiver = receiverSession.createReceiver(receiverQueue, "weight > 2500");
197             receiverConnection.start();
198    
199             TextMessage dummyMessage = senderSession.createTextMessage();
200             dummyMessage.setLongProperty("weight", 1000);
201             dummyMessage.setText("testGreaterThan:1");
202             sender.send(dummyMessage);
203    
204             TextMessage message = senderSession.createTextMessage();
205             message.setLongProperty("weight", 3000);
206             message.setText("testGreaterThan:2");
207             sender.send(message);
208    
209             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
210             assertEquals("testGreaterThan:2", msg.getText());
211         } catch (JMSException e) {
212             fail(e);
213         }
214     }
215
216     /**
217      * Test the "=" condition in message selector.
218      * <br />
219      * <ul>
220      * <li><code>"weight > 2500"</code> is <code>true</code> for 2500 and <code>false</code> for 1000</li>
221      * </ul>
222      */

223     public void testEquals() {
224         try {
225             receiverConnection.stop();
226             receiver = receiverSession.createReceiver(receiverQueue, "weight = 2500");
227             receiverConnection.start();
228
229             TextMessage dummyMessage = senderSession.createTextMessage();
230             dummyMessage.setLongProperty("weight", 1000);
231             dummyMessage.setText("testEquals:1");
232             sender.send(dummyMessage);
233   
234             TextMessage message = senderSession.createTextMessage();
235             message.setLongProperty("weight", 2500);
236             message.setText("testEquals:2");
237             sender.send(message);
238     
239             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
240             assertEquals("testEquals:2", msg.getText());
241         } catch (JMSException e) {
242             fail(e);
243         }
244     }
245
246     /**
247      * Test the "<>" (not equal) condition in message selector.
248      * <br />
249      * <ul>
250      * <li><code>"weight <> 2500"</code> is <code>true</code> for 1000 and <code>false</code> for 2500</li>
251      * </ul>
252      */

253     public void testNotEquals() {
254         try {
255             receiverConnection.stop();
256             receiver = receiverSession.createReceiver(receiverQueue, "weight <> 2500");
257             receiverConnection.start();
258
259             TextMessage dummyMessage = senderSession.createTextMessage();
260             dummyMessage.setLongProperty("weight", 2500);
261             dummyMessage.setText("testEquals:1");
262             sender.send(dummyMessage);
263   
264             TextMessage message = senderSession.createTextMessage();
265             message.setLongProperty("weight", 1000);
266             message.setText("testEquals:2");
267             sender.send(message);
268     
269             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
270             assertEquals("testEquals:2", msg.getText());
271         } catch (JMSException e) {
272             fail(e);
273         }
274     }
275
276
277     /**
278      * Test the BETWEEN condition in message selector.
279      * <br />
280      * <ul>
281      * <li>"age BETWEEN 15 and 19" is <code>true</code> for 17 and <code>false</code> for 20</li>
282      * </ul>
283      */

284     public void testBetween() {
285         try {
286             receiverConnection.stop();
287             receiver = receiverSession.createReceiver(receiverQueue, "age BETWEEN 15 and 19");
288             receiverConnection.start();
289
290             TextMessage dummyMessage = senderSession.createTextMessage();
291             dummyMessage.setIntProperty("age", 20);
292             dummyMessage.setText("testBetween:1");
293             sender.send(dummyMessage);
294
295             TextMessage message = senderSession.createTextMessage();
296             message.setIntProperty("age", 17);
297             message.setText("testBetween:2");
298             sender.send(message);
299
300             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
301             assertTrue("Message not received",
302                        msg != null);
303             assertTrue("Message of another test: "+ msg.getText(),
304                        msg.getText().startsWith("testBetween"));
305             assertEquals("testBetween:2", msg.getText());
306    
307         } catch (JMSException e) {
308             fail(e);
309         }
310     }
311
312     /**
313      * Test the IN condition in message selector.
314      * <br />
315      * <ul>
316      * <li>"Country IN ('UK', 'US', 'France')" is <code>true</code> for 'UK' and <code>false</code> for 'Peru'</li>
317      * </ul>
318      */

319     public void testIn() {
320         try {
321             receiverConnection.stop();
322             receiver = receiverSession.createReceiver(receiverQueue, "Country IN ('UK', 'US', 'France')");
323             receiverConnection.start();
324
325             TextMessage dummyMessage = senderSession.createTextMessage();
326             dummyMessage.setStringProperty("Country", "Peru");
327             dummyMessage.setText("testIn:1");
328             sender.send(dummyMessage);
329
330             TextMessage message = senderSession.createTextMessage();
331             message.setStringProperty("Country", "UK");
332             message.setText("testIn:2");
333             sender.send(message);
334
335             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
336             assertTrue("Message not received",
337                        msg != null);
338             assertTrue("Message of another test: "+ msg.getText(),
339                        msg.getText().startsWith("testIn"));
340             assertEquals("testIn:2", msg.getText());
341    
342         } catch (JMSException e) {
343             fail(e);
344         }
345     }
346
347     /**
348      * Test the LIKE ... ESCAPE condition in message selector
349      * <br />
350      * <ul>
351      * <li>"underscored LIKE '\_%' ESCAPE '\'" is <code>true</code> for '_foo' and <code>false</code> for 'bar'</li>
352      * </ul>
353      */

354     public void testLikeEscape() {
355         try {
356             receiverConnection.stop();
357             receiver = receiverSession.createReceiver(receiverQueue, "underscored LIKE '\\_%' ESCAPE '\\'");
358             receiverConnection.start();
359
360             TextMessage dummyMessage = senderSession.createTextMessage();
361             dummyMessage.setStringProperty("underscored", "bar");
362             dummyMessage.setText("testLikeEscape:1");
363             sender.send(dummyMessage);
364
365             TextMessage message = senderSession.createTextMessage();
366             message.setStringProperty("underscored", "_foo");
367             message.setText("testLikeEscape:2");
368             sender.send(message);
369
370             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
371             assertTrue("Message not received",
372                        msg != null);
373             assertTrue("Message of another test: "+ msg.getText(),
374                        msg.getText().startsWith("testLikeEscape"));
375             assertEquals("testLikeEscape:2", msg.getText());
376    
377         } catch (JMSException e) {
378             fail(e);
379         }
380     }
381
382     /**
383      * Test the LIKE condition with '_' in the pattern.
384      * <br />
385      * <ul>
386      * <li>"word LIKE 'l_se'" is <code>true</code> for 'lose' and <code>false</code> for 'loose'</li>
387      * </ul>
388      */

389     public void testLike_2() {
390         try {
391             receiverConnection.stop();
392             receiver = receiverSession.createReceiver(receiverQueue, "word LIKE 'l_se'");
393             receiverConnection.start();
394
395             TextMessage dummyMessage = senderSession.createTextMessage();
396             dummyMessage.setStringProperty("word", "loose");
397             dummyMessage.setText("testLike_2:1");
398             sender.send(dummyMessage);
399
400             TextMessage message = senderSession.createTextMessage();
401             message.setStringProperty("word", "lose");
402             message.setText("testLike_2:2");
403             sender.send(message);
404
405             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
406             assertTrue("Message not received",
407                        msg != null);
408             assertTrue("Message of another test: "+ msg.getText(),
409                        msg.getText().startsWith("testLike_2"));
410             assertEquals("testLike_2:2", msg.getText());
411    
412         } catch (JMSException e) {
413             fail(e);
414         }
415     }
416
417     /**
418      * Test the LIKE condition with '%' in the pattern.
419      * <br />
420      * <ul>
421      * <li>"phone LIKE '12%3'" is <code>true</code> for '12993' and <code>false</code> for '1234'</li>
422      * </ul>
423      */

424     public void testLike_1() {
425         try {
426             receiverConnection.stop();
427             receiver = receiverSession.createReceiver(receiverQueue, "phone LIKE '12%3'");
428             receiverConnection.start();
429
430             TextMessage dummyMessage = senderSession.createTextMessage();
431             dummyMessage.setStringProperty("phone", "1234");
432             dummyMessage.setText("testLike_1:1");
433             sender.send(dummyMessage);
434
435             TextMessage message = senderSession.createTextMessage();
436             message.setStringProperty("phone", "12993");
437             message.setText("testLike_1:2");
438             sender.send(message);
439
440             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
441             assertTrue("Message not received", msg != null);
442             assertTrue("Message of another test: "+ msg.getText(),
443                        msg.getText().startsWith("testLike_1"));
444             assertEquals("testLike_1:2", msg.getText());
445
446         } catch (JMSException e) {
447             fail(e);
448         }
449     }
450
451     /**
452      * Test the <code>NULL</code> value in message selector.
453      * <br />
454      * <ul>
455      * <li><code>"prop IS NULL"</code></li>
456      * </ul>
457      */

458     public void testNull() {
459         try {
460             receiverConnection.stop();
461             receiver = receiverSession.createReceiver(receiverQueue, "prop_name IS NULL");
462             receiverConnection.start();
463
464             TextMessage dummyMessage = senderSession.createTextMessage();
465             dummyMessage.setStringProperty("prop_name", "not null");
466             dummyMessage.setText("testNull:1");
467             sender.send(dummyMessage);
468
469             TextMessage message = senderSession.createTextMessage();
470             message.setText("testNull:2");
471             sender.send(message);
472  
473             TextMessage msg = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
474             assertTrue(msg != null);
475             assertEquals("testNull:2", msg.getText());
476         } catch (JMSException e) {
477             fail(e);
478         }
479     }
480   
481     /**
482      * Method to use this class in a Test suite
483      */

484     public static Test suite() {
485         return new TestSuite(SelectorTest.class);
486     }
487   
488     public SelectorTest(String JavaDoc name) {
489         super(name);
490     }
491 }
492
493
Popular Tags