qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rob...@apache.org
Subject svn commit: r1546400 - in /qpid/jms/trunk/src: main/java/org/apache/qpid/jms/engine/AmqpMessage.java test/java/org/apache/qpid/jms/engine/AmqpMessageTest.java
Date Thu, 28 Nov 2013 17:16:15 GMT
Author: robbie
Date: Thu Nov 28 17:16:15 2013
New Revision: 1546400

URL: http://svn.apache.org/r1546400
Log:
QPIDJMS-9: rework message-annotation methods, change method keys to be in terms of strings,
add unit testing

Modified:
    qpid/jms/trunk/src/main/java/org/apache/qpid/jms/engine/AmqpMessage.java
    qpid/jms/trunk/src/test/java/org/apache/qpid/jms/engine/AmqpMessageTest.java

Modified: qpid/jms/trunk/src/main/java/org/apache/qpid/jms/engine/AmqpMessage.java
URL: http://svn.apache.org/viewvc/qpid/jms/trunk/src/main/java/org/apache/qpid/jms/engine/AmqpMessage.java?rev=1546400&r1=1546399&r2=1546400&view=diff
==============================================================================
--- qpid/jms/trunk/src/main/java/org/apache/qpid/jms/engine/AmqpMessage.java (original)
+++ qpid/jms/trunk/src/main/java/org/apache/qpid/jms/engine/AmqpMessage.java Thu Nov 28 17:16:15
2013
@@ -22,10 +22,12 @@ package org.apache.qpid.jms.engine;
 
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.qpid.proton.Proton;
+import org.apache.qpid.proton.amqp.Symbol;
 import org.apache.qpid.proton.amqp.messaging.Accepted;
 import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
 import org.apache.qpid.proton.amqp.messaging.MessageAnnotations;
@@ -133,66 +135,91 @@ public abstract class AmqpMessage
 
     //===== MessageAnnotations ======
 
-    public boolean messageAnnotationExists(Object key)
+    /**
+     * @param keyName The name of the symbol key
+     * @return true if an annotation exists with the provided symbol name, false otherwise
+     */
+    public boolean messageAnnotationExists(String keyName)
     {
-        //TODO: this isn't thread-safe, does it need to be?
-        Map<Object,Object> msgAnnotations = _messageAnnotationsMap;
-        if(msgAnnotations == null)
+        if(_messageAnnotationsMap == null)
         {
             return false;
         }
 
-        return msgAnnotations.containsKey(key);
+        return _messageAnnotationsMap.containsKey(Symbol.valueOf(keyName));
+    }
+
+    /**
+     * @param keyName The name of the symbol key
+     * @return the value of the annotation if it exists, or null otherwise
+     */
+    public Object getMessageAnnotation(String keyName)
+    {
+        if(_messageAnnotationsMap == null)
+        {
+            return null;
+        }
+
+        return _messageAnnotationsMap.get(Symbol.valueOf(keyName));
     }
 
-    public void clearMessageAnnotation(Object key)
+    public void clearMessageAnnotation(String keyName)
     {
-        //TODO: this isnt thread-safe, does it need to be?
         if(_messageAnnotationsMap == null)
         {
             return;
         }
 
-        _messageAnnotationsMap.remove(key);
+        _messageAnnotationsMap.remove(Symbol.valueOf(keyName));
+    }
 
-        //If there are now no annotations, clear the field on
-        //the Proton message to avoid encoding an empty map
-        if(_messageAnnotationsMap.isEmpty())
+    /**
+     * @param keyName The name of the symbol key
+     * @param value the annotation value
+     */
+    public void setMessageAnnotation(String keyName, Object value)
+    {
+        if(_messageAnnotationsMap == null)
         {
-            clearAllMessageAnnotations();
+            initializeUnderlyingMessageAnnotations();
         }
+
+        _messageAnnotationsMap.put(Symbol.valueOf(keyName), value);
     }
 
+    /**
+     * Clears any previously set annotations and removes the underlying
+     * message annotations section from the message
+     */
     public void clearAllMessageAnnotations()
     {
+        _messageAnnotationsMap = null;
         _messageAnnotations = null;
         _message.setMessageAnnotations(null);
     }
 
-    public void setMessageAnnotation(Object key, Object value)
+    /**
+     * @return the number of MessageAnnotations.
+     */
+    public int getMessageAnnotationsCount()
     {
-        if(_messageAnnotationsMap == null)
+        if(_messageAnnotationsMap != null)
         {
-            _messageAnnotationsMap = new HashMap<Object,Object>();
+            return _messageAnnotationsMap.size();
         }
-
-        _messageAnnotationsMap.put(key, value);
-
-        //If there were previously no annotations, we need to
-        //set the related field on the Proton message now
-        if(_messageAnnotations == null)
+        else
         {
-            setMessageAnnotations();
+            return 0;
         }
     }
 
