1 18 package org.apache.activemq; 19 20 import java.util.Enumeration ; 21 22 import javax.jms.DeliveryMode ; 23 import javax.jms.Message ; 24 import javax.jms.MessageConsumer ; 25 import javax.jms.MessageProducer ; 26 import javax.jms.Queue ; 27 import javax.jms.QueueBrowser ; 28 import javax.jms.Session ; 29 import javax.jms.TextMessage ; 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 [] 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 []{ 53 new Integer (DeliveryMode.NON_PERSISTENT), 54 new Integer (DeliveryMode.PERSISTENT)} ); 55 addCombinationValues( "destinationType", new Object []{ 56 new Byte (ActiveMQDestination.QUEUE_TYPE), 57 new Byte (ActiveMQDestination.TEMP_QUEUE_TYPE), 58 } ); 59 } 60 public void testQueueBrowser() throws Exception { 61 62 connection.start(); 64 Session session = connection.createSession(false, Session.SESSION_TRANSACTED); 65 destination = createDestination(session, destinationType); 66 sendMessages(session, destination, 5); 67 68 69 QueueBrowser browser = session.createBrowser((Queue ) destination); 70 Enumeration 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 )m).getText()); 77 } 78 assertFalse(enumeration.hasMoreElements()); 79 } 80 81 public void initCombosForTestSendReceive() { 82 addCombinationValues( "deliveryMode", new Object []{ 83 new Integer (DeliveryMode.NON_PERSISTENT), 84 new Integer (DeliveryMode.PERSISTENT)} ); 85 addCombinationValues( "destinationType", new Object []{ 86 new Byte (ActiveMQDestination.QUEUE_TYPE), 87 new Byte (ActiveMQDestination.TOPIC_TYPE), 88 new Byte (ActiveMQDestination.TEMP_QUEUE_TYPE), 89 new Byte (ActiveMQDestination.TEMP_TOPIC_TYPE)} ); 90 } 91 public void testSendReceive() throws Exception { 92 connection.start(); 94 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 95 destination = createDestination(session, destinationType); 96 MessageProducer producer = session.createProducer(destination); 97 MessageConsumer consumer = session.createConsumer(destination); 98 ActiveMQMessage message = new ActiveMQMessage(); 99 producer.send(message); 100 101 assertNotNull(consumer.receive(1000)); 103 assertNull(consumer.receiveNoWait()); 104 } 105 106 public void initCombosForTestSendReceiveTransacted() { 107 addCombinationValues( "deliveryMode", new Object []{ 108 new Integer (DeliveryMode.NON_PERSISTENT), 109 new Integer (DeliveryMode.PERSISTENT)} ); 110 addCombinationValues( "destinationType", new Object []{ 111 new Byte (ActiveMQDestination.QUEUE_TYPE), 112 new Byte (ActiveMQDestination.TOPIC_TYPE), 113 new Byte (ActiveMQDestination.TEMP_QUEUE_TYPE), 114 new Byte (ActiveMQDestination.TEMP_TOPIC_TYPE)} ); 115 } 116 public void testSendReceiveTransacted() throws Exception { 117 connection.start(); 119 Session session = connection.createSession(true, Session.SESSION_TRANSACTED); 120 destination = createDestination(session, destinationType); 121 MessageProducer producer = session.createProducer(destination); 122 MessageConsumer consumer = session.createConsumer(destination); 123 producer.send(session.createTextMessage("test")); 124 125 assertNull(consumer.receiveNoWait()); 127 session.commit(); 128 129 Message message = consumer.receive(1000); 131 assertNotNull(message); 132 assertFalse(message.getJMSRedelivered()); 133 assertNull(consumer.receiveNoWait()); 134 135 session.rollback(); 137 138 message = consumer.receive(2000); 140 assertNotNull(message); 141 assertTrue(message.getJMSRedelivered()); 142 assertNull(consumer.receiveNoWait()); 143 144 session.commit(); 146 assertNull(consumer.receiveNoWait()); 147 } 148 149 } 150 | Popular Tags |