KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Enumeration JavaDoc;
21
22 import javax.jms.DeliveryMode JavaDoc;
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageConsumer JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Queue JavaDoc;
27 import javax.jms.QueueBrowser JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.TextMessage JavaDoc;
30
31 import junit.framework.Test;
32
33 import org.apache.activemq.command.ActiveMQDestination;
34 import org.apache.activemq.command.ActiveMQMessage;
35
36 public class JMSUsecaseTest extends JmsTestSupport {
37
38     public static Test suite() {
39         return suite(JMSUsecaseTest.class);
40     }
41     public static void main(String JavaDoc[] args) {
42         junit.textui.TestRunner.run(suite());
43     }
44
45     public ActiveMQDestination destination;
46     public int deliveryMode;
47     public int prefetch;
48     public byte destinationType;
49     public boolean durableConsumer;
50
51     public void initCombosForTestQueueBrowser() {
52         addCombinationValues( "deliveryMode", new Object JavaDoc[]{
53                 new Integer JavaDoc(DeliveryMode.NON_PERSISTENT),
54                 new Integer JavaDoc(DeliveryMode.PERSISTENT)} );
55         addCombinationValues( "destinationType", new Object JavaDoc[]{
56                 new Byte JavaDoc(ActiveMQDestination.QUEUE_TYPE),
57                 new Byte JavaDoc(ActiveMQDestination.TEMP_QUEUE_TYPE),
58                 } );
59     }
60     public void testQueueBrowser() throws Exception JavaDoc {
61         
62         // Send a message to the broker.
63
connection.start();
64         Session JavaDoc session = connection.createSession(false, Session.SESSION_TRANSACTED);
65         destination = createDestination(session, destinationType);
66         sendMessages(session, destination, 5);
67
68         
69         QueueBrowser JavaDoc browser = session.createBrowser((Queue JavaDoc) destination);
70         Enumeration JavaDoc enumeration = browser.getEnumeration();
71         for(int i=0; i < 5; i++) {
72             Thread.sleep(100);
73             assertTrue(enumeration.hasMoreElements());
74             Message m = (Message)enumeration.nextElement();
75             assertNotNull(m);
76             assertEquals(""+i, ((TextMessage JavaDoc)m).getText());
77         }
78         assertFalse(enumeration.hasMoreElements());
79     }
80
81     public void initCombosForTestSendReceive() {
82         addCombinationValues( "deliveryMode", new Object JavaDoc[]{
83                 new Integer JavaDoc(DeliveryMode.NON_PERSISTENT),
84                 new Integer JavaDoc(DeliveryMode.PERSISTENT)} );
85         addCombinationValues( "destinationType", new Object JavaDoc[]{
86                 new Byte JavaDoc(ActiveMQDestination.QUEUE_TYPE),
87                 new Byte JavaDoc(ActiveMQDestination.TOPIC_TYPE),
88                 new Byte JavaDoc(ActiveMQDestination.TEMP_QUEUE_TYPE),
89                 new Byte JavaDoc(ActiveMQDestination.TEMP_TOPIC_TYPE)} );
90     }
91     public void testSendReceive() throws Exception JavaDoc {
92         // Send a message to the broker.
93
connection.start();
94         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
95         destination = createDestination(session, destinationType);
96         MessageProducer JavaDoc producer = session.createProducer(destination);
97         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
98         ActiveMQMessage message = new ActiveMQMessage();
99         producer.send(message);
100         
101         // Make sure only 1 message was delivered.
102
assertNotNull(consumer.receive(1000));
103         assertNull(consumer.receiveNoWait());
104     }
105
106     public void initCombosForTestSendReceiveTransacted() {
107         addCombinationValues( "deliveryMode", new Object JavaDoc[]{
108                 new Integer JavaDoc(DeliveryMode.NON_PERSISTENT),
109                 new Integer JavaDoc(DeliveryMode.PERSISTENT)} );
110         addCombinationValues( "destinationType", new Object JavaDoc[]{
111                 new Byte JavaDoc(ActiveMQDestination.QUEUE_TYPE),
112                 new Byte JavaDoc(ActiveMQDestination.TOPIC_TYPE),
113                 new Byte JavaDoc(ActiveMQDestination.TEMP_QUEUE_TYPE),
114                 new Byte JavaDoc(ActiveMQDestination.TEMP_TOPIC_TYPE)} );
115     }
116     public void testSendReceiveTransacted() throws Exception JavaDoc {
117         // Send a message to the broker.
118
connection.start();
119         Session JavaDoc session = connection.createSession(true, Session.SESSION_TRANSACTED);
120         destination = createDestination(session, destinationType);
121         MessageProducer JavaDoc producer = session.createProducer(destination);
122         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
123         producer.send(session.createTextMessage("test"));
124         
125         // Message should not be delivered until commit.
126
assertNull(consumer.receiveNoWait());
127         session.commit();
128         
129         // Make sure only 1 message was delivered.
130
Message message = consumer.receive(1000);
131         assertNotNull(message);
132         assertFalse(message.getJMSRedelivered());
133         assertNull(consumer.receiveNoWait());
134         
135         // Message should be redelivered is rollback is used.
136
session.rollback();
137         
138         // Make sure only 1 message was delivered.
139
message = consumer.receive(2000);
140         assertNotNull(message);
141         assertTrue(message.getJMSRedelivered());
142         assertNull(consumer.receiveNoWait());
143         
144         // If we commit now, the message should not be redelivered.
145
session.commit();
146         assertNull(consumer.receiveNoWait());
147     }
148
149 }
150
Popular Tags