001: package com.mockrunner.example.jms;
002:
003: import javax.jms.JMSException;
004: import javax.jms.Queue;
005: import javax.jms.QueueConnectionFactory;
006:
007: import org.mockejb.TransactionPolicy;
008:
009: import com.mockrunner.ejb.EJBTestModule;
010: import com.mockrunner.example.jms.interfaces.PrintSession;
011: import com.mockrunner.jms.JMSTestCaseAdapter;
012: import com.mockrunner.mock.jms.MockTextMessage;
013:
014: /**
015: * Example test for {@link PrintSessionBean}. Demonstrates
016: * how to deal with EJBs in JMS tests. The sender is a session
017: * bean, the receiver is a message driven bean. Please note
018: * that you have to deal the the transaction methods of
019: * {@link com.mockrunner.ejb.EJBTestModule}, because this example
020: * uses a simulated JTA transaction.
021: */
022: public class PrintSessionBeanTest extends JMSTestCaseAdapter {
023: private EJBTestModule ejbModule;
024: private PrintSession bean;
025:
026: protected void setUp() throws Exception {
027: super .setUp();
028: ejbModule = createEJBTestModule();
029: ejbModule
030: .setInterfacePackage("com.mockrunner.example.jms.interfaces");
031: ejbModule.deploySessionBean(
032: "com/mockrunner/example/PrintSession",
033: PrintSessionBean.class, TransactionPolicy.REQUIRED);
034: bean = (PrintSession) ejbModule
035: .createBean("com/mockrunner/example/PrintSession");
036: QueueConnectionFactory factory = getJMSMockObjectFactory()
037: .getMockQueueConnectionFactory();
038: Queue queue = getDestinationManager().createQueue("testQueue");
039: ejbModule.deployMessageBean("java:/ConnectionFactory",
040: "queue/testQueue", factory, queue,
041: new PrintMessageDrivenBean());
042: }
043:
044: //The following commented out setUp method is an alternative approach to
045: //test a message driven bean. The message bean is not deployed to the
046: //mock container but instantiated directly, which means that you cannot
047: //test JTA transactions when *receiving* the message and you cannot use the
048: //MockEJB interceptor framework for the onMessage method.
049: //In this test case we only test JTA transactions while *sending* the message,
050: //so this approach works quite well.
051: /*protected void setUp() throws Exception
052: {
053: super.setUp();
054: ejbModule = createEJBTestModule();
055: ejbModule.bindToContext("java:/ConnectionFactory", getJMSMockObjectFactory().getMockQueueConnectionFactory());
056: Queue queue = getDestinationManager().createQueue("testQueue");
057: ejbModule.bindToContext("queue/testQueue", queue);
058: registerTestMessageListenerForQueue("testQueue", new PrintMessageDrivenBean());
059: ejbModule.setInterfacePackage("com.mockrunner.example.jms.interfaces");
060: ejbModule.deploySessionBean("com/mockrunner/example/PrintSession", PrintSessionBean.class, TransactionPolicy.REQUIRED);
061: bean = (PrintSession)ejbModule.createBean("com/mockrunner/example/PrintSession");
062: }*/
063:
064: public void testSuccessfulDelivery() throws Exception {
065: bean.sendMessage("123");
066: ejbModule.verifyCommitted();
067: verifyNumberQueueSessions(1);
068: verifyNumberQueueSenders(0, "testQueue", 1);
069: verifyAllQueueSessionsClosed();
070: verifyAllQueueSendersClosed(0);
071: verifyQueueConnectionClosed();
072: verifyNumberOfCreatedQueueTextMessages(0, 1);
073: verifyNumberOfReceivedQueueMessages("testQueue", 1);
074: verifyReceivedQueueMessageEquals("testQueue", 0,
075: new MockTextMessage("123"));
076: verifyReceivedQueueMessageAcknowledged("testQueue", 0);
077: }
078:
079: public void testDeliveryMoreMessages() throws Exception {
080: bean.sendMessage("1");
081: ejbModule.verifyCommitted();
082: bean.sendMessage("2");
083: ejbModule.verifyCommitted();
084: bean.sendMessage("3");
085: ejbModule.verifyCommitted();
086: verifyNumberOfReceivedQueueMessages("testQueue", 3);
087: verifyAllReceivedQueueMessagesAcknowledged("testQueue");
088: verifyReceivedQueueMessageEquals("testQueue", 0,
089: new MockTextMessage("1"));
090: verifyReceivedQueueMessageEquals("testQueue", 1,
091: new MockTextMessage("2"));
092: verifyReceivedQueueMessageEquals("testQueue", 2,
093: new MockTextMessage("3"));
094: }
095:
096: public void testFailure() throws Exception {
097: getJMSMockObjectFactory().getMockQueueConnectionFactory()
098: .setJMSException(new JMSException("TestException"));
099: bean.sendMessage("1");
100: ejbModule.verifyNotCommitted();
101: ejbModule.verifyRolledBack();
102: }
103: }
|