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