You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@qpid.apache.org by "Darryl L. Pierce" <dp...@redhat.com> on 2011/07/11 14:43:09 UTC
[PATCH 03/11] Created the encode and decode methods in Qpid::Messaging.
From: "Darryl L. Pierce" <dp...@redhat.com>
These methods differ from the same methods in Cqpid in that they handle
working with symbols and other non-string values.
---
qpid/cpp/bindings/qpid/ruby/lib/qpid.rb | 1 +
qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb | 56 ++++++++
qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb | 146 +++++++++++++++++++++
qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb | 1 +
4 files changed, 204 insertions(+), 0 deletions(-)
create mode 100644 qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb
create mode 100644 qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb
diff --git a/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb b/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
index 597446d..725fa45 100644
--- a/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
+++ b/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
@@ -19,4 +19,5 @@
require 'qpid/errors'
require 'qpid/duration'
+require 'qpid/encoding'
diff --git a/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb b/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb
new file mode 100644
index 0000000..c8b843b
--- /dev/null
+++ b/qpid/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb
@@ -0,0 +1,56 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+require 'cqpid'
+
+module Qpid
+
+ module Messaging
+
+ # Encodes the supplied content into the given message.
+ def self.encode content, message, encoding = nil
+ prepared = content
+ case content
+ when Hash
+ prepared = {}
+ content.each_pair do |key,value|
+ prepared[key.to_s] = value.to_s
+ end
+ Cqpid::encode prepared, message.message_impl
+ when Array
+ prepared = []
+ content.each {|value| prepared << value.to_s}
+ Cqpid::encode prepared, message.message_impl
+ end
+ end
+
+ # Decodes and returns the message's content.
+ def self.decode(message, content_type = nil)
+ content_type = message.content_type unless content_type
+
+ case content_type
+ when "amqp/map": Cqpid.decodeMap message.message_impl
+ when "amqp/list": Cqpid.decodeList message.message_impl
+ end
+ end
+
+ end
+
+end
+
diff --git a/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb b/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb
new file mode 100644
index 0000000..060975a
--- /dev/null
+++ b/qpid/cpp/bindings/qpid/ruby/test/test_encoding.rb
@@ -0,0 +1,146 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
+
+require 'test/unit'
+require 'flexmock/test_unit'
+
+require 'cqpid'
+require 'qpid/encoding'
+
+class TestEncoding < Test::Unit::TestCase
+
+ def setup
+ @cqpid = flexmock(Cqpid)
+
+ @message = flexmock("message")
+ @message_impl = flexmock("message_impl")
+
+ @encoded = {"foo" => "bar"}
+ end
+
+ def test_encode_map_with_symbols
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with({"foo" => "bar"}, @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode({:foo => :bar}, @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_encode_list_with_symbols
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with(["foo", "bar"], @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode([:foo, :bar], @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_encode_with_content_type
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with({"foo" => "bar"}, @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode({:foo => :bar}, @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_encode
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:encode).
+ once.
+ with({"foo" => "bar"}, @message_impl).
+ and_return(@encoded)
+
+ result = Qpid::Messaging.encode({"foo" => "bar"}, @message)
+
+ assert_same @encoded, result
+ end
+
+ def test_decode_for_map
+ decoded = {"foo" => "bar"}
+ @message.
+ should_receive(:content_type).
+ once.
+ and_return("amqp/map")
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:decodeMap).
+ once.
+ with(@message_impl).
+ and_return(decoded)
+
+ result = Qpid::Messaging.decode(@message)
+
+ assert_same decoded, result
+ end
+
+ def test_decode_for_list
+ decoded = ["foo", "bar"]
+ @message.
+ should_receive(:content_type).
+ once.
+ and_return("amqp/list")
+ @message.
+ should_receive(:message_impl).
+ once.
+ and_return(@message_impl)
+ @cqpid.
+ should_receive(:decodeList).
+ once.
+ with(@message_impl).
+ and_return(decoded)
+
+ result = Qpid::Messaging.decode(@message)
+
+ assert_same decoded, result
+ end
+
+end
+
diff --git a/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb b/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb
index f5b7384..68af267 100644
--- a/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb
+++ b/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb
@@ -20,4 +20,5 @@
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require 'test/unit'
+require 'test_encoding'
--
1.7.6
---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project: http://qpid.apache.org
Use/Interact: mailto:dev-subscribe@qpid.apache.org