summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Ross <tross@apache.org>2011-09-12 21:04:02 +0000
committerTed Ross <tross@apache.org>2011-09-12 21:04:02 +0000
commitc6fa51f7ae0ffa5dfba9143651757eded69df9d1 (patch)
tree912ee45d11c112678c90e160c3fd36a94c07bce1
parentc6198c637cf03896563157802dccc4d62012acb9 (diff)
downloadqpid-python-c6fa51f7ae0ffa5dfba9143651757eded69df9d1.tar.gz
QPID-3480 - Directly encodes and decodes message content in Ruby
Committed patch from Darryl Pierce. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1169924 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--cpp/bindings/qpid/ruby/lib/qpid/message.rb32
-rw-r--r--cpp/bindings/qpid/ruby/test/test_message.rb52
2 files changed, 79 insertions, 5 deletions
diff --git a/cpp/bindings/qpid/ruby/lib/qpid/message.rb b/cpp/bindings/qpid/ruby/lib/qpid/message.rb
index 652b9fe564..9b1b68c7c3 100644
--- a/cpp/bindings/qpid/ruby/lib/qpid/message.rb
+++ b/cpp/bindings/qpid/ruby/lib/qpid/message.rb
@@ -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
diff --git a/cpp/bindings/qpid/ruby/test/test_message.rb b/cpp/bindings/qpid/ruby/test/test_message.rb
index 0d71d42360..3fc705bf7e 100644
--- a/cpp/bindings/qpid/ruby/test/test_message.rb
+++ b/cpp/bindings/qpid/ruby/test/test_message.rb
@@ -22,8 +22,7 @@ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
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).