001: package com.mockrunner.test.jms;
002:
003: import java.util.List;
004:
005: import javax.jms.DeliveryMode;
006: import javax.jms.Destination;
007: import javax.jms.InvalidDestinationException;
008: import javax.jms.Message;
009: import javax.jms.MessageConsumer;
010: import javax.jms.MessageListener;
011: import javax.jms.MessageProducer;
012: import javax.jms.QueueSender;
013: import javax.jms.Session;
014:
015: import junit.framework.TestCase;
016:
017: import com.mockrunner.jms.ConfigurationManager;
018: import com.mockrunner.jms.DestinationManager;
019: import com.mockrunner.jms.GenericTransmissionManager;
020: import com.mockrunner.jms.QueueTransmissionManager;
021: import com.mockrunner.jms.TopicTransmissionManager;
022: import com.mockrunner.mock.jms.MockConnection;
023: import com.mockrunner.mock.jms.MockMessage;
024: import com.mockrunner.mock.jms.MockMessageProducer;
025: import com.mockrunner.mock.jms.MockQueue;
026: import com.mockrunner.mock.jms.MockQueueConnection;
027: import com.mockrunner.mock.jms.MockQueueReceiver;
028: import com.mockrunner.mock.jms.MockQueueSender;
029: import com.mockrunner.mock.jms.MockQueueSession;
030: import com.mockrunner.mock.jms.MockSession;
031: import com.mockrunner.mock.jms.MockTextMessage;
032: import com.mockrunner.mock.jms.MockTopic;
033: import com.mockrunner.mock.jms.MockTopicConnection;
034: import com.mockrunner.mock.jms.MockTopicPublisher;
035: import com.mockrunner.mock.jms.MockTopicSession;
036: import com.mockrunner.mock.jms.MockTopicSubscriber;
037:
038: public class MockSessionTest extends TestCase {
039: public void testTransmissionJMSHeaders() throws Exception {
040: DestinationManager destManager = new DestinationManager();
041: ConfigurationManager confManager = new ConfigurationManager();
042: MockQueueConnection connection = new MockQueueConnection(
043: destManager, confManager);
044: MockQueueSession session = (MockQueueSession) connection
045: .createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
046: destManager.createQueue("Queue1");
047: MockQueue queue = (MockQueue) session.createQueue("Queue1");
048: QueueSender sender = session.createSender(queue);
049: MockMessage message = new MockTextMessage("Text1");
050: message.setJMSTimestamp(0);
051: sender.setDisableMessageTimestamp(true);
052: sender.setDisableMessageID(true);
053: sender.setTimeToLive(0);
054: sender.setPriority(9);
055: sender.setDeliveryMode(DeliveryMode.PERSISTENT);
056: sender.send(message);
057: message = (MockMessage) queue.getMessage();
058: assertEquals(0, message.getJMSTimestamp());
059: assertNull(message.getJMSMessageID());
060: assertEquals(0, message.getJMSExpiration());
061: assertEquals(9, message.getJMSPriority());
062: assertEquals(DeliveryMode.PERSISTENT, message
063: .getJMSDeliveryMode());
064: assertEquals(queue, message.getJMSDestination());
065: message.setJMSMessageID("xyz");
066: sender.send(message);
067: message = (MockMessage) queue.getMessage();
068: assertEquals("xyz", message.getJMSMessageID());
069: message = new MockTextMessage("Text1");
070: sender.setDisableMessageTimestamp(false);
071: sender.setDisableMessageID(false);
072: sender.setPriority(7);
073: sender.send(message);
074: message = (MockMessage) queue.getMessage();
075: assertFalse(0 == message.getJMSTimestamp());
076: assertFalse("xyz".equals(message.getJMSMessageID()));
077: assertEquals(7, message.getJMSPriority());
078: assertEquals(queue, message.getJMSDestination());
079: message = new MockTextMessage("Text1");
080: sender.setTimeToLive(10000);
081: sender.send(message);
082: message = (MockMessage) queue.getMessage();
083: assertEquals(message.getJMSTimestamp() + 10000, message
084: .getJMSExpiration());
085: assertEquals(queue, message.getJMSDestination());
086: message = new MockTextMessage("Text1");
087: sender.setTimeToLive(0);
088: sender.send(message);
089: message = (MockMessage) queue.getMessage();
090: assertEquals(0, message.getJMSExpiration());
091: assertEquals(queue, message.getJMSDestination());
092: message = new MockTextMessage("Text1");
093: destManager.createTopic("Topic1");
094: MockTopic topic = (MockTopic) session.createTopic("Topic1");
095: ((MockMessageProducer) sender).send(topic, message);
096: assertEquals(topic, message.getJMSDestination());
097: }
098:
099: public void testGetAcknowledgeMode() throws Exception {
100: DestinationManager destManager = new DestinationManager();
101: ConfigurationManager confManager = new ConfigurationManager();
102: MockQueueConnection connection = new MockQueueConnection(
103: destManager, confManager);
104: MockSession session = (MockQueueSession) connection
105: .createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
106: assertEquals(Session.CLIENT_ACKNOWLEDGE, session
107: .getAcknowledgeMode());
108: session = (MockQueueSession) connection.createQueueSession(
109: false, Session.DUPS_OK_ACKNOWLEDGE);
110: assertEquals(Session.DUPS_OK_ACKNOWLEDGE, session
111: .getAcknowledgeMode());
112: session = (MockQueueSession) connection.createQueueSession(
113: false, Session.AUTO_ACKNOWLEDGE);
114: assertEquals(Session.AUTO_ACKNOWLEDGE, session
115: .getAcknowledgeMode());
116: session = (MockQueueSession) connection.createQueueSession(
117: false, 1234);
118: assertEquals(1234, session.getAcknowledgeMode());
119: session = (MockQueueSession) connection.createQueueSession(
120: true, Session.CLIENT_ACKNOWLEDGE);
121: assertEquals(0, session.getAcknowledgeMode());
122: session = (MockQueueSession) connection.createQueueSession(
123: true, 1234);
124: assertEquals(0, session.getAcknowledgeMode());
125: }
126:
127: public void testCreateProducerWithQueueSession() throws Exception {
128: DestinationManager destManager = new DestinationManager();
129: ConfigurationManager confManager = new ConfigurationManager();
130: MockQueueConnection connection = new MockQueueConnection(
131: destManager, confManager);
132: MockSession session = (MockSession) connection
133: .createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
134: doTestCreateProducer(session, destManager);
135: }
136:
137: public void testCreateProducerWithTopicSession() throws Exception {
138: DestinationManager destManager = new DestinationManager();
139: ConfigurationManager confManager = new ConfigurationManager();
140: MockTopicConnection connection = new MockTopicConnection(
141: destManager, confManager);
142: MockSession session = (MockSession) connection
143: .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
144: doTestCreateProducer(session, destManager);
145: }
146:
147: public void testCreateProducerWithSession() throws Exception {
148: DestinationManager destManager = new DestinationManager();
149: ConfigurationManager confManager = new ConfigurationManager();
150: MockConnection connection = new MockConnection(destManager,
151: confManager);
152: MockSession session = (MockSession) connection.createSession(
153: false, Session.AUTO_ACKNOWLEDGE);
154: doTestCreateProducer(session, destManager);
155: }
156:
157: public void testCreateNullProducer() throws Exception {
158: DestinationManager destManager = new DestinationManager();
159: ConfigurationManager confManager = new ConfigurationManager();
160: MockQueueConnection queueConnection = new MockQueueConnection(
161: destManager, confManager);
162: MockQueueSession queueSession = (MockQueueSession) queueConnection
163: .createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
164: MockTopicConnection topicConnection = new MockTopicConnection(
165: destManager, confManager);
166: MockTopicSession topicSession = (MockTopicSession) topicConnection
167: .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
168: MockConnection connection = new MockConnection(destManager,
169: confManager);
170: MockSession session = (MockSession) connection.createSession(
171: false, Session.AUTO_ACKNOWLEDGE);
172: assertTrue(queueSession.createProducer(null) instanceof MockQueueSender);
173: assertTrue(topicSession.createProducer(null) instanceof MockTopicPublisher);
174: assertTrue(queueSession.createSender(null) instanceof MockQueueSender);
175: assertTrue(topicSession.createPublisher(null) instanceof MockTopicPublisher);
176: assertTrue(session.createProducer(null) instanceof MockMessageProducer);
177: assertFalse(session.createProducer(null) instanceof MockQueueSender);
178: assertFalse(session.createProducer(null) instanceof MockTopicPublisher);
179: QueueTransmissionManager queueManager = queueSession
180: .getQueueTransmissionManager();
181: assertEquals(0, queueManager.getQueueSenderList().size());
182: TopicTransmissionManager topicManager = topicSession
183: .getTopicTransmissionManager();
184: assertEquals(0, topicManager.getTopicPublisherList().size());
185: GenericTransmissionManager genericManager = queueSession
186: .getGenericTransmissionManager();
187: assertEquals(2, genericManager.getMessageProducerList().size());
188: genericManager = topicSession.getGenericTransmissionManager();
189: assertEquals(2, genericManager.getMessageProducerList().size());
190: genericManager = session.getGenericTransmissionManager();
191: assertEquals(3, genericManager.getMessageProducerList().size());
192: }
193:
194: public void testCreateConsumerWithQueueSession() throws Exception {
195: DestinationManager destManager = new DestinationManager();
196: ConfigurationManager confManager = new ConfigurationManager();
197: MockQueueConnection connection = new MockQueueConnection(
198: destManager, confManager);
199: MockSession session = (MockSession) connection
200: .createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
201: doTestCreateConsumer(session, destManager);
202: }
203:
204: public void testCreateComsumerWithTopicSession() throws Exception {
205: DestinationManager destManager = new DestinationManager();
206: ConfigurationManager confManager = new ConfigurationManager();
207: MockTopicConnection connection = new MockTopicConnection(
208: destManager, confManager);
209: MockSession session = (MockSession) connection
210: .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
211: doTestCreateConsumer(session, destManager);
212: }
213:
214: public void testCreateComsumerWithSession() throws Exception {
215: DestinationManager destManager = new DestinationManager();
216: ConfigurationManager confManager = new ConfigurationManager();
217: MockConnection connection = new MockConnection(destManager,
218: confManager);
219: MockSession session = (MockSession) connection.createSession(
220: false, Session.AUTO_ACKNOWLEDGE);
221: doTestCreateConsumer(session, destManager);
222: }
223:
224: private void doTestCreateProducer(MockSession session,
225: DestinationManager manager) throws Exception {
226: MockQueue queue = manager.createQueue("Queue");
227: MockTopic topic = manager.createTopic("Topic");
228: MessageProducer producer1 = session.createProducer(queue);
229: assertTrue(producer1 instanceof MockQueueSender);
230: MessageProducer producer2 = session.createProducer(topic);
231: assertTrue(producer2 instanceof MockTopicPublisher);
232: ((MockQueueSender) producer1).send(queue, new MockTextMessage(
233: "mytext1"));
234: ((MockQueueSender) producer1).send(queue, new MockTextMessage(
235: "mytext2"));
236: ((MockTopicPublisher) producer2).send(topic,
237: new MockTextMessage("mytext3"));
238: List messageQueueList = queue.getCurrentMessageList();
239: assertEquals(2, messageQueueList.size());
240: assertTrue(messageQueueList.contains(new MockTextMessage(
241: "mytext1")));
242: assertTrue(messageQueueList.contains(new MockTextMessage(
243: "mytext2")));
244: List messageTopicList = topic.getCurrentMessageList();
245: assertEquals(1, messageTopicList.size());
246: assertTrue(messageTopicList.contains(new MockTextMessage(
247: "mytext3")));
248: try {
249: session.createProducer(new Destination() {
250: });
251: fail();
252: } catch (InvalidDestinationException exc) {
253: //should throw exception
254: }
255: }
256:
257: private void doTestCreateConsumer(MockSession session,
258: DestinationManager manager) throws Exception {
259: MockQueue queue = manager.createQueue("Queue");
260: MockTopic topic = manager.createTopic("Topic");
261: try {
262: session.createConsumer(null, "", false);
263: fail();
264: } catch (IllegalArgumentException exc) {
265: //should throw exception
266: }
267: MessageConsumer consumer1 = session.createConsumer(queue, "",
268: false);
269: assertTrue(consumer1 instanceof MockQueueReceiver);
270: MessageConsumer consumer2 = session.createConsumer(topic);
271: assertTrue(consumer2 instanceof MockTopicSubscriber);
272: TestMessageListener listener = new TestMessageListener();
273: session.setMessageListener(listener);
274: topic.addMessage(new MockTextMessage("mytext1"));
275: assertEquals(0, topic.getCurrentMessageList().size());
276: assertEquals(1, topic.getReceivedMessageList().size());
277: assertEquals(new MockTextMessage("mytext1"), listener
278: .getMessage());
279: session.setMessageListener(null);
280: listener = new TestMessageListener();
281: consumer1.setMessageListener(listener);
282: topic.addMessage(new MockTextMessage("mytext2"));
283: assertEquals(1, topic.getCurrentMessageList().size());
284: assertEquals(2, topic.getReceivedMessageList().size());
285: assertNull(listener.getMessage());
286: queue.addMessage(new MockTextMessage("mytext3"));
287: assertEquals(0, queue.getCurrentMessageList().size());
288: assertEquals(1, queue.getReceivedMessageList().size());
289: assertEquals(new MockTextMessage("mytext3"), listener
290: .getMessage());
291: MessageProducer producer = session.createProducer(topic);
292: listener = new TestMessageListener();
293: consumer2.setMessageListener(listener);
294: ((MockTopicPublisher) producer).send(topic,
295: new MockTextMessage("mytext4"));
296: assertEquals(1, topic.getCurrentMessageList().size());
297: assertEquals(3, topic.getReceivedMessageList().size());
298: assertEquals(new MockTextMessage("mytext4"), listener
299: .getMessage());
300: try {
301: session.createConsumer(new Destination() {
302: }, "");
303: fail();
304: } catch (InvalidDestinationException exc) {
305: //should throw exception
306: }
307: }
308:
309: public static class TestMessageListener implements MessageListener {
310: private Message message;
311:
312: public Message getMessage() {
313: return message;
314: }
315:
316: public void onMessage(Message message) {
317: this.message = message;
318: }
319: }
320: }
|