KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > JmsTopicWildcardSendReceiveTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq;
19
20 import javax.jms.DeliveryMode JavaDoc;
21 import javax.jms.Session JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.TextMessage JavaDoc;
25 import javax.jms.Destination JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.MessageProducer JavaDoc;
28
29 import org.apache.activemq.command.ActiveMQDestination;
30 import org.apache.activemq.test.JmsTopicSendReceiveTest;
31
32
33 /**
34  * @version $Revision: 1.4 $
35  */

36 public class JmsTopicWildcardSendReceiveTest extends JmsTopicSendReceiveTest {
37
38     private String JavaDoc destination1String = "TEST.ONE.ONE" ;
39     private String JavaDoc destination2String = "TEST.ONE.ONE.ONE" ;
40     private String JavaDoc destination3String = "TEST.ONE.TWO" ;
41     private String JavaDoc destination4String = "TEST.TWO.ONE" ;
42
43     protected void setUp() throws Exception JavaDoc {
44         topic = true;
45         durable = false;
46         deliveryMode = DeliveryMode.NON_PERSISTENT;
47         super.setUp();
48     }
49     
50     protected String JavaDoc getConsumerSubject(){
51         return "FOO.>";
52     }
53     
54     protected String JavaDoc getProducerSubject(){
55         return "FOO.BAR.HUMBUG";
56     }
57
58     public void testReceiveWildcardTopicEndAsterisk() throws Exception JavaDoc {
59         connection.start();
60         Session JavaDoc 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 JavaDoc consumer = null;
67         String JavaDoc 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 JavaDoc)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 JavaDoc)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 JavaDoc {
89         connection.start();
90         Session JavaDoc 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 JavaDoc consumer = null;
98         String JavaDoc 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 JavaDoc)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 JavaDoc {
125         connection.start();
126         Session JavaDoc 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 JavaDoc consumer = null;
133         String JavaDoc 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 JavaDoc)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 JavaDoc)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 JavaDoc session, Destination JavaDoc destination, String JavaDoc text) throws JMSException JavaDoc {
156         MessageProducer JavaDoc producer = session.createProducer(destination);
157         producer.send(session.createTextMessage(text));
158         producer.close();
159     }
160 }
161
Popular Tags