01: package com.mockrunner.test.jms;
02:
03: import javax.jms.JMSException;
04: import javax.jms.Message;
05:
06: import junit.framework.TestCase;
07:
08: import org.activemq.filter.mockrunner.Filter;
09: import org.activemq.selector.mockrunner.SelectorParser;
10:
11: import com.mockrunner.mock.jms.MockDestination;
12: import com.mockrunner.mock.jms.MockQueue;
13: import com.mockrunner.mock.jms.MockTextMessage;
14: import com.mockrunner.mock.jms.MockTopic;
15:
16: public class MockDestinationTest extends TestCase {
17: private Filter filter;
18:
19: protected void setUp() throws Exception {
20: super .setUp();
21: SelectorParser parser = new SelectorParser();
22: filter = parser.parse("number = 1");
23: }
24:
25: public void testGetMatchingMessagesFromQueue() throws Exception {
26: MockQueue queue = new MockQueue("Queue");
27: doTestGetMatchingMessage(queue);
28: }
29:
30: public void testGetMatchingMessagesFromTopic() throws Exception {
31: MockTopic topic = new MockTopic("Topic");
32: doTestGetMatchingMessage(topic);
33: }
34:
35: private void doTestGetMatchingMessage(MockDestination destination)
36: throws Exception {
37: MockTextMessage message1 = new MockTextMessage();
38: MockTextMessage message2 = new MockTextMessage();
39: MockTextMessage message3 = new MockTextMessage();
40: message2.setIntProperty("number", 1);
41: destination.addMessage(message1);
42: destination.addMessage(message2);
43: destination.addMessage(message3);
44: Message message = destination.getMatchingMessage(filter);
45: assertSame(message2, message);
46: assertEquals(2, destination.getCurrentMessageList().size());
47: message = destination.getMatchingMessage(new TestTrueFilter());
48: assertSame(message1, message);
49: assertEquals(1, destination.getCurrentMessageList().size());
50: message = destination.getMatchingMessage(new TestTrueFilter());
51: assertSame(message3, message);
52: assertEquals(0, destination.getCurrentMessageList().size());
53: assertTrue(destination.isEmpty());
54: message1 = new MockTextMessage();
55: message2 = new MockTextMessage();
56: message3 = new MockTextMessage();
57: destination.addMessage(message1);
58: destination.addMessage(message2);
59: destination.addMessage(message3);
60: message = destination.getMatchingMessage(new TestFalseFilter());
61: assertNull(message);
62: destination.getMessage();
63: destination.getMessage();
64: destination.getMessage();
65: message = destination.getMatchingMessage(new TestTrueFilter());
66: assertNull(message);
67: }
68:
69: private static class TestTrueFilter implements Filter {
70: public boolean matches(Message message) throws JMSException {
71: return true;
72: }
73:
74: public boolean isWildcard() {
75: return true;
76: }
77: }
78:
79: private static class TestFalseFilter implements Filter {
80: public boolean matches(Message message) throws JMSException {
81: return false;
82: }
83:
84: public boolean isWildcard() {
85: return true;
86: }
87: }
88: }
|