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/14 18:44:06 UTC
svn commit: r1568389 - 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: Fri Feb 14 17:44:06 2014
New Revision: 1568389
URL: http://svn.apache.org/r1568389
Log:
QPIDJMS-9: add some additional tests for JMSXUserID, consolidate MFE creation into utility methods
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=1568389&r1=1568388&r2=1568389&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 Fri Feb 14 17:44:06 2014
@@ -188,7 +188,7 @@ public abstract class MessageImpl<T exte
object instanceof Double || object instanceof String|| object == null;
if(!valid)
{
- throw new MessageFormatException("Invalid object property value type: " + object.getClass());
+ throw createMessageFormatException("Invalid object property value type: " + object.getClass());
}
return true;
@@ -228,16 +228,12 @@ public abstract class MessageImpl<T exte
}
catch (UnsupportedEncodingException e)
{
- MessageFormatException mfe = new MessageFormatException("Unable to encode user id");
- mfe.setLinkedException(e);
- mfe.initCause(e);
-
- throw mfe;
+ throw createMessageFormatException("Unable to encode user id", e);
}
}
else
{
- throw new MessageFormatException(JMSXUSERID + " must be a String");
+ throw createMessageFormatException(JMSXUSERID + " must be a String");
}
}
@@ -258,7 +254,7 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException(JMS_AMQP_TTL + " must be a long with value in range 0 to 2^31 - 1");
+ throw createMessageFormatException(JMS_AMQP_TTL + " must be a long with value in range 0 to 2^31 - 1");
}
}
@@ -294,15 +290,28 @@ public abstract class MessageImpl<T exte
}
catch (UnsupportedEncodingException e)
{
- MessageFormatException mfe = new MessageFormatException("Unable to decode user id");
- mfe.setLinkedException(e);
- mfe.initCause(e);
-
- throw mfe;
+ throw createMessageFormatException("Unable to decode user id", e);
}
}
}
+ private MessageFormatException createMessageFormatException(String message)
+ {
+ return createMessageFormatException(message, null);
+ }
+
+ private MessageFormatException createMessageFormatException(String message, Exception cause)
+ {
+ MessageFormatException mfe = new MessageFormatException(message);
+ if(cause != null)
+ {
+ mfe.setLinkedException(cause);
+ mfe.initCause(cause);
+ }
+
+ return mfe;
+ }
+
private boolean propertyExistsJMSXUserID()
{
return _amqpMessage.getUserId() != null;
@@ -678,8 +687,9 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException("Property " + name + " of type " + value.getClass().getName()
- + " cannot be converted to boolean.");
+ String message = "Property " + name + " of type " + value.getClass().getName() + " cannot be converted to boolean.";
+
+ throw createMessageFormatException(message);
}
}
@@ -698,8 +708,9 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException("Property " + name + " of type " + value.getClass().getName()
- + " cannot be converted to byte.");
+ String message = "Property " + name + " of type " + value.getClass().getName() + " cannot be converted to byte.";
+
+ throw createMessageFormatException(message);
}
}
@@ -722,8 +733,9 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException("Property " + name + " of type " + value.getClass().getName()
- + " cannot be converted to short.");
+ String message = "Property " + name + " of type " + value.getClass().getName() + " cannot be converted to short.";
+
+ throw createMessageFormatException(message);
}
}
@@ -750,8 +762,9 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException("Property " + name + " of type " + value.getClass().getName()
- + " cannot be converted to int.");
+ String message = "Property " + name + " of type " + value.getClass().getName() + " cannot be converted to int.";
+
+ throw createMessageFormatException(message);
}
}
@@ -782,8 +795,9 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException("Property " + name + " of type " + value.getClass().getName()
- + " cannot be converted to long.");
+ String message = "Property " + name + " of type " + value.getClass().getName() + " cannot be converted to long.";
+
+ throw createMessageFormatException(message);
}
}
@@ -802,8 +816,9 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException("Property " + name + " of type " + value.getClass().getName()
- + " cannot be converted to float.");
+ String message = "Property " + name + " of type " + value.getClass().getName() + " cannot be converted to float.";
+
+ throw createMessageFormatException(message);
}
}
@@ -826,8 +841,9 @@ public abstract class MessageImpl<T exte
}
else
{
- throw new MessageFormatException("Property " + name + " of type " + value.getClass().getName()
- + " cannot be converted to double.");
+ String message = "Property " + name + " of type " + value.getClass().getName() + " cannot be converted to double.";
+
+ throw createMessageFormatException(message);
}
}
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=1568389&r1=1568388&r2=1568389&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 Fri Feb 14 17:44:06 2014
@@ -21,6 +21,7 @@
package org.apache.qpid.jms.impl;
import static org.apache.qpid.jms.impl.ClientProperties.JMS_AMQP_TTL;
+import static org.apache.qpid.jms.impl.ClientProperties.JMSXUSERID;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
@@ -1760,13 +1761,13 @@ public class MessageImplTest extends Qpi
@Test
public void testGetJMSXUserIDIsNullOnNewMessage() throws Exception
{
- assertNull("did not expect a JMSXUserID value to be present", _testMessage.getStringProperty(ClientProperties.JMSXUSERID));
+ assertNull("did not expect a JMSXUserID value to be present", _testMessage.getStringProperty(JMSXUSERID));
}
@Test
public void testGetJMSXUserIDDoesNotExistOnNewMessage() throws Exception
{
- assertFalse("did not expect a JMSXUserID value to be present", _testMessage.propertyExists(ClientProperties.JMSXUSERID));
+ assertFalse("did not expect a JMSXUserID value to be present", _testMessage.propertyExists(JMSXUSERID));
}
/**
@@ -1778,13 +1779,65 @@ public class MessageImplTest extends Qpi
{
String myUserId = "myUserId";
byte[] myUserIdBytes = myUserId.getBytes("UTF-8");
- _testMessage.setStringProperty(ClientProperties.JMSXUSERID, myUserId);
+ _testMessage.setStringProperty(JMSXUSERID, myUserId);
- assertTrue("expected a JMSXUserID value to be present", _testMessage.propertyExists(ClientProperties.JMSXUSERID));
assertTrue("unexpected userId bytes value on underlying message", Arrays.equals(myUserIdBytes, _testAmqpMessage.getUserId()));
}
/**
+ * Test that setting the JMSXUserID property, which is stored in the AMQP message
+ * user-id field, causes the {@link Message#propertyExists(String)} to return
+ * true;
+ */
+ @Test
+ public void testSetJMSXUserIDEnsuresThatPropertyExists() throws Exception
+ {
+ String myUserId = "myUserId";
+ _testMessage.setStringProperty(JMSXUSERID, myUserId);
+
+ assertTrue("expected a JMSXUserID value to be indicated as present", _testMessage.propertyExists(JMSXUSERID));
+ }
+
+ /**
+ * Test that setting the JMSXUserID property, which is stored in the AMQP message
+ * user-id field, causes the {@link Message#getPropertyNames()} to result to contain it.
+ */
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testSetJMSXUserIDEnsuresThatPropertyNamesContainsIt() throws Exception
+ {
+ //verify the name doesn't exist originally
+ Enumeration<String> names = (Enumeration<String>) _testMessage.getPropertyNames();
+ boolean containsJMSXUserId = false;
+ while(names.hasMoreElements())
+ {
+ String prop = names.nextElement();
+ if(JMSXUSERID.equals(prop))
+ {
+ containsJMSXUserId = true;
+ }
+ }
+
+ assertFalse("Didn't expect to find JMSXUserId in property names yet", containsJMSXUserId);
+
+ //set property
+ _testMessage.setStringProperty(JMSXUSERID, "value");
+
+ //verify the name now exists
+ names = (Enumeration<String>) _testMessage.getPropertyNames();
+ while(names.hasMoreElements())
+ {
+ String prop = names.nextElement();
+ if(JMSXUSERID.equals(prop))
+ {
+ containsJMSXUserId = true;
+ }
+ }
+
+ assertTrue("Didn't find JMSXUserId in property names", containsJMSXUserId);
+ }
+
+ /**
* Test that setting the JMSXUserID property does not set an entry in the
* application-properties section of the underlying AMQP message
*/
@@ -1792,7 +1845,7 @@ public class MessageImplTest extends Qpi
public void testSetJMSXUserIDDoesNotSetApplicationProperty() throws Exception
{
String myUserId = "myUserId";
- _testMessage.setStringProperty(ClientProperties.JMSXUSERID, myUserId);
+ _testMessage.setStringProperty(JMSXUSERID, myUserId);
assertEquals("expected no application-properties section to be present", 0, _testAmqpMessage.getApplicationPropertyNames().size());
}
@@ -1805,15 +1858,35 @@ public class MessageImplTest extends Qpi
public void testSetJMSXUserIDNullClearsUnderlyingMessageUserId() throws Exception
{
String myUserId = "myUserId";
- _testMessage.setStringProperty(ClientProperties.JMSXUSERID, myUserId);
+ _testMessage.setStringProperty(JMSXUSERID, myUserId);
- _testMessage.setStringProperty(ClientProperties.JMSXUSERID, null);
+ _testMessage.setStringProperty(JMSXUSERID, null);
- assertFalse("did not expect a JMSXUserID value to be present", _testMessage.propertyExists(ClientProperties.JMSXUSERID));
+ assertFalse("did not expect a JMSXUserID value to be present", _testMessage.propertyExists(JMSXUSERID));
assertNull("unexpected userId bytes value on underlying message", _testAmqpMessage.getUserId());
}
/**
+ * Test that setting the JMSXUserID property to a non-String instance
+ * causes an MFE to be thrown.
+ */
+ @Test
+ public void testSetJMSXUserIDToNonStringInstanceThrowsMFE() throws Exception
+ {
+ long myUserId = 1L;
+
+ try
+ {
+ _testMessage.setLongProperty(JMSXUSERID, myUserId);
+ fail("expected exception not thrown");
+ }
+ catch(MessageFormatException mfe)
+ {
+ //expected
+ }
+ }
+
+ /**
* Test that the JMSXUserID property returns the expected value for a message
* received with the user-id field set.
*/
@@ -1826,8 +1899,8 @@ public class MessageImplTest extends Qpi
_testMessage = TestMessageImpl.createReceivedMessage(_testAmqpMessage, _mockSessionImpl, _mockConnectionImpl, null);
- assertTrue("expected a JMSXUserID value to be present", _testMessage.propertyExists(ClientProperties.JMSXUSERID));
- assertEquals("JMSXUserID value was not as expected", myUserId, _testMessage.getStringProperty(ClientProperties.JMSXUSERID));
+ assertTrue("expected a JMSXUserID value to be present", _testMessage.propertyExists(JMSXUSERID));
+ assertEquals("JMSXUserID value was not as expected", myUserId, _testMessage.getStringProperty(JMSXUSERID));
}
// ====== JMS_AMQP_TTL property =======
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org