001: package com.mockrunner.example.jms;
002:
003: import javax.jms.JMSException;
004: import javax.jms.QueueReceiver;
005: import javax.jms.QueueSender;
006:
007: import com.mockrunner.ejb.EJBTestModule;
008: import com.mockrunner.jms.JMSTestCaseAdapter;
009: import com.mockrunner.mock.jms.MockObjectMessage;
010: import com.mockrunner.mock.jms.MockQueue;
011: import com.mockrunner.mock.jms.MockTextMessage;
012: import com.mockrunner.servlet.ServletTestModule;
013:
014: /**
015: * Example test for {@link PrintMessageServlet}. Demonstrates
016: * the usage of {@link com.mockrunner.jms.JMSTestModule}.
017: * Even though this example does not use EJB, we use
018: * {@link com.mockrunner.ejb.EJBTestModule} here, because we
019: * need the JNDI implementation of MockEJB.
020: * The JMS test framework can work with more than one connection.
021: * Per default, the latest created connection is used, i.e.
022: * unless the servlet <code>doGet</code> or <code>doPost</code>
023: * is called, the framework works with the receiver connection
024: * created in the <code>init</code> method.
025: * Note that you can override the default behavior by calling
026: * {@link com.mockrunner.jms.JMSTestModule#setCurrentQueueConnectionIndex}
027: * or
028: * {@link com.mockrunner.jms.JMSTestModule#setCurrentTopicConnectionIndex}.
029: */
030: public class PrintMessageServletTest extends JMSTestCaseAdapter {
031: private EJBTestModule ejbModule;
032: private ServletTestModule servletModule;
033: private MockQueue queue;
034:
035: protected void setUp() throws Exception {
036: super .setUp();
037: ejbModule = createEJBTestModule();
038: ejbModule.bindToContext("java:/ConnectionFactory",
039: getJMSMockObjectFactory()
040: .getMockQueueConnectionFactory());
041: queue = getDestinationManager().createQueue("testQueue");
042: ejbModule.bindToContext("queue/testQueue", queue);
043: servletModule = createServletTestModule();
044: servletModule.createServlet(PrintMessageServlet.class);
045: }
046:
047: public void testInitPrintMessageReceiver() throws Exception {
048: verifyQueueConnectionStarted();
049: verifyNumberQueueSessions(1);
050: verifyNumberQueueReceivers(0, "testQueue", 1);
051: QueueReceiver receiver = getQueueTransmissionManager(0)
052: .getQueueReceiver("testQueue");
053: assertTrue(receiver.getMessageListener() instanceof PrintMessageListener);
054: }
055:
056: public void testSuccessfulDelivery() throws Exception {
057: servletModule.addRequestParameter("customerId", "1");
058: servletModule.doGet();
059: verifyNumberQueueSessions(1);
060: verifyNumberQueueSenders(0, "testQueue", 1);
061: verifyAllQueueSessionsClosed();
062: verifyAllQueueSendersClosed(0);
063: verifyQueueConnectionClosed();
064: verifyNumberOfCreatedQueueTextMessages(0, 1);
065: verifyNumberOfReceivedQueueMessages("testQueue", 1);
066: verifyReceivedQueueMessageEquals("testQueue", 0,
067: new MockTextMessage("1"));
068: verifyReceivedQueueMessageAcknowledged("testQueue", 0);
069: verifyNumberOfCurrentQueueMessages("testQueue", 0);
070: }
071:
072: public void testDeliveryMoreMessages() throws Exception {
073: servletModule.addRequestParameter("customerId", "1");
074: servletModule.doGet();
075: servletModule.addRequestParameter("customerId", "2");
076: servletModule.doGet();
077: servletModule.addRequestParameter("customerId", "3");
078: servletModule.doGet();
079: verifyNumberOfReceivedQueueMessages("testQueue", 3);
080: verifyAllReceivedQueueMessagesAcknowledged("testQueue");
081: verifyReceivedQueueMessageEquals("testQueue", 0,
082: new MockTextMessage("1"));
083: verifyReceivedQueueMessageEquals("testQueue", 1,
084: new MockTextMessage("2"));
085: verifyReceivedQueueMessageEquals("testQueue", 2,
086: new MockTextMessage("3"));
087: QueueSender sender = getQueueTransmissionManager(0)
088: .createQueueSender(queue);
089: sender.send(new MockObjectMessage(new Integer(3)));
090: verifyNumberOfReceivedQueueMessages("testQueue", 4);
091: verifyReceivedQueueMessageAcknowledged("testQueue", 3);
092: verifyNumberOfCurrentQueueMessages("testQueue", 0);
093: }
094:
095: public void testServletResponse() throws Exception {
096: servletModule.setCaseSensitive(false);
097: servletModule.addRequestParameter("customerId", "1");
098: servletModule.doGet();
099: servletModule.verifyOutputContains("successfully");
100: servletModule.clearOutput();
101: getJMSMockObjectFactory().getMockQueueConnectionFactory()
102: .setJMSException(new JMSException("TestException"));
103: servletModule.doGet();
104: servletModule.verifyOutputContains("error");
105: }
106: }
|