KickJava   Java API By Example, From Geeks To Geeks.

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


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 JmsQueueWildcardSendReceiveTest 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     /**
44      * Sets a test to have a queue destination and non-persistent delivery mode.
45      *
46      * @see junit.framework.TestCase#setUp()
47      */

48     protected void setUp() throws Exception JavaDoc {
49         topic = false;
50         deliveryMode = DeliveryMode.NON_PERSISTENT;
51         super.setUp();
52     }
53     
54     /**
55      * Returns the consumer subject.
56      *
57      * @return String - consumer subject
58      * @see org.apache.activemq.test.TestSupport#getConsumerSubject()
59      */

60     protected String JavaDoc getConsumerSubject(){
61         return "FOO.>";
62     }
63     
64     /**
65      * Returns the producer subject.
66      *
67      * @return String - producer subject
68      * @see org.apache.activemq.test.TestSupport#getProducerSubject()
69      */

70     protected String JavaDoc getProducerSubject(){
71         return "FOO.BAR.HUMBUG";
72     }
73
74     public void testReceiveWildcardQueueEndAsterisk() throws Exception JavaDoc {
75         connection.start();
76         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
77
78         ActiveMQDestination destination1 = (ActiveMQDestination) session.createQueue(destination1String);
79         ActiveMQDestination destination3 = (ActiveMQDestination) session.createQueue(destination3String);
80
81         Message m = null;
82         MessageConsumer JavaDoc consumer = null;
83         String JavaDoc text = null;
84
85         sendMessage(session,destination1,destination1String);
86         sendMessage(session,destination3,destination3String);
87         ActiveMQDestination destination6 = (ActiveMQDestination) session.createQueue("TEST.ONE.*");
88         consumer = session.createConsumer(destination6);
89         m = consumer.receive(1000);
90         assertNotNull(m);
91         text = ((TextMessage JavaDoc)m).getText();
92         if(!(text.equals(destination1String) || text.equals(destination3String))) {
93             fail("unexpected message:" + text);
94         }
95         m = consumer.receive(1000);
96         assertNotNull(m);
97         text = ((TextMessage JavaDoc)m).getText();
98         if(!(text.equals(destination1String) || text.equals(destination3String))) {
99             fail("unexpected message:" + text);
100         }
101         assertNull(consumer.receiveNoWait());
102     }
103
104     public void testReceiveWildcardQueueEndGreaterThan() throws Exception JavaDoc {
105         connection.start();
106         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
107
108         ActiveMQDestination destination1 = (ActiveMQDestination) session.createQueue(destination1String);
109         ActiveMQDestination destination2 = (ActiveMQDestination) session.createQueue(destination2String);
110         ActiveMQDestination destination3 = (ActiveMQDestination) session.createQueue(destination3String);
111
112         Message m = null;
113         MessageConsumer JavaDoc consumer = null;
114         String JavaDoc text = null;
115
116         sendMessage(session,destination1,destination1String);
117         sendMessage(session,destination2,destination2String);
118         sendMessage(session,destination3,destination3String);
119         ActiveMQDestination destination7 = (ActiveMQDestination) session.createQueue("TEST.ONE.>");
120         consumer = session.createConsumer(destination7);
121         m = consumer.receive(1000);
122         assertNotNull(m);
123         text = ((TextMessage JavaDoc)m).getText();
124         if(!(text.equals(destination1String) || text.equals(destination2String) || text.equals(destination3String))) {
125             fail("unexpected message:" + text);
126         }
127         m = consumer.receive(1000);
128         assertNotNull(m);
129         if(!(text.equals(destination1String) || text.equals(destination2String) || text.equals(destination3String))) {
130             fail("unexpected message:" + text);
131         }
132         m = consumer.receive(1000);
133         assertNotNull(m);
134         if(!(text.equals(destination1String) || text.equals(destination2String) || text.equals(destination3String))) {
135             fail("unexpected message:" + text);
136         }
137         assertNull(consumer.receiveNoWait());
138     }
139
140     public void testReceiveWildcardQueueMidAsterisk() throws Exception JavaDoc {
141         connection.start();
142         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
143
144         ActiveMQDestination destination1 = (ActiveMQDestination) session.createQueue(destination1String);
145         ActiveMQDestination destination4 = (ActiveMQDestination) session.createQueue(destination4String);
146
147         Message m = null;
148         MessageConsumer JavaDoc consumer = null;
149         String JavaDoc text = null;
150
151         sendMessage(session,destination1,destination1String);
152         sendMessage(session,destination4,destination4String);
153         ActiveMQDestination destination8 = (ActiveMQDestination) session.createQueue("TEST.*.ONE");
154         consumer = session.createConsumer(destination8);
155         m = consumer.receive(1000);
156         assertNotNull(m);
157         text = ((TextMessage JavaDoc)m).getText();
158         if(!(text.equals(destination1String) || text.equals(destination4String))) {
159             fail("unexpected message:" + text);
160         }
161         m = consumer.receive(1000);
162         assertNotNull(m);
163         text = ((TextMessage JavaDoc)m).getText();
164         if(!(text.equals(destination1String) || text.equals(destination4String))) {
165             fail("unexpected message:" + text);
166         }
167         assertNull(consumer.receiveNoWait());
168
169     }
170
171     private void sendMessage(Session JavaDoc session, Destination JavaDoc destination, String JavaDoc text) throws JMSException JavaDoc {
172         MessageProducer JavaDoc producer = session.createProducer(destination);
173         producer.send(session.createTextMessage(text));
174         producer.close();
175     }
176 }
177
Popular Tags