001: package org.mockejb.jms.test;
002:
003: import javax.jms.*;
004: import javax.naming.*;
005:
006: import junit.framework.TestCase;
007:
008: import org.mockejb.jndi.*;
009: import org.mockejb.jms.*;
010: import org.mockejb.interceptor.*;
011:
012: /**
013: * @author Dimitar Gospodinov
014: */
015: public class ConsumersTest extends TestCase {
016:
017: private MockQueue mockQueue = null;
018: private final AspectSystem aspSys = AspectSystemFactory
019: .getAspectSystem();
020: private final InvocationRecorder recorder = new InvocationRecorder();
021:
022: public ConsumersTest(String name) {
023: super (name);
024: }
025:
026: protected void setUp() throws Exception {
027: MockContextFactory.setAsInitial();
028: Context c = new InitialContext();
029:
030: mockQueue = new MockQueue("TestQueue");
031: c.bind("TestQueue", mockQueue);
032: QueueConnectionFactory connFactory = new QueueConnectionFactoryImpl();
033: c.bind("QueueConnFactory", connFactory);
034:
035: // Send three messages
036: QueueConnection conn = connFactory.createQueueConnection();
037: QueueSession sess = conn.createQueueSession(false,
038: Session.AUTO_ACKNOWLEDGE);
039: QueueSender sender = sess.createSender(mockQueue);
040:
041: sender.send(new TextMessageImpl("Message 1"));
042: sender.send(new MessageImpl());
043: sender.send(new TextMessageImpl("Message 2"));
044: conn.close();
045:
046: // Setup invocation recorder
047: aspSys.add(new MethodPatternPointcut(
048: "javax.jms.MessageListener.onMessage"), recorder);
049: }
050:
051: protected void tearDown() throws Exception {
052: mockQueue.clear();
053: aspSys.clear();
054: MockContextFactory.revertSetAsInitial();
055: }
056:
057: public void testSynchronousReceive() throws Exception {
058:
059: Context c = new InitialContext();
060:
061: Queue queue = (Queue) c.lookup("TestQueue");
062: QueueConnectionFactory connFactory = (QueueConnectionFactory) c
063: .lookup("QueueConnFactory");
064:
065: QueueConnection conn = connFactory.createQueueConnection();
066: QueueSession sess = conn.createQueueSession(false,
067: Session.AUTO_ACKNOWLEDGE);
068: QueueReceiver receiver = sess.createReceiver(queue);
069:
070: conn.start();
071:
072: Message m = receiver.receive();
073: assertTrue(m instanceof TextMessage);
074: TextMessage tm = (TextMessage) m;
075: assertTrue(tm.getText().equals("Message 1"));
076: try {
077: tm.setText("Something");
078: fail();
079: } catch (MessageNotWriteableException ex) {
080: }
081: m = receiver.receive();
082: assertTrue(m instanceof Message);
083: try {
084: m.setBooleanProperty("prop", false);
085: fail();
086: } catch (MessageNotWriteableException ex) {
087: }
088: m = receiver.receive();
089: assertTrue(m instanceof TextMessage);
090: tm = (TextMessage) m;
091: assertTrue(tm.getText().equals("Message 2"));
092: assertNull(receiver.receive(11));
093: assertNull(receiver.receiveNoWait());
094: conn.close();
095: }
096:
097: public void testAsynchronousReceive() throws Exception {
098: Context c = new InitialContext();
099:
100: Queue queue = (Queue) c.lookup("TestQueue");
101: QueueConnectionFactory connFactory = (QueueConnectionFactory) c
102: .lookup("QueueConnFactory");
103:
104: QueueConnection conn = connFactory.createQueueConnection();
105: QueueSession sess = conn.createQueueSession(false,
106: Session.AUTO_ACKNOWLEDGE);
107: QueueReceiver receiver = sess.createReceiver(queue);
108:
109: receiver.setMessageListener(new TestListener());
110:
111: conn.start();
112: assertEquals(recorder.getMethodInvocationList().size(), 3);
113: conn.close();
114: }
115:
116: }
|