You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2009/10/12 17:47:02 UTC
svn commit: r824393 - in /activemq/activemq-dotnet:
Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/
Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/
Apache.NMS/trunk/src/main/csharp/Util/
Author: tabish
Date: Mon Oct 12 15:47:01 2009
New Revision: 824393
URL: http://svn.apache.org/viewvc?rev=824393&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQNET-193
Changes to enable read-only mode and proper message property type conversion where the conversion is safe to do. Adds in many new unit tests for the Message classes.
Added:
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMessageTest.cs (with props)
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyIntercepter.cs
- copied, changed from r823288, activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyHelper.cs
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMapInterceptor.cs (with props)
Removed:
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyHelper.cs
Modified:
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMessage.cs
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/Message.cs
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/MessageId.cs
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ProducerId.cs
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMap.cs
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMessage.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMessage.cs?rev=824393&r1=824392&r2=824393&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMessage.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMessage.cs Mon Oct 12 15:47:01 2009
@@ -28,7 +28,7 @@
{
public const byte ID_ACTIVEMQMESSAGE = 23;
- private MessagePropertyHelper propertyHelper;
+ private MessagePropertyIntercepter propertyHelper;
private PrimitiveMap properties;
public event AcknowledgeHandler Acknowledger;
@@ -47,11 +47,42 @@
Timestamp = DateUtils.ToJavaTimeUtc(DateTime.UtcNow);
}
- public override byte GetDataStructureType()
+ public override int GetHashCode()
+ {
+ MessageId id = this.MessageId;
+
+ if(id != null)
+ {
+ return id.GetHashCode();
+ }
+ else
+ {
+ return base.GetHashCode();
+ }
+ }
+
+ public override byte GetDataStructureType()
{
return ID_ACTIVEMQMESSAGE;
}
+ public override bool Equals(object that)
+ {
+ if(that is ActiveMQMessage)
+ {
+ return Equals((ActiveMQMessage) that);
+ }
+ return false;
+ }
+
+ public virtual bool Equals(ActiveMQMessage that)
+ {
+ MessageId oMsg = that.MessageId;
+ MessageId thisMsg = this.MessageId;
+
+ return thisMsg != null && oMsg != null && oMsg.Equals(thisMsg);
+ }
+
public void Acknowledge()
{
if(null == Acknowledger)
@@ -67,14 +98,14 @@
public virtual void ClearBody()
{
this.Content = null;
- this.readOnlyMsgBody = false;
+ this.ReadOnlyBody = false;
}
public virtual void ClearProperties()
{
this.MarshalledProperties = null;
this.Properties.Clear();
- this.readOnlyMsgProperties = false;
+ this.ReadOnlyProperties = false;
}
protected void FailIfReadOnlyBody()
@@ -93,6 +124,20 @@
}
}
+ public override bool ReadOnlyProperties
+ {
+ get{ return base.ReadOnlyProperties; }
+
+ set
+ {
+ if(this.propertyHelper != null)
+ {
+ this.propertyHelper.ReadOnly = value;
+ }
+ base.ReadOnlyProperties = value;
+ }
+ }
+
#region Properties
public IPrimitiveMap Properties
@@ -102,7 +147,7 @@
if(null == properties)
{
properties = PrimitiveMap.Unmarshal(MarshalledProperties);
- propertyHelper = new MessagePropertyHelper(this, properties);
+ propertyHelper = new MessagePropertyIntercepter(this, properties, this.ReadOnlyProperties);
}
return propertyHelper;
@@ -168,6 +213,29 @@
return String.Empty;
}
+
+ set
+ {
+ if(value != null)
+ {
+ try
+ {
+ MessageId id = new MessageId(value);
+ this.MessageId = id;
+ }
+ catch(FormatException)
+ {
+ // we must be some foreign JMS provider or strange user-supplied
+ // String so lets set the IDs to be 1
+ MessageId id = new MessageId();
+ this.MessageId = id;
+ }
+ }
+ else
+ {
+ this.MessageId = null;
+ }
+ }
}
/// <summary>
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/Message.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/Message.cs?rev=824393&r1=824392&r2=824393&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/Message.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/Message.cs Mon Oct 12 15:47:01 2009
@@ -65,8 +65,8 @@
long brokerInTime;
long brokerOutTime;
- protected bool readOnlyMsgProperties;
- protected bool readOnlyMsgBody;
+ private bool readOnlyMsgProperties;
+ private bool readOnlyMsgBody;
public const int DEFAULT_MINIMUM_MESSAGE_SIZE = 1024;
@@ -339,16 +339,16 @@
set { this.brokerOutTime = value; }
}
- public bool ReadOnlyProperties
+ public virtual bool ReadOnlyProperties
{
- get { return readOnlyMsgProperties; }
- set { readOnlyMsgProperties = value; }
+ get { return this.readOnlyMsgProperties; }
+ set { this.readOnlyMsgProperties = value; }
}
- public bool ReadOnlyBody
+ public virtual bool ReadOnlyBody
{
- get { return readOnlyMsgBody; }
- set { readOnlyMsgBody = value; }
+ get { return this.readOnlyMsgBody; }
+ set { this.readOnlyMsgBody = value; }
}
///
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/MessageId.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/MessageId.cs?rev=824393&r1=824392&r2=824393&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/MessageId.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/MessageId.cs Mon Oct 12 15:47:01 2009
@@ -37,6 +37,23 @@
long producerSequenceId;
long brokerSequenceId;
+ private string key = null;
+
+ public MessageId() : base()
+ {
+ }
+
+ public MessageId(ProducerId producerId, long producerSequenceId) : base()
+ {
+ this.producerId = producerId;
+ this.producerSequenceId = producerSequenceId;
+ }
+
+ public MessageId(string value) : base()
+ {
+ this.SetValue(value);
+ }
+
///
/// <summery>
/// Get the unique identifier that this object and its own
@@ -56,11 +73,29 @@
///
public override string ToString()
{
- return GetType().Name + "[" +
- "ProducerId=" + ProducerId +
- "ProducerSequenceId=" + ProducerSequenceId +
- "BrokerSequenceId=" + BrokerSequenceId +
- "]";
+ if(key == null)
+ {
+ key = producerId.ToString() + ":" + producerSequenceId;
+ }
+
+ return key;
+ }
+
+ /// <summary>
+ /// Sets the value as a String
+ /// </summary>
+ public void SetValue(string messageKey)
+ {
+ this.key = messageKey;
+
+ // Parse off the sequenceId
+ int p = messageKey.LastIndexOf(":");
+ if(p >= 0)
+ {
+ producerSequenceId = Int64.Parse(messageKey.Substring(p + 1));
+ messageKey = messageKey.Substring(0, p);
+ }
+ producerId = new ProducerId(messageKey);
}
public ProducerId ProducerId
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ProducerId.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ProducerId.cs?rev=824393&r1=824392&r2=824393&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ProducerId.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ProducerId.cs Mon Oct 12 15:47:01 2009
@@ -50,6 +50,17 @@
this.value = consumerId;
}
+ public ProducerId(string producerKey)
+ {
+ // Parse off the producerId
+ int p = producerKey.LastIndexOf(":");
+ if(p >= 0)
+ {
+ value = Int64.Parse(producerKey.Substring(p + 1));
+ producerKey = producerKey.Substring(0, p);
+ }
+ }
+
///
/// <summery>
/// Get the unique identifier that this object and its own
Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMessageTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMessageTest.cs?rev=824393&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMessageTest.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMessageTest.cs Mon Oct 12 15:47:01 2009
@@ -0,0 +1,1031 @@
+/*
+ * 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.
+ */
+
+using NUnit.Framework;
+using System;
+using System.Text;
+using System.Collections;
+using Apache.NMS.ActiveMQ.Commands;
+using Apache.NMS.ActiveMQ.OpenWire;
+using Apache.NMS.Util;
+
+namespace Apache.NMS.ActiveMQ.Test.Commands
+{
+ [TestFixture]
+ public class ActiveMQMessageTest
+ {
+ private string nmsMessageID;
+ private string nmsCorrelationID;
+ private ActiveMQTopic nmsDestination;
+ private ActiveMQTempTopic nmsReplyTo;
+ private MsgDeliveryMode nmsDeliveryMode;
+ private bool nmsRedelivered;
+ private string nmsType;
+ private MsgPriority nmsPriority;
+ private DateTime nmsTimestamp;
+ private long[] consumerIDs;
+
+ [SetUp]
+ public virtual void SetUp()
+ {
+ this.nmsMessageID = "testid";
+ this.nmsCorrelationID = "testcorrelationid";
+ this.nmsDestination = new ActiveMQTopic("test.topic");
+ this.nmsReplyTo = new ActiveMQTempTopic("test.replyto.topic:001");
+ this.nmsDeliveryMode = MsgDeliveryMode.NonPersistent;
+ this.nmsRedelivered = true;
+ this.nmsType = "test type";
+ this.nmsPriority = MsgPriority.High;
+ this.nmsTimestamp = DateTime.Now;
+ this.consumerIDs = new long[3];
+
+ for(int i = 0; i < this.consumerIDs.Length; i++)
+ {
+ this.consumerIDs[i] = i;
+ }
+ }
+
+ [Test]
+ public void TestSetReadOnly()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.ReadOnlyProperties = true;
+ bool test = false;
+
+ try
+ {
+ msg.Properties.SetInt("test", 1);
+ }
+ catch(MessageNotWriteableException)
+ {
+ test = true;
+ }
+ catch(NMSException)
+ {
+ test = false;
+ }
+
+ Assert.IsTrue(test);
+ }
+
+ [Test]
+ public void TestSetToForeignNMSID()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.NMSMessageId = "ID:EMS-SERVER.8B443C380083:429";
+ }
+
+ [Test]
+ public void TestEqualsObject()
+ {
+ ActiveMQMessage msg1 = new ActiveMQMessage();
+ ActiveMQMessage msg2 = new ActiveMQMessage();
+ msg1.NMSMessageId = this.nmsMessageID;
+ Assert.IsTrue(!msg1.Equals(msg2));
+ msg2.NMSMessageId = this.nmsMessageID;
+ Assert.IsTrue(msg1.Equals(msg2));
+ }
+
+ [Test]
+ public void TestShallowCopy()
+ {
+ ActiveMQMessage msg1 = new ActiveMQMessage();
+ msg1.NMSMessageId = nmsMessageID;
+ ActiveMQMessage msg2 = (ActiveMQMessage) msg1.Clone();
+ Assert.IsTrue(msg1 != msg2 && msg1.Equals(msg2));
+ }
+
+ [Test]
+ public void TestCopy()
+ {
+ this.nmsMessageID = "ID:1141:45278:429";
+ this.nmsCorrelationID = "testcorrelationid";
+ this.nmsDestination = new ActiveMQTopic("test.topic");
+ this.nmsReplyTo = new ActiveMQTempTopic("test.replyto.topic:001");
+ this.nmsDeliveryMode = MsgDeliveryMode.NonPersistent;
+ this.nmsType = "test type";
+ this.nmsPriority = MsgPriority.High;
+ this.nmsTimestamp = DateTime.Now;
+
+ ActiveMQMessage msg1 = new ActiveMQMessage();
+ msg1.NMSMessageId = this.nmsMessageID;
+ msg1.NMSCorrelationID = this.nmsCorrelationID;
+ msg1.FromDestination = this.nmsDestination;
+ msg1.NMSReplyTo = this.nmsReplyTo;
+ msg1.NMSDeliveryMode = this.nmsDeliveryMode;
+ msg1.NMSType = this.nmsType;
+ msg1.NMSPriority = this.nmsPriority;
+ msg1.NMSTimestamp = this.nmsTimestamp;
+ msg1.ReadOnlyProperties = true;
+
+ ActiveMQMessage msg2 = msg1.Clone() as ActiveMQMessage;
+
+ Assert.IsTrue(msg1.NMSMessageId.Equals(msg2.NMSMessageId));
+ Assert.IsTrue(msg1.NMSCorrelationID.Equals(msg2.NMSCorrelationID));
+ Assert.IsTrue(msg1.NMSDestination.Equals(msg2.NMSDestination));
+ Assert.IsTrue(msg1.NMSReplyTo.Equals(msg2.NMSReplyTo));
+ Assert.IsTrue(msg1.NMSDeliveryMode == msg2.NMSDeliveryMode);
+ Assert.IsTrue(msg1.NMSRedelivered == msg2.NMSRedelivered);
+ Assert.IsTrue(msg1.NMSType.Equals(msg2.NMSType));
+ Assert.IsTrue(msg1.NMSPriority == msg2.NMSPriority);
+ Assert.IsTrue(msg1.NMSTimestamp == msg2.NMSTimestamp);
+ }
+
+ [Test]
+ public void TestGetAndSetNMSCorrelationID()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.NMSCorrelationID = this.nmsCorrelationID;
+ Assert.IsTrue(msg.NMSCorrelationID.Equals(this.nmsCorrelationID));
+ }
+
+ [Test]
+ public void TestGetAndSetNMSReplyTo()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.NMSReplyTo = this.nmsReplyTo;
+ Assert.AreEqual(msg.NMSReplyTo, this.nmsReplyTo);
+ }
+
+ [Test]
+ public void TestGetAndSetNMSDestination()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.FromDestination = this.nmsDestination;
+ Assert.AreEqual(msg.NMSDestination, this.nmsDestination);
+ }
+
+ [Test]
+ public void TestGetAndSetNMSDeliveryMode()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.NMSDeliveryMode = this.nmsDeliveryMode;
+ Assert.IsTrue(msg.NMSDeliveryMode == this.nmsDeliveryMode);
+ }
+
+ [Test]
+ public void TestGetAndSetMSRedelivered()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.RedeliveryCounter = 2;
+ Assert.IsTrue(msg.NMSRedelivered == this.nmsRedelivered);
+ }
+
+ [Test]
+ public void TestGetAndSetNMSType()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.NMSType = this.nmsType;
+ Assert.AreEqual(msg.NMSType, this.nmsType);
+ }
+
+ [Test]
+ public void TestGetAndSetNMSPriority()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.NMSPriority = this.nmsPriority;
+ Assert.IsTrue(msg.NMSPriority == this.nmsPriority);
+ }
+
+ public void TestClearProperties()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.Properties.SetString("test", "test");
+ msg.Content = new byte[1];
+ msg.NMSMessageId = this.nmsMessageID;
+ msg.ClearProperties();
+ Assert.IsNull(msg.Properties.GetString("test"));
+ Assert.IsNotNull(msg.NMSMessageId);
+ Assert.IsNotNull(msg.Content);
+ }
+
+ [Test]
+ public void TestPropertyExists()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.Properties.SetString("test", "test");
+ Assert.IsTrue(msg.Properties.Contains("test"));
+ }
+
+ [Test]
+ public void TestGetBooleanProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "booleanProperty";
+ msg.Properties.SetBool(name, true);
+ Assert.IsTrue(msg.Properties.GetBool(name));
+ }
+
+ [Test]
+ public void TestGetByteProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "byteProperty";
+ msg.Properties.SetByte(name, (byte)1);
+ Assert.IsTrue(msg.Properties.GetByte(name) == 1);
+ }
+
+ [Test]
+ public void TestGetShortProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "shortProperty";
+ msg.Properties.SetShort(name, (short)1);
+ Assert.IsTrue(msg.Properties.GetShort(name) == 1);
+ }
+
+ [Test]
+ public void TestGetIntProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "intProperty";
+ msg.Properties.SetInt(name, 1);
+ Assert.IsTrue(msg.Properties.GetInt(name) == 1);
+ }
+
+ [Test]
+ public void TestGetLongProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "longProperty";
+ msg.Properties.SetLong(name, 1);
+ Assert.IsTrue(msg.Properties.GetLong(name) == 1);
+ }
+
+ [Test]
+ public void TestGetFloatProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "floatProperty";
+ msg.Properties.SetFloat(name, 1.3f);
+ Assert.IsTrue(msg.Properties.GetFloat(name) == 1.3f);
+ }
+
+ [Test]
+ public void TestGetDoubleProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "doubleProperty";
+ msg.Properties.SetDouble(name, 1.3d);
+ Assert.IsTrue(msg.Properties.GetDouble(name) == 1.3);
+ }
+
+ [Test]
+ public void TestGetStringProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "stringProperty";
+ msg.Properties.SetString(name, name);
+ Assert.IsTrue(msg.Properties.GetString(name).Equals(name));
+ }
+
+ [Test]
+ public void TestGetObjectProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "floatProperty";
+ msg.Properties.SetFloat(name, 1.3f);
+ Assert.IsTrue(msg.Properties[name] is float);
+ Assert.IsTrue((float)msg.Properties[name] == 1.3f);
+ }
+
+ [Test]
+ public void TestGetPropertyNames()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "floatProperty";
+ msg.Properties.SetFloat(name, 1.3f);
+
+ foreach(string key in msg.Properties.Keys)
+ {
+ Assert.IsTrue(key.Equals(name));
+ }
+ }
+
+ [Test]
+ public void TestSetObjectProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "property";
+
+ try
+ {
+ msg.Properties[name] = "string";
+ msg.Properties[name] = (Byte) 1;
+ msg.Properties[name] = (Int16) 1;
+ msg.Properties[name] = (Int32) 1;
+ msg.Properties[name] = (Int64) 1;
+ msg.Properties[name] = (Single) 1.1f;
+ msg.Properties[name] = (Double) 1.1;
+ msg.Properties[name] = (Boolean) true;
+ msg.Properties[name] = null;
+ }
+ catch(MessageFormatException)
+ {
+ Assert.Fail("should accept object primitives and String");
+ }
+
+ try
+ {
+ msg.Properties[name] = new Object();
+ Assert.Fail("should accept only object primitives and String");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties[name] = new StringBuilder();
+ Assert.Fail("should accept only object primitives and String");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestConvertProperties()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+
+ msg.Properties["stringProperty"] = "string";
+ msg.Properties["byteProperty"] = (Byte) 1;
+ msg.Properties["shortProperty"] = (Int16) 1;
+ msg.Properties["intProperty"] = (Int32) 1;
+ msg.Properties["longProperty"] = (Int64) 1;
+ msg.Properties["floatProperty"] = (Single) 1.1f;
+ msg.Properties["doubleProperty"] = (Double) 1.1;
+ msg.Properties["booleanProperty"] = (Boolean) true;
+ msg.Properties["nullProperty"] = null;
+
+ msg.BeforeMarshall(new OpenWireFormat());
+
+ IPrimitiveMap properties = msg.Properties;
+ Assert.AreEqual(properties["stringProperty"], "string");
+ Assert.AreEqual((byte)properties["byteProperty"], (byte) 1);
+ Assert.AreEqual((short)properties["shortProperty"], (short) 1);
+ Assert.AreEqual((int)properties["intProperty"], (int) 1);
+ Assert.AreEqual((long)properties["longProperty"], (long) 1);
+ Assert.AreEqual((float)properties["floatProperty"], 1.1f, 0);
+ Assert.AreEqual((double)properties["doubleProperty"], 1.1, 0);
+ Assert.AreEqual((bool)properties["booleanProperty"], true);
+ Assert.IsNull(properties["nullProperty"]);
+
+ }
+
+ [Test]
+ public void TestSetNullProperty()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ string name = "cheese";
+ msg.Properties.SetString(name, "Cheddar");
+ Assert.AreEqual("Cheddar", msg.Properties.GetString(name));
+
+ msg.Properties.SetString(name, null);
+ Assert.AreEqual(null, msg.Properties.GetString(name));
+ }
+
+ [Test]
+ public void TestSetNullPropertyName()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+
+ try
+ {
+ msg.Properties.SetString(null, "Cheese");
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(Exception)
+ {
+ }
+ }
+
+ [Test]
+ public void TestSetEmptyPropertyName()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+
+ try
+ {
+ msg.Properties.SetString("", "Cheese");
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(Exception)
+ {
+ }
+ }
+
+ [Test]
+ public void TestGetAndSetNMSXDeliveryCount()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ msg.Properties.SetInt("NMSXDeliveryCount", 1);
+ int count = msg.Properties.GetInt("NMSXDeliveryCount");
+ Assert.IsTrue(count == 1, "expected delivery count = 1 - got: " + count);
+ }
+
+ [Test]
+ public void TestClearBody()
+ {
+ ActiveMQBytesMessage message = new ActiveMQBytesMessage();
+ message.ClearBody();
+ Assert.IsFalse(message.ReadOnlyBody);
+ Assert.IsNull(message.Content);
+ }
+
+ [Test]
+ public void TestBooleanPropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.Properties.SetBool(propertyName, true);
+
+ Assert.AreEqual((bool)msg.Properties[propertyName], true);
+ Assert.IsTrue(msg.Properties.GetBool(propertyName));
+ Assert.AreEqual(msg.Properties.GetString(propertyName), "True");
+ try
+ {
+ msg.Properties.GetByte(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetShort(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetInt(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetLong(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetFloat(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetDouble(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestBytePropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.Properties.SetByte(propertyName, (byte)1);
+
+ Assert.AreEqual((byte)msg.Properties[propertyName], 1);
+ Assert.AreEqual(msg.Properties.GetByte(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetShort(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetInt(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetLong(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetString(propertyName), "1");
+
+ try
+ {
+ msg.Properties.GetBool(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetFloat(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try {
+ msg.Properties.GetDouble(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestShortPropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.Properties.SetShort(propertyName, (short)1);
+
+ Assert.AreEqual((short)msg.Properties[propertyName], 1);
+ Assert.AreEqual(msg.Properties.GetShort(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetInt(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetLong(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetString(propertyName), "1");
+
+ try
+ {
+ msg.Properties.GetBool(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetByte(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetFloat(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetDouble(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestIntPropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.Properties.SetInt(propertyName, (int)1);
+
+ Assert.AreEqual((int)msg.Properties[propertyName], 1);
+ Assert.AreEqual(msg.Properties.GetInt(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetLong(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetString(propertyName), "1");
+
+ try
+ {
+ msg.Properties.GetBool(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetByte(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetShort(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetFloat(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetDouble(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestLongPropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.Properties.SetLong(propertyName, 1);
+
+ Assert.AreEqual((long)msg.Properties[propertyName], 1);
+ Assert.AreEqual(msg.Properties.GetLong(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetString(propertyName), "1");
+
+ try
+ {
+ msg.Properties.GetBool(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetByte(propertyName);
+ Assert.Fail("Should have thrown exception");
+ } catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetShort(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetInt(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetFloat(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetDouble(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestFloatPropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.Properties.SetFloat(propertyName, (float)1.5);
+ Assert.AreEqual((float)msg.Properties[propertyName], 1.5, 0);
+ Assert.AreEqual(msg.Properties.GetFloat(propertyName), 1.5, 0);
+ Assert.AreEqual(msg.Properties.GetDouble(propertyName), 1.5, 0);
+ Assert.AreEqual(msg.Properties.GetString(propertyName), "1.5");
+
+ try
+ {
+ msg.Properties.GetBool(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetByte(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetShort(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetInt(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetLong(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestDoublePropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.Properties.SetDouble(propertyName, 1.5);
+ Assert.AreEqual((double)msg.Properties[propertyName], 1.5, 0);
+ Assert.AreEqual(msg.Properties.GetDouble(propertyName), 1.5, 0);
+ Assert.AreEqual(msg.Properties.GetString(propertyName), "1.5");
+
+ try
+ {
+ msg.Properties.GetBool(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetByte(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetShort(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetInt(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetLong(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetFloat(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestStringPropertyConversion()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ String stringValue = "True";
+ msg.Properties.SetString(propertyName, stringValue);
+ Assert.AreEqual(msg.Properties.GetString(propertyName), stringValue);
+ Assert.AreEqual((string)msg.Properties[propertyName], stringValue);
+ Assert.AreEqual(msg.Properties.GetBool(propertyName), true);
+
+ stringValue = "1";
+ msg.Properties.SetString(propertyName, stringValue);
+ Assert.AreEqual(msg.Properties.GetByte(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetShort(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetInt(propertyName), 1);
+ Assert.AreEqual(msg.Properties.GetLong(propertyName), 1);
+
+ stringValue = "1.5";
+ msg.Properties.SetString(propertyName, stringValue);
+ Assert.AreEqual(msg.Properties.GetFloat(propertyName), 1.5, 0);
+ Assert.AreEqual(msg.Properties.GetDouble(propertyName), 1.5, 0);
+
+ stringValue = "bad";
+ msg.Properties.SetString(propertyName, stringValue);
+
+ try
+ {
+ msg.Properties.GetByte(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetShort(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetInt(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetLong(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetFloat(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.GetDouble(propertyName);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ Assert.IsFalse(msg.Properties.GetBool(propertyName));
+ }
+
+ [Test]
+ public void TestReadOnlyProperties()
+ {
+ ActiveMQMessage msg = new ActiveMQMessage();
+ String propertyName = "property";
+ msg.ReadOnlyProperties = true;
+
+ try
+ {
+ msg.Properties[propertyName] = new Object();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.SetString(propertyName, "test");
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.SetBool(propertyName, true);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.SetByte(propertyName, (byte)1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException) {
+ }
+
+ try
+ {
+ msg.Properties.SetShort(propertyName, (short)1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.SetInt(propertyName, 1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.SetLong(propertyName, 1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.SetFloat(propertyName, (float)1.5);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+
+ try
+ {
+ msg.Properties.SetDouble(propertyName, 1.5);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ }
+
+// public void TestIsExpired() {
+// ActiveMQMessage msg = new ActiveMQMessage();
+// msg.NMSExpiration(System.currentTimeMillis() - 1);
+// Assert.IsTrue(msg.isExpired());
+// msg.NMSExpiration(System.currentTimeMillis() + 10000);
+// Assert.IsFalse(msg.isExpired());
+// }
+ }
+}
Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMessageTest.cs
------------------------------------------------------------------------------
svn:eol-style = native
Copied: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyIntercepter.cs (from r823288, activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyHelper.cs)
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyIntercepter.cs?p2=activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyIntercepter.cs&p1=activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyHelper.cs&r1=823288&r2=824393&rev=824393&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyHelper.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MessagePropertyIntercepter.cs Mon Oct 12 15:47:01 2009
@@ -20,22 +20,38 @@
namespace Apache.NMS.Util
{
- // Set NMS properties via introspection
- public class MessagePropertyHelper : IPrimitiveMap
+ /// <summary>
+ /// Utility class used to set NMS properties via introspection for IMessage derived
+ /// instances. This class allows IMessage classes to define Message specific properties
+ /// that can be accessed using the standard property get / set semantics.
+ ///
+ /// This is especially useful for NMSX type properties which can vary by provider and
+ /// are obtianed via a call to IConnectionMetaData.NMSXPropertyNames. The client can
+ /// set the properties on an IMessage instance without a direct cast to the providers
+ /// specific Message types.
+ ///
+ /// Properties accessed in this way are treated as NMS Message headers which are never
+ /// read-only therefore there is no exception thrown if the message itself is in the
+ /// read-only property mode.
+ /// </summary>
+ public class MessagePropertyIntercepter : PrimitiveMapInterceptor
{
private static BindingFlags publicBinding = BindingFlags.Public | BindingFlags.Instance;
- private IMessage message;
- private IPrimitiveMap properties;
private Type messageType;
- public MessagePropertyHelper(IMessage _message, IPrimitiveMap _properties)
+ public MessagePropertyIntercepter(IMessage message, IPrimitiveMap properties)
+ : base(message, properties)
{
- this.message = _message;
- this.properties = _properties;
- this.messageType = _message.GetType();
+ this.messageType = message.GetType();
}
- protected object GetObjectProperty(string name)
+ public MessagePropertyIntercepter(IMessage message, IPrimitiveMap properties, bool readOnly)
+ : base(message, properties, readOnly)
+ {
+ this.messageType = message.GetType();
+ }
+
+ protected override object GetObjectProperty(string name)
{
PropertyInfo propertyInfo = this.messageType.GetProperty(name, publicBinding);
@@ -53,10 +69,10 @@
}
}
- return this.properties[name];
+ return base.GetObjectProperty(name);
}
- protected void SetObjectProperty(string name, object value)
+ protected override void SetObjectProperty(string name, object value)
{
PropertyInfo propertyInfo = this.messageType.GetProperty(name, publicBinding);
@@ -74,159 +90,9 @@
}
else
{
- this.properties[name] = value;
+ base.SetObjectProperty(name, value);
}
}
}
-
- #region IPrimitiveMap Members
-
- public void Clear()
- {
- this.properties.Clear();
- }
-
- public bool Contains(object key)
- {
- return this.properties.Contains(key);
- }
-
- public void Remove(object key)
- {
- this.properties.Remove(key);
- }
-
- public int Count
- {
- get { return this.properties.Count; }
- }
-
- public System.Collections.ICollection Keys
- {
- get { return this.properties.Keys; }
- }
-
- public System.Collections.ICollection Values
- {
- get { return this.properties.Values; }
- }
-
- public object this[string key]
- {
- get { return GetObjectProperty(key); }
- set { SetObjectProperty(key, value); }
- }
-
- public string GetString(string key)
- {
- return (string) GetObjectProperty(key);
- }
-
- public void SetString(string key, string value)
- {
- SetObjectProperty(key, value);
- }
-
- public bool GetBool(string key)
- {
- return (bool) GetObjectProperty(key);
- }
-
- public void SetBool(string key, bool value)
- {
- SetObjectProperty(key, value);
- }
-
- public byte GetByte(string key)
- {
- return (byte) GetObjectProperty(key);
- }
-
- public void SetByte(string key, byte value)
- {
- SetObjectProperty(key, value);
- }
-
- public char GetChar(string key)
- {
- return (char) GetObjectProperty(key);
- }
-
- public void SetChar(string key, char value)
- {
- SetObjectProperty(key, value);
- }
-
- public short GetShort(string key)
- {
- return (short) GetObjectProperty(key);
- }
-
- public void SetShort(string key, short value)
- {
- SetObjectProperty(key, value);
- }
-
- public int GetInt(string key)
- {
- return (int) GetObjectProperty(key);
- }
-
- public void SetInt(string key, int value)
- {
- SetObjectProperty(key, value);
- }
-
- public long GetLong(string key)
- {
- return (long) GetObjectProperty(key);
- }
-
- public void SetLong(string key, long value)
- {
- SetObjectProperty(key, value);
- }
-
- public float GetFloat(string key)
- {
- return (float) GetObjectProperty(key);
- }
-
- public void SetFloat(string key, float value)
- {
- SetObjectProperty(key, value);
- }
-
- public double GetDouble(string key)
- {
- return (double) GetObjectProperty(key);
- }
-
- public void SetDouble(string key, double value)
- {
- SetObjectProperty(key, value);
- }
-
- public System.Collections.IList GetList(string key)
- {
- return (System.Collections.IList) GetObjectProperty(key);
- }
-
- public void SetList(string key, System.Collections.IList list)
- {
- SetObjectProperty(key, list);
- }
-
- public System.Collections.IDictionary GetDictionary(string key)
- {
- return (System.Collections.IDictionary) GetObjectProperty(key);
- }
-
- public void SetDictionary(string key, System.Collections.IDictionary dictionary)
- {
- SetObjectProperty(key, dictionary);
- }
-
- #endregion
}
}
Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMap.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMap.cs?rev=824393&r1=824392&r2=824393&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMap.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMap.cs Mon Oct 12 15:47:01 2009
@@ -248,7 +248,9 @@
if(value != null && !(value is IList) && !(value is IDictionary))
{
Type type = value.GetType();
- if(!type.IsPrimitive && !type.IsValueType && !type.IsAssignableFrom(typeof(string)))
+
+ if(type.IsInstanceOfType(typeof(Object)) ||
+ (!type.IsPrimitive && !type.IsValueType && !type.IsAssignableFrom(typeof(string))))
{
throw new NMSException("Invalid type: " + type.Name + " for value: " + value);
}
Added: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMapInterceptor.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMapInterceptor.cs?rev=824393&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMapInterceptor.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMapInterceptor.cs Mon Oct 12 15:47:01 2009
@@ -0,0 +1,417 @@
+/*
+ * 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.
+*/
+
+using System;
+using System.Collections;
+
+namespace Apache.NMS.Util
+{
+ /// <summary>
+ /// This class provides a mechanism to intercept calls to a IPrimitiveMap
+ /// instance and perform validation, handle type conversion, or some other
+ /// function necessary to use the PrimitiveMap in a Message or other NMS
+ /// object.
+ ///
+ /// Be default this class enforces the standard conversion policy for primitive
+ /// types in NMS shown in the table below:
+ ///
+ /// | | boolean byte short char int long float double String
+ /// |--------------------------------------------------------------
+ /// |boolean | X X
+ /// |byte | X X X X X
+ /// |short | X X X X
+ /// |char | X X
+ /// |int | X X X
+ /// |long | X X
+ /// |float | X X X
+ /// |double | X X
+ /// |String | X X X X X X X X
+ /// |--------------------------------------------------------------
+ ///
+ /// </summary>
+ public class PrimitiveMapInterceptor : IPrimitiveMap
+ {
+ protected IMessage message;
+ protected IPrimitiveMap properties;
+ private bool readOnly = false;
+
+ public PrimitiveMapInterceptor(IMessage message, IPrimitiveMap properties)
+ {
+ this.message = message;
+ this.properties = properties;
+ }
+
+ public PrimitiveMapInterceptor(IMessage message, IPrimitiveMap properties, bool readOnly)
+ {
+ this.message = message;
+ this.properties = properties;
+ this.readOnly = readOnly;
+ }
+
+ protected virtual object GetObjectProperty(string name)
+ {
+ return this.properties[name];
+ }
+
+ protected virtual void SetObjectProperty(string name, object value)
+ {
+ FailIfReadOnly();
+
+ try
+ {
+ this.properties[name] = value;
+ }
+ catch(Exception ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ #region IPrimitiveMap Members
+
+ public void Clear()
+ {
+ FailIfReadOnly();
+ this.properties.Clear();
+ }
+
+ public bool Contains(object key)
+ {
+ return this.properties.Contains(key);
+ }
+
+ public void Remove(object key)
+ {
+ FailIfReadOnly();
+ this.properties.Remove(key);
+ }
+
+ public int Count
+ {
+ get { return this.properties.Count; }
+ }
+
+ public System.Collections.ICollection Keys
+ {
+ get { return this.properties.Keys; }
+ }
+
+ public System.Collections.ICollection Values
+ {
+ get { return this.properties.Values; }
+ }
+
+ public object this[string key]
+ {
+ get { return GetObjectProperty(key); }
+ set { SetObjectProperty(key, value); }
+ }
+
+ public string GetString(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ if(value == null)
+ {
+ return null;
+ }
+ else if((value is IList) || (value is IDictionary))
+ {
+ throw new MessageFormatException(" cannot read a boolean from " + value.GetType().Name);
+ }
+
+ return value.ToString();
+ }
+
+ public void SetString(string key, string value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public bool GetBool(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Boolean)
+ {
+ return (bool) value;
+ }
+ else if(value is String)
+ {
+ return ((string) value).ToLower() == "true";
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a boolean from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetBool(string key, bool value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public byte GetByte(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Byte)
+ {
+ return (byte) value;
+ }
+ else if(value is String)
+ {
+ return Convert.ToByte(value);
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a byte from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetByte(string key, byte value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public char GetChar(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Char)
+ {
+ return (char) value;
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a char from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetChar(string key, char value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public short GetShort(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Int16)
+ {
+ return (short) value;
+ }
+ else if(value is Byte || value is String)
+ {
+ return Convert.ToInt16(value);
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a short from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetShort(string key, short value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public int GetInt(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Int32)
+ {
+ return (int) value;
+ }
+ else if(value is Int16 || value is Byte || value is String)
+ {
+ return Convert.ToInt32(value);
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a int from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetInt(string key, int value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public long GetLong(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Int64)
+ {
+ return (long) value;
+ }
+ else if(value is Int32 || value is Int16 || value is Byte || value is String)
+ {
+ return Convert.ToInt64(value);
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a long from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetLong(string key, long value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public float GetFloat(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Single)
+ {
+ return (float) value;
+ }
+ else if(value is String)
+ {
+ return Convert.ToSingle(value);
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a float from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetFloat(string key, float value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public double GetDouble(string key)
+ {
+ Object value = GetObjectProperty(key);
+
+ try
+ {
+ if(value is Double)
+ {
+ return (double) value;
+ }
+ else if(value is Single || value is String)
+ {
+ return Convert.ToDouble(value);
+ }
+ else
+ {
+ throw new MessageFormatException(" cannot read a double from " + value.GetType().Name);
+ }
+ }
+ catch(FormatException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public void SetDouble(string key, double value)
+ {
+ SetObjectProperty(key, value);
+ }
+
+ public System.Collections.IList GetList(string key)
+ {
+ return (System.Collections.IList) GetObjectProperty(key);
+ }
+
+ public void SetList(string key, System.Collections.IList list)
+ {
+ SetObjectProperty(key, list);
+ }
+
+ public System.Collections.IDictionary GetDictionary(string key)
+ {
+ return (System.Collections.IDictionary) GetObjectProperty(key);
+ }
+
+ public void SetDictionary(string key, System.Collections.IDictionary dictionary)
+ {
+ SetObjectProperty(key, dictionary);
+ }
+
+ #endregion
+
+ public bool ReadOnly
+ {
+ get{ return this.readOnly; }
+ set{ this.readOnly = value; }
+ }
+
+ protected virtual void FailIfReadOnly()
+ {
+ if(this.ReadOnly == true)
+ {
+ throw new MessageNotWriteableException("Properties are in Read-Only mode.");
+ }
+ }
+ }
+}
Propchange: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/PrimitiveMapInterceptor.cs
------------------------------------------------------------------------------
svn:eol-style = native