You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ro...@apache.org on 2014/02/12 00:13:53 UTC
svn commit: r1567451 - in /qpid/jms/trunk/src:
main/java/org/apache/qpid/jms/impl/MessageImpl.java
test/java/org/apache/qpid/jms/impl/MessageImplTest.java
Author: robbie
Date: Tue Feb 11 23:13:53 2014
New Revision: 1567451
URL: http://svn.apache.org/r1567451
Log:
QPIDJMS-9: implement get/setJMSCorrelationIDAsBytes
Modified:
qpid/jms/trunk/src/main/java/org/apache/qpid/jms/impl/MessageImpl.java
qpid/jms/trunk/src/test/java/org/apache/qpid/jms/impl/MessageImplTest.java
Modified: qpid/jms/trunk/src/main/java/org/apache/qpid/jms/impl/MessageImpl.java
URL: http://svn.apache.org/viewvc/qpid/jms/trunk/src/main/java/org/apache/qpid/jms/impl/MessageImpl.java?rev=1567451&r1=1567450&r2=1567451&view=diff
==============================================================================
--- qpid/jms/trunk/src/main/java/org/apache/qpid/jms/impl/MessageImpl.java (original)
+++ qpid/jms/trunk/src/main/java/org/apache/qpid/jms/impl/MessageImpl.java Tue Feb 11 23:13:53 2014
@@ -21,7 +21,9 @@ package org.apache.qpid.jms.impl;
import static org.apache.qpid.jms.impl.ClientProperties.JMS_AMQP_TTL;
import static org.apache.qpid.jms.impl.MessageIdHelper.JMS_ID_PREFIX;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
@@ -235,15 +237,37 @@ public abstract class MessageImpl<T exte
@Override
public byte[] getJMSCorrelationIDAsBytes() throws JMSException
{
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("Not Implemented");
+ Object correlationId = _amqpMessage.getCorrelationId();
+ if(correlationId == null)
+ {
+ return null;
+ }
+ else if(correlationId instanceof ByteBuffer)
+ {
+ ByteBuffer dup = ((ByteBuffer) correlationId).duplicate();
+ byte[] bytes = new byte[dup.remaining()];
+ dup.get(bytes);
+
+ return bytes;
+ }
+ else
+ {
+ throw new QpidJmsException("The underlying correlation-id is not binary and so can't be returned");
+ }
}
@Override
public void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException
{
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("Not Implemented");
+ if(correlationID == null)
+ {
+ _amqpMessage.setCorrelationId(correlationID);
+ }
+ else
+ {
+ byte[] bytes = Arrays.copyOf(correlationID, correlationID.length);
+ _amqpMessage.setCorrelationId(ByteBuffer.wrap(bytes));
+ }
}
@Override
Modified: qpid/jms/trunk/src/test/java/org/apache/qpid/jms/impl/MessageImplTest.java
URL: http://svn.apache.org/viewvc/qpid/jms/trunk/src/test/java/org/apache/qpid/jms/impl/MessageImplTest.java?rev=1567451&r1=1567450&r2=1567451&view=diff
==============================================================================
--- qpid/jms/trunk/src/test/java/org/apache/qpid/jms/impl/MessageImplTest.java (original)
+++ qpid/jms/trunk/src/test/java/org/apache/qpid/jms/impl/MessageImplTest.java Tue Feb 11 23:13:53 2014
@@ -23,6 +23,8 @@ package org.apache.qpid.jms.impl;
import static org.apache.qpid.jms.impl.ClientProperties.JMS_AMQP_TTL;
import static org.junit.Assert.*;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
import java.util.Enumeration;
import javax.jms.Destination;
@@ -35,6 +37,7 @@ import javax.jms.Topic;
import org.apache.qpid.jms.QpidJmsTestCase;
import org.apache.qpid.jms.engine.AmqpMessage;
import org.apache.qpid.jms.engine.TestAmqpMessage;
+import org.apache.qpid.proton.amqp.Binary;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
@@ -1186,6 +1189,119 @@ public class MessageImplTest extends Qpi
assertEquals("expected JMSCorrelationID value not present", expectedJmsCorrelationId, _testMessage.getJMSCorrelationID());
}
+ @Test
+ public void testGetJMSCorrelationIDAsBytesOnNewMessageReturnsNull() throws Exception
+ {
+ assertNull("expected JMSCorrelationID bytes to be null on new message", _testMessage.getJMSCorrelationIDAsBytes());
+ }
+
+ /**
+ * Test that receiving a message with a binary typed correlation-id value results in the expected
+ * JMSCorrelationID value being returned via {@link MessageImpl#getJMSCorrelationIDAsBytes()}
+ * i.e. the bytes contained in the binary value.
+ */
+ @Test
+ public void testGetJMSCorrelationIDAsBytesOnReceivedMessageWithBinaryId() throws Exception
+ {
+ String baseId = "myBaseId";
+ byte[] expectedJmsCorrelationIdAsBytes = baseId.getBytes();
+
+ ByteBuffer binary = ByteBuffer.wrap(expectedJmsCorrelationIdAsBytes);
+ _testAmqpMessage.setCorrelationId(binary);
+ _testMessage = TestMessageImpl.createReceivedMessage(_testAmqpMessage, _mockSessionImpl, _mockConnectionImpl, null);
+
+ assertTrue("expected JMSCorrelationID bytes value not present", Arrays.equals(expectedJmsCorrelationIdAsBytes, _testMessage.getJMSCorrelationIDAsBytes()));
+ }
+
+ /**
+ * Test that the JMSCorrelationID value being returned via {@link MessageImpl#getJMSCorrelationIDAsBytes()}
+ * is a copy of the underlying data, i.e. not the value provided, and different on each call.
+ */
+ @Test
+ public void testGetJMSCorrelationIDAsBytesOnReceivedMessageWithBinaryIdReturnsArrayCopy() throws Exception
+ {
+ String baseId = "myBaseId";
+ byte[] expectedJmsCorrelationIdAsBytes = baseId.getBytes();
+
+ ByteBuffer binary = ByteBuffer.wrap(expectedJmsCorrelationIdAsBytes);
+ _testAmqpMessage.setCorrelationId(binary);
+ _testMessage = TestMessageImpl.createReceivedMessage(_testAmqpMessage, _mockSessionImpl, _mockConnectionImpl, null);
+
+ //check the return value doesn't contain the original bytes
+ byte[] jmsCorrelationIDAsBytes1 = _testMessage.getJMSCorrelationIDAsBytes();
+ assertNotSame("expected byte arrays to be different objects", expectedJmsCorrelationIdAsBytes, jmsCorrelationIDAsBytes1);
+
+ //check a second call returns a further different array object
+ assertNotSame("expected byte arrays to be different objects", jmsCorrelationIDAsBytes1, _testMessage.getJMSCorrelationIDAsBytes());
+ }
+
+ /**
+ * Test that receiving a message with a binary typed correlation-id value results in the expected
+ * JMSCorrelationID value being returned via {@link MessageImpl#getJMSCorrelationIDAsBytes()}
+ * i.e. the bytes contained in the binary value.
+ */
+ @Test
+ public void testGetJMSCorrelationIDAsBytesOnReceivedMessageWithStringIdThrowsJMSE() throws Exception
+ {
+ String baseId = "myBaseId";
+
+ _testAmqpMessage.setCorrelationId(baseId);
+ _testMessage = TestMessageImpl.createReceivedMessage(_testAmqpMessage, _mockSessionImpl, _mockConnectionImpl, null);
+
+ try
+ {
+ _testMessage.getJMSCorrelationIDAsBytes();
+ fail("expected exception now thrown");
+ }
+ catch(JMSException jmse)
+ {
+ //expected
+ }
+ }
+
+ /**
+ * Test that the setting a JMSCorrelationID value via {@link MessageImpl#getJMSCorrelationIDAsBytes()}
+ * results in a copy of the bytes being set on the underlying message, i.e. not the value provided.
+ */
+ @Test
+ public void testSetJMSCorrelationIDAsBytesOnNewMessageWithBinaryIdSetsArrayCopyOnUnderlying() throws Exception
+ {
+ String baseId = "myBaseId";
+ byte[] providedJmsCorrelationIdAsBytes = baseId.getBytes();
+
+ assertNull("unexpected underlying correlationId value found", _testAmqpMessage.getCorrelationId());
+ _testMessage.setJMSCorrelationIDAsBytes(providedJmsCorrelationIdAsBytes);
+
+ Object id = _testAmqpMessage.getCorrelationId();
+ assertNotNull("expected underlying correlationId value to be set", id);
+ assertTrue("unexpected type of underlying correlation-id object", id instanceof ByteBuffer);
+ ByteBuffer buf = (ByteBuffer) id;
+ assertNotSame("expected byte arrays to be different objects", providedJmsCorrelationIdAsBytes, buf.array());
+ }
+
+ /**
+ * Test that the setting a JMSCorrelationID value via {@link MessageImpl#getJMSCorrelationIDAsBytes()}
+ * results in a copy of the bytes being set on the underlying message, i.e. not the value provided.
+ */
+ @Test
+ public void testSetJMSCorrelationIDAsBytesToNullOnRecievedMessageWithBinaryIdClearsValue() throws Exception
+ {
+ String baseId = "myBaseId";
+ byte[] expectedJmsCorrelationIdAsBytes = baseId.getBytes();
+
+ ByteBuffer binary = ByteBuffer.wrap(expectedJmsCorrelationIdAsBytes);
+ _testAmqpMessage.setCorrelationId(binary);
+ _testMessage = TestMessageImpl.createReceivedMessage(_testAmqpMessage, _mockSessionImpl, _mockConnectionImpl, null);
+
+ //check setting null clears the underlying value
+ assertNotNull("expected underlying correlationId value to be set", _testAmqpMessage.getCorrelationId());
+ _testMessage.setJMSCorrelationIDAsBytes(null);
+ assertNull("expected underlying correlationId value to be cleared", _testAmqpMessage.getCorrelationId());
+
+ //check the JMS message returns null
+ assertNull("expected correlationId value to be cleared", _testMessage.getJMSCorrelationIDAsBytes());
+ }
+
// ====== JMS_AMQP_TTL property =======
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org