01: package com.mockrunner.example.jms;
02:
03: import java.util.HashMap;
04: import java.util.List;
05: import java.util.Map;
06:
07: import javax.jms.MapMessage;
08: import javax.jms.TextMessage;
09:
10: import com.mockrunner.ejb.EJBTestModule;
11: import com.mockrunner.jms.JMSTestCaseAdapter;
12: import com.mockrunner.mock.jms.MockTopic;
13:
14: /**
15: * Example test for {@link StockQuotePublisher}. Demonstrates
16: * the usage of {@link com.mockrunner.jms.JMSTestModule}.
17: */
18: public class StockQuotePublisherTest extends JMSTestCaseAdapter {
19: private EJBTestModule ejbModule;
20: private MockTopic topic;
21: private StockQuotePublisher sender;
22:
23: protected void setUp() throws Exception {
24: super .setUp();
25: ejbModule = createEJBTestModule();
26: ejbModule.bindToContext("java:/ConnectionFactory",
27: getJMSMockObjectFactory()
28: .getMockTopicConnectionFactory());
29: topic = getDestinationManager().createTopic("testTopic");
30: ejbModule.bindToContext("topic/quoteTopic", topic);
31: sender = new StockQuotePublisher();
32: }
33:
34: private void prepareTestRates(Map nasdaqRates, Map dowRates) {
35: nasdaqRates.put("NasdaqCompany1", "10.3");
36: nasdaqRates.put("NasdaqCompany2", "30.1");
37: dowRates.put("DowJonesCompany1", "11.5");
38: }
39:
40: public void testClosedAndCommitted() {
41: sender.send();
42: verifyNumberTopicSessions(1);
43: verifyNumberTopicPublishers(0, "testTopic", 1);
44: verifyAllTopicSessionsClosed();
45: verifyAllTopicPublishersClosed(0);
46: verifyTopicConnectionClosed();
47: verifyAllTopicSessionsCommitted();
48: verifyNumberOfReceivedTopicMessages("testTopic", 3);
49: }
50:
51: public void testVerifyReceivedMessages() throws Exception {
52: Map nasdaqRates = new HashMap();
53: Map dowRates = new HashMap();
54: prepareTestRates(nasdaqRates, dowRates);
55: sender.setQuotes(nasdaqRates, dowRates);
56: sender.send();
57: List receivedMessages = getReceivedMessageListFromTopic("testTopic");
58: TextMessage timeMessage = (TextMessage) receivedMessages.get(0);
59: MapMessage nasdaqMessage = (MapMessage) receivedMessages.get(1);
60: MapMessage dowMessage = (MapMessage) receivedMessages.get(2);
61: assertEquals(timeMessage.getText(), nasdaqMessage
62: .getJMSCorrelationID());
63: assertEquals(timeMessage.getText(), dowMessage
64: .getJMSCorrelationID());
65: assertEquals("10.3", nasdaqMessage.getString("NasdaqCompany1"));
66: assertEquals("30.1", nasdaqMessage.getString("NasdaqCompany2"));
67: assertEquals("11.5", dowMessage.getString("DowJonesCompany1"));
68: }
69: }
|