activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r824531 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src: main/csharp/Commands/ActiveMQMapMessage.cs test/csharp/Commands/ActiveMQMapMessageTest.cs
Date Mon, 12 Oct 2009 23:03:09 GMT
Author: tabish
Date: Mon Oct 12 23:03:09 2009
New Revision: 824531

URL: http://svn.apache.org/viewvc?rev=824531&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 MapMessage class.

Added:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMapMessageTest.cs
  (with props)
Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMapMessage.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMapMessage.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMapMessage.cs?rev=824531&r1=824530&r2=824531&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMapMessage.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQMapMessage.cs
Mon Oct 12 23:03:09 2009
@@ -25,13 +25,39 @@
 		public const byte ID_ACTIVEMQMAPMESSAGE = 25;
 
 		private PrimitiveMap body;
-
+        private PrimitiveMapInterceptor typeConverter;
 
 		public override byte GetDataStructureType()
 		{
 			return ID_ACTIVEMQMAPMESSAGE;
 		}
 
+        public override void ClearBody()
+        {
+            this.body = null;
+            this.typeConverter = null;
+            base.ClearBody();
+        }
+
+        public override bool ReadOnlyBody 
+        {
+            get 
+            {
+                return base.ReadOnlyBody;
+            }
+            
+            set 
+            {
+                if(this.typeConverter != null)
+                {
+                    this.typeConverter.ReadOnly = true;
+                }
+                
+                base.ReadOnlyBody = value;
+            }
+        }
+
+        
 		public IPrimitiveMap Body
 		{
 			get
@@ -39,8 +65,10 @@
 				if(body == null)
 				{
 					body = PrimitiveMap.Unmarshal(Content);
+                    typeConverter = new PrimitiveMapInterceptor(this, body);
 				}
-				return body;
+				
+                return typeConverter;
 			}
 		}
 

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMapMessageTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMapMessageTest.cs?rev=824531&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMapMessageTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMapMessageTest.cs
Mon Oct 12 23:03:09 2009
@@ -0,0 +1,547 @@
+/*
+ * 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;
+
+namespace Apache.NMS.ActiveMQ.Test.Commands
+{    
+    [TestFixture]
+    public class ActiveMQMapMessageTest
+    {
+
+        private string name = "testName";
+        
+        [Test]
+        public void TestBytesConversion()
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            msg.Body.SetBool("boolean", true);
+            msg.Body.SetByte("byte", (byte)1);
+            msg.Body["bytes"] = new byte[1];
+            msg.Body.SetChar("char", 'a');
+            msg.Body.SetDouble("double", 1.5);
+            msg.Body.SetFloat("float", 1.5f);
+            msg.Body.SetInt("int", 1);
+            msg.Body.SetLong("long", 1);
+            msg.Body["object"] = "stringObj";
+            msg.Body.SetShort("short", (short)1);
+            msg.Body.SetString("string", "string");
+    
+            // Test with a 1Meg String
+            StringBuilder bigSB = new StringBuilder(1024 * 1024);
+            for(int i = 0; i < 1024 * 1024; i++) 
+            {
+                bigSB.Append((char)'a' + i % 26);
+            }
+            String bigString = bigSB.ToString();
+    
+            msg.Body.SetString("bigString", bigString);
+    
+            msg = (ActiveMQMapMessage)msg.Clone();
+    
+            Assert.AreEqual(msg.Body.GetBool("boolean"), true);
+            Assert.AreEqual(msg.Body.GetByte("byte"), (byte)1);
+            Assert.AreEqual((msg.Body["bytes"] as byte[]).Length, 1);
+            Assert.AreEqual(msg.Body.GetChar("char"), 'a');
+            Assert.AreEqual(msg.Body.GetDouble("double"), 1.5, 0);
+            Assert.AreEqual(msg.Body.GetFloat("float"), 1.5f, 0);
+            Assert.AreEqual(msg.Body.GetInt("int"), 1);
+            Assert.AreEqual(msg.Body.GetLong("long"), 1);
+            Assert.AreEqual(msg.Body["object"], "stringObj");
+            Assert.AreEqual(msg.Body.GetShort("short"), (short)1);
+            Assert.AreEqual(msg.Body.GetString("string"), "string");
+            Assert.AreEqual(msg.Body.GetString("bigString"), bigString);
+        }
+
+        [Test]
+        public void TestGetBoolean()
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            msg.Body.SetBool(name, true);
+            msg.ReadOnlyBody = true;
+            Assert.IsTrue(msg.Body.GetBool(name));
+            msg.ClearBody();
+            msg.Body.SetString(name, "True");
+    
+            msg = (ActiveMQMapMessage)msg.Clone();
+    
+            Assert.IsTrue(msg.Body.GetBool(name));
+        }
+    
+        [Test]
+        public void TestGetByte() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            msg.Body.SetByte(this.name, (byte)1);
+            msg = (ActiveMQMapMessage)msg.Clone();
+            Assert.IsTrue(msg.Body.GetByte(this.name) == (byte)1);
+        }
+    
+        [Test]
+        public void TestGetShort() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try 
+            {
+                msg.Body.SetShort(this.name, (short)1);
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.IsTrue(msg.Body.GetShort(this.name) == (short)1);
+            } 
+            catch(NMSException)
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetChar() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try 
+            {
+                msg.Body.SetChar(this.name, 'a');
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.IsTrue(msg.Body.GetChar(this.name) == 'a');
+            }
+            catch(NMSException) 
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetInt() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try 
+            {
+                msg.Body.SetInt(this.name, 1);
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.IsTrue(msg.Body.GetInt(this.name) == 1);
+            } 
+            catch(NMSException) 
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetLong() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try 
+            {
+                msg.Body.SetLong(this.name, 1);
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.IsTrue(msg.Body.GetLong(this.name) == 1);
+            } 
+            catch(NMSException) 
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetFloat() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try 
+            {
+                msg.Body.SetFloat(this.name, 1.5f);
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.IsTrue(msg.Body.GetFloat(this.name) == 1.5f);
+            } 
+            catch(NMSException)
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetDouble() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try 
+            {
+                msg.Body.SetDouble(this.name, 1.5);
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.IsTrue(msg.Body.GetDouble(this.name) == 1.5);
+            } 
+            catch(NMSException)
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetString()
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try 
+            {
+                String str = "test";
+                msg.Body.SetString(this.name, str);
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.AreEqual(msg.Body.GetString(this.name), str);
+            } 
+            catch(NMSException)
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetBytes() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            try
+            {
+                byte[] bytes1 = new byte[3];
+                byte[] bytes2 = new byte[2];
+                System.Array.Copy(bytes1, 0, bytes2, 0, 2);
+                msg.Body[this.name] = bytes1;
+                msg.Body[this.name + "2"] = bytes2;
+                msg = (ActiveMQMapMessage)msg.Clone();
+                Assert.IsTrue(System.Array.Equals(msg.Body[this.name], bytes1));
+                Assert.AreEqual(((byte[]) msg.Body[this.name + "2"]).Length, bytes2.Length);
+            } 
+            catch(NMSException) 
+            {
+                Assert.IsTrue(false);
+            }
+        }
+    
+        [Test]
+        public void TestGetObject() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            Boolean booleanValue = true;
+            Byte byteValue = Byte.Parse("1");
+            byte[] bytesValue = new byte[3];
+            Char charValue = (Char) 'a';
+            Double doubleValue = Double.Parse("1.5");
+            Single floatValue = Single.Parse("1.5");
+            Int32 intValue = Int32.Parse("1");
+            Int64 longValue = Int64.Parse("1");
+            Int16 shortValue = Int16.Parse("1");
+            String stringValue = "string";
+    
+            try 
+            {
+                msg.Body["boolean"] = booleanValue;
+                msg.Body["byte"] = byteValue;
+                msg.Body["bytes"] = bytesValue;
+                msg.Body["char"] = charValue;
+                msg.Body["double"] = doubleValue;
+                msg.Body["float"] = floatValue;
+                msg.Body["int"] = intValue;
+                msg.Body["long"] = longValue;
+                msg.Body["short"] = shortValue;
+                msg.Body["string"] = stringValue;
+            } 
+            catch(MessageFormatException)
+            {
+                Assert.Fail("object formats should be correct");
+            }
+    
+            msg = (ActiveMQMapMessage)msg.Clone();
+    
+            Assert.IsTrue(msg.Body["boolean"] is Boolean);
+            Assert.AreEqual(msg.Body["boolean"], booleanValue);
+            Assert.AreEqual(msg.Body.GetBool("boolean"), booleanValue);
+            Assert.IsTrue(msg.Body["byte"] is Byte);
+            Assert.AreEqual(msg.Body["byte"], byteValue);
+            Assert.AreEqual(msg.Body.GetByte("byte"), byteValue);
+            Assert.IsTrue(msg.Body["bytes"] is byte[]);
+            Assert.AreEqual(((byte[])msg.Body["bytes"]).Length, bytesValue.Length);
+            Assert.AreEqual((msg.Body["bytes"] as byte[]).Length, bytesValue.Length);
+            Assert.IsTrue(msg.Body["char"] is Char);
+            Assert.AreEqual(msg.Body["char"], charValue);
+            Assert.AreEqual(msg.Body.GetChar("char"), charValue);
+            Assert.IsTrue(msg.Body["double"] is Double);
+            Assert.AreEqual(msg.Body["double"], doubleValue);
+            Assert.AreEqual(msg.Body.GetDouble("double"), doubleValue, 0);
+            Assert.IsTrue(msg.Body["float"] is Single);
+            Assert.AreEqual(msg.Body["float"], floatValue);
+            Assert.AreEqual(msg.Body.GetFloat("float"), floatValue, 0);
+            Assert.IsTrue(msg.Body["int"] is Int32);
+            Assert.AreEqual(msg.Body["int"], intValue);
+            Assert.AreEqual(msg.Body.GetInt("int"), intValue);
+            Assert.IsTrue(msg.Body["long"] is Int64);
+            Assert.AreEqual(msg.Body["long"], longValue);
+            Assert.AreEqual(msg.Body.GetLong("long"), longValue);
+            Assert.IsTrue(msg.Body["short"] is Int16);
+            Assert.AreEqual(msg.Body["short"], shortValue);
+            Assert.AreEqual(msg.Body.GetShort("short"), shortValue);
+            Assert.IsTrue(msg.Body["string"] is String);
+            Assert.AreEqual(msg.Body["string"], stringValue);
+            Assert.AreEqual(msg.Body.GetString("string"), stringValue);
+    
+            msg.ClearBody();
+            try 
+            {
+                msg.Body["object"] = new Object();
+                Assert.Fail("should have thrown exception");
+            }
+            catch(MessageFormatException)
+            {
+            }
+    
+        }
+    
+        [Test]
+        public void TestGetMapNames() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            msg.Body.SetBool("boolean", true);
+            msg.Body.SetByte("byte", (byte)1);
+            msg.Body["bytes1"] = new byte[1];
+            msg.Body.SetChar("char", 'a');
+            msg.Body.SetDouble("double", 1.5);
+            msg.Body.SetFloat("float", 1.5f);
+            msg.Body.SetInt("int", 1);
+            msg.Body.SetLong("long", 1);
+            msg.Body["object"] = "stringObj";
+            msg.Body.SetShort("short", (short)1);
+            msg.Body.SetString("string", "string");
+    
+            msg = (ActiveMQMapMessage)msg.Clone();
+    
+            ICollection mapNames = msg.Body.Keys;
+            System.Collections.ArrayList mapNamesList = new System.Collections.ArrayList(mapNames);
+    
+            Assert.AreEqual(mapNamesList.Count, 11);
+            Assert.IsTrue(mapNamesList.Contains("boolean"));
+            Assert.IsTrue(mapNamesList.Contains("byte"));
+            Assert.IsTrue(mapNamesList.Contains("bytes1"));
+            Assert.IsTrue(mapNamesList.Contains("char"));
+            Assert.IsTrue(mapNamesList.Contains("double"));
+            Assert.IsTrue(mapNamesList.Contains("float"));
+            Assert.IsTrue(mapNamesList.Contains("int"));
+            Assert.IsTrue(mapNamesList.Contains("long"));
+            Assert.IsTrue(mapNamesList.Contains("object"));
+            Assert.IsTrue(mapNamesList.Contains("short"));
+            Assert.IsTrue(mapNamesList.Contains("string"));
+        }
+    
+        [Test]
+        public void TestItemExists() 
+        {
+            ActiveMQMapMessage mapMessage = new ActiveMQMapMessage();
+    
+            mapMessage.Body.SetString("exists", "test");
+    
+            mapMessage = (ActiveMQMapMessage)mapMessage.Clone();
+    
+            Assert.IsTrue(mapMessage.Body.Contains("exists"));
+            Assert.IsFalse(mapMessage.Body.Contains("doesntExist"));
+        }
+    
+        [Test]
+        public void TestClearBody() 
+        {
+            ActiveMQMapMessage mapMessage = new ActiveMQMapMessage();
+            mapMessage.Body.SetString("String", "String");
+            mapMessage.ClearBody();
+            Assert.IsFalse(mapMessage.ReadOnlyBody);
+    
+            mapMessage.OnSend();
+            mapMessage.Content = mapMessage.Content;
+            Assert.IsNull(mapMessage.Body.GetString("String"));
+            mapMessage.ClearBody();
+            mapMessage.Body.SetString("String", "String");
+    
+            mapMessage = (ActiveMQMapMessage)mapMessage.Clone();
+    
+            mapMessage.Body.GetString("String");
+        }
+
+        [Test]
+        public void TestReadOnlyBody() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            msg.Body.SetBool("boolean", true);
+            msg.Body.SetByte("byte", (byte)1);
+            msg.Body["bytes"] = new byte[1];
+            msg.Body.SetChar("char", 'a');
+            msg.Body.SetDouble("double", 1.5);
+            msg.Body.SetFloat("float", 1.5f);
+            msg.Body.SetInt("int", 1);
+            msg.Body.SetLong("long", 1);
+            msg.Body["object"] = "stringObj";
+            msg.Body.SetShort("short", (short)1);
+            msg.Body.SetString("string", "string");
+    
+            msg.ReadOnlyBody = true;
+    
+            try 
+            {
+                msg.Body.GetBool("boolean");
+                msg.Body.GetByte("byte");
+                Assert.IsNotNull(msg.Body["bytes"]);
+                msg.Body.GetChar("char");
+                msg.Body.GetDouble("double");
+                msg.Body.GetFloat("float");
+                msg.Body.GetInt("int");
+                msg.Body.GetLong("long");
+                Assert.IsNotNull(msg.Body["object"]); 
+                msg.Body.GetShort("short");
+                msg.Body.GetString("string");
+            } 
+            catch(MessageNotReadableException) 
+            {
+                Assert.Fail("should be readable");
+            }
+            
+            try 
+            {
+                msg.Body.SetBool("boolean", true);
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try 
+            {
+                msg.Body.SetByte("byte", (byte)1);
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try 
+            {
+                msg.Body["bytes"] = new byte[1];
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try 
+            {
+                msg.Body.SetChar("char", 'a');
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try 
+            {
+                msg.Body.SetDouble("double", 1.5);
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try {
+                msg.Body.SetFloat("float", 1.5f);
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try
+            {
+                msg.Body.SetInt("int", 1);
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException)
+            {
+            }
+            
+            try 
+            {
+                msg.Body.SetLong("long", 1);
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try
+            {
+                msg.Body["object"] = "stringObj";
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try 
+            {
+                msg.Body.SetShort("short", (short)1);
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+            
+            try 
+            {
+                msg.Body.SetString("string", "string");
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+        }
+    
+        [Test]
+        public void TestWriteOnlyBody() 
+        {
+            ActiveMQMapMessage msg = new ActiveMQMapMessage();
+            msg.ReadOnlyBody = false;
+    
+            msg.Body.SetBool("boolean", true);
+            msg.Body.SetByte("byte", (byte)1);
+            msg.Body["bytes"] = new byte[1];
+            msg.Body.SetChar("char", 'a');
+            msg.Body.SetDouble("double", 1.5);
+            msg.Body.SetFloat("float", 1.5f);
+            msg.Body.SetInt("int", 1);
+            msg.Body.SetLong("long", 1);
+            msg.Body["object"] = "stringObj";
+            msg.Body.SetShort("short", (short)1);
+            msg.Body.SetString("string", "string");
+    
+            msg.ReadOnlyBody = true;
+    
+            msg.Body.GetBool("boolean");
+            msg.Body.GetByte("byte");
+            Assert.IsNotNull(msg.Body["bytes"]); 
+            msg.Body.GetChar("char");
+            msg.Body.GetDouble("double");
+            msg.Body.GetFloat("float");
+            msg.Body.GetInt("int");
+            msg.Body.GetLong("long");
+            Assert.IsNotNull(msg.Body["object"]); 
+            msg.Body.GetShort("short");
+            msg.Body.GetString("string");
+        }
+        
+    }
+}

Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQMapMessageTest.cs
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message