001: package com.mockrunner.test.jms;
002:
003: import java.util.ArrayList;
004: import java.util.Enumeration;
005: import java.util.List;
006:
007: import javax.jms.BytesMessage;
008: import javax.jms.JMSException;
009: import javax.jms.Message;
010: import javax.jms.MessageListener;
011: import javax.jms.ObjectMessage;
012: import javax.jms.Queue;
013: import javax.jms.QueueBrowser;
014: import javax.jms.QueueReceiver;
015: import javax.jms.QueueSender;
016: import javax.jms.QueueSession;
017: import javax.jms.Session;
018: import javax.jms.StreamMessage;
019: import javax.jms.TemporaryQueue;
020: import javax.jms.TextMessage;
021:
022: import junit.framework.TestCase;
023:
024: import com.mockrunner.jms.ConfigurationManager;
025: import com.mockrunner.jms.DestinationManager;
026: import com.mockrunner.jms.MessageManager;
027: import com.mockrunner.jms.QueueTransmissionManager;
028: import com.mockrunner.jms.TransmissionManagerWrapper;
029: import com.mockrunner.mock.jms.MockBytesMessage;
030: import com.mockrunner.mock.jms.MockMapMessage;
031: import com.mockrunner.mock.jms.MockObjectMessage;
032: import com.mockrunner.mock.jms.MockQueue;
033: import com.mockrunner.mock.jms.MockQueueBrowser;
034: import com.mockrunner.mock.jms.MockQueueConnection;
035: import com.mockrunner.mock.jms.MockQueueReceiver;
036: import com.mockrunner.mock.jms.MockQueueSender;
037: import com.mockrunner.mock.jms.MockQueueSession;
038: import com.mockrunner.mock.jms.MockStreamMessage;
039: import com.mockrunner.mock.jms.MockTemporaryQueue;
040: import com.mockrunner.mock.jms.MockTextMessage;
041:
042: public class MockQueueSessionTest extends TestCase {
043: private MockQueueConnection connection;
044: private MockQueueSession session;
045: private MockQueueSession anotherSession;
046: private MockQueue queue1;
047: private MockQueue queue2;
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: DestinationManager destManager = new DestinationManager();
052: ConfigurationManager confManager = new ConfigurationManager();
053: connection = new MockQueueConnection(destManager, confManager);
054: session = (MockQueueSession) connection.createQueueSession(
055: false, Session.CLIENT_ACKNOWLEDGE);
056: anotherSession = (MockQueueSession) connection
057: .createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
058: }
059:
060: public void testCreateMessages() throws Exception {
061: session.createTextMessage("Text1");
062: TextMessage message = session.createTextMessage();
063: message.setText("Text2");
064: session.createObjectMessage("Object1");
065: session.createMapMessage();
066: session.createMapMessage();
067: session.createMapMessage();
068: session.createStreamMessage();
069: MessageManager manager = session.getMessageManager();
070: assertEquals("Text1", manager.getTextMessage(0).getText());
071: assertEquals("Text2", manager.getTextMessage(1).getText());
072: assertNull(manager.getTextMessage(2));
073: assertEquals("Object1", manager.getObjectMessage(0).getObject());
074: assertNotNull(manager.getMapMessage(0));
075: assertNotNull(manager.getMapMessage(1));
076: assertNotNull(manager.getMapMessage(2));
077: assertNull(manager.getMapMessage(3));
078: assertNotNull(manager.getStreamMessage(0));
079: assertNull(manager.getStreamMessage(1));
080: }
081:
082: public void testCreateQueues() throws Exception {
083: try {
084: session.createQueue("Queue1");
085: fail();
086: } catch (JMSException exc) {
087: //should throw exception
088: }
089: DestinationManager manager = connection.getDestinationManager();
090: Queue managerQueue1 = manager.createQueue("Queue1");
091: Queue managerQueue2 = manager.getQueue("Queue1");
092: Queue queue = session.createQueue("Queue1");
093: assertTrue(managerQueue1 == managerQueue2);
094: assertTrue(queue == managerQueue1);
095: assertEquals("Queue1", queue.getQueueName());
096: manager.createQueue("Queue2");
097: assertNotNull(session.createQueue("Queue2"));
098: manager.removeQueue("Queue1");
099: try {
100: session.createQueue("Queue1");
101: fail();
102: } catch (JMSException exc) {
103: //should throw exception
104: }
105: session.createTemporaryQueue();
106: TemporaryQueue tempQueue = session.createTemporaryQueue();
107: session.createTemporaryQueue();
108: assertNotNull(session.getTemporaryQueue(0));
109: assertNotNull(session.getTemporaryQueue(1));
110: assertNotNull(session.getTemporaryQueue(2));
111: assertNull(session.getTemporaryQueue(3));
112: assertTrue(tempQueue == session.getTemporaryQueue(1));
113: }
114:
115: public void testCreateSenderAndReceiver() throws Exception {
116: DestinationManager manager = connection.getDestinationManager();
117: manager.createQueue("Queue1");
118: queue1 = manager.createQueue("Queue1");
119: queue2 = manager.createQueue("Queue2");
120: QueueSender sender = session.createSender(queue1);
121: session.createSender(queue2);
122: session.createSender(queue1);
123: QueueTransmissionManager queueTransManager = session
124: .getQueueTransmissionManager();
125: TransmissionManagerWrapper transManager = session
126: .getTransmissionManagerWrapper();
127: assertNotNull(queueTransManager.getQueueSender(0));
128: assertNotNull(queueTransManager.getQueueSender(1));
129: assertNotNull(queueTransManager.getQueueSender(2));
130: assertNull(queueTransManager.getQueueSender(3));
131: assertEquals(3, queueTransManager.getQueueSenderList().size());
132: assertNotNull(transManager.getMessageProducer(0));
133: assertNotNull(transManager.getMessageProducer(1));
134: assertNotNull(transManager.getMessageProducer(2));
135: assertNull(transManager.getMessageProducer(3));
136: assertEquals(3, transManager.getMessageProducerList().size());
137: assertTrue(sender == queueTransManager.getQueueSender(0));
138: assertTrue(sender == queueTransManager.getQueueSender("Queue1"));
139: assertTrue(queue1 == queueTransManager.getQueueSender(0)
140: .getQueue());
141: assertTrue(queue2 == queueTransManager.getQueueSender(1)
142: .getQueue());
143: assertTrue(queue1 == queueTransManager.getQueueSender(2)
144: .getQueue());
145: assertTrue(queue1 == queueTransManager.getQueueSender("Queue1")
146: .getQueue());
147: assertEquals(0, queueTransManager.getQueueReceiverList().size());
148: assertEquals(0, transManager.getMessageConsumerList().size());
149: session.createReceiver(queue1);
150: QueueReceiver receiver = session.createReceiver(queue2);
151: assertNotNull(queueTransManager.getQueueReceiver(0));
152: assertNotNull(queueTransManager.getQueueReceiver(1));
153: assertNotNull(transManager.getMessageConsumer(0));
154: assertNotNull(transManager.getMessageConsumer(1));
155: assertTrue(receiver == queueTransManager
156: .getQueueReceiver("Queue2"));
157: assertTrue("Queue2" == queueTransManager.getQueueReceiver(
158: "Queue2").getQueue().getQueueName());
159: assertNull(queueTransManager.getQueueReceiver(2));
160: assertNull(transManager.getMessageConsumer(2));
161: assertTrue(receiver == queueTransManager.getQueueReceiver(1));
162: assertTrue(queue1 == queueTransManager.getQueueReceiver(0)
163: .getQueue());
164: assertTrue(queue2 == queueTransManager.getQueueReceiver(1)
165: .getQueue());
166: assertEquals(2, queueTransManager.getQueueReceiverList().size());
167: assertEquals(2, transManager.getMessageConsumerList().size());
168: QueueBrowser browser = session.createBrowser(queue2);
169: assertNotNull(queueTransManager.getQueueBrowser(0));
170: assertNull(queueTransManager.getQueueBrowser(1));
171: assertTrue(browser == queueTransManager.getQueueBrowser(0));
172: assertTrue(browser == queueTransManager
173: .getQueueBrowser("Queue2"));
174: assertTrue(queue2 == queueTransManager.getQueueBrowser(0)
175: .getQueue());
176: assertEquals(1, queueTransManager.getQueueBrowserList().size());
177: }
178:
179: public void testTransmissionGlobalListener() throws Exception {
180: DestinationManager manager = connection.getDestinationManager();
181: manager.createQueue("Queue1");
182: MockQueue queue = (MockQueue) session.createQueue("Queue1");
183: QueueSender sender = session.createSender(queue);
184: TestMessageListener globalListener = new TestMessageListener();
185: session.setMessageListener(globalListener);
186: sender.send(new MockTextMessage("Text1"));
187: assertEquals("Text1", ((TextMessage) globalListener
188: .getMessage()).getText());
189: QueueReceiver receiver = session.createReceiver(queue);
190: TestMessageListener listener = new TestMessageListener();
191: receiver.setMessageListener(listener);
192: sender.send(new MockObjectMessage("Object1"));
193: assertEquals("Object1", ((ObjectMessage) globalListener
194: .getMessage()).getObject());
195: assertNull(listener.getMessage());
196: }
197:
198: public void testTransmissionMultipleReceiversWithListener()
199: throws Exception {
200: DestinationManager manager = connection.getDestinationManager();
201: manager.createQueue("Queue1");
202: MockQueue queue = (MockQueue) session.createQueue("Queue1");
203: QueueSender sender = session.createSender(queue);
204: MockQueueReceiver receiver1 = (MockQueueReceiver) session
205: .createReceiver(queue);
206: TestMessageListener listener1 = new TestMessageListener();
207: receiver1.setMessageListener(listener1);
208: MockQueueReceiver receiver2 = (MockQueueReceiver) session
209: .createReceiver(queue);
210: TestMessageListener listener2 = new TestMessageListener();
211: receiver2.setMessageListener(listener2);
212: MockQueueReceiver receiver3 = (MockQueueReceiver) session
213: .createReceiver(queue);
214: TestMessageListener listener3 = new TestMessageListener();
215: receiver3.setMessageListener(listener3);
216: sender.send(new MockTextMessage("Text1"));
217: assertEquals("Text1", ((TextMessage) listener1.getMessage())
218: .getText());
219: assertNull(listener2.getMessage());
220: assertNull(listener3.getMessage());
221: assertEquals(1, queue.getReceivedMessageList().size());
222: assertEquals(0, queue.getCurrentMessageList().size());
223: assertNull(queue.getMessage());
224: assertTrue(queue.isEmpty());
225: sender.send(new MockTextMessage("Text2"));
226: assertEquals("Text2", ((TextMessage) listener1.getMessage())
227: .getText());
228: sender.send(new MockTextMessage("Text3"));
229: assertEquals("Text3", ((TextMessage) listener1.getMessage())
230: .getText());
231: assertEquals(3, queue.getReceivedMessageList().size());
232: assertEquals(0, queue.getCurrentMessageList().size());
233: manager.createQueue("Queue2");
234: MockQueue anotherQueue = (MockQueue) session
235: .createQueue("Queue2");
236: sender = session.createSender(anotherQueue);
237: MockQueueReceiver receiver4 = (MockQueueReceiver) session
238: .createReceiver(anotherQueue);
239: TestMessageListener listener4 = new TestMessageListener();
240: receiver4.setMessageListener(listener4);
241: sender.send(new MockTextMessage("Text4"));
242: sender.send(new MockTextMessage("Text5"));
243: assertEquals(3, queue.getReceivedMessageList().size());
244: assertEquals(2, anotherQueue.getReceivedMessageList().size());
245: assertEquals(0, anotherQueue.getCurrentMessageList().size());
246: assertEquals("Text5", ((TextMessage) listener4.getMessage())
247: .getText());
248: }
249:
250: public void testTransmissionMultipleReceiversWithoutListener()
251: throws Exception {
252: DestinationManager manager = connection.getDestinationManager();
253: manager.createQueue("Queue1");
254: MockQueue queue = (MockQueue) session.createQueue("Queue1");
255: QueueSender sender = session.createSender(queue);
256: MockQueueReceiver receiver1 = (MockQueueReceiver) session
257: .createReceiver(queue);
258: MockQueueReceiver receiver2 = (MockQueueReceiver) session
259: .createReceiver(queue);
260: MockQueueReceiver receiver3 = (MockQueueReceiver) session
261: .createReceiver(queue);
262: sender.send(queue, new MockTextMessage("Text1"), 1, 2, 3);
263: sender.send(new MockTextMessage("Text2"));
264: assertEquals(2, queue.getReceivedMessageList().size());
265: assertEquals(2, queue.getCurrentMessageList().size());
266: assertEquals(new MockTextMessage("Text1"), receiver1.receive());
267: assertEquals(new MockTextMessage("Text2"), receiver3
268: .receiveNoWait());
269: assertEquals(0, queue.getCurrentMessageList().size());
270: assertNull(queue.getMessage());
271: assertTrue(queue.isEmpty());
272: assertNull(receiver2.receive(3));
273: manager.createQueue("Queue2");
274: MockQueue anotherQueue = (MockQueue) session
275: .createQueue("Queue2");
276: MockQueueReceiver receiver4 = (MockQueueReceiver) session
277: .createReceiver(anotherQueue);
278: sender.send(new MockTextMessage("Text3"));
279: assertEquals(3, queue.getReceivedMessageList().size());
280: assertEquals(1, queue.getCurrentMessageList().size());
281: assertEquals(0, anotherQueue.getReceivedMessageList().size());
282: sender = session.createSender(anotherQueue);
283: sender.send(new MockTextMessage("Text4"));
284: assertEquals(1, anotherQueue.getReceivedMessageList().size());
285: assertEquals(new MockTextMessage("Text4"), receiver4.receive());
286: sender = session.createSender(queue);
287: TestMessageListener listener = new TestMessageListener();
288: receiver3.setMessageListener(listener);
289: sender.send(new MockTextMessage("Text5"));
290: assertEquals(new MockTextMessage("Text5"), listener
291: .getMessage());
292: }
293:
294: public void testTransmissionResetCalled() throws Exception {
295: DestinationManager manager = connection.getDestinationManager();
296: manager.createQueue("Queue1");
297: MockQueue queue = (MockQueue) session.createQueue("Queue1");
298: QueueSender sender = session.createSender(queue);
299: BytesMessage bytesMessage = new MockBytesMessage();
300: StreamMessage streamMessage = new MockStreamMessage();
301: bytesMessage.writeInt(1);
302: streamMessage.writeLong(2);
303: sender.send(bytesMessage);
304: sender.send(streamMessage);
305: bytesMessage = (BytesMessage) queue.getMessage();
306: streamMessage = (StreamMessage) queue.getMessage();
307: assertEquals(1, bytesMessage.readInt());
308: assertEquals(2, streamMessage.readLong());
309: }
310:
311: public void testTransmissionSenderOrReceiverClosed()
312: throws Exception {
313: DestinationManager manager = connection.getDestinationManager();
314: manager.createQueue("Queue");
315: MockQueue queue = (MockQueue) session.createQueue("Queue");
316: MockQueueReceiver receiver1 = (MockQueueReceiver) session
317: .createReceiver(queue);
318: TestMessageListener listener1 = new TestMessageListener();
319: receiver1.setMessageListener(listener1);
320: QueueSender sender = session.createSender(queue);
321: sender.send(new MockTextMessage("Text"));
322: assertNull(receiver1.receive());
323: assertEquals(new MockTextMessage("Text"), listener1
324: .getMessage());
325: listener1.reset();
326: receiver1.close();
327: sender.send(new MockTextMessage("Text"));
328: assertNull(listener1.getMessage());
329: try {
330: receiver1.receive();
331: fail();
332: } catch (JMSException exc) {
333: //should throw exception
334: }
335: MockQueueReceiver receiver2 = (MockQueueReceiver) session
336: .createReceiver(queue);
337: assertEquals(new MockTextMessage("Text"), receiver2.receive());
338: TestMessageListener listener2 = new TestMessageListener();
339: receiver2.setMessageListener(listener2);
340: sender.send(new MockTextMessage("Text"));
341: assertEquals(new MockTextMessage("Text"), listener2
342: .getMessage());
343: sender.close();
344: try {
345: sender.send(new MockTextMessage("Text"));
346: fail();
347: } catch (JMSException exc) {
348: //should throw exception
349: }
350: }
351:
352: public void testTransmissionWithMessageSelector() throws Exception {
353: DestinationManager manager = connection.getDestinationManager();
354: manager.createQueue("Queue");
355: MockQueue queue = (MockQueue) session.createQueue("Queue");
356: MockQueueReceiver receiver1 = (MockQueueReceiver) session
357: .createReceiver(queue, "text = 'test'");
358: TestListMessageListener listener1 = new TestListMessageListener();
359: receiver1.setMessageListener(listener1);
360: QueueSender sender = session.createSender(queue);
361: MockBytesMessage message1 = new MockBytesMessage();
362: sender.send(message1);
363: MockBytesMessage message2 = new MockBytesMessage();
364: message2.setStringProperty("text", "test");
365: sender.send(message2);
366: assertEquals(1, listener1.getMessageList().size());
367: assertSame(message2, listener1.getMessageList().get(0));
368: assertNull(receiver1.receive());
369: MockQueueReceiver receiver2 = (MockQueueReceiver) session
370: .createReceiver(queue);
371: assertSame(message1, receiver2.receiveNoWait());
372: receiver2.setMessageListener(listener1);
373: listener1.clearMessageList();
374: sender.send(message1);
375: sender.send(message2);
376: assertEquals(2, listener1.getMessageList().size());
377: }
378:
379: public void testTransmissionMessageAcknowledged() throws Exception {
380: MockQueueSession session1 = (MockQueueSession) connection
381: .createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
382: DestinationManager manager = connection.getDestinationManager();
383: manager.createQueue("Queue");
384: MockQueue queue = (MockQueue) session1.createQueue("Queue");
385: QueueSender sender = session1.createSender(queue);
386: MockTextMessage message = new MockTextMessage("Text");
387: queue.reset();
388: sender.send(message);
389: message = (MockTextMessage) queue.getReceivedMessageList().get(
390: 0);
391: MockQueueReceiver receiver = (MockQueueReceiver) session1
392: .createReceiver(queue);
393: assertFalse(message.isAcknowledged());
394: receiver.receiveNoWait();
395: assertTrue(message.isAcknowledged());
396: TestMessageListener listener = new TestMessageListener();
397: receiver.setMessageListener(listener);
398: message = new MockTextMessage("Text");
399: queue.reset();
400: sender.send(message);
401: message = (MockTextMessage) queue.getReceivedMessageList().get(
402: 0);
403: assertTrue(message.isAcknowledged());
404: receiver.setMessageListener(null);
405: session1.setMessageListener(listener);
406: message = new MockTextMessage("Text");
407: queue.reset();
408: sender.send(message);
409: message = (MockTextMessage) queue.getReceivedMessageList().get(
410: 0);
411: assertTrue(message.isAcknowledged());
412: session1.setMessageListener(null);
413: MockQueueSession session2 = (MockQueueSession) connection
414: .createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
415: receiver = (MockQueueReceiver) session2.createReceiver(queue);
416: receiver.setMessageListener(listener);
417: sender = session2.createSender(queue);
418: message = new MockTextMessage("Text");
419: queue.reset();
420: sender.send(message);
421: message = (MockTextMessage) queue.getReceivedMessageList().get(
422: 0);
423: assertFalse(message.isAcknowledged());
424: receiver.setMessageListener(null);
425: message = new MockTextMessage("Text");
426: queue.reset();
427: sender.send(message);
428: message = (MockTextMessage) queue.getReceivedMessageList().get(
429: 0);
430: assertFalse(message.isAcknowledged());
431: receiver.receive();
432: assertFalse(message.isAcknowledged());
433: session1.setMessageListener(listener);
434: message = new MockTextMessage("Text");
435: queue.reset();
436: sender.send(message);
437: message = (MockTextMessage) queue.getReceivedMessageList().get(
438: 0);
439: assertTrue(message.isAcknowledged());
440: }
441:
442: public void testTransmissionMultipleSessions() throws Exception {
443: DestinationManager manager = connection.getDestinationManager();
444: manager.createQueue("Queue1");
445: MockQueue queue = (MockQueue) session.createQueue("Queue1");
446: MockQueue sameQueue = (MockQueue) anotherSession
447: .createQueue("Queue1");
448: TestListMessageListener listener = new TestListMessageListener();
449: session.setMessageListener(listener);
450: MockQueueReceiver receiver = (MockQueueReceiver) anotherSession
451: .createReceiver(queue);
452: receiver.setMessageListener(listener);
453: QueueSender sender = anotherSession.createSender(queue);
454: sender.send(new MockTextMessage("Text1"));
455: assertEquals(1, queue.getReceivedMessageList().size());
456: assertEquals(0, queue.getCurrentMessageList().size());
457: assertEquals(1, listener.getMessageList().size());
458: assertEquals(new MockTextMessage("Text1"), listener
459: .getMessageList().get(0));
460: MockQueueReceiver receiver2 = (MockQueueReceiver) session
461: .createReceiver(queue);
462: receiver2.setMessageListener(listener);
463: session.setMessageListener(null);
464: sender.send(new MockTextMessage("Text2"));
465: assertEquals(2, queue.getReceivedMessageList().size());
466: assertEquals(0, queue.getCurrentMessageList().size());
467: assertEquals(2, listener.getMessageList().size());
468: assertEquals(new MockTextMessage("Text2"), listener
469: .getMessageList().get(1));
470: MockQueueReceiver receiver3 = (MockQueueReceiver) session
471: .createReceiver(queue);
472: receiver3.setMessageListener(listener);
473: sender = anotherSession.createSender(sameQueue);
474: sender.send(new MockObjectMessage(new Integer(1)));
475: assertEquals(3, queue.getReceivedMessageList().size());
476: assertEquals(0, queue.getCurrentMessageList().size());
477: assertEquals(3, listener.getMessageList().size());
478: Object object = listener.getMessageList().get(2);
479: assertEquals(new Integer(1), ((MockObjectMessage) object)
480: .getObject());
481: }
482:
483: public void testQueueBrowser() throws Exception {
484: DestinationManager manager = connection.getDestinationManager();
485: manager.createQueue("Queue1");
486: MockQueue queue = (MockQueue) session.createQueue("Queue1");
487: QueueSender sender = session.createSender(queue);
488: sender.send(new MockTextMessage("Text"));
489: sender.send(new MockObjectMessage("Object"));
490: sender.send(new MockMapMessage());
491: sender.send(new MockStreamMessage());
492: sender.send(new MockBytesMessage());
493: QueueBrowser browser = session.createBrowser(queue);
494: Enumeration messages = browser.getEnumeration();
495: TextMessage message1 = (TextMessage) messages.nextElement();
496: assertEquals("Text", message1.getText());
497: ObjectMessage message2 = (ObjectMessage) messages.nextElement();
498: assertEquals("Object", message2.getObject());
499: assertTrue(messages.nextElement() instanceof MockMapMessage);
500: assertTrue(messages.nextElement() instanceof MockStreamMessage);
501: assertTrue(messages.nextElement() instanceof MockBytesMessage);
502: assertFalse(messages.hasMoreElements());
503: sender.send(new MockTextMessage("Text"));
504: sender.send(new MockObjectMessage("Object"));
505: browser.close();
506: try {
507: browser.getEnumeration();
508: fail();
509: } catch (JMSException exc) {
510: //should throw exception
511: }
512: }
513:
514: public void testCloseSession() throws Exception {
515: DestinationManager manager = connection.getDestinationManager();
516: manager.createQueue("Queue");
517: MockQueueSession session = (MockQueueSession) connection
518: .createQueueSession(false,
519: QueueSession.CLIENT_ACKNOWLEDGE);
520: MockQueue queue = (MockQueue) session.createQueue("Queue");
521: MockQueueReceiver receiver1 = (MockQueueReceiver) session
522: .createReceiver(queue);
523: MockQueueSender sender1 = (MockQueueSender) session
524: .createSender(queue);
525: MockQueueSender sender2 = (MockQueueSender) session
526: .createSender(queue);
527: MockQueueBrowser browser1 = (MockQueueBrowser) session
528: .createBrowser(queue);
529: session.close();
530: assertTrue(session.isClosed());
531: assertFalse(session.isRolledBack());
532: assertTrue(receiver1.isClosed());
533: assertTrue(sender1.isClosed());
534: assertTrue(sender2.isClosed());
535: assertTrue(browser1.isClosed());
536: session = (MockQueueSession) connection.createQueueSession(
537: true, QueueSession.CLIENT_ACKNOWLEDGE);
538: queue = (MockQueue) session.createQueue("Queue");
539: receiver1 = (MockQueueReceiver) session.createReceiver(queue);
540: sender1 = (MockQueueSender) session.createSender(queue);
541: sender2 = (MockQueueSender) session.createSender(queue);
542: browser1 = (MockQueueBrowser) session.createBrowser(queue);
543: session.close();
544: assertTrue(session.isClosed());
545: assertTrue(session.isRolledBack());
546: assertTrue(receiver1.isClosed());
547: assertTrue(sender1.isClosed());
548: assertTrue(sender2.isClosed());
549: assertTrue(browser1.isClosed());
550: session = (MockQueueSession) connection.createQueueSession(
551: true, QueueSession.CLIENT_ACKNOWLEDGE);
552: session.commit();
553: session.close();
554: assertTrue(session.isClosed());
555: assertFalse(session.isRolledBack());
556: }
557:
558: public void testCloseSessionRemove() throws Exception {
559: DestinationManager manager = connection.getDestinationManager();
560: MockQueue queue1 = manager.createQueue("Queue1");
561: MockQueue queue2 = manager.createQueue("Queue2");
562: MockQueueSession session = (MockQueueSession) connection
563: .createQueueSession(false,
564: QueueSession.CLIENT_ACKNOWLEDGE);
565: session.createQueue("Queue1");
566: MockTemporaryQueue tempQueue = (MockTemporaryQueue) session
567: .createTemporaryQueue();
568: assertTrue(queue1.sessionSet().contains(session));
569: assertFalse(queue2.sessionSet().contains(session));
570: assertTrue(tempQueue.sessionSet().contains(session));
571: session.close();
572: assertFalse(queue1.sessionSet().contains(session));
573: assertFalse(queue2.sessionSet().contains(session));
574: assertFalse(tempQueue.sessionSet().contains(session));
575: }
576:
577: public void testTransmissionWithNullDestination() throws Exception {
578: MockQueueSession session = (MockQueueSession) connection
579: .createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
580: DestinationManager manager = connection.getDestinationManager();
581: MockQueue queue = (MockQueue) manager.createQueue("Queue");
582: QueueSender sender = session.createSender(null);
583: MockTextMessage message = new MockTextMessage("Text");
584: sender.send(queue, message);
585: assertEquals(1, queue.getReceivedMessageList().size());
586: assertEquals(1, queue.getCurrentMessageList().size());
587: }
588:
589: public static class TestListMessageListener implements
590: MessageListener {
591: private List messages = new ArrayList();
592:
593: public List getMessageList() {
594: return messages;
595: }
596:
597: public void clearMessageList() {
598: messages.clear();
599: }
600:
601: public void onMessage(Message message) {
602: messages.add(message);
603: }
604: }
605:
606: public static class TestMessageListener implements MessageListener {
607: private Message message;
608:
609: public Message getMessage() {
610: return message;
611: }
612:
613: public void reset() {
614: message = null;
615: }
616:
617: public void onMessage(Message message) {
618: this.message = message;
619: }
620: }
621: }
|