/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.hornetq.jms.tests;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.TemporaryTopic;
import javax.jms.TextMessage;
import javax.naming.NamingException;
import org.hornetq.jms.tests.util.ProxyAssertSupport;
/**
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
* @author <a href="mailto:ovidiu@feodorov.com">Ovidiu Feodorov</a>
* @version <tt>$Revision: 8611 $</tt>
*
* $Id: TemporaryDestinationTest.java 8611 2009-12-08 01:06:31Z timfox $
*/
public class TemporaryDestinationTest extends JMSTestCase
{
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
// Attributes ----------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
public void testTemp() throws Exception
{
Connection conn = null;
try
{
conn = JMSTestCase.cf.createConnection();
Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryTopic tempTopic = producerSession.createTemporaryTopic();
MessageProducer producer = producerSession.createProducer(tempTopic);
MessageConsumer consumer = consumerSession.createConsumer(tempTopic);
conn.start();
final String messageText = "This is a message";
Message m = producerSession.createTextMessage(messageText);
producer.send(m);
TextMessage m2 = (TextMessage)consumer.receive(2000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals(messageText, m2.getText());
try
{
tempTopic.delete();
ProxyAssertSupport.fail();
}
catch (javax.jms.IllegalStateException e)
{
// Can't delete temp dest if there are open consumers
}
consumer.close();
tempTopic.delete();
}
finally
{
if (conn != null)
{
conn.close();
}
}
}
public void testTemporaryQueueBasic() throws Exception
{
Connection conn = null;
try
{
conn = JMSTestCase.cf.createConnection();
Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = producerSession.createTemporaryQueue();
MessageProducer producer = producerSession.createProducer(tempQueue);
MessageConsumer consumer = consumerSession.createConsumer(tempQueue);
conn.start();
final String messageText = "This is a message";
Message m = producerSession.createTextMessage(messageText);
producer.send(m);
TextMessage m2 = (TextMessage)consumer.receive(2000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals(messageText, m2.getText());
}
finally
{
if (conn != null)
{
conn.close();
}
}
}
/**
* http://jira.jboss.com/jira/browse/JBMESSAGING-93
*/
public void testTemporaryQueueOnClosedSession() throws Exception
{
Connection producerConnection = null;
try
{
producerConnection = JMSTestCase.cf.createConnection();
Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producerSession.close();
try
{
producerSession.createTemporaryQueue();
ProxyAssertSupport.fail("should throw exception");
}
catch (javax.jms.IllegalStateException e)
{
// OK
}
}
finally
{
if (producerConnection != null)
{
producerConnection.close();
}
}
}
public void testTemporaryQueueDeleteWithConsumer() throws Exception
{
Connection conn = null;
try
{
conn = JMSTestCase.cf.createConnection();
Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = producerSession.createTemporaryQueue();
MessageConsumer consumer = consumerSession.createConsumer(tempQueue);
try
{
tempQueue.delete();
ProxyAssertSupport.fail("Should throw JMSException");
}
catch (JMSException e)
{
// Should fail - you can't delete a temp queue if it has active consumers
}
consumer.close();
}
finally
{
if (conn != null)
{
conn.close();
}
}
}
public void testTemporaryTopicDeleteWithConsumer() throws Exception
{
Connection conn = null;
try
{
conn = JMSTestCase.cf.createConnection();
Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryTopic tempTopic = producerSession.createTemporaryTopic();
MessageConsumer consumer = consumerSession.createConsumer(tempTopic);
try
{
tempTopic.delete();
ProxyAssertSupport.fail("Should throw JMSException");
}
catch (JMSException e)
{
// Should fail - you can't delete a temp topic if it has active consumers
}
consumer.close();
}
finally
{
if (conn != null)
{
conn.close();
}
}
}
public void testTemporaryQueueDeleted() throws Exception
{
Connection conn = null;
try
{
conn = JMSTestCase.cf.createConnection();
Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Make sure temporary queue cannot be used after it has been deleted
TemporaryQueue tempQueue = producerSession.createTemporaryQueue();
MessageProducer producer = producerSession.createProducer(tempQueue);
MessageConsumer consumer = consumerSession.createConsumer(tempQueue);
conn.start();
final String messageText = "This is a message";
Message m = producerSession.createTextMessage(messageText);
producer.send(m);
TextMessage m2 = (TextMessage)consumer.receive(2000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals(messageText, m2.getText());
consumer.close();
tempQueue.delete();
conn.close();
conn = JMSTestCase.cf.createConnection("guest", "guest");
try
{
producer.send(m);
ProxyAssertSupport.fail();
}
catch (JMSException e)
{
}
}
finally
{
if (conn != null)
{
conn.close();
}
}
}
public void testTemporaryTopicBasic() throws Exception
{
Connection conn = null;
try
{
conn = JMSTestCase.cf.createConnection();
Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryTopic tempTopic = producerSession.createTemporaryTopic();
final MessageProducer producer = producerSession.createProducer(tempTopic);
MessageConsumer consumer = consumerSession.createConsumer(tempTopic);
conn.start();
final String messageText = "This is a message";
final Message m = producerSession.createTextMessage(messageText);
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
// this is needed to make sure the main thread has enough time to block
Thread.sleep(500);
producer.send(m);
}
catch (Exception e)
{
log.error(e);
}
}
}, "Producer");
t.start();
TextMessage m2 = (TextMessage)consumer.receive(3000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals(messageText, m2.getText());
t.join();
}
finally
{
if (conn != null)
{
conn.close();
}
}
}
/**
* http://jira.jboss.com/jira/browse/JBMESSAGING-93
*/
public void testTemporaryTopicOnClosedSession() throws Exception
{
Connection producerConnection = null;
try
{
producerConnection = JMSTestCase.cf.createConnection();
Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producerSession.close();
try
{
producerSession.createTemporaryTopic();
ProxyAssertSupport.fail("should throw exception");
}
catch (javax.jms.IllegalStateException e)
{
// OK
}
}
finally
{
if (producerConnection != null)
{
producerConnection.close();
}
}
}
public void testTemporaryTopicShouldNotBeInJNDI() throws Exception
{
Connection producerConnection = null;
try
{
producerConnection = JMSTestCase.cf.createConnection();
Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryTopic tempTopic = producerSession.createTemporaryTopic();
String topicName = tempTopic.getTopicName();
try
{
JMSTestCase.ic.lookup("/topic/" + topicName);
ProxyAssertSupport.fail("The temporary queue should not be bound to JNDI");
}
catch (NamingException e)
{
// Expected
}
}
finally
{
if (producerConnection != null)
{
producerConnection.close();
}
}
}
public void testTemporaryQueueShouldNotBeInJNDI() throws Exception
{
Connection producerConnection = null;
try
{
producerConnection = JMSTestCase.cf.createConnection();
Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = producerSession.createTemporaryQueue();
String queueName = tempQueue.getQueueName();
try
{
JMSTestCase.ic.lookup("/queue/" + queueName);
ProxyAssertSupport.fail("The temporary queue should not be bound to JNDI");
}
catch (NamingException e)
{
// Expected
}
}
finally
{
if (producerConnection != null)
{
producerConnection.close();
}
}
}
/**
* https://jira.jboss.org/jira/browse/JBMESSAGING-1566
*/
public void testCanNotCreateConsumerFromAnotherConnectionForTemporaryQueue() throws Exception
{
Connection conn = JMSTestCase.cf.createConnection();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = sess.createTemporaryQueue();
Connection anotherConn = JMSTestCase.cf.createConnection();
Session sessFromAnotherConn = anotherConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
try
{
sessFromAnotherConn.createConsumer(tempQueue);
ProxyAssertSupport.fail("Only temporary destination's own connection is allowed to create MessageConsumers for them.");
}
catch (JMSException e)
{
}
conn.close();
anotherConn.close();
}
/**
* https://jira.jboss.org/jira/browse/JBMESSAGING-1566
*/
public void testCanNotCreateConsumerFromAnotherCnnectionForTemporaryTopic() throws Exception
{
Connection conn = JMSTestCase.cf.createConnection();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryTopic tempTopic = sess.createTemporaryTopic();
Connection anotherConn = JMSTestCase.cf.createConnection();
Session sessFromAnotherConn = anotherConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
try
{
sessFromAnotherConn.createConsumer(tempTopic);
ProxyAssertSupport.fail("Only temporary destination's own connection is allowed to create MessageConsumers for them.");
}
catch (JMSException e)
{
}
conn.close();
anotherConn.close();
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}