001: package com.mockrunner.test.jms;
002:
003: import java.util.Set;
004:
005: import javax.jms.DeliveryMode;
006: import javax.jms.Destination;
007: import javax.jms.InvalidDestinationException;
008: import javax.jms.JMSException;
009: import javax.jms.Message;
010: import javax.jms.MessageListener;
011: import javax.jms.MessageNotWriteableException;
012: import javax.jms.Session;
013:
014: import junit.framework.TestCase;
015:
016: import com.mockrunner.jms.ConfigurationManager;
017: import com.mockrunner.jms.DestinationManager;
018: import com.mockrunner.mock.jms.MockBytesMessage;
019: import com.mockrunner.mock.jms.MockConnection;
020: import com.mockrunner.mock.jms.MockMapMessage;
021: import com.mockrunner.mock.jms.MockMessageProducer;
022: import com.mockrunner.mock.jms.MockObjectMessage;
023: import com.mockrunner.mock.jms.MockQueue;
024: import com.mockrunner.mock.jms.MockQueueConnection;
025: import com.mockrunner.mock.jms.MockQueueSender;
026: import com.mockrunner.mock.jms.MockSession;
027: import com.mockrunner.mock.jms.MockStreamMessage;
028: import com.mockrunner.mock.jms.MockTextMessage;
029: import com.mockrunner.mock.jms.MockTopic;
030: import com.mockrunner.mock.jms.MockTopicConnection;
031: import com.mockrunner.mock.jms.MockTopicPublisher;
032:
033: public class MockMessageProducerTest extends TestCase {
034: private MockQueue queue;
035: private MockTopic topic;
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: queue = new MockQueue("Queue");
040: topic = new MockTopic("Topic");
041: }
042:
043: public void testReadOnly() throws Exception {
044: DestinationManager destManager = new DestinationManager();
045: ConfigurationManager confManager = new ConfigurationManager();
046: MockQueueConnection connection = new MockQueueConnection(
047: destManager, confManager);
048: MockQueueSender sender = new MockQueueSender(connection,
049: new MockSession(connection, true,
050: Session.CLIENT_ACKNOWLEDGE), queue);
051: MockTopicPublisher publisher = new MockTopicPublisher(
052: connection, new MockSession(connection, true,
053: Session.CLIENT_ACKNOWLEDGE), topic);
054: MockTextMessage textMessage = new MockTextMessage();
055: MockMapMessage mapMessage = new MockMapMessage();
056: MockBytesMessage bytesMessage = new MockBytesMessage();
057: MockStreamMessage streamMessage = new MockStreamMessage();
058: MockObjectMessage objectMessage = new MockObjectMessage();
059: sender.send(textMessage);
060: sender.send(mapMessage);
061: publisher.publish(bytesMessage);
062: publisher.publish(streamMessage);
063: publisher.publish(objectMessage);
064: try {
065: textMessage.setText("test");
066: fail();
067: } catch (MessageNotWriteableException exc) {
068: //should throw exception
069: }
070: try {
071: mapMessage.setObject("name", "value");
072: fail();
073: } catch (MessageNotWriteableException exc) {
074: //should throw exception
075: }
076: try {
077: bytesMessage.writeInt(1);
078: fail();
079: } catch (MessageNotWriteableException exc) {
080: //should throw exception
081: }
082: try {
083: streamMessage.writeLong(2);
084: fail();
085: } catch (MessageNotWriteableException exc) {
086: //should throw exception
087: }
088: try {
089: objectMessage.setObject("Object");
090: fail();
091: } catch (MessageNotWriteableException exc) {
092: //should throw exception
093: }
094: try {
095: textMessage.setStringProperty("test", "test");
096: fail();
097: } catch (MessageNotWriteableException exc) {
098: //should throw exception
099: }
100: textMessage.setReadOnly(false);
101: textMessage.setText("test");
102: try {
103: textMessage.setStringProperty("test", "test");
104: fail();
105: } catch (MessageNotWriteableException exc) {
106: //should throw exception
107: }
108: textMessage.setReadOnlyProperties(false);
109: textMessage.setStringProperty("test", "test");
110: }
111:
112: public void testReadOnlyDoCloneOnSend() throws Exception {
113: DestinationManager destManager = new DestinationManager();
114: ConfigurationManager confManager = new ConfigurationManager();
115: MockQueueConnection connection = new MockQueueConnection(
116: destManager, confManager);
117: MockQueueSender sender = new MockQueueSender(connection,
118: new MockSession(connection, true,
119: Session.CLIENT_ACKNOWLEDGE), queue);
120: MockTopicPublisher publisher = new MockTopicPublisher(
121: connection, new MockSession(connection, true,
122: Session.CLIENT_ACKNOWLEDGE), topic);
123: MockTextMessage textMessage = new MockTextMessage();
124: MockMapMessage mapMessage = new MockMapMessage();
125: MockBytesMessage bytesMessage = new MockBytesMessage();
126: MockStreamMessage streamMessage = new MockStreamMessage();
127: MockObjectMessage objectMessage = new MockObjectMessage();
128: confManager.setDoCloneOnSend(true);
129: publisher.publish(textMessage);
130: publisher.publish(mapMessage);
131: sender.send(bytesMessage);
132: sender.send(streamMessage);
133: sender.send(objectMessage);
134: textMessage.setText("test");
135: mapMessage.setObject("name", "value");
136: bytesMessage.writeInt(1);
137: streamMessage.writeLong(2);
138: objectMessage.setObject("Object");
139: textMessage.setStringProperty("test", "test");
140: }
141:
142: public void testSendWithQueueSender() throws Exception {
143: DestinationManager destManager = new DestinationManager();
144: ConfigurationManager confManager = new ConfigurationManager();
145: MockQueueConnection connection = new MockQueueConnection(
146: destManager, confManager);
147: MockQueueSender sender = new MockQueueSender(connection,
148: new MockSession(connection, true,
149: Session.CLIENT_ACKNOWLEDGE), queue);
150: doTestSendMessage(sender);
151: assertEquals(1, queue.getCurrentMessageList().size());
152: assertEquals(1, queue.getReceivedMessageList().size());
153: assertEquals(0, topic.getCurrentMessageList().size());
154: assertEquals(0, topic.getReceivedMessageList().size());
155: assertEquals(new MockTextMessage("aMessage"), queue
156: .getMessage());
157: doTestSendWithInvalidParameters(sender);
158: }
159:
160: public void testSendWithTopicPublisher() throws Exception {
161: DestinationManager destManager = new DestinationManager();
162: ConfigurationManager confManager = new ConfigurationManager();
163: MockTopicConnection connection = new MockTopicConnection(
164: destManager, confManager);
165: MockTopicPublisher publisher = new MockTopicPublisher(
166: connection, new MockSession(connection, true,
167: Session.CLIENT_ACKNOWLEDGE), topic);
168: doTestSendMessage(publisher);
169: assertEquals(1, topic.getCurrentMessageList().size());
170: assertEquals(1, topic.getReceivedMessageList().size());
171: assertEquals(0, queue.getCurrentMessageList().size());
172: assertEquals(0, queue.getReceivedMessageList().size());
173: assertEquals(new MockTextMessage("aMessage"), topic
174: .getMessage());
175: doTestSendWithInvalidParameters(publisher);
176: }
177:
178: public void testSendWithMessageProducerToTopic() throws Exception {
179: DestinationManager destManager = new DestinationManager();
180: ConfigurationManager confManager = new ConfigurationManager();
181: MockConnection connection = new MockConnection(destManager,
182: confManager);
183: MockSession session = new MockSession(connection, true,
184: Session.CLIENT_ACKNOWLEDGE);
185: MockMessageProducer producer = new MockMessageProducer(
186: connection, session, topic);
187: doTestSendMessage(producer);
188: assertEquals(1, topic.getCurrentMessageList().size());
189: assertEquals(1, topic.getReceivedMessageList().size());
190: assertEquals(0, queue.getCurrentMessageList().size());
191: assertEquals(0, queue.getReceivedMessageList().size());
192: assertEquals(new MockTextMessage("aMessage"), topic
193: .getMessage());
194: doTestSendWithInvalidParameters(producer);
195: session.setMessageListener(new TestMessageListener());
196: topic.addMessage(new MockTextMessage("anotherMessage"));
197: assertEquals(0, topic.getCurrentMessageList().size());
198: assertEquals(2, topic.getReceivedMessageList().size());
199: }
200:
201: public void testSendWithMessageProducerToQueue() throws Exception {
202: DestinationManager destManager = new DestinationManager();
203: ConfigurationManager confManager = new ConfigurationManager();
204: MockConnection connection = new MockConnection(destManager,
205: confManager);
206: MockSession session = new MockSession(connection, true,
207: Session.CLIENT_ACKNOWLEDGE);
208: MockMessageProducer producer = new MockMessageProducer(
209: connection, session, queue);
210: doTestSendMessage(producer);
211: assertEquals(0, topic.getCurrentMessageList().size());
212: assertEquals(0, topic.getReceivedMessageList().size());
213: assertEquals(1, queue.getCurrentMessageList().size());
214: assertEquals(1, queue.getReceivedMessageList().size());
215: assertEquals(new MockTextMessage("aMessage"), queue
216: .getMessage());
217: doTestSendWithInvalidParameters(producer);
218: session.setMessageListener(new TestMessageListener());
219: queue.addMessage(new MockTextMessage("anotherMessage"));
220: assertEquals(0, queue.getCurrentMessageList().size());
221: assertEquals(2, queue.getReceivedMessageList().size());
222: }
223:
224: private void doTestSendWithInvalidParameters(
225: MockMessageProducer producer) throws Exception {
226: try {
227: producer.send(null, null);
228: fail();
229: } catch (InvalidDestinationException exc) {
230: //should throw exception
231: }
232: try {
233: producer.send(new Destination() {
234: }, new MockTextMessage(""));
235: fail();
236: } catch (InvalidDestinationException exc) {
237: //should throw exception
238: }
239: producer.close();
240: try {
241: producer.send(new MockTextMessage("aMessage"));
242: fail();
243: } catch (JMSException exc) {
244: //should throw exception
245: }
246: }
247:
248: private void doTestSendMessage(MockMessageProducer producer)
249: throws Exception {
250: producer.send(new MockTextMessage("aMessage"));
251: }
252:
253: public void testCloneMessageWithSession() throws Exception {
254: DestinationManager destManager = new DestinationManager();
255: ConfigurationManager confManager = new ConfigurationManager();
256: MockConnection connection = new MockConnection(destManager,
257: confManager);
258: MockSession session = (MockSession) connection.createSession(
259: false, Session.AUTO_ACKNOWLEDGE);
260: doTestCloneMessage(session, destManager, confManager);
261: }
262:
263: public void testCloneMessageWithQueueSession() throws Exception {
264: DestinationManager destManager = new DestinationManager();
265: ConfigurationManager confManager = new ConfigurationManager();
266: MockQueueConnection connection = new MockQueueConnection(
267: destManager, confManager);
268: MockSession session = (MockSession) connection
269: .createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
270: doTestCloneMessage(session, destManager, confManager);
271: }
272:
273: public void testCloneMessageWithTopicSession() throws Exception {
274: DestinationManager destManager = new DestinationManager();
275: ConfigurationManager confManager = new ConfigurationManager();
276: MockTopicConnection connection = new MockTopicConnection(
277: destManager, confManager);
278: MockSession session = (MockSession) connection
279: .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
280: doTestCloneMessage(session, destManager, confManager);
281: }
282:
283: private void doTestCloneMessage(MockSession session,
284: DestinationManager destManager,
285: ConfigurationManager confManager) throws Exception {
286: MockQueue queue = destManager.createQueue("Queue");
287: MockTopic topic = destManager.createTopic("Topic");
288: MockMessageProducer producer1 = (MockMessageProducer) session
289: .createProducer(queue);
290: MockMessageProducer producer2 = (MockMessageProducer) session
291: .createProducer(topic);
292: MockTextMessage textMessage = new MockTextMessage();
293: textMessage.setText("test");
294: MockObjectMessage objectMessage = new MockObjectMessage();
295: objectMessage.setObject(new Long(3));
296: producer1.send(textMessage);
297: producer1.send(textMessage);
298: producer2.send(textMessage);
299: producer2.send(objectMessage, DeliveryMode.PERSISTENT, 2, 0);
300: assertSame(textMessage, queue.getMessage());
301: assertSame(textMessage, queue.getMessage());
302: assertSame(textMessage, topic.getMessage());
303: assertSame(objectMessage, topic.getMessage());
304: confManager.setDoCloneOnSend(true);
305: producer1.send(textMessage);
306: producer1.send(textMessage, DeliveryMode.PERSISTENT, 8, 0);
307: producer2.send(textMessage);
308: producer2.send(objectMessage, DeliveryMode.PERSISTENT, 5, 0);
309: MockTextMessage receivedQueueMessage1 = (MockTextMessage) queue
310: .getMessage();
311: MockTextMessage receivedQueueMessage2 = (MockTextMessage) queue
312: .getMessage();
313: MockTextMessage receivedTopicMessage1 = (MockTextMessage) topic
314: .getMessage();
315: MockObjectMessage receivedTopicMessage2 = (MockObjectMessage) topic
316: .getMessage();
317: assertNotSame(textMessage, receivedQueueMessage1);
318: assertNotSame(textMessage, receivedQueueMessage2);
319: assertNotSame(textMessage, receivedTopicMessage1);
320: assertNotSame(objectMessage, receivedTopicMessage2);
321: assertNotSame(receivedQueueMessage1, receivedQueueMessage2);
322: assertNotSame(receivedQueueMessage1, receivedTopicMessage1);
323: assertEquals(textMessage, receivedQueueMessage1);
324: assertEquals(textMessage, receivedQueueMessage2);
325: assertEquals(textMessage, receivedTopicMessage1);
326: assertEquals(objectMessage, receivedTopicMessage2);
327: assertEquals(receivedQueueMessage1, receivedQueueMessage2);
328: assertEquals(receivedQueueMessage1, receivedTopicMessage1);
329: }
330:
331: public void testAddSessionOnSend() throws Exception {
332: DestinationManager destManager = new DestinationManager();
333: ConfigurationManager confManager = new ConfigurationManager();
334: MockConnection connection = new MockConnection(destManager,
335: confManager);
336: TestQueue testQueue = new TestQueue();
337: TestTopic testTopic = new TestTopic();
338: MockSession session1 = new MockSession(connection, true,
339: Session.CLIENT_ACKNOWLEDGE);
340: MockSession session2 = new MockSession(connection, true,
341: Session.CLIENT_ACKNOWLEDGE);
342: MockMessageProducer producer1 = new MockMessageProducer(
343: connection, session1, null);
344: MockMessageProducer producer2 = new MockMessageProducer(
345: connection, session2, null);
346: producer1.send(testQueue, new MockTextMessage());
347: producer1.send(testTopic, new MockTextMessage());
348: producer2.send(testQueue, new MockTextMessage());
349: producer2.send(testTopic, new MockTextMessage());
350: assertEquals(2, testQueue.getSessions().size());
351: assertEquals(2, testTopic.getSessions().size());
352: assertTrue(testQueue.getSessions().contains(session1));
353: assertTrue(testQueue.getSessions().contains(session2));
354: }
355:
356: private class TestQueue extends MockQueue {
357: public TestQueue() {
358: super ("");
359: }
360:
361: public void addMessage(Message message) throws JMSException {
362:
363: }
364:
365: public String getQueueName() throws JMSException {
366: return null;
367: }
368:
369: public Set getSessions() {
370: return super .sessionSet();
371: }
372: }
373:
374: private class TestTopic extends MockTopic {
375: public TestTopic() {
376: super ("");
377: }
378:
379: public void addMessage(Message message) throws JMSException {
380:
381: }
382:
383: public String getTopicName() throws JMSException {
384: // TODO Auto-generated method stub
385: return null;
386: }
387:
388: public Set getSessions() {
389: return super .sessionSet();
390: }
391: }
392:
393: private class TestMessageListener implements MessageListener {
394: public void onMessage(Message message) {
395:
396: }
397: }
398: }
|