001: package org.mockejb.jms.test;
002:
003: import junit.framework.TestCase;
004: import javax.jms.*;
005:
006: import org.mockejb.jms.BytesMessageImpl;
007: import org.mockejb.jms.MessageUtility;
008: import org.mockejb.jms.MockQueue;
009: import org.mockejb.jms.QueueConnectionFactoryImpl;
010: import org.mockejb.jms.TextMessageImpl;
011:
012: /**
013: * Tests sending messages to a <code>Queue</code>.
014: *
015: * @author Dimitar Gospodinov
016: */
017: public class QueueTest extends TestCase {
018:
019: private QueueConnectionFactoryImpl queueFactory = new QueueConnectionFactoryImpl();
020: private QueueConnection queueConnection;
021: private MockQueue queue;
022:
023: public QueueTest(String name) {
024: super (name);
025: }
026:
027: protected void setUp() throws Exception {
028: queueConnection = queueFactory.createQueueConnection();
029: queue = new MockQueue("Test Queue");
030: }
031:
032: protected void tearDown() throws Exception {
033: super .tearDown();
034: queueConnection = null;
035: queue = null;
036: }
037:
038: public void testQueueSession() throws JMSException {
039: QueueSession session = queueConnection.createQueueSession(
040: false, Session.CLIENT_ACKNOWLEDGE);
041:
042: assertFalse(session.getTransacted());
043: // Uncomment if using JMS 1.1 or later
044: // assertEquals(session.getAcknowledgeMode(), Session.CLIENT_ACKNOWLEDGE);
045:
046: session = queueConnection.createQueueSession(true,
047: Session.AUTO_ACKNOWLEDGE);
048:
049: assertTrue(session.getTransacted());
050: // Uncomment if using JMS 1.1 or later
051: //assertEquals(session.getAcknowledgeMode(), Session.AUTO_ACKNOWLEDGE);
052: }
053:
054: public void testQueueSender() throws JMSException {
055: QueueSession session = queueConnection.createQueueSession(
056: false, Session.CLIENT_ACKNOWLEDGE);
057: QueueSender sender = session.createSender(queue);
058:
059: // Default priority should be 4
060: assertEquals(sender.getPriority(), 4);
061: sender.setPriority(5);
062: assertEquals(sender.getPriority(), 5);
063: // Default expiration should be 0 (message never expires)
064: assertEquals(sender.getTimeToLive(), 0);
065: sender.setTimeToLive(7777888899991111L);
066: assertEquals(sender.getTimeToLive(), 7777888899991111L);
067: // Default delivery mode should be DeliveryMode.PERSISTENT
068: assertEquals(sender.getDeliveryMode(), DeliveryMode.PERSISTENT);
069: sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
070: assertEquals(sender.getDeliveryMode(),
071: DeliveryMode.NON_PERSISTENT);
072:
073: assertSame(sender.getQueue(), queue);
074: // Uncomment if using JMS 1.1 or later
075: // assertSame(sender.getDestination(), queue);
076: }
077:
078: public void testQueue() throws JMSException {
079:
080: QueueSession session = queueConnection.createQueueSession(
081: false, Session.AUTO_ACKNOWLEDGE);
082: QueueSender sender = session.createSender(queue);
083:
084: TextMessage textMsg = session.createTextMessage();
085: assertNull(textMsg.getText());
086: textMsg.setText("TextMessage:1");
087: textMsg.setJMSType("TestType:1");
088:
089: BytesMessage bytesMsg = session.createBytesMessage();
090:
091: bytesMsg.writeBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
092: bytesMsg.setJMSType("TestType:2");
093:
094: Message[] sentMsgs = new Message[4];
095:
096: sender.send(textMsg);
097: sentMsgs[1] = new TextMessageImpl(textMsg);
098:
099: sender.send(bytesMsg, DeliveryMode.PERSISTENT, 9, 100);
100: sentMsgs[0] = new BytesMessageImpl(bytesMsg);
101:
102: try {
103: bytesMsg.readByte();
104: fail();
105: } catch (MessageNotReadableException ex) {
106: }
107:
108: assertNotNull(textMsg.getJMSMessageID());
109: assertSame(textMsg.getJMSDestination(), queue);
110: assertEquals(textMsg.getJMSExpiration(), sender.getTimeToLive());
111: assertEquals(textMsg.getJMSType(), "TestType:1");
112: assertEquals(textMsg.getJMSDeliveryMode(), sender
113: .getDeliveryMode());
114:
115: assertNotNull(bytesMsg.getJMSMessageID());
116: assertSame(bytesMsg.getJMSDestination(), queue);
117: assertEquals(bytesMsg.getJMSType(), "TestType:2");
118: assertEquals(bytesMsg.getJMSDeliveryMode(),
119: DeliveryMode.PERSISTENT);
120:
121: bytesMsg.writeBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6 }, 0, 4);
122: bytesMsg.reset();
123: sender.send(bytesMsg, DeliveryMode.NON_PERSISTENT, 1, 1000);
124: sentMsgs[3] = new BytesMessageImpl(bytesMsg);
125:
126: try {
127: bytesMsg.writeBoolean(false);
128: fail();
129: } catch (MessageNotWriteableException ex) {
130: }
131:
132: byte[] bytes1 = new byte[11];
133: assertEquals(bytesMsg.readBytes(bytes1), 11);
134: assertEquals(bytesMsg.readBytes(bytes1), -1);
135: assertTrue(MessageUtility.compare(bytes1, new byte[] { 0, 1, 2,
136: 3, 4, 5, 6, 0, 1, 2, 3 }));
137:
138: textMsg.clearBody();
139: sender.send(textMsg);
140: sentMsgs[2] = textMsg;
141:
142: // Examine the queue
143: assertEquals(queue.size(), 4);
144: for (int i = 0; i < 4; i++) {
145: assertTrue(MessageUtility.compare(queue.getMessageAt(i),
146: sentMsgs[i]));
147: }
148: }
149:
150: }
|