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/selector/SelectorTest.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.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.1 2002/04/21 21:15:19 chirino Exp $
37   */
38  public class SelectorTest extends PTPTestCase {
39  
40    /**
41     * Test the message selector using the filter example provided by the JMS specifications
42     * <br />
43     * <ul>
44     *  <li><code>"JMSType = 'car' AND color = 'blue' AND weight > 2500"</code></li>
45     * </ul>
46     */
47    public void testSelectorExampleFromSpecs() {
48      try {
49        receiverConnection.stop();
50        receiver  = receiverSession.createReceiver(receiverQueue, "JMSType = 'car' AND color = 'blue' AND weight > 2500");
51        receiverConnection.start();
52  
53        TextMessage dummyMessage = senderSession.createTextMessage();
54        dummyMessage.setJMSType("car");
55        dummyMessage.setStringProperty("color", "red");
56        dummyMessage.setLongProperty("weight", 3000);
57        dummyMessage.setText("testSelectorExampleFromSpecs:1");
58        sender.send(dummyMessage);
59        
60        TextMessage message = senderSession.createTextMessage();
61        message.setJMSType("car");
62        message.setStringProperty("color", "blue");
63        message.setLongProperty("weight", 3000);
64        message.setText("testSelectorExampleFromSpecs:2");
65        sender.send(message);
66  
67        TextMessage msg = (TextMessage)receiver.receive();
68        assertEquals("testSelectorExampleFromSpecs:2", msg.getText());
69      } catch (JMSException e) {
70         e.printStackTrace();
71        fail(e);
72      }
73    }
74  
75    /**
76     * Test the ">" condition in message selector
77     * <br />
78     * <ul>
79     *  <li><code>"weight > 2500"</code></li>
80     * </ul>
81     */
82    public void testGreaterThan() {
83      try {
84        receiverConnection.stop();
85        receiver  = receiverSession.createReceiver(receiverQueue, "weight > 2500");
86        receiverConnection.start();
87        
88        TextMessage dummyMessage = senderSession.createTextMessage();
89        dummyMessage.setLongProperty("weight", 1000);
90        dummyMessage.setText("testGreaterThan:1");
91        sender.send(dummyMessage);
92        
93        TextMessage message = senderSession.createTextMessage();
94        message.setLongProperty("weight", 3000);
95        message.setText("testGreaterThan:2");
96        sender.send(message);
97        
98        TextMessage msg = (TextMessage)receiver.receive();
99        assertEquals("testGreaterThan:2", msg.getText());
100     } catch (JMSException e) {
101       fail(e);
102     }
103   }
104 
105   /**
106    * Test the "=" condition in message selector
107    * <br />
108    * <ul>
109    *  <li><code>"weight > 2500"</code></li>
110    * </ul>
111    */
112    public void testEquals() {
113      try {
114        receiverConnection.stop();
115        receiver  = receiverSession.createReceiver(receiverQueue, "weight = 2500");
116        receiverConnection.start();
117    
118        TextMessage dummyMessage = senderSession.createTextMessage();
119        dummyMessage.setLongProperty("weight", 1000);
120        dummyMessage.setText("testEquals:1");
121        sender.send(dummyMessage);
122      
123        TextMessage message = senderSession.createTextMessage();
124        message.setLongProperty("weight", 2500);
125        message.setText("testEquals:2");
126        sender.send(message);
127        
128        TextMessage msg = (TextMessage)receiver.receive();
129        assertEquals("testEquals:2", msg.getText());
130      } catch (JMSException e) {
131        fail(e);
132      }
133    }
134 
135   /**
136    * Test the BETWEEN condition
137    * <br />
138    * "age BETWEEN 15 and 19" is <code>true</code> for 17 and <code>false</code> for 20
139    */
140   public void testBetween() {
141     try {
142       receiverConnection.stop();
143       receiver  = receiverSession.createReceiver(receiverQueue, "age BETWEEN 15 and 19");
144       receiverConnection.start();
145 
146       TextMessage dummyMessage = senderSession.createTextMessage();
147       dummyMessage.setIntProperty("age", 20);
148       dummyMessage.setText("testBetween:1");
149       sender.send(dummyMessage);
150 
151       TextMessage message = senderSession.createTextMessage();
152       message.setIntProperty("age", 17);
153       message.setText("testBetween:2");
154       sender.send(message);
155 
156       TextMessage msg = (TextMessage)receiver.receive();
157       assertTrue("Message not received",
158      msg != null);
159       assertTrue("Message of another test: "+ msg.getText(),
160      msg.getText().startsWith("testBetween"));
161       assertEquals("testBetween:2", msg.getText());
162       
163     } catch (JMSException e) {
164       fail(e);
165     }
166   }
167 
168   /**
169    * Test the IN condition
170    * <br />
171    * "Country IN ('UK', 'US', 'France')" is <code>true</code> for 'UK' and <code>false</code> for 'Peru'
172    */
173   public void testIn() {
174     try {
175       receiverConnection.stop();
176       receiver  = receiverSession.createReceiver(receiverQueue, "Country IN ('UK', 'US', 'France')");
177       receiverConnection.start();
178 
179       TextMessage dummyMessage = senderSession.createTextMessage();
180       dummyMessage.setStringProperty("Country", "Peru");
181       dummyMessage.setText("testIn:1");
182       sender.send(dummyMessage);
183   
184       TextMessage message = senderSession.createTextMessage();
185       message.setStringProperty("Country", "UK");
186       message.setText("testIn:2");
187       sender.send(message);
188   
189       TextMessage msg = (TextMessage)receiver.receive();
190       assertTrue("Message not received",
191      msg != null);
192       assertTrue("Message of another test: "+ msg.getText(),
193      msg.getText().startsWith("testIn"));
194       assertEquals("testIn:2", msg.getText());
195       
196     } catch (JMSException e) {
197       fail(e);
198     }
199   }
200   
201   /**
202    * Test the LIKE ... ESCAPE condition
203    * <br />
204    * "underscored LIKE '\_%' ESCAPE '\'" is <code>true</code> for '_foo' and <code>false</code> for 'bar'
205    */
206   public void testLikeEscape() {
207     try {
208       receiverConnection.stop();
209       receiver  = receiverSession.createReceiver(receiverQueue, "underscored LIKE '\\_%' ESCAPE '\\'");
210       receiverConnection.start();
211 
212       TextMessage dummyMessage = senderSession.createTextMessage();
213       dummyMessage.setStringProperty("underscored", "bar");
214       dummyMessage.setText("testLikeEscape:1");
215       sender.send(dummyMessage);
216 
217       TextMessage message = senderSession.createTextMessage();
218       message.setStringProperty("underscored", "_foo");
219       message.setText("testLikeEscape:2");
220       sender.send(message);
221 
222       TextMessage msg = (TextMessage)receiver.receive();
223       assertTrue("Message not received",
224      msg != null);
225       assertTrue("Message of another test: "+ msg.getText(),
226      msg.getText().startsWith("testLikeEscape"));
227       assertEquals("testLikeEscape:2", msg.getText());
228       
229     } catch (JMSException e) {
230       fail(e);
231     }
232    }
233 
234   /**
235    * Test the LIKE condition with '_' in the pattern
236    * <br />
237    * "word LIKE 'l_se'" is <code>true</code> for 'lose' and <code>false</code> for 'loose'
238    */
239   public void testLike_2() {
240     try {
241       receiverConnection.stop();
242       receiver  = receiverSession.createReceiver(receiverQueue, "word LIKE 'l_se'");
243       receiverConnection.start();
244 
245       TextMessage dummyMessage = senderSession.createTextMessage();
246       dummyMessage.setStringProperty("word", "loose");
247       dummyMessage.setText("testLike_2:1");
248       sender.send(dummyMessage);
249 
250       TextMessage message = senderSession.createTextMessage();
251       message.setStringProperty("word", "lose");
252       message.setText("testLike_2:2");
253       sender.send(message);
254 
255       TextMessage msg = (TextMessage)receiver.receive();
256       assertTrue("Message not received",
257      msg != null);
258       assertTrue("Message of another test: "+ msg.getText(),
259      msg.getText().startsWith("testLike_2"));
260       assertEquals("testLike_2:2", msg.getText());
261       
262     } catch (JMSException e) {
263       fail(e);
264     }
265    }
266 
267   /**
268    * Test the LIKE condition with '%' in the pattern
269    * <br />
270    * "phone LIKE '12%3'" is <code>true</code> for '12993' and <code>false</code> for '1234'
271    */
272   public void testLike_1() {
273     try {
274       receiverConnection.stop();
275       receiver  = receiverSession.createReceiver(receiverQueue, "phone LIKE '12%3'");
276       receiverConnection.start();
277 
278       TextMessage dummyMessage = senderSession.createTextMessage();
279       dummyMessage.setStringProperty("phone", "1234");
280       dummyMessage.setText("testLike_1:1");
281       sender.send(dummyMessage);
282 
283       TextMessage message = senderSession.createTextMessage();
284       message.setStringProperty("phone", "12993");
285       message.setText("testLike_1:2");
286       sender.send(message);
287 
288       TextMessage msg = (TextMessage)receiver.receive();
289       assertTrue("Message not received", msg != null);
290       assertTrue("Message of another test: "+ msg.getText(),
291      msg.getText().startsWith("testLike_1"));
292       assertEquals("testLike_1:2", msg.getText());
293       
294     } catch (JMSException e) {
295       fail(e);
296     }
297   }
298     
299   /**
300    * Test the <code>NULL</code> value in message selector
301    * <br />
302    * <ul>
303    *  <li><code>"prop IS NULL"</code></li>
304    * </ul>
305    */
306   public void testNull() {
307      try {
308        receiverConnection.stop();
309        receiver  = receiverSession.createReceiver(receiverQueue, "prop_name IS NULL");
310        receiverConnection.start();
311    
312        TextMessage dummyMessage = senderSession.createTextMessage();
313        dummyMessage.setStringProperty("prop_name", "not null");
314        dummyMessage.setText("testNull:1");
315        sender.send(dummyMessage);
316      
317        TextMessage message = senderSession.createTextMessage();
318        message.setText("testNull:2");
319        sender.send(message);
320        
321        TextMessage msg = (TextMessage)receiver.receive();
322        assertTrue(msg != null);
323        assertEquals("testNull:2", msg.getText());
324      } catch (JMSException e) {
325        fail(e);
326        }
327    }
328   
329   /** 
330    * Method to use this class in a Test suite
331    */
332   public static Test suite() {
333      return new TestSuite(SelectorTest.class);
334    }
335   
336   public SelectorTest(String name) {
337     super(name);
338   }
339 }
340