001: package com.mockrunner.test.jms;
002:
003: import javax.jms.Message;
004: import javax.jms.MessageListener;
005: import javax.jms.Session;
006:
007: import junit.framework.TestCase;
008:
009: import com.mockrunner.jms.ConfigurationManager;
010: import com.mockrunner.jms.DestinationManager;
011: import com.mockrunner.mock.jms.MockMessage;
012: import com.mockrunner.mock.jms.MockQueue;
013: import com.mockrunner.mock.jms.MockQueueConnection;
014: import com.mockrunner.mock.jms.MockQueueReceiver;
015: import com.mockrunner.mock.jms.MockQueueSession;
016: import com.mockrunner.mock.jms.MockTextMessage;
017:
018: public class MockQueueTest extends TestCase {
019: private MockQueueConnection connection;
020: private MockQueue queue;
021:
022: protected void setUp() throws Exception {
023: super .setUp();
024: DestinationManager destManager = new DestinationManager();
025: ConfigurationManager confManager = new ConfigurationManager();
026: connection = new MockQueueConnection(destManager, confManager);
027: queue = new MockQueue("TestQueue");
028: }
029:
030: public void testGetMessageList() throws Exception {
031: assertTrue(queue.isEmpty());
032: assertEquals(0, queue.getCurrentMessageList().size());
033: assertEquals(0, queue.getReceivedMessageList().size());
034: assertNull(queue.getMessage());
035: queue.addMessage(new MockTextMessage("test1"));
036: queue.addMessage(new MockTextMessage("test2"));
037: queue.addMessage(new MockTextMessage("test3"));
038: assertFalse(queue.isEmpty());
039: assertEquals(3, queue.getCurrentMessageList().size());
040: assertEquals(3, queue.getReceivedMessageList().size());
041: assertEquals(new MockTextMessage("test1"), queue.getMessage());
042: assertFalse(queue.isEmpty());
043: assertEquals(2, queue.getCurrentMessageList().size());
044: assertEquals(3, queue.getReceivedMessageList().size());
045: assertEquals(new MockTextMessage("test2"), queue.getMessage());
046: assertFalse(queue.isEmpty());
047: assertEquals(1, queue.getCurrentMessageList().size());
048: assertEquals(3, queue.getReceivedMessageList().size());
049: assertEquals(new MockTextMessage("test3"), queue.getMessage());
050: assertTrue(queue.isEmpty());
051: assertEquals(0, queue.getCurrentMessageList().size());
052: assertEquals(3, queue.getReceivedMessageList().size());
053: assertNull(queue.getMessage());
054: }
055:
056: public void testLoadMessage() throws Exception {
057: MockQueueSession session = new MockQueueSession(connection,
058: false, Session.CLIENT_ACKNOWLEDGE);
059: queue.addSession(session);
060: queue.loadMessage(new MockTextMessage("test1"));
061: queue.loadMessage(new MockTextMessage("test2"));
062: assertEquals(2, queue.getCurrentMessageList().size());
063: assertEquals(0, queue.getReceivedMessageList().size());
064: assertEquals(new MockTextMessage("test1"), queue
065: .getCurrentMessageList().get(0));
066: assertEquals(new MockTextMessage("test2"), queue
067: .getCurrentMessageList().get(1));
068: }
069:
070: public void testAddMessage() throws Exception {
071: MockQueueSession session = new MockQueueSession(connection,
072: false, Session.CLIENT_ACKNOWLEDGE);
073: queue.addSession(session);
074: queue.addMessage(new MockTextMessage("test"));
075: assertEquals(1, queue.getCurrentMessageList().size());
076: assertEquals(1, queue.getReceivedMessageList().size());
077: assertEquals(new MockTextMessage("test"), queue.getMessage());
078: TestMessageListener listener1 = new TestMessageListener();
079: TestMessageListener listener2 = new TestMessageListener();
080: MockQueueReceiver receiver1 = (MockQueueReceiver) session
081: .createReceiver(queue);
082: session.setMessageListener(listener1);
083: receiver1.setMessageListener(listener2);
084: queue.reset();
085: queue.addMessage(new MockTextMessage("test"));
086: assertEquals(0, queue.getCurrentMessageList().size());
087: assertEquals(1, queue.getReceivedMessageList().size());
088: assertNull(queue.getMessage());
089: assertEquals(new MockTextMessage("test"), listener1
090: .getMessage());
091: assertFalse(((MockMessage) listener1.getMessage())
092: .isAcknowledged());
093: assertNull(listener2.getMessage());
094: session.setMessageListener(null);
095: queue.reset();
096: listener1.reset();
097: listener2.reset();
098: queue.addMessage(new MockTextMessage("test"));
099: assertEquals(0, queue.getCurrentMessageList().size());
100: assertEquals(1, queue.getReceivedMessageList().size());
101: assertNull(queue.getMessage());
102: assertEquals(new MockTextMessage("test"), listener2
103: .getMessage());
104: assertFalse(((MockMessage) listener2.getMessage())
105: .isAcknowledged());
106: assertNull(listener1.getMessage());
107: queue.reset();
108: listener1.reset();
109: listener2.reset();
110: MockQueueReceiver receiver2 = (MockQueueReceiver) session
111: .createReceiver(queue);
112: receiver1.setMessageListener(null);
113: receiver2.setMessageListener(listener2);
114: queue.addMessage(new MockTextMessage("test"));
115: assertEquals(0, queue.getCurrentMessageList().size());
116: assertEquals(1, queue.getReceivedMessageList().size());
117: assertNull(queue.getMessage());
118: assertEquals(new MockTextMessage("test"), listener2
119: .getMessage());
120: assertFalse(((MockMessage) listener2.getMessage())
121: .isAcknowledged());
122: assertNull(listener1.getMessage());
123: queue.reset();
124: listener1.reset();
125: listener2.reset();
126: receiver1.setMessageListener(listener1);
127: receiver1.close();
128: queue.addMessage(new MockTextMessage("test"));
129: assertEquals(0, queue.getCurrentMessageList().size());
130: assertEquals(1, queue.getReceivedMessageList().size());
131: assertNull(queue.getMessage());
132: assertEquals(new MockTextMessage("test"), listener2
133: .getMessage());
134: assertFalse(((MockMessage) listener2.getMessage())
135: .isAcknowledged());
136: assertNull(listener1.getMessage());
137: queue.reset();
138: listener1.reset();
139: listener2.reset();
140: receiver2.close();
141: queue.addMessage(new MockTextMessage("test"));
142: assertEquals(1, queue.getCurrentMessageList().size());
143: assertEquals(1, queue.getReceivedMessageList().size());
144: assertNull(listener1.getMessage());
145: assertNull(listener2.getMessage());
146: assertEquals(new MockTextMessage("test"), queue.getMessage());
147: }
148:
149: public void testAddMessageAutoAcknowledge() throws Exception {
150: MockQueueSession session = new MockQueueSession(connection,
151: false, Session.AUTO_ACKNOWLEDGE);
152: doTestAcknowledge(session);
153: }
154:
155: public void testAddMessageDupOkAcknowledge() throws Exception {
156: MockQueueSession session = new MockQueueSession(connection,
157: false, Session.DUPS_OK_ACKNOWLEDGE);
158: doTestAcknowledge(session);
159: }
160:
161: private void doTestAcknowledge(MockQueueSession session)
162: throws Exception {
163: queue.addSession(session);
164: MockTextMessage message = new MockTextMessage("text");
165: queue.addMessage(message);
166: assertFalse(message.isAcknowledged());
167: message = new MockTextMessage("text");
168: TestMessageListener listener = new TestMessageListener();
169: session.setMessageListener(listener);
170: queue.addMessage(message);
171: assertTrue(message.isAcknowledged());
172: session.setMessageListener(null);
173: message = new MockTextMessage("text");
174: MockQueueReceiver receiver = (MockQueueReceiver) session
175: .createReceiver(queue);
176: receiver.setMessageListener(listener);
177: queue.addMessage(message);
178: assertTrue(message.isAcknowledged());
179: receiver.setMessageListener(null);
180: message = new MockTextMessage("text");
181: queue.addMessage(message);
182: assertFalse(message.isAcknowledged());
183: }
184:
185: public static class TestMessageListener implements MessageListener {
186: private Message message;
187:
188: public Message getMessage() {
189: return message;
190: }
191:
192: public void reset() {
193: message = null;
194: }
195:
196: public void onMessage(Message message) {
197: this.message = message;
198: }
199: }
200: }
|