KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > selector > SelectorTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.selector;
19
20 import javax.jms.InvalidSelectorException JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.activemq.command.ActiveMQMessage;
27 import org.apache.activemq.command.ActiveMQTextMessage;
28 import org.apache.activemq.command.ActiveMQTopic;
29 import org.apache.activemq.filter.BooleanExpression;
30 import org.apache.activemq.filter.MessageEvaluationContext;
31 import org.apache.activemq.selector.SelectorParser;
32
33 /**
34  * @version $Revision: 1.7 $
35  */

36 public class SelectorTest extends TestCase {
37     
38     public void testBooleanSelector() throws Exception JavaDoc {
39         Message JavaDoc message = createMessage();
40
41         assertSelector(message, "(trueProp OR falseProp) AND trueProp", true);
42         assertSelector(message, "(trueProp OR falseProp) AND falseProp", false);
43         
44     }
45
46     public void testXPathSelectors() throws Exception JavaDoc {
47         ActiveMQTextMessage message = new ActiveMQTextMessage();
48         
49         message.setJMSType("xml");
50         message.setText("<root><a key='first'/><b key='second'/></root>");
51
52         assertSelector(message, "XPATH 'root/a'", true);
53         assertSelector(message, "XPATH '//root/b'", true);
54         assertSelector(message, "XPATH 'root/c'", false);
55
56         assertSelector(message, "XPATH '//root/*[@key=''second'']'", true);
57         assertSelector(message, "XPATH '//root/*[@key=''third'']'", false);
58
59         assertSelector(message, "XPATH '//root/a[@key=''first'']'", true);
60         assertSelector(message, "XPATH '//root/a[@key=''second'']'", false);
61     }
62
63     public void testJMSPropertySelectors() throws Exception JavaDoc {
64         Message JavaDoc message = createMessage();
65         message.setJMSType("selector-test");
66         message.setJMSMessageID("id:test:1:1:1:1");
67
68         assertSelector(message, "JMSType = 'selector-test'", true);
69         assertSelector(message, "JMSType = 'crap'", false);
70         
71         assertSelector(message, "JMSMessageID = 'id:test:1:1:1:1'", true);
72         assertSelector(message, "JMSMessageID = 'id:not-test:1:1:1:1'", false);
73         
74         message = createMessage();
75         message.setJMSType("1001");
76         
77         assertSelector(message, "JMSType='1001'", true);
78         assertSelector(message, "JMSType='1001' OR JMSType='1002'", true);
79         assertSelector(message, "JMSType = 'crap'", false);
80     }
81     
82     public void testBasicSelectors() throws Exception JavaDoc {
83         Message JavaDoc message = createMessage();
84
85         assertSelector(message, "name = 'James'", true);
86         assertSelector(message, "rank > 100", true);
87         assertSelector(message, "rank >= 123", true);
88         assertSelector(message, "rank >= 124", false);
89         
90     }
91
92     public void testPropertyTypes() throws Exception JavaDoc {
93         Message JavaDoc message = createMessage();
94         assertSelector(message, "byteProp = 123", true);
95         assertSelector(message, "byteProp = 10", false);
96         assertSelector(message, "byteProp2 = 33", true);
97         assertSelector(message, "byteProp2 = 10", false);
98
99         assertSelector(message, "shortProp = 123", true);
100         assertSelector(message, "shortProp = 10", false);
101
102
103         assertSelector(message, "shortProp = 123", true);
104         assertSelector(message, "shortProp = 10", false);
105
106         assertSelector(message, "intProp = 123", true);
107         assertSelector(message, "intProp = 10", false);
108
109         assertSelector(message, "longProp = 123", true);
110         assertSelector(message, "longProp = 10", false);
111
112         assertSelector(message, "floatProp = 123", true);
113         assertSelector(message, "floatProp = 10", false);
114
115         assertSelector(message, "doubleProp = 123", true);
116         assertSelector(message, "doubleProp = 10", false);
117     }
118     public void testAndSelectors() throws Exception JavaDoc {
119         Message JavaDoc message = createMessage();
120
121         assertSelector(message, "name = 'James' and rank < 200", true);
122         assertSelector(message, "name = 'James' and rank > 200", false);
123         assertSelector(message, "name = 'Foo' and rank < 200", false);
124         assertSelector(message, "unknown = 'Foo' and anotherUnknown < 200", false);
125     }
126
127     public void testOrSelectors() throws Exception JavaDoc {
128         Message JavaDoc message = createMessage();
129
130         assertSelector(message, "name = 'James' or rank < 200", true);
131         assertSelector(message, "name = 'James' or rank > 200", true);
132         assertSelector(message, "name = 'Foo' or rank < 200", true);
133         assertSelector(message, "name = 'Foo' or rank > 200", false);
134         assertSelector(message, "unknown = 'Foo' or anotherUnknown < 200", false);
135     }
136
137     public void testPlus() throws Exception JavaDoc {
138         Message JavaDoc message = createMessage();
139
140         assertSelector(message, "rank + 2 = 125", true);
141         assertSelector(message, "(rank + 2) = 125", true);
142         assertSelector(message, "125 = (rank + 2)", true);
143         assertSelector(message, "rank + version = 125", true);
144         assertSelector(message, "rank + 2 < 124", false);
145         assertSelector(message, "name + '!' = 'James!'", true);
146     }
147
148     public void testMinus() throws Exception JavaDoc {
149         Message JavaDoc message = createMessage();
150
151         assertSelector(message, "rank - 2 = 121", true);
152         assertSelector(message, "rank - version = 121", true);
153         assertSelector(message, "rank - 2 > 122", false);
154     }
155
156     public void testMultiply() throws Exception JavaDoc {
157         Message JavaDoc message = createMessage();
158
159         assertSelector(message, "rank * 2 = 246", true);
160         assertSelector(message, "rank * version = 246", true);
161         assertSelector(message, "rank * 2 < 130", false);
162     }
163
164     public void testDivide() throws Exception JavaDoc {
165         Message JavaDoc message = createMessage();
166
167         assertSelector(message, "rank / version = 61.5", true);
168         assertSelector(message, "rank / 3 > 100.0", false);
169         assertSelector(message, "rank / 3 > 100", false);
170         assertSelector(message, "version / 2 = 1", true);
171
172     }
173
174     public void testBetween() throws Exception JavaDoc {
175         Message JavaDoc message = createMessage();
176
177         assertSelector(message, "rank between 100 and 150", true);
178         assertSelector(message, "rank between 10 and 120", false);
179     }
180
181     public void testIn() throws Exception JavaDoc {
182         Message JavaDoc message = createMessage();
183
184         assertSelector(message, "name in ('James', 'Bob', 'Gromit')", true);
185         assertSelector(message, "name in ('Bob', 'James', 'Gromit')", true);
186         assertSelector(message, "name in ('Gromit', 'Bob', 'James')", true);
187
188         assertSelector(message, "name in ('Gromit', 'Bob', 'Cheddar')", false);
189         assertSelector(message, "name not in ('Gromit', 'Bob', 'Cheddar')", true);
190     }
191
192     public void testIsNull() throws Exception JavaDoc {
193         Message JavaDoc message = createMessage();
194
195         assertSelector(message, "dummy is null", true);
196         assertSelector(message, "dummy is not null", false);
197         assertSelector(message, "name is not null", true);
198         assertSelector(message, "name is null", false);
199     }
200
201
202     public void testLike() throws Exception JavaDoc {
203         Message JavaDoc message = createMessage();
204         message.setStringProperty("modelClassId", "com.whatever.something.foo.bar");
205         message.setStringProperty("modelInstanceId", "170");
206         message.setStringProperty("modelRequestError", "abc");
207         message.setStringProperty("modelCorrelatedClientId", "whatever");
208
209         assertSelector(message, "modelClassId LIKE 'com.whatever.something.%' AND modelInstanceId = '170' AND (modelRequestError IS NULL OR modelCorrelatedClientId = 'whatever')", true);
210
211         message.setStringProperty("modelCorrelatedClientId", "shouldFailNow");
212
213         assertSelector(message, "modelClassId LIKE 'com.whatever.something.%' AND modelInstanceId = '170' AND (modelRequestError IS NULL OR modelCorrelatedClientId = 'whatever')", false);
214
215         message = createMessage();
216         message.setStringProperty("modelClassId", "com.whatever.something.foo.bar");
217         message.setStringProperty("modelInstanceId", "170");
218         message.setStringProperty("modelCorrelatedClientId", "shouldNotMatch");
219
220         assertSelector(message, "modelClassId LIKE 'com.whatever.something.%' AND modelInstanceId = '170' AND (modelRequestError IS NULL OR modelCorrelatedClientId = 'whatever')", true);
221     }
222
223     /**
224      * Test cases from Mats Henricson
225      */

226     public void testMatsHenricsonUseCases() throws Exception JavaDoc {
227         Message JavaDoc message = createMessage();
228         assertSelector(message, "SessionserverId=1870414179", false);
229
230         message.setLongProperty("SessionserverId", 1870414179);
231         assertSelector(message, "SessionserverId=1870414179", true);
232
233         message.setLongProperty("SessionserverId", 1234);
234         assertSelector(message, "SessionserverId=1870414179", false);
235
236
237         assertSelector(message, "Command NOT IN ('MirrorLobbyRequest', 'MirrorLobbyReply')", false);
238
239         message.setStringProperty("Command", "Cheese");
240         assertSelector(message, "Command NOT IN ('MirrorLobbyRequest', 'MirrorLobbyReply')", true);
241
242         message.setStringProperty("Command", "MirrorLobbyRequest");
243         assertSelector(message, "Command NOT IN ('MirrorLobbyRequest', 'MirrorLobbyReply')", false);
244         message.setStringProperty("Command", "MirrorLobbyReply");
245         assertSelector(message, "Command NOT IN ('MirrorLobbyRequest', 'MirrorLobbyReply')", false);
246     }
247
248     public void testFloatComparisons() throws Exception JavaDoc {
249         Message JavaDoc message = createMessage();
250         
251         // JMS 1.1 Section 3.8.1.1 : Approximate literals use the Java floating-point literal syntax.
252
// We will use the java varible x to demo valid floating point syntaxs.
253
double x;
254         
255         // test decimals like x.x
256
x = 1.0;
257         x = -1.1;
258         x = 1.0E1;
259         x = 1.1E1;
260         x = -1.1E1;
261         assertSelector(message, "1.0 < 1.1", true);
262         assertSelector(message, "-1.1 < 1.0", true);
263         assertSelector(message, "1.0E1 < 1.1E1", true);
264         assertSelector(message, "-1.1E1 < 1.0E1", true);
265
266         // test decimals like x.
267
x = 1.;
268         x = 1.E1;
269         assertSelector(message, "1. < 1.1", true);
270         assertSelector(message, "-1.1 < 1.", true);
271         assertSelector(message, "1.E1 < 1.1E1", true);
272         assertSelector(message, "-1.1E1 < 1.E1", true);
273         
274         // test decimals like .x
275
x = .5;
276         x = -.5;
277         x = .5E1;
278         assertSelector(message, ".1 < .5", true);
279         assertSelector(message, "-.5 < .1", true);
280         assertSelector(message, ".1E1 < .5E1", true);
281         assertSelector(message, "-.5E1 < .1E1", true);
282         
283         // test exponents
284
x = 4E10;
285         x = -4E10;
286         x = 5E+10;
287         x = 5E-10;
288         assertSelector(message, "4E10 < 5E10", true);
289         assertSelector(message, "5E8 < 5E10", true);
290         assertSelector(message, "-4E10 < 2E10", true);
291         assertSelector(message, "-5E8 < 2E2", true);
292         assertSelector(message, "4E+10 < 5E+10", true);
293         assertSelector(message, "4E-10 < 5E-10", true);
294     }
295
296     public void testStringQuoteParsing() throws Exception JavaDoc {
297         Message JavaDoc message = createMessage();
298         assertSelector(message, "quote = '''In God We Trust'''", true);
299     }
300
301     public void testLikeComparisons() throws Exception JavaDoc {
302         Message JavaDoc message = createMessage();
303
304         assertSelector(message, "quote LIKE '''In G_d We Trust'''", true);
305         assertSelector(message, "quote LIKE '''In Gd_ We Trust'''", false);
306         assertSelector(message, "quote NOT LIKE '''In G_d We Trust'''", false);
307         assertSelector(message, "quote NOT LIKE '''In Gd_ We Trust'''", true);
308
309         assertSelector(message, "foo LIKE '%oo'", true);
310         assertSelector(message, "foo LIKE '%ar'", false);
311         assertSelector(message, "foo NOT LIKE '%oo'", false);
312         assertSelector(message, "foo NOT LIKE '%ar'", true);
313
314         assertSelector(message, "foo LIKE '!_%' ESCAPE '!'", true);
315         assertSelector(message, "quote LIKE '!_%' ESCAPE '!'", false);
316         assertSelector(message, "foo NOT LIKE '!_%' ESCAPE '!'", false);
317         assertSelector(message, "quote NOT LIKE '!_%' ESCAPE '!'", true);
318
319         assertSelector(message, "punctuation LIKE '!#$&()*+,-./:;<=>?@[\\]^`{|}~'", true);
320     }
321
322     public void testInvalidSelector() throws Exception JavaDoc {
323         Message JavaDoc message = createMessage();
324         assertInvalidSelector(message, "3+5");
325         assertInvalidSelector(message, "True AND 3+5");
326         assertInvalidSelector(message, "=TEST 'test'");
327     }
328
329     protected Message JavaDoc createMessage() throws JMSException JavaDoc {
330         Message JavaDoc message = createMessage("FOO.BAR");
331         message.setJMSType("selector-test");
332         message.setJMSMessageID("connection:1:1:1:1");
333         message.setObjectProperty("name", "James");
334         message.setObjectProperty("location", "London");
335         
336         message.setByteProperty("byteProp", (byte)123);
337         message.setByteProperty("byteProp2", (byte) 33);
338         message.setShortProperty("shortProp", (short)123);
339         message.setIntProperty("intProp", (int)123);
340         message.setLongProperty("longProp", (long)123);
341         message.setFloatProperty("floatProp", (float)123);
342         message.setDoubleProperty("doubleProp", (double)123);
343
344         message.setIntProperty("rank", 123);
345         message.setIntProperty("version", 2);
346         message.setStringProperty("quote", "'In God We Trust'");
347         message.setStringProperty("foo", "_foo");
348         message.setStringProperty("punctuation", "!#$&()*+,-./:;<=>?@[\\]^`{|}~");
349         message.setBooleanProperty("trueProp", true);
350         message.setBooleanProperty("falseProp", false);
351         return message;
352     }
353
354
355     protected void assertInvalidSelector(Message JavaDoc message, String JavaDoc text) throws JMSException JavaDoc {
356         try {
357             new SelectorParser().parse(text);
358             fail("Created a valid selector");
359         }
360         catch (InvalidSelectorException JavaDoc e) {
361         }
362     }
363
364     protected void assertSelector(Message JavaDoc message, String JavaDoc text, boolean expected) throws JMSException JavaDoc {
365         BooleanExpression selector = new SelectorParser().parse(text);
366         assertTrue("Created a valid selector", selector != null);
367         MessageEvaluationContext context = new MessageEvaluationContext();
368         context.setMessageReference((org.apache.activemq.command.Message)message);
369         boolean value = selector.matches(context);
370         assertEquals("Selector for: " + text, expected, value);
371     }
372
373     protected Message JavaDoc createMessage(String JavaDoc subject) throws JMSException JavaDoc {
374         ActiveMQMessage message = new ActiveMQMessage();
375         message.setJMSDestination(new ActiveMQTopic(subject));
376         return message;
377     }
378 }
379
Popular Tags