KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > client > impl > SelectorTestCase


1 /*
2  * Copyright (c) 2004 Rhombus Technologies, Inc.
3  * All rights reserved.
4  */

5 package com.ubermq.client.impl;
6
7 import java.util.*;
8 import java.util.regex.*;
9
10 import javax.jms.*;
11 import javax.naming.*;
12
13 import com.ubermq.jms.common.routing.impl.*;
14
15 import junit.framework.*;
16
17 /**
18  * Tests message selectors in various capacities.
19  */

20 public class SelectorTestCase
21     extends TestCase
22 {
23     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SelectorTestCase.class);
24     
25     public static final String JavaDoc JMS_SCHEME = "ubermq";
26     private static final String JavaDoc JMS_HOST = "localhost";
27     public static final String JavaDoc JMS_PORT = "3999";
28     public static final String JavaDoc JMS_CONTEXT_FACTORY = "com.ubermq.jms.client.JMSInitialContextFactory";
29     public static final String JavaDoc JMS_TOPIC_FACTORY = "connectionFactory";
30     private static final String JavaDoc jndiURL = JMS_SCHEME + "://" + JMS_HOST + ":" + JMS_PORT;
31
32     public static final TestSuite suite()
33     {
34         return new TestSuite(SelectorTestCase.class);
35     }
36
37     public SelectorTestCase(String JavaDoc arg0)
38     {
39         super(arg0);
40     }
41     
42     public void testSimpleSelectorRegex() throws Exception JavaDoc
43     {
44         Pattern p = Pattern.compile(SimpleSelector.WHERE_REGEX, Pattern.CASE_INSENSITIVE);
45         Matcher m = p.matcher("xdsadsad != 2");
46         Assert.assertTrue(m.matches());
47         Assert.assertEquals(m.group(1), "xdsadsad");
48         Assert.assertEquals(m.group(2), "!=");
49         Assert.assertEquals(m.group(3), "2");
50
51         m = p.matcher("hello = 'how are you'");
52         Assert.assertTrue(m.matches());
53         Assert.assertEquals(m.group(1), "hello");
54         Assert.assertEquals(m.group(2), "=");
55         Assert.assertEquals(m.group(3), "how are you");
56     }
57
58
59     public void testMultipleSelectors() throws Exception JavaDoc
60     {
61         Hashtable props = new Hashtable();
62         props.put(Context.PROVIDER_URL, jndiURL);
63         props.put(Context.INITIAL_CONTEXT_FACTORY, JMS_CONTEXT_FACTORY);
64         Context jndiContext = new InitialContext(props);
65
66         // lookup the connection factorys from the context
67
TopicConnectionFactory topicConnFact =
68             (TopicConnectionFactory) jndiContext.lookup(JMS_TOPIC_FACTORY);
69
70         // Get a topic connection
71
TopicConnection topicConn = topicConnFact.createTopicConnection();
72         topicConn.start();
73
74         // Create Topic Session
75
TopicSession topicSession = topicConn.createTopicSession(
76             false, Session.AUTO_ACKNOWLEDGE);
77
78         // Create Topic
79
String JavaDoc topicName = "hello";
80         Topic topic = topicSession.createTopic(topicName);
81
82         // Create Topic Subscriber
83
String JavaDoc msgSel1 = "WHERE MSG_SEL = 'MSG1'"; // UberMQ
84
String JavaDoc msgSel2 = "WHERE MSG_SEL = 'MSG2'"; // UberMQ
85

86         TopicSubscriber topicSub1 = topicSession.createSubscriber(topic, msgSel1, false);
87         topicSub1.setMessageListener(new MsgAction1());
88         TopicSubscriber topicSub2 = topicSession.createSubscriber(topic, msgSel2, false);
89         topicSub2.setMessageListener(new MsgAction2());
90
91         // Get a topic connection
92
TopicConnection subConn = topicConnFact.createTopicConnection();
93         subConn.start();
94
95         // Create Topic Session
96
TopicSession subSession = subConn.createTopicSession(
97             false, Session.AUTO_ACKNOWLEDGE);
98
99         // Create Topic Publisher
100
TopicPublisher topicPub = subSession.createPublisher(topic);
101
102         // Create Text Messages
103
TextMessage textMsg1 = subSession.createTextMessage("Message 1");
104         textMsg1.setStringProperty("MSG_SEL", "MSG1");
105
106         TextMessage textMsg2 = subSession.createTextMessage("Message 2");
107         textMsg2.setStringProperty("MSG_SEL", "MSG2");
108
109         // Send messages
110
topicPub.publish(textMsg1);
111         topicPub.publish(textMsg2);
112         
113         // pause and check
114
Thread.sleep(500);
115         Assert.assertTrue(((MsgAction1)topicSub1.getMessageListener()).gotMessage);
116         Assert.assertTrue(((MsgAction2)topicSub2.getMessageListener()).gotMessage);
117     }
118
119     class MsgAction1 implements MessageListener
120     {
121         boolean gotMessage = false;
122
123         public void onMessage (Message msg) {
124             try {
125                 if (msg instanceof TextMessage) {
126                     log.debug("Received message 1: " + ((TextMessage) msg).getText());
127                     gotMessage = true;
128                 }
129             } catch (Exception JavaDoc e) {
130                 System.out.println("Error processing message.");
131                 e.printStackTrace();
132             }
133         }
134     }
135
136     class MsgAction2 implements MessageListener {
137
138         boolean gotMessage = false;
139
140         public void onMessage (Message msg) {
141             try {
142                 if (msg instanceof TextMessage) {
143                     log.debug("Received message 2: " + ((TextMessage) msg).getText());
144                     gotMessage = true;
145                 }
146             } catch (Exception JavaDoc e) {
147                 System.out.println("Error processing message.");
148                 e.printStackTrace();
149             }
150         }
151     }
152 }
153
Popular Tags