01: package com.mockrunner.test.jms;
02:
03: import org.activemq.filter.mockrunner.Filter;
04: import org.activemq.selector.mockrunner.SelectorParser;
05:
06: import com.mockrunner.mock.jms.MockTextMessage;
07:
08: import junit.framework.TestCase;
09:
10: public class MessageSelectorTest extends TestCase {
11: private SelectorParser parser;
12: private MockTextMessage message;
13:
14: protected void setUp() throws Exception {
15: super .setUp();
16: parser = new SelectorParser();
17: message = new MockTextMessage();
18: }
19:
20: public void testSimpleExpression() throws Exception {
21: Filter intPropertyFilter = parser.parse("intProperty > 1");
22: assertFalse(intPropertyFilter.matches(message));
23: message.setIntProperty("intProperty", 3);
24: assertTrue(intPropertyFilter.matches(message));
25: Filter correlationIDFilter = parser
26: .parse("JMSCorrelationID LIKE '12%4'");
27: message.setJMSCorrelationID("12");
28: assertFalse(correlationIDFilter.matches(message));
29: message.setJMSCorrelationID("12334");
30: assertTrue(correlationIDFilter.matches(message));
31: message.setJMSCorrelationID("124");
32: assertTrue(correlationIDFilter.matches(message));
33: Filter stringPropertyFilter = parser
34: .parse("stringProperty NOT LIKE 'tes_t'");
35: message.setStringProperty("stringProperty", "te");
36: assertTrue(stringPropertyFilter.matches(message));
37: message.setStringProperty("stringProperty", "test");
38: assertTrue(stringPropertyFilter.matches(message));
39: message.setStringProperty("stringProperty", "tesst");
40: assertFalse(stringPropertyFilter.matches(message));
41: message.setStringProperty("stringProperty", "tessst");
42: assertTrue(stringPropertyFilter.matches(message));
43: }
44:
45: public void testComplexExpression() throws Exception {
46: Filter complexFilter = parser
47: .parse("((JMSMessageID IS NOT NULL) AND (number BETWEEN 1 AND 8) AND (text IN ('1', '2', '3'))) OR (JMSCorrelationID = '42')");
48: assertFalse(complexFilter.matches(message));
49: message.setJMSMessageID("test");
50: assertFalse(complexFilter.matches(message));
51: message.setIntProperty("number", 2);
52: assertFalse(complexFilter.matches(message));
53: message.setStringProperty("text", "3");
54: assertTrue(complexFilter.matches(message));
55: message.setStringProperty("text", "4");
56: assertFalse(complexFilter.matches(message));
57: message.setJMSCorrelationID("42");
58: assertTrue(complexFilter.matches(message));
59: message.setStringProperty("text", "1");
60: assertTrue(complexFilter.matches(message));
61: message.setJMSCorrelationID("41");
62: assertTrue(complexFilter.matches(message));
63: message.setIntProperty("number", 0);
64: assertFalse(complexFilter.matches(message));
65: }
66:
67: public void testWildcardExpression() throws Exception {
68: Filter wildcardFilter = parser
69: .parse("stringProperty LIKE '__%XYZ'");
70: message.setStringProperty("stringProperty", "");
71: assertFalse(wildcardFilter.matches(message));
72: message.setStringProperty("stringProperty", "XYZ");
73: assertFalse(wildcardFilter.matches(message));
74: message.setStringProperty("stringProperty", "12XYZ");
75: assertTrue(wildcardFilter.matches(message));
76: message.setStringProperty("stringProperty", "cc1234567XYZ");
77: assertTrue(wildcardFilter.matches(message));
78: message.setStringProperty("stringProperty", "1XYZ");
79: assertFalse(wildcardFilter.matches(message));
80: wildcardFilter = parser.parse("stringProperty LIKE '__xyz'");
81: message.setStringProperty("stringProperty", "xyz");
82: assertFalse(wildcardFilter.matches(message));
83: message.setStringProperty("stringProperty", "\nbxyz");
84: assertTrue(wildcardFilter.matches(message));
85: }
86: }
|