diff options
| author | Ted Ross <tross@apache.org> | 2011-07-15 16:57:19 +0000 |
|---|---|---|
| committer | Ted Ross <tross@apache.org> | 2011-07-15 16:57:19 +0000 |
| commit | 7cff9ce1ed874ced2f7c195b48399af8dc01c611 (patch) | |
| tree | d334bbb47dde03add97f7bf740cf4b95f4756fe1 /cpp | |
| parent | d61b82e49d30cdcba385ee166a1cd624f6fbf7d0 (diff) | |
| download | qpid-python-7cff9ce1ed874ced2f7c195b48399af8dc01c611.tar.gz | |
QPID-3306 - Provides a more Ruby-like set of APIs on top of the bindings created by swig.
Applied patch from Darryl Pierce
Created the encode and decode methods in
Qpid::Messaging.
These methods differ from the same methods in Cqpid in that they handle
working with symbols and other non-string values.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1147239 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
| -rw-r--r-- | cpp/bindings/qpid/ruby/lib/qpid.rb | 1 | ||||
| -rw-r--r-- | cpp/bindings/qpid/ruby/lib/qpid/encoding.rb | 56 | ||||
| -rw-r--r-- | cpp/bindings/qpid/ruby/test/test_encoding.rb | 146 | ||||
| -rw-r--r-- | cpp/bindings/qpid/ruby/test/ts_bindings.rb | 1 |
4 files changed, 204 insertions, 0 deletions
diff --git a/cpp/bindings/qpid/ruby/lib/qpid.rb b/cpp/bindings/qpid/ruby/lib/qpid.rb index 597446d2b1..725fa45f77 100644 --- a/cpp/bindings/qpid/ruby/lib/qpid.rb +++ b/cpp/bindings/qpid/ruby/lib/qpid.rb @@ -19,4 +19,5 @@ require 'qpid/errors' require 'qpid/duration' +require 'qpid/encoding' diff --git a/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb b/cpp/bindings/qpid/ruby/lib/qpid/encoding.rb new file mode 100644 index 0000000000..c8b843b597 --- /dev/null +++ b/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/cpp/bindings/qpid/ruby/test/test_encoding.rb b/cpp/bindings/qpid/ruby/test/test_encoding.rb new file mode 100644 index 0000000000..060975a1d5 --- /dev/null +++ b/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/cpp/bindings/qpid/ruby/test/ts_bindings.rb b/cpp/bindings/qpid/ruby/test/ts_bindings.rb index f5b7384513..68af26786e 100644 --- a/cpp/bindings/qpid/ruby/test/ts_bindings.rb +++ b/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' |
