1 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 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 JMS_SCHEME = "ubermq"; 26 private static final String JMS_HOST = "localhost"; 27 public static final String JMS_PORT = "3999"; 28 public static final String JMS_CONTEXT_FACTORY = "com.ubermq.jms.client.JMSInitialContextFactory"; 29 public static final String JMS_TOPIC_FACTORY = "connectionFactory"; 30 private static final String 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 arg0) 38 { 39 super(arg0); 40 } 41 42 public void testSimpleSelectorRegex() throws Exception 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 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 TopicConnectionFactory topicConnFact = 68 (TopicConnectionFactory) jndiContext.lookup(JMS_TOPIC_FACTORY); 69 70 TopicConnection topicConn = topicConnFact.createTopicConnection(); 72 topicConn.start(); 73 74 TopicSession topicSession = topicConn.createTopicSession( 76 false, Session.AUTO_ACKNOWLEDGE); 77 78 String topicName = "hello"; 80 Topic topic = topicSession.createTopic(topicName); 81 82 String msgSel1 = "WHERE MSG_SEL = 'MSG1'"; String msgSel2 = "WHERE MSG_SEL = 'MSG2'"; 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 TopicConnection subConn = topicConnFact.createTopicConnection(); 93 subConn.start(); 94 95 TopicSession subSession = subConn.createTopicSession( 97 false, Session.AUTO_ACKNOWLEDGE); 98 99 TopicPublisher topicPub = subSession.createPublisher(topic); 101 102 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 topicPub.publish(textMsg1); 111 topicPub.publish(textMsg2); 112 113 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 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 e) { 147 System.out.println("Error processing message."); 148 e.printStackTrace(); 149 } 150 } 151 } 152 } 153 | Popular Tags |