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:11 UTC

[PATCH 05/11] Created the Message class and its unit tests.

From: "Darryl L. Pierce" <dp...@redhat.com>

The class is packaged as:

Qpid::Messaging::Message

A Message can return its content and be used to send a message over an
instance of Sender.
---
 qpid/cpp/bindings/qpid/ruby/lib/qpid.rb          |    1 +
 qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb  |  129 +++++++++
 qpid/cpp/bindings/qpid/ruby/test/test_message.rb |  307 ++++++++++++++++++++++
 qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb  |    1 +
 4 files changed, 438 insertions(+), 0 deletions(-)
 create mode 100644 qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb
 create mode 100644 qpid/cpp/bindings/qpid/ruby/test/test_message.rb

diff --git a/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb b/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
index f9d5822..b5ac578 100644
--- a/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
+++ b/qpid/cpp/bindings/qpid/ruby/lib/qpid.rb
@@ -21,4 +21,5 @@ require 'qpid/errors'
 require 'qpid/duration'
 require 'qpid/address'
 require 'qpid/encoding'
+require 'qpid/message'
 
diff --git a/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb b/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb
new file mode 100644
index 0000000..652b9fe
--- /dev/null
+++ b/qpid/cpp/bindings/qpid/ruby/lib/qpid/message.rb
@@ -0,0 +1,129 @@
+#
+# 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
+
+    # Message represents a message.
+    class Message
+
+      def initialize(args = {}, message_impl = nil)
+        @message_impl = message_impl
+        @message_impl = Cqpid::Message.new if @message_impl.nil?
+        @message_impl.setContent args[:content].to_s if args[:content]
+      end
+
+      def message_impl # :nodoc:
+        @message_impl
+      end
+
+      # Assigns the reply to address.
+      # The address must be an instance of Address.
+      def reply_to=(address); @message_impl.setReplyTo address.address_impl; end
+
+      # Returns the reply to address for the message as an instance of +Address+.
+      def reply_to
+        address_impl = @message_impl.getReplyTo
+        # only return an address if a reply to was specified
+        Qpid::Messaging::Address.new(nil, nil, nil, nil, address_impl) if address_impl
+      end
+
+      # Sets the subject.
+      def subject=(subject); @message_impl.setSubject subject; end
+
+      # Returns the subject.
+      def subject; @message_impl.getSubject; end
+
+      # Sets the content type.
+      def content_type=(content_type); @message_impl.setContentType content_type; end
+
+      # Returns the content type.
+      def content_type; @message_impl.getContentType; end
+
+      # Sets the message id.
+      def message_id=(message_id); @message_impl.setMessageId message_id.to_s; end
+
+      # Returns the message id.
+      def message_id; @message_impl.getMessageId; end
+
+      # Sets the user id.
+      def user_id=(user_id); @message_impl.setUserId user_id; end
+
+      # Returns the user id.
+      def user_id; @message_impl.getUserId; end
+
+      # Sets the correlation id.
+      def correlation_id=(correlation_id); @message_impl.setCorrelationId correlation_id; end
+
+      # Returns the correlation id.
+      def correlation_id; @message_impl.getCorrelationId; end
+
+      # Sets the priority.
+      def priority=(priority); @message_impl.setPriority priority; end
+
+      # Returns the priority.
+      def priority; @message_impl.getPriority; end
+
+      # Sets the time-to-live in milliseconds.
+      def ttl=(duration); @message_impl.setTtl duration; end
+
+      # Returns the time-to-live in milliseconds.
+      def ttl; @message_impl.getTtl; end
+
+      # Sets the durability.
+      def durable=(durable); @message_impl.setDurable durable; end
+
+      # Returns the durability.
+      def durable; @message_impl.getDurable; end
+
+      # Allows marking the message as redelivered.
+      def redelivered=(redelivered); @message_impl.setRedelivered redelivered; end
+
+      # Returns if the message was redelivered.
+      def redelivered; @message_impl.getRedelivered; end
+
+      # Returns all named properties.
+      # *NOTE:* It is recommended to use the +foo[key]+ method for
+      # retrieving properties.
+      def properties; @message_impl.getProperties; end
+
+      # Returns the value for the named property.
+      def [](key); self.properties[key.to_s]; end
+
+      # Assigns a value to the named property.
+      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
+
+      # Returns the content.
+      def content; @message_impl.getContent; end
+
+      # Returns the content's size.
+      def content_size; @message_impl.getContentSize; end
+
+    end
+
+  end
+
+end
+
diff --git a/qpid/cpp/bindings/qpid/ruby/test/test_message.rb b/qpid/cpp/bindings/qpid/ruby/test/test_message.rb
new file mode 100644
index 0000000..0d71d42
--- /dev/null
+++ b/qpid/cpp/bindings/qpid/ruby/test/test_message.rb
@@ -0,0 +1,307 @@
+#
+# 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 'qpid/encoding'
+require 'qpid/message'
+
+class TestMessage < Test::Unit::TestCase
+
+  def setup
+    @address = flexmock("address")
+    @address_impl = flexmock("address_impl")
+
+    @message_impl = flexmock("message")
+    @message = Qpid::Messaging::Message.new({}, @message_impl)
+ end
+
+  def test_message_impl
+    assert_same @message_impl, @message.message_impl
+  end
+
+  def test_set_reply_to
+    @address.
+      should_receive(:address_impl).
+      once.
+      and_return(@address_impl)
+    @message_impl.
+      should_receive(:setReplyTo).
+      once.
+      with(@address_impl)
+
+    @message.reply_to = @address
+  end
+
+  def test_get_reply_to
+    @message_impl.
+      should_receive(:getReplyTo).
+      once.
+      and_return(@address_impl)
+
+    result = @message.reply_to
+
+    assert_not_nil result
+    assert_same @address_impl, result.address_impl
+  end
+
+  def test_set_subject
+    @message_impl.
+      should_receive(:setSubject).
+      once.
+      with("New Subject")
+
+    @message.subject = "New Subject"
+  end
+
+  def test_get_subject
+    @message_impl.
+      should_receive(:getSubject).
+      once.
+      and_return("Old Subject")
+
+    assert_equal "Old Subject", @message.subject
+  end
+
+  def test_set_content_type
+    @message_impl.
+      should_receive(:setContentType).
+      once.
+      and_return("amqp/map")
+
+    @message.content_type = "amqp/map"
+  end
+
+  def test_get_content_type
+    @message_impl.
+      should_receive(:getContentType).
+      once.
+      and_return("amqp/list")
+
+    assert_equal "amqp/list", @message.content_type
+  end
+
+  def test_set_message_id
+    @message_impl.
+      should_receive(:setMessageId).
+      once.
+      with("717")
+
+    @message.message_id = "717"
+  end
+
+  def test_get_message_id
+    @message_impl.
+      should_receive(:getMessageId).
+      once.
+      and_return("1965")
+
+    assert_equal "1965", @message.message_id
+  end
+
+  def test_set_user_id
+    @message_impl.
+      should_receive(:setUserId).
+      once.
+      with("129")
+
+    @message.user_id = "129"
+  end
+
+  def test_get_user_id
+    @message_impl.
+      should_receive(:getUserId).
+      once.
+      and_return("1971")
+
+    assert_equal "1971", @message.user_id
+  end
+
+  def test_set_correlation_id
+    @message_impl.
+      should_receive(:setCorrelationId).
+      once.
+      with("320")
+
+    @message.correlation_id = "320"
+  end
+
+  def test_get_correlation_id
+    @message_impl.
+      should_receive(:getCorrelationId).
+      once.
+      and_return("1996")
+
+    assert_equal "1996", @message.correlation_id
+  end
+
+  def test_set_priority
+    @message_impl.
+      should_receive(:setPriority).
+      once.
+      with(9)
+
+    @message.priority = 9
+  end
+
+  def test_get_priority
+    @message_impl.
+      should_receive(:getPriority).
+      once.
+      and_return(21)
+
+    assert_equal 21, @message.priority
+  end
+
+  def test_set_ttl
+    @message_impl.
+      should_receive(:setTtl).
+      once.
+      with(Qpid::Messaging::Duration::FOREVER)
+
+    @message.ttl = Qpid::Messaging::Duration::FOREVER
+  end
+
+  def test_get_ttl
+    @message_impl.
+      should_receive(:getTtl).
+      once.
+      and_return(Qpid::Messaging::Duration::SECOND)
+
+    assert_equal Qpid::Messaging::Duration::SECOND, @message.ttl
+  end
+
+  def test_set_durable
+    @message_impl.
+      should_receive(:setDurable).
+      once.
+      with(true)
+
+    @message.durable = true
+  end
+
+  def test_set_not_durable
+    @message_impl.
+      should_receive(:setDurable).
+      once.
+      with(false)
+
+    @message.durable = false
+  end
+
+  def test_get_durable
+    @message_impl.
+      should_receive(:getDurable).
+      once.
+      and_return(true)
+
+    assert @message.durable
+  end
+
+  def test_set_redelivered
+    @message_impl.
+      should_receive(:setRedelivered).
+      once.
+      with(true)
+
+    @message.redelivered = true
+  end
+
+  def test_set_not_redelivered
+    @message_impl.
+      should_receive(:setRedelivered).
+      once.
+      with(false)
+
+    @message.redelivered = false
+  end
+
+  def test_get_redelivered
+    @message_impl.
+      should_receive(:getRedelivered).
+      once.
+      and_return(false)
+
+    assert !@message.redelivered
+  end
+
+  def test_get_properties
+    properties = {"foo" => "bar"}
+    @message_impl.
+      should_receive(:getProperties).
+      once.
+      and_return(properties)
+
+    result = @message.properties
+
+    assert_equal properties, result
+  end
+
+  def test_get_property
+    @message_impl.
+      should_receive(:getProperties).
+      once.
+      and_return({"foo" => "bar"})
+
+    result = @message["foo"]
+
+    assert_equal "bar", result
+  end
+
+  def test_set_property
+    @message_impl.
+      should_receive(:setProperty).
+      once.
+      with("foo", "bar")
+
+    @message["foo"] = "bar"
+  end
+
+  def test_set_content
+    @message_impl.
+      should_receive(:setContent).
+      once.
+      with("foo")
+
+    @message.content = "foo"
+  end
+
+  def test_get_content
+    @message_impl.
+      should_receive(:getContent).
+      once.
+      and_return("foo")
+
+    assert_equal "foo", @message.content
+  end
+
+  def test_get_content_size
+    @message_impl.
+      should_receive(:getContentSize).
+      once.
+      and_return(68)
+
+    assert_equal 68, @message.content_size
+  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 750d335..8139983 100644
--- a/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb
+++ b/qpid/cpp/bindings/qpid/ruby/test/ts_bindings.rb
@@ -22,4 +22,5 @@ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
 require 'test/unit'
 require 'test_encoding'
 require 'test_address'
+require 'test_message'
 
-- 
1.7.6


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