-    private void setMessageAnnotations()
+    private void initializeUnderlyingMessageAnnotations()
     {
+        _messageAnnotationsMap = new HashMap<Object,Object>();
         _messageAnnotations = new MessageAnnotations(_messageAnnotationsMap);
         _message.setMessageAnnotations(_messageAnnotations);
     }
 
-
     //===== Properties ======
 
     public void setContentType(String contentType)

Modified: qpid/jms/trunk/src/test/java/org/apache/qpid/jms/engine/AmqpMessageTest.java
URL: http://svn.apache.org/viewvc/qpid/jms/trunk/src/test/java/org/apache/qpid/jms/engine/AmqpMessageTest.java?rev=1546400&r1=1546399&r2=1546400&view=diff
==============================================================================
--- qpid/jms/trunk/src/test/java/org/apache/qpid/jms/engine/AmqpMessageTest.java (original)
+++ qpid/jms/trunk/src/test/java/org/apache/qpid/jms/engine/AmqpMessageTest.java Thu Nov 28
17:16:15 2013
@@ -30,6 +30,7 @@ import org.apache.qpid.jms.QpidJmsTestCa
 import org.apache.qpid.proton.Proton;
 import org.apache.qpid.proton.amqp.Symbol;
 import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
+import org.apache.qpid.proton.amqp.messaging.MessageAnnotations;
 import org.apache.qpid.proton.amqp.messaging.Properties;
 import org.apache.qpid.proton.engine.Delivery;
 import org.apache.qpid.proton.message.Message;
@@ -56,6 +57,8 @@ public class AmqpMessageTest extends Qpi
         _mockDelivery = Mockito.mock(Delivery.class);
     }
 
+    // ====== Application Properties =======
+
     @Test
     public void testGetApplicationPropertyNames()
     {
@@ -162,6 +165,8 @@ public class AmqpMessageTest extends Qpi
         }
     }
 
+    // ====== Properties =======
+
     @Test
     public void testGetToWithReceivedMessageWithNoProperties()
     {
@@ -235,4 +240,195 @@ public class AmqpMessageTest extends Qpi
         assertNotNull(testAmqpMessage.getTo());
         assertEquals(testToAddress, testAmqpMessage.getTo());
     }
