You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by tr...@apache.org on 2011/09/12 23:04:02 UTC

svn commit: r1169924 - in /qpid/trunk/qpid/cpp/bindings/qpid/ruby: lib/qpid/message.rb test/test_message.rb

Author: tross
Date: Mon Sep 12 21:04:02 2011
New Revision: 1169924

URL: http://svn.apache.org/viewvc?rev=1169924&view=rev
Log:
QPID-3480 - Directly encodes and decodes message content in Ruby
Committed patch from Darryl Pierce.

Modified:
    qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb
    qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_message.rb

Modified: qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb?rev=1169924&r1=1169923&r2=1169924&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb (original)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb Mon Sep 12 21:04:02 2011
@@ -30,6 +30,7 @@ module Qpid
         @message_impl = message_impl
         @message_impl = Cqpid::Message.new if @message_impl.nil?
         @message_impl.setContent args[:content].to_s if args[:content]
+        @content = nil
       end
 
       def message_impl # :nodoc:
@@ -113,10 +114,37 @@ module Qpid
       def []=(key, value); @message_impl.setProperty(key.to_s, value.to_s); end
 
       # Sets the content.
-      def content=(content); @message_impl.setContent content.to_s; end
+      def content=(content)
+        content_type = nil
+        @content = content
+        case @content
+        when Hash
+          content_type = "amqp/map"
+        when Array
+          content_type = "amqp/list"
+        end
+        if content_type.nil?
+          @message_impl.setContent @content
+        else
+          Qpid::Messaging.encode @content, self, content_type
+        end
+      end
 
       # Returns the content.
-      def content; @message_impl.getContent; end
+      def content
+        if @content.nil?
+          @content = @message_impl.getContent
+
+          # decode the content is necessary if it
+          # has an encoded content type
+          if ["amqp/list", "amqp/map"].include? @message_impl.getContentType
+            @content = Qpid::Messaging.decode(self,
+                                              @message_impl.getContentType)
+          end
+
+        end
+        @content
+      end
 
       # Returns the content's size.
       def content_size; @message_impl.getContentSize; end

Modified: qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_message.rb
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_message.rb?rev=1169924&r1=1169923&r2=1169924&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_message.rb (original)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/test/test_message.rb Mon Sep 12 21:04:02 2011
@@ -22,8 +22,7 @@ $:.unshift File.join(File.dirname(__FILE
 require 'test/unit'
 require 'flexmock/test_unit'
 
-require 'qpid/encoding'
-require 'qpid/message'
+require 'qpid'
 
 class TestMessage < Test::Unit::TestCase
 
@@ -31,6 +30,7 @@ class TestMessage < Test::Unit::TestCase
     @address = flexmock("address")
     @address_impl = flexmock("address_impl")
 
+    @messaging = flexmock(Qpid::Messaging)
     @message_impl = flexmock("message")
     @message = Qpid::Messaging::Message.new({}, @message_impl)
  end
@@ -283,17 +283,63 @@ class TestMessage < Test::Unit::TestCase
       with("foo")
 
     @message.content = "foo"
+    assert_equal "foo", @message.content
+  end
+
+  def test_set_content_with_array
+    content = ["one", "two", "three"]
+
+    @messaging.
+      should_receive(:encode).
+      once.
+      with(content, @message, "amqp/list")
+
+    @message.content = content
+    assert_same content, @message.content
+  end
+
+  def test_set_content_with_map
+    content = {:foo => "bar", :dog => "cat"}
+
+    @messaging.
+      should_receive(:encode).
+      once.
+      with(content, @message, "amqp/map")
+
+    @message.content = content
+    assert_same content, @message.content
   end
 
   def test_get_content
     @message_impl.
       should_receive(:getContent).
-      once.
       and_return("foo")
+    @message_impl.
+      should_receive(:getContentType).
+      and_return(String)
 
     assert_equal "foo", @message.content
   end
 
+  def test_get_content_with_array
+    decoded = ["foo", "bar"]
+
+    @message_impl.
+      should_receive(:getContent).
+      and_return("[foo,bar]")
+    @message_impl.
+      should_receive(:getContentType).
+      and_return("amqp/list")
+    @messaging.
+      should_receive(:decode).
+      once.
+      with(@message, "amqp/list").
+      and_return(decoded)
+
+    result = @message.content
+    assert_same decoded, result
+  end
+
   def test_get_content_size
     @message_impl.
       should_receive(:getContentSize).



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org