1 18 package org.apache.activemq; 19 20 import javax.jms.DeliveryMode ; 21 import javax.jms.Session ; 22 import javax.jms.Message ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.TextMessage ; 25 import javax.jms.Destination ; 26 import javax.jms.JMSException ; 27 import javax.jms.MessageProducer ; 28 29 import org.apache.activemq.command.ActiveMQDestination; 30 import org.apache.activemq.test.JmsTopicSendReceiveTest; 31 32 33 36 public class JmsTopicWildcardSendReceiveTest extends JmsTopicSendReceiveTest { 37 38 private String destination1String = "TEST.ONE.ONE" ; 39 private String destination2String = "TEST.ONE.ONE.ONE" ; 40 private String destination3String = "TEST.ONE.TWO" ; 41 private String destination4String = "TEST.TWO.ONE" ; 42 43 protected void setUp() throws Exception { 44 topic = true; 45 durable = false; 46 deliveryMode = DeliveryMode.NON_PERSISTENT; 47 super.setUp(); 48 } 49 50 protected String getConsumerSubject(){ 51 return "FOO.>"; 52 } 53 54 protected String getProducerSubject(){ 55 return "FOO.BAR.HUMBUG"; 56 } 57 58 public void testReceiveWildcardTopicEndAsterisk() throws Exception { 59 connection.start(); 60 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 61 62 ActiveMQDestination destination1 = (ActiveMQDestination) session.createTopic(destination1String); 63 ActiveMQDestination destination3 = (ActiveMQDestination) session.createTopic(destination3String); 64 65 Message m = null; 66 MessageConsumer consumer = null; 67 String text = null; 68 69 ActiveMQDestination destination6 = (ActiveMQDestination) session.createTopic("TEST.ONE.*"); 70 consumer = session.createConsumer(destination6); 71 sendMessage(session,destination1,destination1String); 72 sendMessage(session,destination3,destination3String); 73 m = consumer.receive(1000); 74 assertNotNull(m); 75 text = ((TextMessage )m).getText(); 76 if(!(text.equals(destination1String) || text.equals(destination3String))) { 77 fail("unexpected message:" + text); 78 } 79 m = consumer.receive(1000); 80 assertNotNull(m); 81 text = ((TextMessage )m).getText(); 82 if(!(text.equals(destination1String) || text.equals(destination3String))) { 83 fail("unexpected message:" + text); 84 } 85 assertNull(consumer.receiveNoWait()); 86 } 87 88 public void testReceiveWildcardTopicEndGreaterThan() throws Exception { 89 connection.start(); 90 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 91 92 ActiveMQDestination destination1 = (ActiveMQDestination) session.createTopic(destination1String); 93 ActiveMQDestination destination2 = (ActiveMQDestination) session.createTopic(destination2String); 94 ActiveMQDestination destination3 = (ActiveMQDestination) session.createTopic(destination3String); 95 96 Message m = null; 97 MessageConsumer consumer = null; 98 String text = null; 99 100 ActiveMQDestination destination7 = (ActiveMQDestination) session.createTopic("TEST.ONE.>"); 101 consumer = session.createConsumer(destination7); 102 sendMessage(session,destination1,destination1String); 103 sendMessage(session,destination2,destination2String); 104 sendMessage(session,destination3,destination3String); 105 m = consumer.receive(1000); 106 assertNotNull(m); 107 text = ((TextMessage )m).getText(); 108 if(!(text.equals(destination1String) || text.equals(destination2String) || text.equals(destination3String))) { 109 fail("unexpected message:" + text); 110 } 111 m = consumer.receive(1000); 112 assertNotNull(m); 113 if(!(text.equals(destination1String) || text.equals(destination2String) || text.equals(destination3String))) { 114 fail("unexpected message:" + text); 115 } 116 m = consumer.receive(1000); 117 assertNotNull(m); 118 if(!(text.equals(destination1String) || text.equals(destination2String) || text.equals(destination3String))) { 119 fail("unexpected message:" + text); 120 } 121 assertNull(consumer.receiveNoWait()); 122 } 123 124 public void testReceiveWildcardTopicMidAsterisk() throws Exception { 125 connection.start(); 126 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 127 128 ActiveMQDestination destination1 = (ActiveMQDestination) session.createTopic(destination1String); 129 ActiveMQDestination destination4 = (ActiveMQDestination) session.createTopic(destination4String); 130 131 Message m = null; 132 MessageConsumer consumer = null; 133 String text = null; 134 135 ActiveMQDestination destination8 = (ActiveMQDestination) session.createTopic("TEST.*.ONE"); 136 consumer = session.createConsumer(destination8); 137 sendMessage(session,destination1,destination1String); 138 sendMessage(session,destination4,destination4String); 139 m = consumer.receive(1000); 140 assertNotNull(m); 141 text = ((TextMessage )m).getText(); 142 if(!(text.equals(destination1String) || text.equals(destination4String))) { 143 fail("unexpected message:" + text); 144 } 145 m = consumer.receive(1000); 146 assertNotNull(m); 147 text = ((TextMessage )m).getText(); 148 if(!(text.equals(destination1String) || text.equals(destination4String))) { 149 fail("unexpected message:" + text); 150 } 151 assertNull(consumer.receiveNoWait()); 152 153 } 154 155 private void sendMessage(Session session, Destination destination, String text) throws JMSException { 156 MessageProducer producer = session.createProducer(destination); 157 producer.send(session.createTextMessage(text)); 158 producer.close(); 159 } 160 } 161 | Popular Tags |