+
+    // ====== Message Annotations =======
+
+    @Test
+    public void testMessageAnnotationExistsUsingReceivedMessageWithoutMessageAnnotationsSection()
+    {
+        String symbolKeyName = "myTestSymbolName";
+
+        Message message = Proton.message();
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        assertFalse(testAmqpMessage.messageAnnotationExists(symbolKeyName));
+    }
+
+    @Test
+    public void testMessageAnnotationExistsUsingReceivedMessageWithMessageAnnotationsSection()
+    {
+        String symbolKeyName = "myTestSymbolName";
+        String value = "myTestValue";
+
+        Message message = Proton.message();
+
+        Map<Object,Object> annotationsMap = new HashMap<Object,Object>();
+        annotationsMap.put(Symbol.valueOf(symbolKeyName), value);
+        message.setMessageAnnotations(new MessageAnnotations(annotationsMap));
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        assertTrue(testAmqpMessage.messageAnnotationExists(symbolKeyName));
+        assertFalse(testAmqpMessage.messageAnnotationExists("otherName"));
+    }
+
+    @Test
+    public void testGetMessageAnnotationUsingReceivedMessageWithoutMessageAnnotationsSection()
+    {
+        String symbolKeyName = "myTestSymbolName";
+
+        Message message = Proton.message();
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        assertNull(testAmqpMessage.getMessageAnnotation(symbolKeyName));
+    }
+
+    @Test
+    public void testGetMessageAnnotationUsingReceivedMessageWithMessageAnnotationsSection()
+    {
+        String symbolKeyName = "myTestSymbolName";
+        String value = "myTestValue";
+
+        Message message = Proton.message();
+
+        Map<Object,Object> annotationsMap = new HashMap<Object,Object>();
+        annotationsMap.put(Symbol.valueOf(symbolKeyName), value);
+        message.setMessageAnnotations(new MessageAnnotations(annotationsMap));
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        assertEquals(value, testAmqpMessage.getMessageAnnotation(symbolKeyName));
+        assertNull(testAmqpMessage.getMessageAnnotation("otherName"));
+    }
+
+    @Test
+    public void testNewMessageHasNoUnderlyingMessageAnnotationsSection()
+    {
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage();
+
+        Message underlying = testAmqpMessage.getMessage();
+        assertNull(underlying.getMessageAnnotations());
+    }
+
+    @Test
+    public void testSetMessageAnnotationOnNewMessage()
+    {
+        String symbolKeyName = "myTestSymbolName";
+        String symbolKeyName2 = "myTestSymbolName2";
+        String value = "myTestValue";
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage();
+
+        //check setting first annotation creates the annotations section
+        testAmqpMessage.setMessageAnnotation(symbolKeyName, value);
+
+        MessageAnnotations underlyingAnnotations = testAmqpMessage.getMessage().getMessageAnnotations();
+        assertNotNull(underlyingAnnotations);
+
+        assertEquals(1, underlyingAnnotations.getValue().size());
+        assertTrue(underlyingAnnotations.getValue().containsKey(Symbol.valueOf(symbolKeyName)));
+        assertEquals(value, underlyingAnnotations.getValue().get(Symbol.valueOf(symbolKeyName)));
+
+        //set another
+        testAmqpMessage.setMessageAnnotation(symbolKeyName2, value);
+
+        assertEquals(2, underlyingAnnotations.getValue().size());
+        assertTrue(underlyingAnnotations.getValue().containsKey(Symbol.valueOf(symbolKeyName)));
+        assertTrue(underlyingAnnotations.getValue().containsKey(Symbol.valueOf(symbolKeyName2)));
+    }
+
+    @Test
+    public void testClearMessageAnnotation()
+    {
+        String symbolKeyName = "myTestSymbolName";
+        String value = "myTestValue";
+
+        Message message = Proton.message();
+
+        Map<Object,Object> annotationsMap = new HashMap<Object,Object>();
+        annotationsMap.put(Symbol.valueOf(symbolKeyName), value);
+        message.setMessageAnnotations(new MessageAnnotations(annotationsMap));
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        assertEquals(value, testAmqpMessage.getMessageAnnotation(symbolKeyName));
+        assertNull(testAmqpMessage.getMessageAnnotation("otherName"));
+
+        testAmqpMessage.clearMessageAnnotation(symbolKeyName);
+        assertNull(testAmqpMessage.getMessageAnnotation(symbolKeyName));
+    }
+
+    @Test
+    public void testClearMessageAnnotationONMessageWithNoMessageAnnotationSectionDoesntThrowException()
+    {
+        Message message = Proton.message();
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        testAmqpMessage.clearMessageAnnotation("keyName");
+    }
+
+    @Test
+    public void testClearAllMessageAnnotationsUsingNewMessage()
+    {
+        Message message = Proton.message();
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        testAmqpMessage.clearAllMessageAnnotations();
+
+        Message underlying = testAmqpMessage.getMessage();
+        assertNull(underlying.getMessageAnnotations());
+    }
+
+    @Test
+    public void testClearAllMessageAnnotationsUsingReceivedMessageWithMessageAnnotationsSection()
+    {
+        String symbolKeyName = "myTestSymbolName";
+        String value = "myTestValue";
+
+        Message message = Proton.message();
+
+        Map<Object,Object> annotationsMap = new HashMap<Object,Object>();
+        annotationsMap.put(Symbol.valueOf(symbolKeyName), value);
+        message.setMessageAnnotations(new MessageAnnotations(annotationsMap));
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        testAmqpMessage.clearAllMessageAnnotations();
+
+        Message underlying = testAmqpMessage.getMessage();
+        assertNull(underlying.getMessageAnnotations());
+    }
+
+    @Test
+    public void testGetMessageAnnotationsCountUsingReceivedMessageWithoutMessageAnnotationsSection()
+    {
+        Message message = Proton.message();
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        assertEquals(0, testAmqpMessage.getMessageAnnotationsCount());
+    }
+
+    @Test
+    public void testGetMessageAnnotationsCountUsingReceivedMessageWithMessageAnnotationsSection()
+    {
+        String symbolKeyName = "myTestSymbolName";
+        String symbolKeyName2 = "myTestSymbolName2";
+        String value = "myTestValue";
+
+        Message message = Proton.message();
+
+        Map<Object,Object> annotationsMap = new HashMap<Object,Object>();
+        annotationsMap.put(Symbol.valueOf(symbolKeyName), value);
+        annotationsMap.put(Symbol.valueOf(symbolKeyName2), value);
+        message.setMessageAnnotations(new MessageAnnotations(annotationsMap));
+
+        TestAmqpMessage testAmqpMessage = new TestAmqpMessage(message, _mockDelivery, _mockAmqpConnection);
+
+        assertEquals(2, testAmqpMessage.getMessageAnnotationsCount());
+        testAmqpMessage.clearMessageAnnotation(symbolKeyName);
+        assertEquals(1, testAmqpMessage.getMessageAnnotationsCount());
+    }
 }



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


Mime
View raw message