001: package com.mockrunner.test.jms;
002:
003: import javax.jms.JMSException;
004: import javax.jms.Message;
005: import javax.jms.MessageListener;
006: import javax.jms.Session;
007:
008: import junit.framework.TestCase;
009:
010: import com.mockrunner.jms.ConfigurationManager;
011: import com.mockrunner.jms.DestinationManager;
012: import com.mockrunner.mock.jms.MockMapMessage;
013: import com.mockrunner.mock.jms.MockMessageConsumer;
014: import com.mockrunner.mock.jms.MockObjectMessage;
015: import com.mockrunner.mock.jms.MockQueue;
016: import com.mockrunner.mock.jms.MockQueueConnection;
017: import com.mockrunner.mock.jms.MockQueueReceiver;
018: import com.mockrunner.mock.jms.MockQueueSession;
019: import com.mockrunner.mock.jms.MockTopic;
020: import com.mockrunner.mock.jms.MockTopicConnection;
021: import com.mockrunner.mock.jms.MockTopicSession;
022: import com.mockrunner.mock.jms.MockTopicSubscriber;
023:
024: public class MockMessageConsumerTest extends TestCase {
025: private MockQueue queue;
026: private MockTopic topic;
027: private MockQueueConnection queueConnection;
028: private MockTopicConnection topicConnection;
029: private MockQueueSession queueSession;
030: private MockTopicSession topicSession;
031:
032: protected void setUp() throws Exception {
033: super .setUp();
034: queue = new MockQueue("Queue");
035: topic = new MockTopic("Topic");
036: DestinationManager destManager = new DestinationManager();
037: ConfigurationManager confManager = new ConfigurationManager();
038: queueConnection = new MockQueueConnection(destManager,
039: confManager);
040: topicConnection = new MockTopicConnection(destManager,
041: confManager);
042: queueSession = (MockQueueSession) queueConnection
043: .createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
044: topicSession = (MockTopicSession) topicConnection
045: .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
046: }
047:
048: public void testCreateSelector() throws Exception {
049: MockQueueReceiver receiver = new MockQueueReceiver(
050: queueConnection, queueSession, queue, "");
051: assertEquals("", receiver.getMessageSelector());
052: try {
053: receiver = new MockQueueReceiver(queueConnection,
054: queueSession, queue, "123");
055: fail();
056: } catch (RuntimeException exc) {
057: //should throw exception
058: }
059: MockTopicSubscriber subscriber = new MockTopicSubscriber(
060: topicConnection, topicSession, topic, "", true);
061: assertEquals("", subscriber.getMessageSelector());
062: try {
063: subscriber = new MockTopicSubscriber(topicConnection,
064: topicSession, topic, "1x", false);
065: fail();
066: } catch (RuntimeException exc) {
067: //should throw exception
068: }
069: }
070:
071: public void testCanConsumeQueueReceiver() throws Exception {
072: MockQueueReceiver receiver = new MockQueueReceiver(
073: queueConnection, queueSession, queue);
074: doTestCanConsumeWithoutSelector(receiver);
075: receiver = new MockQueueReceiver(queueConnection, queueSession,
076: queue, "string = 'test' AND number = 3");
077: doTestCanConsumeWithSelector(receiver);
078: receiver = new MockQueueReceiver(queueConnection, queueSession,
079: queue, "string = 'test'");
080: queueConnection.getConfigurationManager()
081: .setUseMessageSelectors(false);
082: doTestCanConsumeWithSelectorDisabled(receiver);
083: }
084:
085: public void testCanConsumeTopicSubscriber() throws Exception {
086: MockTopicSubscriber subscriber = new MockTopicSubscriber(
087: topicConnection, topicSession, topic);
088: doTestCanConsumeWithoutSelector(subscriber);
089: subscriber = new MockTopicSubscriber(topicConnection,
090: topicSession, topic, "string = 'test' AND number = 3",
091: true);
092: doTestCanConsumeWithSelector(subscriber);
093: subscriber = new MockTopicSubscriber(topicConnection,
094: topicSession, topic, "string = 'test'", false);
095: topicConnection.getConfigurationManager()
096: .setUseMessageSelectors(false);
097: doTestCanConsumeWithSelectorDisabled(subscriber);
098: }
099:
100: public void testReceiveQueueReceiver() throws Exception {
101: MockQueueReceiver receiver = new MockQueueReceiver(
102: queueConnection, queueSession, queue);
103: MockMapMessage message1 = new MockMapMessage();
104: MockMapMessage message2 = new MockMapMessage();
105: MockMapMessage message3 = new MockMapMessage();
106: message2.setJMSCorrelationID("myId");
107: message3.setJMSCorrelationID("myId");
108: queue.addMessage(message1);
109: queue.addMessage(message2);
110: queue.addMessage(message3);
111: doTestReceiveWithoutSelector(receiver);
112: receiver = new MockQueueReceiver(queueConnection, queueSession,
113: queue, "JMSCorrelationID = 'myId'");
114: queue.addMessage(message1);
115: queue.addMessage(message2);
116: queue.addMessage(message3);
117: doTestReceiveWithSelector(receiver);
118: queueConnection.getConfigurationManager()
119: .setUseMessageSelectors(false);
120: doTestReceiveWithSelectorDisabled(receiver);
121: }
122:
123: public void testReceiveTopicSubscriber() throws Exception {
124: MockTopicSubscriber subscriber = new MockTopicSubscriber(
125: topicConnection, topicSession, topic);
126: MockMapMessage message1 = new MockMapMessage();
127: MockMapMessage message2 = new MockMapMessage();
128: MockMapMessage message3 = new MockMapMessage();
129: message2.setJMSCorrelationID("myId");
130: message3.setJMSCorrelationID("myId");
131: topic.addMessage(message1);
132: topic.addMessage(message2);
133: topic.addMessage(message3);
134: doTestReceiveWithoutSelector(subscriber);
135: subscriber = new MockTopicSubscriber(topicConnection,
136: topicSession, topic, "JMSCorrelationID = 'myId'", true);
137: topic.addMessage(message1);
138: topic.addMessage(message2);
139: topic.addMessage(message3);
140: doTestReceiveWithSelector(subscriber);
141: topicConnection.getConfigurationManager()
142: .setUseMessageSelectors(false);
143: doTestReceiveWithSelectorDisabled(subscriber);
144: }
145:
146: private void doTestCanConsumeWithoutSelector(
147: MockMessageConsumer consumer) throws JMSException {
148: MockObjectMessage message = new MockObjectMessage();
149: assertFalse(consumer.canConsume(message));
150: consumer.setMessageListener(new TestMessageListener());
151: assertTrue(consumer.canConsume(message));
152: consumer.close();
153: assertFalse(consumer.canConsume(message));
154: }
155:
156: private void doTestCanConsumeWithSelector(
157: MockMessageConsumer consumer) throws JMSException {
158: MockObjectMessage message = new MockObjectMessage();
159: assertFalse(consumer.canConsume(message));
160: consumer.setMessageListener(new TestMessageListener());
161: message.setStringProperty("string", "test");
162: assertFalse(consumer.canConsume(message));
163: message.setIntProperty("number", 2);
164: assertFalse(consumer.canConsume(message));
165: message.setIntProperty("number", 3);
166: assertTrue(consumer.canConsume(message));
167: consumer.close();
168: assertFalse(consumer.canConsume(message));
169: }
170:
171: private void doTestCanConsumeWithSelectorDisabled(
172: MockMessageConsumer consumer) throws JMSException {
173: MockObjectMessage message = new MockObjectMessage();
174: assertFalse(consumer.canConsume(message));
175: consumer.setMessageListener(new TestMessageListener());
176: assertTrue(consumer.canConsume(message));
177: message.setStringProperty("string", "xyz");
178: assertTrue(consumer.canConsume(message));
179: }
180:
181: private void doTestReceiveWithoutSelector(
182: MockMessageConsumer consumer) throws JMSException {
183: assertNotNull(consumer.receive());
184: assertNotNull(consumer.receiveNoWait());
185: assertNotNull(consumer.receive(1000));
186: assertNull(consumer.receive());
187: }
188:
189: private void doTestReceiveWithSelector(MockMessageConsumer consumer)
190: throws JMSException {
191: assertNotNull(consumer.receiveNoWait());
192: assertNotNull(consumer.receive(1000));
193: assertNull(consumer.receive());
194: }
195:
196: private void doTestReceiveWithSelectorDisabled(
197: MockMessageConsumer consumer) throws JMSException {
198: assertNotNull(consumer.receive());
199: assertNull(consumer.receive());
200: }
201:
202: public static class TestMessageListener implements MessageListener {
203: public void onMessage(Message message) {
204: }
205: }
206: }
|