/* * This class comes as is, no warranties whatsoever. * Copyright Comerge AG 2009. All Rights Reserved." * * http://comerge.net */ package org.apache.activemq.internal.tests; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; import java.util.UUID; import java.util.concurrent.TimeoutException; import javax.jms.Connection; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.tests.EmbeddedActiveMQTestCase; import org.apache.activemq.util.IndentPrinter; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * @author Benno Baumgartner (benno.baumgartner@comerge.net) */ public class ActiveMQSimpleTests extends EmbeddedActiveMQTestCase { /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { } /** * @throws java.lang.Exception */ @After public void tearDown() throws Exception { } @Test public void sendPlainActiveMQ() throws Exception { ActiveMQConnectionFactory factory= new ActiveMQConnectionFactory(MESSAGE_QUEUE_ADDRESS); Connection senderConnection= factory.createConnection(); try { senderConnection.start(); Session senderSession= senderConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue senderDestination= senderSession.createQueue("sendPlainActiveMQ"); MessageProducer sender= senderSession.createProducer(senderDestination); Connection receiverConnection= factory.createConnection(); try { receiverConnection.start(); Session receiverSession= receiverConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue receiverDestination= receiverSession.createQueue("sendPlainActiveMQ"); MessageConsumer consumer= receiverSession.createConsumer(receiverDestination); String messageContent= UUID.randomUUID().toString(); TextMessage message= senderSession.createTextMessage(messageContent); sender.send(message); Message receivedMessage= receiveMessage(consumer); assertTrue("Expected a TextMessage but was " + receivedMessage.getClass(), receivedMessage instanceof TextMessage); String receivedContent= ((TextMessage) receivedMessage).getText(); assertEquals(messageContent, receivedContent); ((ActiveMQConnection) receiverConnection).getConnectionStats().dump(new IndentPrinter()); } finally { receiverConnection.stop(); } } finally { senderConnection.stop(); } } private static Message receiveMessage(MessageConsumer consumer) throws TimeoutException, JMSException { int count= 10; while (true) { Message result= consumer.receive(1000); if (result != null) { return result; } else if (count <= 0) { throw new TimeoutException("Could not receive message within timeout"); } count--; } } }