下面列出了怎么用javax.jms.InvalidDestinationException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test(timeout = 60000)
public void testDurableSubscriptionUnsubscribeNoExistingSubThrowsJMSEx() throws Exception {
connection = createAmqpConnection();
connection.setClientID("DURABLE-AMQP");
connection.start();
assertEquals(0, brokerService.getAdminView().getDurableTopicSubscribers().length);
assertEquals(0, brokerService.getAdminView().getInactiveDurableTopicSubscribers().length);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
assertNotNull(session);
BrokerViewMBean broker = getProxyToBroker();
assertEquals(0, broker.getDurableTopicSubscribers().length);
assertEquals(0, broker.getInactiveDurableTopicSubscribers().length);
try {
session.unsubscribe(getSubscriptionName());
fail("Should have thrown an InvalidDestinationException");
} catch (InvalidDestinationException ide) {
}
}
@Test
public void sendToUnknownQueue() throws Exception
{
QueueConnection connection = ((QueueConnection) getConnectionBuilder().build());
try
{
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue invalidDestination = session.createQueue("unknown");
try
{
QueueSender sender = session.createSender(invalidDestination);
sender.send(session.createMessage());
fail("Exception not thrown");
}
catch (InvalidDestinationException e)
{
//PASS
}
}
finally
{
connection.close();
}
}
@Test
public void anonymousSenderSendToUnknownQueue() throws Exception
{
QueueConnection connection = ((QueueConnection) getConnectionBuilder().setSyncPublish(true).build());
try
{
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue invalidDestination = session.createQueue("unknown");
try
{
QueueSender sender = session.createSender(null);
sender.send(invalidDestination, session.createMessage());
fail("Exception not thrown");
}
catch (InvalidDestinationException e)
{
//PASS
}
}
finally
{
connection.close();
}
}
@Test
public void testFQQNTopicWhenQueueDoesNotExist() throws Exception {
Exception e = null;
String queueName = "testQueue";
Connection connection = createConnection(false);
try {
connection.setClientID("FQQNconn");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(multicastAddress.toString() + "::" + queueName);
session.createConsumer(topic);
} catch (InvalidDestinationException ide) {
e = ide;
} finally {
connection.close();
}
assertNotNull(e);
assertTrue(e.getMessage().contains("Queue: '" + queueName + "' does not exist"));
}
/**
* Broker should return exception if no address is passed in FQQN.
* @throws Exception
*/
@Test
public void testQueueSpecial() throws Exception {
server.createQueue(new QueueConfiguration(anycastQ1).setAddress(anycastAddress).setRoutingType(RoutingType.ANYCAST));
Connection connection = createConnection();
Exception expectedException = null;
try {
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//::queue ok!
String specialName = CompositeAddress.toFullyQualified(new SimpleString(""), anycastQ1).toString();
javax.jms.Queue q1 = session.createQueue(specialName);
session.createConsumer(q1);
} catch (InvalidDestinationException e) {
expectedException = e;
}
assertNotNull(expectedException);
assertTrue(expectedException.getMessage().contains("Queue: 'q1' does not exist for address ''"));
}
@Test
public void testCreateConsumerOnNonExistentTopic() throws Exception {
Connection pconn = null;
try {
pconn = createConnection();
Session ps = pconn.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
ps.createConsumer(new Topic() {
@Override
public String getTopicName() throws JMSException {
return "NoSuchTopic";
}
});
ProxyAssertSupport.fail("should throw exception");
} catch (InvalidDestinationException e) {
// OK
}
} finally {
if (pconn != null) {
pconn.close();
}
}
}
@Test
public void testCreateConsumerOnNonExistentQueue() throws Exception {
Connection pconn = null;
try {
pconn = createConnection();
Session ps = pconn.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
ps.createConsumer(new Queue() {
@Override
public String getQueueName() throws JMSException {
return "NoSuchQueue";
}
});
ProxyAssertSupport.fail("should throw exception");
} catch (InvalidDestinationException e) {
// OK
}
} finally {
if (pconn != null) {
pconn.close();
}
}
}
@Test
public void testDurableSubscriptionOnTemporaryTopic() throws Exception {
Connection conn = null;
conn = createConnection();
try {
conn.setClientID("doesn't actually matter");
Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic temporaryTopic = s.createTemporaryTopic();
try {
s.createDurableSubscriber(temporaryTopic, "mySubscription");
ProxyAssertSupport.fail("this should throw exception");
} catch (InvalidDestinationException e) {
// OK
}
} finally {
if (conn != null) {
conn.close();
}
}
}
@Test
public void testCreateProducerOnInexistentDestination() throws Exception {
getJmsServer().getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateQueues(false));
getJmsServer().getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateAddresses(false));
Connection pconn = createConnection();
try {
Session ps = pconn.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
ps.createProducer(ActiveMQJMSClient.createTopic("NoSuchTopic"));
ProxyAssertSupport.fail("should throw exception");
} catch (InvalidDestinationException e) {
// OK
}
} finally {
pconn.close();
}
}
@Test
public void testCreateBrowserOnNonExistentQueue() throws Exception {
Connection pconn = getConnectionFactory().createConnection();
try {
Session ps = pconn.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
ps.createBrowser(new Queue() {
@Override
public String getQueueName() throws JMSException {
return "NoSuchQueue";
}
});
ProxyAssertSupport.fail("should throw exception");
} catch (InvalidDestinationException e) {
// OK
}
} finally {
if (pconn != null) {
pconn.close();
}
}
}
private Destination createDestination(Session session, String destinationName) throws JMSException {
try {
Destination destination = null;
switch (destinationType) {
case DurableTopic:
case Topic:
destination = session.createTopic(destinationName);
break;
case Queue:
destination = session.createQueue(destinationName);
break;
}
return destination;
}
catch (InvalidDestinationException ide) {
throw new CougarFrameworkException("Error creating "+destinationType+" for destination name '"+destinationName+"'",ide);
}
}
@Test(timeout = 60000)
public void testCantConsumeFromTemporaryTopicCreatedOnAnotherConnection() throws Exception {
connection = createAmqpConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryTopic tempTopic = session.createTemporaryTopic();
session.createConsumer(tempTopic);
Connection connection2 = createAmqpConnection();
try {
Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
session2.createConsumer(tempTopic);
fail("should not be able to consumer from temporary topic from another connection");
} catch (InvalidDestinationException ide) {
// expected
}
} finally {
connection2.close();
}
}
@Override
public void setJMSReplyTo(final Destination dest) throws JMSException {
if (dest == null) {
MessageUtil.setJMSReplyTo(message, (String) null);
replyTo = null;
} else {
if (dest instanceof ActiveMQDestination == false) {
throw new InvalidDestinationException("Foreign destination " + dest);
}
String prefix = prefixOf(dest);
ActiveMQDestination jbd = (ActiveMQDestination) dest;
MessageUtil.setJMSReplyTo(message, prefix + jbd.getAddress());
replyTo = jbd;
}
}
@Test(timeout = 60000)
public void testCantConsumeFromTemporaryQueueCreatedOnAnotherConnection() throws Exception {
connection = createAmqpConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = session.createTemporaryQueue();
session.createConsumer(tempQueue);
Connection connection2 = createAmqpConnection();
try {
Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
session2.createConsumer(tempQueue);
fail("should not be able to consumer from temporary queue from another connection");
} catch (InvalidDestinationException ide) {
// expected
}
} finally {
connection2.close();
}
}
@Override
public TopicSubscriber createDurableSubscriber(final Topic topic,
final String name,
String messageSelector,
final boolean noLocal) throws JMSException {
// As per spec. section 4.11
if (sessionType == ActiveMQSession.TYPE_QUEUE_SESSION) {
throw new IllegalStateException("Cannot create a durable subscriber on a QueueSession");
}
checkTopic(topic);
if (!(topic instanceof ActiveMQDestination)) {
throw new InvalidDestinationException("Not an ActiveMQTopic:" + topic);
}
if ("".equals(messageSelector)) {
messageSelector = null;
}
ActiveMQDestination jbdest = (ActiveMQDestination) topic;
if (jbdest.isQueue()) {
throw new InvalidDestinationException("Cannot create a subscriber on a queue");
}
return createConsumer(jbdest, name, messageSelector, noLocal, ConsumerDurability.DURABLE);
}
public void deleteTemporaryQueue(final ActiveMQDestination tempQueue) throws JMSException {
if (!tempQueue.isTemporary()) {
throw new InvalidDestinationException("Not a temporary queue " + tempQueue);
}
try {
QueueQuery response = session.queueQuery(tempQueue.getSimpleAddress());
if (!response.isExists()) {
throw new InvalidDestinationException("Cannot delete temporary queue " + tempQueue.getName() +
" does not exist");
}
if (response.getConsumerCount() > 0) {
throw new IllegalStateException("Cannot delete temporary queue " + tempQueue.getName() +
" since it has subscribers");
}
SimpleString address = tempQueue.getSimpleAddress();
session.deleteQueue(address);
connection.removeTemporaryQueue(address);
} catch (ActiveMQException e) {
throw JMSExceptionHelper.convertFromActiveMQException(e);
}
}
static Destination setCompatibleReplyTo(Destination dest, ClientMessage message) throws InvalidDestinationException {
if (dest == null) {
MessageUtil.setJMSReplyTo(message, (String) null);
return null;
} else {
if (dest instanceof ActiveMQDestination == false) {
throw new InvalidDestinationException("Foreign destination " + dest);
}
ActiveMQDestination jbd = (ActiveMQDestination) dest;
final String address = jbd.getAddress();
if (hasPrefix1X(address)) {
MessageUtil.setJMSReplyTo(message, jbd.getAddress());
} else {
String prefix = prefixOf(dest);
MessageUtil.setJMSReplyTo(message, prefix + jbd.getAddress());
}
return jbd;
}
}
protected void send(JmsMessageProducer producer, Destination dest, Message msg, int deliveryMode, int priority, long timeToLive, boolean disableMsgId, boolean disableTimestamp, long deliveryDelay, CompletionListener listener) throws JMSException {
if (dest == null) {
throw new InvalidDestinationException("Destination must not be null");
}
if (msg == null) {
throw new MessageFormatException("Message must not be null");
}
JmsDestination destination = JmsMessageTransformation.transformDestination(connection, dest);
if (destination.isTemporary() && ((JmsTemporaryDestination) destination).isDeleted()) {
throw new IllegalStateException("Temporary destination has been deleted");
}
send(producer, destination, msg, deliveryMode, priority, timeToLive, disableMsgId, disableTimestamp, deliveryDelay, listener);
}
@Test(timeout = 10000)
public void testPublishMessageOnProvidedTopicWhenNotAnonymous() throws Exception {
Topic topic = session.createTopic(getTestName());
TopicPublisher publisher = session.createPublisher(topic);
Message message = session.createMessage();
try {
publisher.publish(session.createTopic(getTestName() + "1"), message);
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException uoe) {}
try {
publisher.publish((Topic) null, message);
fail("Should throw InvalidDestinationException");
} catch (InvalidDestinationException ide) {}
}
@Test(timeout = 10000)
public void testPublishMessageWithOptionsOnProvidedTopicWhenNotAnonymous() throws Exception {
Topic topic = session.createTopic(getTestName());
TopicPublisher publisher = session.createPublisher(topic);
Message message = session.createMessage();
try {
publisher.publish(session.createTopic(getTestName() + "1"), message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException uoe) {}
try {
publisher.publish((Topic) null, message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
fail("Should throw InvalidDestinationException");
} catch (InvalidDestinationException ide) {}
}
@Test(timeout = 20000)
public void testConnectWithNotFoundErrorThrowsJMSEWhenInvalidContainerHintNotPresent() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
testPeer.rejectConnect(AmqpError.NOT_FOUND, "Virtual Host does not exist", null);
try {
establishAnonymousConnecton(testPeer, true);
fail("Should have thrown JMSException");
} catch (InvalidDestinationException destEx) {
fail("Should not convert to destination exception for this case.");
} catch (JMSException jmsEx) {
LOG.info("Caught expected Exception: {}", jmsEx.getMessage(), jmsEx);
// Expected
} catch (Exception ex) {
fail("Should have thrown JMSException: " + ex);
}
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test
public void testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
final TextMessage sentTextMessage = mock(TextMessage.class);
// correlation ID is queried when response is being created...
given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
// Reply-To is queried when response is being created...
given(sentTextMessage.getJMSReplyTo()).willReturn(null);
TextMessage responseTextMessage = mock(TextMessage.class);
final QueueSession session = mock(QueueSession.class);
given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
@Override
protected Object extractMessage(Message message) {
return message;
}
};
try {
adapter.onMessage(sentTextMessage, session);
fail("expected CouldNotSendReplyException with InvalidDestinationException");
}
catch (ReplyFailureException ex) {
assertEquals(InvalidDestinationException.class, ex.getCause().getClass());
}
verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
verify(delegate).handleMessage(sentTextMessage);
}
@Test
public void emptySendTo() throws JMSException {
MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);
TextMessage reply = mock(TextMessage.class);
Session session = mock(Session.class);
given(session.createTextMessage("content")).willReturn(reply);
assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() ->
listener.onMessage(createSimpleJmsTextMessage("content"), session))
.withCauseInstanceOf(InvalidDestinationException.class);
}
@Test
public void testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
final TextMessage sentTextMessage = mock(TextMessage.class);
// correlation ID is queried when response is being created...
given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
// Reply-To is queried when response is being created...
given(sentTextMessage.getJMSReplyTo()).willReturn(null);
TextMessage responseTextMessage = mock(TextMessage.class);
final QueueSession session = mock(QueueSession.class);
given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
@Override
protected Object extractMessage(Message message) {
return message;
}
};
try {
adapter.onMessage(sentTextMessage, session);
fail("expected CouldNotSendReplyException with InvalidDestinationException");
}
catch (ReplyFailureException ex) {
assertEquals(InvalidDestinationException.class, ex.getCause().getClass());
}
verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
verify(delegate).handleMessage(sentTextMessage);
}
@Test
public void emptySendTo() throws JMSException {
MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);
TextMessage reply = mock(TextMessage.class);
Session session = mock(Session.class);
given(session.createTextMessage("content")).willReturn(reply);
this.thrown.expect(ReplyFailureException.class);
this.thrown.expectCause(Matchers.isA(InvalidDestinationException.class));
listener.onMessage(createSimpleJmsTextMessage("content"), session);
}
@Test
public void testNullDestinationOnSendToAnonymousProducer() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
MessageProducer producer = session.createProducer(null);
try {
producer.send(null, session.createMessage());
fail("Should not be able to send with null destination");
} catch (InvalidDestinationException ide) {}
}
@Test
public void testNullDestinationOnSendToTargetedProducer() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
MessageProducer producer = session.createProducer(session.createTemporaryQueue());
try {
producer.send(null, session.createMessage());
fail("Should not be able to send with null destination");
} catch (InvalidDestinationException ide) {}
}
@Test(timeout = 10000)
public void testCannotCreateConsumerOnTempDestinationFromSomeOtherSource() throws JMSException {
JmsSession session = (JmsSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = new JmsTemporaryQueue("ID:" + UUID.randomUUID().toString());
try {
session.createConsumer(tempQueue);
fail("Should not be able to create a consumer");
} catch (InvalidDestinationException idex) {}
}
public JmsQueueRequestor(QueueSession session, Queue queue) throws JMSException {
super();
if (queue == null) {
throw new InvalidDestinationException("Invalid queue");
}
setSession(session);
setTemporaryQueue(session.createTemporaryQueue());
setSender(session.createSender(queue));
setReceiver(session.createReceiver(getTemporaryQueue()));
}
@Test
public void testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
final TextMessage sentTextMessage = mock(TextMessage.class);
// correlation ID is queried when response is being created...
given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
// Reply-To is queried when response is being created...
given(sentTextMessage.getJMSReplyTo()).willReturn(null);
TextMessage responseTextMessage = mock(TextMessage.class);
final QueueSession session = mock(QueueSession.class);
given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
@Override
protected Object extractMessage(Message message) {
return message;
}
};
try {
adapter.onMessage(sentTextMessage, session);
fail("expected CouldNotSendReplyException with InvalidDestinationException");
} catch(ReplyFailureException ex) {
assertEquals(InvalidDestinationException.class, ex.getCause().getClass());
}
verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
verify(delegate).handleMessage(sentTextMessage);
}