qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject qpid-jms git commit: QPIDJMS-207 Handle null message passed to send calls
Date Thu, 10 Nov 2016 02:42:40 GMT
Repository: qpid-jms
Updated Branches:
  refs/heads/master fdedbd246 -> 28b3810a1


QPIDJMS-207 Handle null message passed to send calls

Throw the correct exception when a null message gets passed to a send
method (MessageFormatException) instead of waiting to hit an NPE

Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/28b3810a
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/28b3810a
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/28b3810a

Branch: refs/heads/master
Commit: 28b3810a11bb0c1a5b3033b90bdc25344116b564
Parents: fdedbd2
Author: Timothy Bish <tabish121@gmail.com>
Authored: Wed Nov 9 21:42:19 2016 -0500
Committer: Timothy Bish <tabish121@gmail.com>
Committed: Wed Nov 9 21:42:19 2016 -0500

----------------------------------------------------------------------
 .../java/org/apache/qpid/jms/JmsProducer.java   |  5 ++
 .../java/org/apache/qpid/jms/JmsSession.java    |  5 ++
 .../jms/producer/JmsMessageProducerTest.java    | 69 ++++++++++++++++++++
 .../qpid/jms/producer/JmsProducerTest.java      | 16 +++++
 4 files changed, 95 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/28b3810a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsProducer.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsProducer.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsProducer.java
index 11392f6..3071009 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsProducer.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsProducer.java
@@ -34,6 +34,7 @@ import javax.jms.JMSException;
 import javax.jms.JMSProducer;
 import javax.jms.MapMessage;
 import javax.jms.Message;
+import javax.jms.MessageFormatException;
 import javax.jms.ObjectMessage;
 import javax.jms.TextMessage;
 
@@ -149,6 +150,10 @@ public class JmsProducer implements JMSProducer {
 
     private void doSend(Destination destination, Message message) throws JMSException {
 
+        if (message == null) {
+            throw new MessageFormatException("Message must not be null");
+        }
+
         for (Map.Entry<String, Object> entry : messageProperties.entrySet()) {
             message.setObjectProperty(entry.getKey(), entry.getValue());
         }

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/28b3810a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsSession.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsSession.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsSession.java
index 0bca8bc..e6c85cc 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsSession.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsSession.java
@@ -44,6 +44,7 @@ import javax.jms.JMSException;
 import javax.jms.MapMessage;
 import javax.jms.Message;
 import javax.jms.MessageConsumer;
+import javax.jms.MessageFormatException;
 import javax.jms.MessageListener;
 import javax.jms.MessageProducer;
 import javax.jms.ObjectMessage;
@@ -715,6 +716,10 @@ public class JmsSession implements AutoCloseable, Session, QueueSession,
TopicSe
             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())
{

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/28b3810a/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsMessageProducerTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsMessageProducerTest.java
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsMessageProducerTest.java
index 87f94c8..c0c92c3 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsMessageProducerTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsMessageProducerTest.java
@@ -36,6 +36,7 @@ import javax.jms.IllegalStateException;
 import javax.jms.InvalidDestinationException;
 import javax.jms.JMSException;
 import javax.jms.Message;
+import javax.jms.MessageFormatException;
 import javax.jms.MessageProducer;
 import javax.jms.Session;
 
@@ -328,6 +329,74 @@ public class JmsMessageProducerTest extends JmsConnectionTestSupport
{
     }
 
     @Test(timeout = 10000)
+    public void testAnonymousProducerThrowsMFEWhenNullMessageProvided() throws Exception
{
+        JmsDestination dest = new JmsQueue("explicitDestination");
+        JmsMessageProducer producer = (JmsMessageProducer) session.createProducer(null);
+
+        try {
+            producer.send(dest, (Message) null);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+
+        try {
+            producer.send(dest, (Message) null, completionListener);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+
+        try {
+            producer.send(dest, (Message) null, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY,
Message.DEFAULT_TIME_TO_LIVE);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+
+        try {
+            producer.send(dest, (Message) null, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY,
Message.DEFAULT_TIME_TO_LIVE, completionListener);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+    }
+
+    @Test(timeout = 10000)
+    public void testExplicitProducerThrowsMFEWhenNullMessageProvided() throws Exception {
+        JmsDestination dest = new JmsQueue("explicitDestination");
+        JmsMessageProducer producer = (JmsMessageProducer) session.createProducer(dest);
+
+        try {
+            producer.send((Message) null);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+
+        try {
+            producer.send((Message) null, completionListener);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+
+        try {
+            producer.send((Message) null, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY,
Message.DEFAULT_TIME_TO_LIVE);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+
+        try {
+            producer.send((Message) null, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY,
Message.DEFAULT_TIME_TO_LIVE, completionListener);
+            fail("Expected exception not thrown");
+        } catch (MessageFormatException mfe) {
+            // expected
+        }
+    }
+
+    @Test(timeout = 10000)
     public void testInOrderSendAcksCompletionsReturnInOrder() throws Exception {
         final int MESSAGE_COUNT = 3;
 

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/28b3810a/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsProducerTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsProducerTest.java
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsProducerTest.java
index eeba336..5cc381e 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsProducerTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/producer/JmsProducerTest.java
@@ -814,6 +814,22 @@ public class JmsProducerTest extends JmsConnectionTestSupport {
     //----- Test Send Methods -----------------------------------------------//
 
     @Test
+    public void testSendNullMessageThrowsMFRE() throws JMSException {
+        JmsSession session = Mockito.mock(JmsSession.class);
+        JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class);
+
+        JmsProducer producer = new JmsProducer(session, messageProducer);
+
+        try {
+            producer.send(JMS_DESTINATION, (Message) null);
+            fail("Should throw a MessageFormatRuntimeException");
+        } catch (MessageFormatRuntimeException mfre) {
+        } catch (Exception e) {
+            fail("Should throw a MessageFormatRuntimeException");
+        }
+    }
+
+    @Test
     public void testSendJMSMessage() throws JMSException {
         JmsSession session = Mockito.mock(JmsSession.class);
         JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message