ensure the approproate UnsupportedOperation and InvalidDestination exceptions are thrown from producer send methods
Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/d53274f6 Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/d53274f6 Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/d53274f6 Branch: refs/heads/master Commit: d53274f604970e8b7a7fb457fe754c6078d130f5 Parents: 23e8a22 Author: Robert Gemmell <[email protected]> Authored: Mon Jan 12 12:26:33 2015 +0000 Committer: Robert Gemmell <[email protected]> Committed: Mon Jan 12 12:53:51 2015 +0000 ---------------------------------------------------------------------- .../org/apache/qpid/jms/JmsMessageProducer.java | 21 ++- .../apache/qpid/jms/JmsMessageProducerTest.java | 131 +++++++++++++++++++ 2 files changed, 147 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/d53274f6/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsMessageProducer.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsMessageProducer.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsMessageProducer.java index 4d09c04..50e70a8 100644 --- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsMessageProducer.java +++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsMessageProducer.java @@ -170,7 +170,7 @@ public class JmsMessageProducer implements MessageProducer { */ @Override public void send(Message message) throws JMSException { - send(producerInfo.getDestination(), message, this.deliveryMode, this.priority, this.timeToLive); + send(message, this.deliveryMode, this.priority, this.timeToLive); } /** @@ -195,7 +195,13 @@ public class JmsMessageProducer implements MessageProducer { */ @Override public void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { - send(producerInfo.getDestination(), message, deliveryMode, priority, timeToLive); + checkClosed(); + + if (flexibleDestination) { + throw new UnsupportedOperationException("Using this method is not supported on producers created without an explicit Destination"); + } + + sendMessage(producerInfo.getDestination(), message, deliveryMode, priority, timeToLive); } /** @@ -212,12 +218,17 @@ public class JmsMessageProducer implements MessageProducer { public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { checkClosed(); + if (!flexibleDestination) { + throw new UnsupportedOperationException("Using this method is not supported on producers created with an explicit Destination."); + } + + sendMessage(destination, message, deliveryMode, priority, timeToLive); + } + + private void sendMessage(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { if (destination == null) { throw new InvalidDestinationException("Don't understand null destinations"); } - if (!this.flexibleDestination && !destination.equals(producerInfo.getDestination())) { - throw new UnsupportedOperationException("This producer can only send messages to: " + producerInfo.getDestination().getName()); - } this.session.send(this, destination, message, deliveryMode, priority, timeToLive, disableMessageId, disableTimestamp); } http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/d53274f6/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsMessageProducerTest.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsMessageProducerTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsMessageProducerTest.java new file mode 100644 index 0000000..1d6a870 --- /dev/null +++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsMessageProducerTest.java @@ -0,0 +1,131 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.qpid.jms; + +import static org.junit.Assert.fail; + +import javax.jms.Destination; +import javax.jms.InvalidDestinationException; +import javax.jms.Message; + +import org.apache.qpid.jms.meta.JmsProducerId; +import org.apache.qpid.jms.meta.JmsProducerInfo; +import org.apache.qpid.jms.meta.JmsResource; +import org.apache.qpid.jms.provider.Provider; +import org.apache.qpid.jms.provider.ProviderFuture; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test basic functionality around JmsConnection + */ +public class JmsMessageProducerTest { + + private static final Logger LOG = LoggerFactory.getLogger(JmsMessageProducerTest.class); + + private Provider provider; + private JmsConnection connection; + private JmsSession session; + private JmsProducerId producerId; + + @Before + public void setUp() throws Exception { + connection = Mockito.mock(JmsConnection.class); + session = Mockito.mock(JmsSession.class); + producerId = new JmsProducerId("key"); + + Mockito.when(session.getConnection()).thenReturn(connection); + Mockito.doAnswer(new Answer<Object>() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + LOG.debug("Handling connection createResource call"); + if (args[0] instanceof JmsProducerInfo) { + return args[0]; + } + + throw new IllegalArgumentException("Not implemented"); + } + }).when(connection).createResource(Mockito.any(JmsResource.class)); + } + + @Test + public void testAnonymousProducerThrowsUOEWhenExplictDestinationNotProvided() throws Exception { + JmsMessageProducer producer = new JmsMessageProducer(producerId, session, null); + + Message message = Mockito.mock(Message.class); + try { + producer.send(message); + fail("Expected exception not thrown"); + } catch (UnsupportedOperationException uoe) { + // expected + } + + try { + producer.send(message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + fail("Expected exception not thrown"); + } catch (UnsupportedOperationException uoe) { + // expected + } + } + + @Test + public void testExplicitProducerThrowsUOEWhenExplictDestinationIsProvided() throws Exception { + JmsDestination dest = new JmsQueue("explicitDestination"); + JmsMessageProducer producer = new JmsMessageProducer(producerId, session, dest); + + Message message = Mockito.mock(Message.class); + try { + producer.send(dest, message); + fail("Expected exception not thrown"); + } catch (UnsupportedOperationException uoe) { + // expected + } + + try { + producer.send(dest, message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + fail("Expected exception not thrown"); + } catch (UnsupportedOperationException uoe) { + // expected + } + } + + @Test + public void testAnonymousDestinationProducerThrowsIDEWhenNullDestinationIsProvided() throws Exception { + JmsMessageProducer producer = new JmsMessageProducer(producerId, session, null); + + Message message = Mockito.mock(Message.class); + try { + producer.send(null, message); + fail("Expected exception not thrown"); + } catch (InvalidDestinationException ide) { + // expected + } + + try { + producer.send(null, message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + fail("Expected exception not thrown"); + } catch (InvalidDestinationException ide) { + // expected + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
