01: package com.mockrunner.example.jms;
02:
03: import javax.jms.JMSException;
04: import javax.jms.Session;
05: import javax.jms.Topic;
06: import javax.jms.TopicConnection;
07: import javax.jms.TopicPublisher;
08: import javax.jms.TopicSession;
09:
10: import com.mockrunner.ejb.EJBTestModule;
11: import com.mockrunner.jms.JMSTestCaseAdapter;
12: import com.mockrunner.mock.jms.MockTextMessage;
13:
14: /**
15: * Example test for {@link NewsSubscriber}. Demonstrates
16: * message selector usage. Only messages with the <code>subject</code>
17: * property <code>Java</code> will be received by {@link NewsSubscriber}.
18: * There's one problem with {@link NewsSubscriber}. It blocks the current
19: * thread. To get around of this, we can use a simple trick. We call the
20: * main method of {@link NewsSubscriber} from a different thread and interrupt
21: * it immediately. Then we can send messages from the main thread, that are
22: * received synchronously by {@link NewsSubscriber}.
23: * Please note, that the JMS test framework is not thread safe at the moment
24: * and should not be accessed by multiple threads concurrently. In this test
25: * the <code>TestNewsSubscriberThread</code> and the main thread do not run
26: * concurrently, so it's ok.
27: */
28: public class NewsSubscriberTest extends JMSTestCaseAdapter {
29: private EJBTestModule ejbModule;
30:
31: protected void setUp() throws Exception {
32: super .setUp();
33: ejbModule = createEJBTestModule();
34: ejbModule.bindToContext("ConnectionFactory",
35: getJMSMockObjectFactory()
36: .getMockTopicConnectionFactory());
37: Topic topic = getDestinationManager().createTopic("topic");
38: ejbModule.bindToContext("topic/newsTopic", topic);
39: }
40:
41: public void testMain() throws Exception {
42: callMain("Java");
43: TopicPublisher publisher = createTestPublisher();
44: MockTextMessage message1 = new MockTextMessage("message1");
45: message1.setStringProperty("subject", "C++");
46: MockTextMessage message2 = new MockTextMessage("message2");
47: message2.setStringProperty("subject", "Java");
48: publisher.publish(message1);
49: publisher.publish(message2);
50: verifyNumberOfReceivedTopicMessages("topic", 2);
51: verifyNumberOfCurrentTopicMessages("topic", 1);
52: }
53:
54: private TopicPublisher createTestPublisher() throws JMSException {
55: TopicConnection connection = getJMSMockObjectFactory()
56: .getMockTopicConnectionFactory()
57: .createTopicConnection();
58: TopicSession session = connection.createTopicSession(false,
59: Session.AUTO_ACKNOWLEDGE);
60: return session.createPublisher(getDestinationManager()
61: .getTopic("topic"));
62: }
63:
64: private void callMain(String subject) throws Exception {
65: String contextFactory = "org.mockejb.jndi.MockContextFactory";
66: String providerURL = "org.mockejb.jndi";
67: TestNewsSubscriberThread thread = new TestNewsSubscriberThread(
68: contextFactory, providerURL, subject);
69: thread.start();
70: thread.interrupt();
71: thread.join();
72: }
73:
74: private class TestNewsSubscriberThread extends Thread {
75: private String contextFactory;
76: private String providerURL;
77: private String subject;
78:
79: public TestNewsSubscriberThread(String contextFactory,
80: String providerURL, String subject) {
81: this .contextFactory = contextFactory;
82: this .providerURL = providerURL;
83: this .subject = subject;
84: }
85:
86: public void run() {
87: try {
88: NewsSubscriber.main(new String[] { contextFactory,
89: providerURL, subject });
90: } catch (RuntimeException exc) {
91:
92: }
93: }
94: }
95: }
|