activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r823287 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src: main/csharp/Commands/ActiveMQTextMessage.cs test/csharp/Commands/ActiveMQTextMessageTest.cs
Date Thu, 08 Oct 2009 19:25:28 GMT
Author: tabish
Date: Thu Oct  8 19:25:28 2009
New Revision: 823287

URL: http://svn.apache.org/viewvc?rev=823287&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQNET-193

Enhance ActiveMQTextMessage to honor Read-Only message body state, also refactored to do lazy
text encode / decode of the message text.  

Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQTextMessage.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQTextMessage.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQTextMessage.cs?rev=823287&r1=823286&r2=823287&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQTextMessage.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQTextMessage.cs
Thu Oct  8 19:25:28 2009
@@ -17,7 +17,9 @@
 
 using System;
 using System.IO;
+using Apache.NMS;
 using Apache.NMS.Util;
+using Apache.NMS.ActiveMQ.OpenWire;
 
 namespace Apache.NMS.ActiveMQ.Commands
 {
@@ -25,7 +27,7 @@
 	{
 		public const byte ID_ACTIVEMQTEXTMESSAGE = 28;
 
-		private String text;
+		private String text = null;
 
 		public ActiveMQTextMessage()
 		{
@@ -36,15 +38,23 @@
 			this.Text = text;
 		}
 
-		// TODO generate Equals method
-		// TODO generate GetHashCode method
-		// TODO generate ToString method
-
 		public override string ToString()
 		{
-			return base.ToString() + " Text=" + Text;
+            string text = this.Text;
+
+            if(text != null && text.Length > 63)
+            {
+                text = text.Substring(0, 45) + "..." + text.Substring(text.Length - 12);
+            }
+			return base.ToString() + " Text = " + (text ?? "null");
 		}
 
+        public override void ClearBody()
+        {
+            base.ClearBody();
+            this.text = null;
+        }
+
 		public override byte GetDataStructureType()
 		{
 			return ID_ACTIVEMQTEXTMESSAGE;
@@ -56,39 +66,70 @@
 		{
 			get
 			{
-				if(text == null)
-				{
-					// now lets read the content
-					byte[] data = this.Content;
-					if(data != null)
-					{
-						MemoryStream stream = new MemoryStream(data);
-						EndianBinaryReader reader = new EndianBinaryReader(stream);
-						text = reader.ReadString32();
-					}
-				}
-				return text;
+                try
+                {
+    				if(this.text == null && this.Content != null)
+    				{
+                        // TODO - Handle Compression
+    					MemoryStream stream = new MemoryStream(this.Content);
+    					EndianBinaryReader reader = new EndianBinaryReader(stream);
+    					this.text = reader.ReadString32();
+                        this.Content = null;
+    				}
+    				return this.text;
+                }
+                catch(IOException ex)
+                {
+                    throw NMSExceptionSupport.create(ex);
+                }
 			}
 
 			set
 			{
+                FailIfReadOnlyBody();                
 				this.text = value;
-				byte[] data = null;
-				if(text != null)
-				{
-					// TODO lets make the evaluation of the Content lazy!
-
-					// Set initial size to the size of the string the UTF-8 encode could
-					// result in more if there are chars that encode to multibye values.
-					MemoryStream stream = new MemoryStream(text.Length);
-					EndianBinaryWriter writer = new EndianBinaryWriter(stream);
-					writer.WriteString32(text);
-					data = stream.GetBuffer();
-				}
-				this.Content = data;
-
+                this.Content = null;
 			}
 		}
+
+        public override void BeforeMarshall(OpenWireFormat wireFormat)
+        {
+            base.BeforeMarshall(wireFormat);
+
+            if(this.Content == null && text != null)
+            {
+                byte[] data = null;
+
+                // TODO - Deal with Compressoin.
+                
+                // Set initial size to the size of the string the UTF-8 encode could
+                // result in more if there are chars that encode to multibye values.
+                MemoryStream stream = new MemoryStream(text.Length);
+                EndianBinaryWriter writer = new EndianBinaryWriter(stream);
+                writer.WriteString32(text);
+                data = stream.GetBuffer();
+                
+                this.Content = data;
+                this.text = null;
+            }
+        }
+
+        public override int Size()
+        {
+            if(this.Content == null && text != null) 
+            {
+                int size = DEFAULT_MINIMUM_MESSAGE_SIZE;
+
+                if(MarshalledProperties != null) 
+                {
+                    size += MarshalledProperties.Length;
+                }
+                
+                return (size += this.text.Length * 2);
+            }
+
+            return base.Size();
+        }        
 	}
 }
 

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs?rev=823287&r1=823286&r2=823287&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs
Thu Oct  8 19:25:28 2009
@@ -16,9 +16,12 @@
  */
 
 using Apache.NMS.ActiveMQ.Commands;
+using Apache.NMS;
+using Apache.NMS.Util;
 using NUnit.Framework;
 using System;
 using System.Text;
+using System.IO;
 
 namespace Apache.NMS.ActiveMQ.Test.Commands
 {
@@ -46,5 +49,152 @@
             Assert.IsNotNull(message.Text);
             Assert.AreEqual(unicodeString, message.Text);
         }
+
+        [Test]
+        public void TestShallowCopy()
+        {
+            ActiveMQTextMessage msg = new ActiveMQTextMessage();
+            string testString = "str";
+            msg.Text = testString;
+            Message copy = msg.Clone() as Message;
+            Assert.IsTrue(msg.Text == ((ActiveMQTextMessage) copy).Text);
+        }
+    
+        [Test]
+        public void TestSetText() 
+        {
+            ActiveMQTextMessage msg = new ActiveMQTextMessage();
+            string str = "testText";
+            msg.Text = str;
+            Assert.AreEqual(msg.Text, str);
+        }
+    
+        [Test]
+        public void TestGetBytes() 
+        {
+            ActiveMQTextMessage msg = new ActiveMQTextMessage();
+            String str = "testText";
+            msg.Text = str;
+            msg.BeforeMarshall(null);
+            
+            byte[] bytes = msg.Content;
+            msg = new ActiveMQTextMessage();
+            msg.Content = bytes;
+            
+            Assert.AreEqual(msg.Text, str);
+        }
+    
+        [Test]
+        public void TestClearBody()
+        {
+            ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
+            textMessage.Text = "string";
+            textMessage.ClearBody();
+            Assert.IsFalse(textMessage.ReadOnlyBody);
+            Assert.IsNull(textMessage.Text);
+            try
+            {
+                textMessage.Text = "String";
+                Assert.IsTrue(textMessage.Text.Length > 0);
+            }
+            catch(MessageNotWriteableException)
+            {
+                Assert.Fail("should be writeable");
+            }
+            catch(MessageNotReadableException)
+            {
+                Assert.Fail("should be readable");
+            }
+        }
+    
+        [Test]
+        public void TestReadOnlyBody() 
+        {
+            ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
+            textMessage.Text = "test";
+            textMessage.ReadOnlyBody = true;
+            try 
+            {
+                Assert.IsTrue(textMessage.Text.Length > 0);
+            } 
+            catch(MessageNotReadableException) 
+            {
+                Assert.Fail("should be readable");
+            }
+            try 
+            {
+                textMessage.Text = "test";
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+        }
+    
+        [Test]
+        public void TtestWriteOnlyBody() 
+        { 
+            // should always be readable
+            ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
+            textMessage.ReadOnlyBody = false;
+            try 
+            {
+                textMessage.Text = "test";
+                Assert.IsTrue(textMessage.Text.Length > 0);
+            } 
+            catch(MessageNotReadableException) 
+            {
+                Assert.Fail("should be readable");
+            }
+            textMessage.ReadOnlyBody = true;
+            try 
+            {
+                Assert.IsTrue(textMessage.Text.Length > 0);
+                textMessage.Text = "test";
+                Assert.Fail("should throw exception");
+            } 
+            catch(MessageNotReadableException)
+            {
+                Assert.Fail("should be readable");
+            } 
+            catch(MessageNotWriteableException) 
+            {
+            }
+        }
+        
+        [Test]
+        public void TestShortText() 
+        {
+            string shortText = "Content";
+            ActiveMQTextMessage shortMessage = new ActiveMQTextMessage();
+            SetContent(shortMessage, shortText);
+            Assert.IsTrue(shortMessage.ToString().Contains("Text = " + shortText));
+            Assert.IsTrue(shortMessage.Text == shortText);
+            
+            string longText = "Very very very very veeeeeeery loooooooooooooooooooooooooooooooooong
text";
+            string longExpectedText = "Very very very very veeeeeeery looooooooooooo...ooooong
text";
+            ActiveMQTextMessage longMessage = new ActiveMQTextMessage();
+            SetContent(longMessage, longText);
+            Assert.IsTrue(longMessage.ToString().Contains("Text = " + longExpectedText));
+            Assert.IsTrue(longMessage.Text == longText);         
+        }
+        
+        [Test]
+        public void TestNullText() 
+        {
+            ActiveMQTextMessage nullMessage = new ActiveMQTextMessage();
+            SetContent(nullMessage, null);
+            Assert.IsNull(nullMessage.Text);
+            Assert.IsTrue(nullMessage.ToString().Contains("Text = null"));
+        }
+        
+        protected void SetContent(Message message, String text)
+        {
+            MemoryStream mstream = new MemoryStream();
+            EndianBinaryWriter dataOut = new EndianBinaryWriter(mstream);
+            dataOut.WriteString32(text);
+            dataOut.Close();
+            message.Content = mstream.ToArray();
+        }
     }
 }



Mime
View raw message