001: package com.mockrunner.test.jms;
002:
003: import javax.jms.Connection;
004: import javax.jms.ConnectionConsumer;
005: import javax.jms.Destination;
006: import javax.jms.ExceptionListener;
007: import javax.jms.JMSException;
008: import javax.jms.Queue;
009: import javax.jms.QueueConnection;
010: import javax.jms.QueueSession;
011: import javax.jms.Session;
012: import javax.jms.Topic;
013: import javax.jms.TopicConnection;
014: import javax.jms.TopicSession;
015:
016: import junit.framework.TestCase;
017:
018: import com.mockrunner.jms.ConfigurationManager;
019: import com.mockrunner.jms.DestinationManager;
020: import com.mockrunner.mock.jms.JMSMockObjectFactory;
021: import com.mockrunner.mock.jms.MockConnection;
022: import com.mockrunner.mock.jms.MockConnectionFactory;
023: import com.mockrunner.mock.jms.MockMessageConsumer;
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.MockQueueConnectionFactory;
028: import com.mockrunner.mock.jms.MockQueueReceiver;
029: import com.mockrunner.mock.jms.MockQueueSender;
030: import com.mockrunner.mock.jms.MockQueueSession;
031: import com.mockrunner.mock.jms.MockSession;
032: import com.mockrunner.mock.jms.MockTopic;
033: import com.mockrunner.mock.jms.MockTopicConnection;
034: import com.mockrunner.mock.jms.MockTopicConnectionFactory;
035: import com.mockrunner.mock.jms.MockTopicPublisher;
036: import com.mockrunner.mock.jms.MockTopicSession;
037: import com.mockrunner.mock.jms.MockTopicSubscriber;
038:
039: public class MockConnectionTest extends TestCase {
040: private MockQueueConnection queueConnection;
041: private MockTopicConnection topicConnection;
042: private MockConnection connection;
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046: DestinationManager destManager = new DestinationManager();
047: ConfigurationManager confManager = new ConfigurationManager();
048: queueConnection = new MockQueueConnection(destManager,
049: confManager);
050: topicConnection = new MockTopicConnection(destManager,
051: confManager);
052: connection = new MockConnection(destManager, confManager);
053: }
054:
055: public void testQueueFactory() throws Exception {
056: MockQueueConnectionFactory queueFactory = new MockQueueConnectionFactory(
057: new DestinationManager(), new ConfigurationManager());
058: MockQueueConnection queueConnection1 = (MockQueueConnection) queueFactory
059: .createQueueConnection();
060: MockQueueConnection queueConnection2 = (MockQueueConnection) queueFactory
061: .createQueueConnection();
062: JMSException exception = new JMSException("");
063: queueFactory.setJMSException(exception);
064: MockQueueConnection queueConnection3 = (MockQueueConnection) queueFactory
065: .createQueueConnection("", "");
066: assertEquals(queueConnection1, queueFactory
067: .getQueueConnection(0));
068: assertEquals(queueConnection2, queueFactory
069: .getQueueConnection(1));
070: assertEquals(queueConnection3, queueFactory
071: .getQueueConnection(2));
072: assertEquals(queueConnection3, queueFactory
073: .getLatestQueueConnection());
074: assertNull(queueFactory.getQueueConnection(3));
075: queueConnection1.throwJMSException();
076: queueConnection2.throwJMSException();
077: try {
078: queueConnection3.throwJMSException();
079: fail();
080: } catch (JMSException exc) {
081: assertEquals(exc, exception);
082: }
083: }
084:
085: public void testTopicFactory() throws Exception {
086: MockTopicConnectionFactory topicFactory = new MockTopicConnectionFactory(
087: new DestinationManager(), new ConfigurationManager());
088: JMSException exception = new JMSException("");
089: topicFactory.setJMSException(exception);
090: MockTopicConnection topicConnection1 = (MockTopicConnection) topicFactory
091: .createTopicConnection(null, null);
092: MockTopicConnection topicConnection2 = (MockTopicConnection) topicFactory
093: .createConnection();
094: topicFactory.setJMSException(null);
095: MockTopicConnection topicConnection3 = (MockTopicConnection) topicFactory
096: .createTopicConnection();
097: assertEquals(topicConnection1, topicFactory
098: .getTopicConnection(0));
099: assertEquals(topicConnection2, topicFactory
100: .getTopicConnection(1));
101: assertEquals(topicConnection3, topicFactory
102: .getTopicConnection(2));
103: assertEquals(topicConnection3, topicFactory
104: .getLatestTopicConnection());
105: assertNull(topicFactory.getTopicConnection(3));
106: try {
107: topicConnection1.throwJMSException();
108: fail();
109: } catch (JMSException exc) {
110: assertEquals(exc, exception);
111: }
112: try {
113: topicConnection2.throwJMSException();
114: fail();
115: } catch (JMSException exc) {
116: assertEquals(exc, exception);
117: }
118: topicConnection3.throwJMSException();
119: }
120:
121: public void testFactory() throws Exception {
122: MockConnectionFactory factory = new MockConnectionFactory(
123: new DestinationManager(), new ConfigurationManager());
124: JMSException exception = new JMSException("");
125: factory.setJMSException(exception);
126: MockConnection connection1 = (MockConnection) factory
127: .createConnection(null, null);
128: MockConnection connection2 = (MockConnection) factory
129: .createQueueConnection();
130: factory.setJMSException(null);
131: MockConnection connection3 = (MockConnection) factory
132: .createTopicConnection();
133: assertEquals(connection1, factory.getConnection(0));
134: assertEquals(connection2, factory.getConnection(1));
135: assertEquals(connection3, factory.getConnection(2));
136: assertEquals(connection3, factory.getLatestConnection());
137: assertNull(factory.getConnection(3));
138: try {
139: connection1.throwJMSException();
140: fail();
141: } catch (JMSException exc) {
142: assertEquals(exc, exception);
143: }
144: try {
145: connection2.throwJMSException();
146: fail();
147: } catch (JMSException exc) {
148: assertEquals(exc, exception);
149: }
150: connection3.throwJMSException();
151: }
152:
153: public void testFactoriesCreateConnectionType() throws Exception {
154: MockQueueConnectionFactory queueFactory = new MockQueueConnectionFactory(
155: new DestinationManager(), new ConfigurationManager());
156: assertTrue(queueFactory.createConnection() instanceof QueueConnection);
157: assertTrue(queueFactory.createConnection(null, null) instanceof QueueConnection);
158: MockTopicConnectionFactory topicFactory = new MockTopicConnectionFactory(
159: new DestinationManager(), new ConfigurationManager());
160: assertTrue(topicFactory.createConnection() instanceof TopicConnection);
161: assertTrue(topicFactory.createConnection(null, null) instanceof TopicConnection);
162: MockConnectionFactory factory = new MockConnectionFactory(
163: new DestinationManager(), new ConfigurationManager());
164: assertTrue(factory.createConnection() instanceof Connection);
165: assertTrue(factory.createConnection(null, null) instanceof Connection);
166: assertTrue(factory.createQueueConnection() instanceof QueueConnection);
167: assertTrue(factory.createQueueConnection(null, null) instanceof QueueConnection);
168: assertTrue(factory.createTopicConnection() instanceof TopicConnection);
169: assertTrue(factory.createTopicConnection(null, null) instanceof TopicConnection);
170: assertFalse(factory.createConnection() instanceof QueueConnection);
171: assertFalse(factory.createConnection(null, null) instanceof TopicConnection);
172: }
173:
174: public void testGenericFactoryConnectionType() throws Exception {
175: MockConnectionFactory factory = new MockConnectionFactory(
176: new DestinationManager(), new ConfigurationManager());
177: QueueConnection queueConnection = factory
178: .createQueueConnection();
179: TopicConnection topicConnection = factory
180: .createTopicConnection();
181: MockConnection connection = (MockConnection) factory
182: .createConnection();
183: assertSame(queueConnection, factory.getConnection(0));
184: assertSame(topicConnection, factory.getConnection(1));
185: assertSame(connection, factory.getConnection(2));
186: }
187:
188: public void testDistinctFactoriesButSameDestinationManager()
189: throws Exception {
190: JMSMockObjectFactory mockFactory = new JMSMockObjectFactory();
191: MockQueueConnectionFactory queueFactory = mockFactory
192: .getMockQueueConnectionFactory();
193: MockTopicConnectionFactory topicFactory = mockFactory
194: .getMockTopicConnectionFactory();
195: MockConnectionFactory factory = mockFactory
196: .getMockConnectionFactory();
197: MockConnection queueConnection = (MockConnection) queueFactory
198: .createConnection();
199: MockConnection topicConnection = (MockConnection) topicFactory
200: .createConnection();
201: MockConnection connection = (MockConnection) factory
202: .createConnection();
203: assertNotSame(queueFactory, topicFactory);
204: assertNotSame(queueFactory, factory);
205: assertNotSame(topicFactory, factory);
206: assertNotSame(queueConnection, topicConnection);
207: assertNotSame(queueConnection, connection);
208: assertNotSame(topicConnection, connection);
209: assertSame(queueConnection.getDestinationManager(),
210: topicConnection.getDestinationManager());
211: assertSame(connection.getDestinationManager(), topicConnection
212: .getDestinationManager());
213: }
214:
215: public void testQueueConnectionClose() throws Exception {
216: MockQueueSession queueSession1 = (MockQueueSession) queueConnection
217: .createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
218: MockQueueSession queueSession2 = (MockQueueSession) queueConnection
219: .createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
220: MockQueueSession queueSession3 = (MockQueueSession) queueConnection
221: .createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
222: MockQueueSender sender1 = (MockQueueSender) queueSession1
223: .createSender(new MockQueue(""));
224: MockQueueSender sender2 = (MockQueueSender) queueSession1
225: .createSender(new MockQueue(""));
226: MockQueueReceiver receiver1 = (MockQueueReceiver) queueSession1
227: .createReceiver(new MockQueue(""));
228: queueConnection.close();
229: assertTrue(queueConnection.isClosed());
230: assertTrue(queueSession1.isClosed());
231: assertTrue(queueSession2.isClosed());
232: assertTrue(queueSession3.isClosed());
233: assertTrue(sender1.isClosed());
234: assertTrue(sender2.isClosed());
235: assertTrue(receiver1.isClosed());
236: assertTrue(queueSession1.isRolledBack());
237: assertFalse(queueSession2.isRolledBack());
238: assertFalse(queueSession3.isRolledBack());
239: assertTrue(queueSession1.isRecovered());
240: assertFalse(queueSession2.isRecovered());
241: assertFalse(queueSession3.isRecovered());
242: }
243:
244: public void testTopicConnectionClose() throws Exception {
245: MockTopicSession topicSession1 = (MockTopicSession) topicConnection
246: .createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
247: MockTopicSession topicSession2 = (MockTopicSession) topicConnection
248: .createTopicSession(true, Session.CLIENT_ACKNOWLEDGE);
249: MockTopicPublisher publisher1 = (MockTopicPublisher) topicSession2
250: .createPublisher(new MockTopic(""));
251: MockTopicSubscriber subscriber1 = (MockTopicSubscriber) topicSession2
252: .createSubscriber(new MockTopic(""));
253: MockTopicSubscriber subscriber2 = (MockTopicSubscriber) topicSession2
254: .createSubscriber(new MockTopic(""));
255: topicConnection.close();
256: assertTrue(topicConnection.isClosed());
257: assertTrue(topicSession1.isClosed());
258: assertTrue(topicSession2.isClosed());
259: assertTrue(publisher1.isClosed());
260: assertTrue(subscriber1.isClosed());
261: assertTrue(subscriber2.isClosed());
262: assertFalse(topicSession1.isRolledBack());
263: assertTrue(topicSession2.isRolledBack());
264: assertFalse(topicSession1.isRecovered());
265: assertTrue(topicSession2.isRecovered());
266: }
267:
268: public void testConnectionClose() throws Exception {
269: MockSession session1 = (MockSession) connection.createSession(
270: true, Session.CLIENT_ACKNOWLEDGE);
271: MockSession session2 = (MockSession) connection.createSession(
272: false, Session.CLIENT_ACKNOWLEDGE);
273: MockSession session3 = (MockSession) connection.createSession(
274: true, Session.CLIENT_ACKNOWLEDGE);
275: MockMessageProducer producer1 = (MockMessageProducer) session1
276: .createProducer(new MockQueue(""));
277: MockMessageProducer producer2 = (MockMessageProducer) session1
278: .createProducer(new MockTopic(""));
279: MockMessageConsumer consumer1 = (MockMessageConsumer) session1
280: .createConsumer(new MockQueue(""));
281: connection.close();
282: assertTrue(connection.isClosed());
283: assertTrue(session1.isClosed());
284: assertTrue(session2.isClosed());
285: assertTrue(session3.isClosed());
286: assertTrue(producer1.isClosed());
287: assertTrue(producer2.isClosed());
288: assertTrue(consumer1.isClosed());
289: assertTrue(session1.isRolledBack());
290: assertFalse(session2.isRolledBack());
291: assertTrue(session3.isRolledBack());
292: assertTrue(session1.isRecovered());
293: assertFalse(session2.isRecovered());
294: assertTrue(session3.isRecovered());
295: }
296:
297: public void testCreateConsumerAndSession() throws Exception {
298: assertTrue(queueConnection.createSession(true,
299: Session.CLIENT_ACKNOWLEDGE) instanceof QueueSession);
300: assertNotNull(queueConnection.createConnectionConsumer(
301: (Destination) null, null, null, 0));
302: assertNotNull(queueConnection.createConnectionConsumer(
303: (Queue) null, null, null, 0));
304: assertNotNull(queueConnection.createDurableConnectionConsumer(
305: null, null, null, null, 0));
306: assertTrue(topicConnection.createSession(true,
307: Session.CLIENT_ACKNOWLEDGE) instanceof TopicSession);
308: assertNotNull(topicConnection.createConnectionConsumer(
309: (Destination) null, null, null, 0));
310: assertNotNull(topicConnection.createConnectionConsumer(
311: (Topic) null, null, null, 0));
312: assertNotNull(topicConnection.createDurableConnectionConsumer(
313: null, null, null, null, 0));
314: assertTrue(connection.createSession(true,
315: Session.CLIENT_ACKNOWLEDGE) instanceof Session);
316: assertNotNull(connection.createConnectionConsumer(
317: (Destination) null, null, null, 0));
318: assertNotNull(connection.createConnectionConsumer((Topic) null,
319: null, null, 0));
320: assertNotNull(connection.createDurableConnectionConsumer(null,
321: null, null, null, 0));
322: }
323:
324: public void testCreateQueueSession() throws Exception {
325: queueConnection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
326: queueConnection.createQueueSession(true,
327: Session.CLIENT_ACKNOWLEDGE);
328: queueConnection.createQueueSession(true,
329: Session.CLIENT_ACKNOWLEDGE);
330: assertTrue(queueConnection.getSession(0) instanceof MockQueueSession);
331: assertTrue(queueConnection.getQueueSession(0) instanceof MockQueueSession);
332: assertTrue(queueConnection.getSession(1) instanceof MockQueueSession);
333: assertTrue(queueConnection.getQueueSession(1) instanceof MockQueueSession);
334: assertTrue(queueConnection.getSession(2) instanceof MockQueueSession);
335: assertTrue(queueConnection.getQueueSession(2) instanceof MockQueueSession);
336: assertNull(queueConnection.getSession(3));
337: assertNull(queueConnection.getQueueSession(3));
338: assertEquals(3, queueConnection.getSessionList().size());
339: assertEquals(3, queueConnection.getQueueSessionList().size());
340: }
341:
342: public void testCreateTopicSession() throws Exception {
343: topicConnection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
344: topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
345: topicConnection.createTopicSession(true,
346: Session.CLIENT_ACKNOWLEDGE);
347: assertTrue(topicConnection.getSession(0) instanceof MockTopicSession);
348: assertTrue(topicConnection.getTopicSession(0) instanceof MockTopicSession);
349: assertTrue(topicConnection.getSession(1) instanceof MockTopicSession);
350: assertTrue(topicConnection.getTopicSession(1) instanceof MockTopicSession);
351: assertTrue(topicConnection.getSession(2) instanceof MockTopicSession);
352: assertTrue(topicConnection.getTopicSession(2) instanceof MockTopicSession);
353: assertNull(topicConnection.getSession(3));
354: assertNull(topicConnection.getTopicSession(3));
355: assertEquals(3, topicConnection.getSessionList().size());
356: assertEquals(3, topicConnection.getTopicSessionList().size());
357: }
358:
359: public void testCreateSession() throws Exception {
360: connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
361: connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
362: assertTrue(connection.getSession(0) instanceof MockSession);
363: assertTrue(connection.getSession(1) instanceof MockSession);
364: assertNull(connection.getSession(3));
365: assertEquals(2, connection.getSessionList().size());
366: }
367:
368: public void testThrowJMSException() throws Exception {
369: queueConnection.createQueueSession(true,
370: QueueSession.CLIENT_ACKNOWLEDGE);
371: JMSException exception = new JMSException("MyReason");
372: queueConnection.setJMSException(exception);
373: try {
374: queueConnection.createQueueSession(true,
375: QueueSession.CLIENT_ACKNOWLEDGE);
376: fail();
377: } catch (JMSException exc) {
378: assertSame(exception, exc);
379: }
380: queueConnection.start();
381: ConnectionConsumer consumer = queueConnection
382: .createConnectionConsumer(null, null, null, 0);
383: consumer.getServerSessionPool();
384: exception = new JMSException("MyReason");
385: queueConnection.setJMSException(exception);
386: try {
387: consumer.getServerSessionPool();
388: fail();
389: } catch (JMSException exc) {
390: assertSame(exception, exc);
391: }
392: consumer.getServerSessionPool();
393: }
394:
395: public void testCallExceptionListener() throws Exception {
396: topicConnection.createTopicSession(true,
397: QueueSession.CLIENT_ACKNOWLEDGE);
398: JMSException exception = new JMSException("MyReason");
399: topicConnection.setJMSException(exception);
400: topicConnection.callExceptionListener();
401: TestExceptionListener listener = new TestExceptionListener();
402: topicConnection.setExceptionListener(listener);
403: topicConnection.callExceptionListener();
404: assertNull(listener.getException());
405: listener.reset();
406: topicConnection.setJMSException(exception);
407: topicConnection.callExceptionListener();
408: assertSame(exception, listener.getException());
409: listener.reset();
410: topicConnection.callExceptionListener();
411: assertNull(listener.getException());
412: listener.reset();
413: topicConnection.callExceptionListener(exception);
414: assertSame(exception, listener.getException());
415: listener.reset();
416: topicConnection.callExceptionListener(null);
417: assertNull(listener.getException());
418: }
419:
420: private static class TestExceptionListener implements
421: ExceptionListener {
422: private JMSException exception;
423:
424: public void onException(JMSException exception) {
425: this .exception = exception;
426: }
427:
428: public void reset() {
429: exception = null;
430: }
431:
432: public JMSException getException() {
433: return exception;
434: }
435: }
436: }
|