activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r767309 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk: ./ src/main/csharp/Commands/ src/main/csharp/OpenWire/ src/test/csharp/Commands/ src/test/csharp/OpenWire/
Date Tue, 21 Apr 2009 21:49:40 GMT
Author: tabish
Date: Tue Apr 21 21:49:39 2009
New Revision: 767309

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

Change the String marshaling code to write strings in Modified UTF-8 so that the strings sent
by this client are compatible with Java and C++ clients.  The Strings are encoded with a short
or an int depending on size of the string and where it is being marshaled.  The strings keys
or the primitive map are always written with the short size prefix, strings in the values
of the map are written with an int or a short depending on length of the string.  Strings
in the body of the TextMessage are written using the int size prefix always.

Added:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryReaderTest.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryWriterTest.cs
Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQTextMessage.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryReader.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryWriter.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/PrimitiveMap.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/vs2008-activemq-test.csproj

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=767309&r1=767308&r2=767309&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
Tue Apr 21 21:49:39 2009
@@ -15,20 +15,17 @@
  * limitations under the License.
  */
 using Apache.NMS;
+using Apache.NMS.ActiveMQ.OpenWire;
 using System;
-
+using System.IO;
 
 namespace Apache.NMS.ActiveMQ.Commands
 {
 	public class ActiveMQTextMessage : ActiveMQMessage, ITextMessage
     {
         public const byte ID_ActiveMQTextMessage = 28;
-
-		public const int SIZE_OF_INT = 4; // sizeof(int) - though causes unsafe issues with net
1.1
         
         private String text;
-
-        private static System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
         
         public ActiveMQTextMessage()
         {
@@ -53,7 +50,6 @@
             return ID_ActiveMQTextMessage;
         }
         
-        
         // Properties
         
         public string Text
@@ -65,7 +61,9 @@
                     byte[] data = this.Content;
                     if (data != null)
                     {
-                        text = encoder.GetString(data, SIZE_OF_INT, data.Length - SIZE_OF_INT);
+                        MemoryStream stream = new MemoryStream(data);
+                        OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+                        text = reader.ReadString32();
                     }
                 }
                 return text;
@@ -77,28 +75,15 @@
                 if (text != null)
                 {
 					// TODO lets make the evaluation of the Content lazy!
-					
-					// TODO assume that the text is ASCII
-                    byte[] utf8bytes = encoder.GetBytes( this.text );
-                    byte[] sizePrefix = System.BitConverter.GetBytes(utf8bytes.Length);
-                    data = new byte[utf8bytes.Length + sizePrefix.Length];  //int at the
front of it
-															
-					// add the size prefix
-					for (int j = 0; j < sizePrefix.Length; j++)
-                    {
-						// The bytes need to be encoded in big endian
-						if ( BitConverter.IsLittleEndian ) {
-							data[j] = sizePrefix[sizePrefix.Length - j - 1];
-						} else {
-							data[j] = sizePrefix[j];
-						}
-                    }
-					
-					// Add the data.
-                    for (int i = 0; i < utf8bytes.Length; i++)
-                    {
-                        data[i + sizePrefix.Length] = (byte)utf8bytes[i];
-                    }
+
+                    // 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 );
+                    OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+
+                    writer.WriteString32(text);
+
+                    data = stream.GetBuffer();
 				}
 				this.Content = data;
 					

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryReader.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryReader.cs?rev=767309&r1=767308&r2=767309&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryReader.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryReader.cs
Tue Apr 21 21:49:39 2009
@@ -219,6 +219,158 @@
             }
         }
 
+        /// <summary>
+        /// Method ReadString16, reads a String value encoded in the Java modified
+        /// UTF-8 format with a length index encoded as a 16bit unsigned short.
+        /// </summary>
+        /// <returns>A string</returns>
+        public String ReadString16()
+        {
+            int utfLength = ReadUInt16();
+
+            if (utfLength <= 0)
+            {
+                return "";
+            }
+
+            char[] result = new char[utfLength];
+            byte[] buffer = new byte[utfLength];
+
+            int bytesRead = 0;
+            while (bytesRead < utfLength)
+            {
+                int rc = Read(buffer, bytesRead, utfLength - bytesRead);
+                if (rc == 0)
+                    throw new IOException("premature end of stream");
+                bytesRead += rc;
+            }
+
+            int count = 0;
+            int index = 0;
+            byte a = 0;
+
+            while (count < utfLength)
+            {
+                if ((result[index] = (char)buffer[count++]) < 0x80)
+                {
+                    index++;
+                }
+                else if (((a = (byte)result[index]) & 0xE0) == 0xC0)
+                {
+                    if (count >= utfLength)
+                    {
+                        throw new IOException("Invalid UTF-8 encoding found, start of two
byte char found at end.");
+                    }
+
+                    byte b = buffer[count++];
+                    if ((b & 0xC0) != 0x80)
+                    {
+                        throw new IOException(
+                            "Invalid UTF-8 encoding found, byte two does not start with 0x80.");
+                    }
+
+                    result[index++] = (char)(((a & 0x1F) << 6) | (b & 0x3F));
+
+                }
+                else if ((a & 0xF0) == 0xE0)
+                {
+
+                    if (count + 1 >= utfLength)
+                    {
+                        throw new IOException(
+                            "Invalid UTF-8 encoding found, start of three byte char found
at end.");
+                    }
+
+                    byte b = buffer[count++];
+                    byte c = buffer[count++];
+                    if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80))
+                    {
+                        throw new IOException(
+                            "Invalid UTF-8 encoding found, byte two does not start with 0x80.");
+                    }
+
+                    result[index++] = (char)(((a & 0x0F) << 12) |
+                                              ((b & 0x3F) << 6) | (c & 0x3F));
+
+                }
+                else
+                {
+                    throw new IOException("Invalid UTF-8 encoding found, aborting.");
+                }
+            }
+
+            return new String(result, 0, index);    
+        }
+
+        /// <summary>
+        /// Method ReadString32, reads a String value encoded in the Java modified 
+        /// UTF-8 format with a length index encoded as a singed integer value.
+        /// </summary>
+        /// <returns>A string</returns>
+        public String ReadString32()
+        {
+            int utfLength = ReadInt32();
+
+            if( utfLength <= 0 ) {
+                return "";
+            }
+
+            char[] result = new char[utfLength];
+            byte[] buffer = new byte[utfLength];
+
+            int bytesRead = 0;
+            while (bytesRead < utfLength)
+            {
+                int rc = Read(buffer, bytesRead, utfLength - bytesRead);
+                if (rc == 0)
+                    throw new IOException("premature end of stream");
+                bytesRead += rc;
+            }
+
+            int count = 0;
+            int index = 0;
+            byte a = 0;
+
+            while( count < utfLength ) {
+                if( ( result[index] = (char)buffer[count++] ) < 0x80 ) {
+                    index++;
+                } else if( ( ( a = (byte)result[index] ) & 0xE0 ) == 0xC0 ) {
+                    if( count >= utfLength ) {
+                        throw new IOException("Invalid UTF-8 encoding found, start of two
byte char found at end.");
+                    }
+
+                    byte b = buffer[count++];
+                    if( ( b & 0xC0 ) != 0x80 ) {
+                        throw new IOException(
+                            "Invalid UTF-8 encoding found, byte two does not start with 0x80."
);
+                    }
+
+                    result[index++] = (char)( ( ( a & 0x1F ) << 6 ) | ( b &
0x3F ) );
+
+                } else if( ( a & 0xF0 ) == 0xE0 ) {
+
+                    if( count + 1 >= utfLength ) {
+                        throw new IOException(
+                            "Invalid UTF-8 encoding found, start of three byte char found
at end.");
+                    } 
+
+                    byte b = buffer[count++];
+                    byte c = buffer[count++];
+                    if( ( ( b & 0xC0 ) != 0x80 ) || ( ( c & 0xC0 ) != 0x80 ) ) {
+                        throw new IOException(
+                            "Invalid UTF-8 encoding found, byte two does not start with 0x80."
);
+                    }
+                    
+                    result[index++] = (char)( ( ( a & 0x0F ) << 12 ) |
+                                              ( ( b & 0x3F ) << 6 ) | ( c &
0x3F ) );
+
+                } else {
+                    throw new IOException( "Invalid UTF-8 encoding found, aborting." );
+                }
+            }
+
+            return new String( result, 0, index );        
+        }
 		
         public override float ReadSingle()
         {

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryWriter.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryWriter.cs?rev=767309&r1=767308&r2=767309&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryWriter.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/OpenWireBinaryWriter.cs
Tue Apr 21 21:49:39 2009
@@ -125,82 +125,104 @@
 			base.Write(EndianSupport.SwitchEndian(value));
 		}
 
-		public override void Write(String text)
+		/// <summary>
+		/// Method Write, writes a string to the output using the WriteString16
+        /// method.
+		/// </summary>
+		/// <param name="text">A  string</param>
+        public override void Write(String text)
 		{
-			foreach(string textPackage in new StringPackageSplitter(text))
-			{
-				WriteString(textPackage);
-			}
+            WriteString16(text);
 		}
 
 		/// <summary>
-		/// Method Write
-		/// </summary>
+        /// Method WriteString16, writes a string to the output using the Java 
+        /// standard modified UTF-8 encoding with an unsigned short value written first to

+        /// indicate the length of the encoded data, the short is read as an unsigned 
+        /// value so the max amount of data this method can write is 65535 encoded bytes.
+        /// 
+        /// Unlike the WriteString32 method this method does not encode the length
+        /// value to -1 if the string is null, this is to match the behaviour of 
+        /// the Java DataOuputStream class's writeUTF method.	
+        /// 
+        /// Because modified UTF-8 encding can result in a number of bytes greater that 
+        /// the size of the String this method must first check that the encoding proces

+        /// will not result in a value that cannot be written becuase it is greater than
+        /// the max value of an unsigned short.
+        /// </summary>
 		/// <param name="text">A  string</param>
-		private void WriteString(String text)
+		public void WriteString16(String text)
 		{
-			if(text != null)
-			{
-				if(text.Length > OpenWireBinaryWriter.MAXSTRINGLEN)
-				{
-					throw new IOException(String.Format("Cannot marshall string longer than: {0} characters,
supplied string was: {1} characters", OpenWireBinaryWriter.MAXSTRINGLEN, text.Length));
-				}
-
-				int strlen = text.Length;
-				short utflen = 0;
-				int c = 0;
-				int count = 0;
+            if (text != null)
+            {
+                if (text.Length > ushort.MaxValue)
+                {
+                    throw new IOException(
+                        String.Format(
+                            "Cannot marshall string longer than: {0} characters, supplied
string was: " +
+                            "{1} characters", ushort.MaxValue, text.Length));
+                }
+
+                char[] charr = text.ToCharArray();
+                uint utfLength = CountUtf8Bytes(charr);
+
+                if (utfLength > ushort.MaxValue)
+                {
+                    throw new IOException(
+                        String.Format(
+                            "Cannot marshall an encoded string longer than: {0} bytes, supplied"
+
+                            "string requires: {1} characters to encode", ushort.MaxValue,
utfLength));
+                }
+
+                byte[] bytearr = new byte[utfLength];
+                encodeUTF8toBuffer(charr, bytearr);
+
+                Write( (ushort)utfLength);
+                Write(bytearr);
+            }
+		}
+
+        /// <summary>
+        /// Method WriteString32, writes a string to the output using the Openwire 
+        /// standard modified UTF-8 encoding which an int value written first to 
+        /// indicate the length of the encoded data, the int is read as an signed 
+        /// value so the max amount of data this method can write is 2^31 encoded bytes.
+        /// 
+        /// In the case of a null value being passed this method writes a -1 to the 
+        /// stream to indicate that the string is null.
+        /// 
+        /// Because modified UTF-8 encding can result in a number of bytes greater that 
+        /// the size of the String this method must first check that the encoding proces

+        /// will not result in a value that cannot be written becuase it is greater than
+        /// the max value of an int.
+        /// </summary>
+        /// <param name="text">A  string</param>
+        public void WriteString32(String text)
+        {
+            if( text != null ) {
 
 				char[] charr = text.ToCharArray();
+                uint utfLength = CountUtf8Bytes(charr);
 
-				for(int i = 0; i < strlen; i++)
-				{
-					c = charr[i];
-					if((c >= 0x0001) && (c <= 0x007F))
-					{
-						utflen++;
-					}
-					else if(c > 0x07FF)
-					{
-						utflen += 3;
-					}
-					else
-					{
-						utflen += 2;
-					}
-				}
-
-				Write((short) utflen);
-
-				byte[] bytearr = new byte[utflen];
-				for(int i = 0; i < strlen; i++)
-				{
-					c = charr[i];
-					if((c >= 0x0001) && (c <= 0x007F))
-					{
-						bytearr[count++] = (byte) c;
-					}
-					else if(c > 0x07FF)
-					{
-						bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
-						bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
-						bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
-					}
-					else
-					{
-						bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
-						bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
-					}
-				}
-
-				Write(bytearr);
-
-			}
-			else
-			{
-				Write((short) -1);
-			}
-		}
+                if( utfLength > int.MaxValue ) 
+                {
+					throw new IOException(
+                        String.Format(
+                            "Cannot marshall an encoded string longer than: {0} bytes, supplied"
+ 
+                            "string requires: {1} characters to encode", int.MaxValue, utfLength
) );
+                }
+
+                byte[] bytearr = new byte[utfLength];
+                encodeUTF8toBuffer(charr, bytearr);
+
+                Write(utfLength);
+                Write(bytearr);
+            } 
+            else
+            {
+                Write((int)-1);
+            }
+        }
 
 		/// <summary>
 		/// Method Write
@@ -219,130 +241,57 @@
 		{
 			base.Write(EndianSupport.SwitchEndian(value));
 		}
-	}
-
-	#region StringPackageSplitter
-
-	/// <summary>
-	/// StringPackageSplitter
-	/// </summary>
-	class StringPackageSplitter : IEnumerable
-	{
-		public StringPackageSplitter(string value)
-		{
-			this.value = value;
-		}
 
-		/// <summary>
-		/// Emumerator class for StringPackageSplitter
-		/// </summary>
-		class StringPackageSplitterEnumerator : IEnumerator
-		{
-			/// <summary>
-			/// </summary>
-			/// <param name="parent"></param>
-			public StringPackageSplitterEnumerator(StringPackageSplitter parent)
-			{
-				this.parent = parent;
-			}
-
-			private int Position = -1;
-			private StringPackageSplitter parent;
-
-			#region IEnumerator Members
-
-			public string Current
-			{
-				get
-				{
-					int delta = parent.value.Length - Position;
-
-					if(delta >= OpenWireBinaryWriter.MAXSTRINGLEN)
-					{
-						return parent.value.Substring(Position, OpenWireBinaryWriter.MAXSTRINGLEN);
-					}
-					else
-					{
-						return parent.value.Substring(Position, delta);
-					}
-				}
-			}
-
-			#endregion
-
-			#region IDisposable Members
-
-			public void Dispose()
-			{
-			}
-
-			#endregion
-
-			#region IEnumerator Members
-
-			object IEnumerator.Current
-			{
-				get
-                {
-                    int delta;
-
-                    delta = parent.value.Length - Position;
-
-                    if (delta >= OpenWireBinaryWriter.MAXSTRINGLEN)
-                    {
-                        return parent.value.Substring(Position, OpenWireBinaryWriter.MAXSTRINGLEN);
-                    }
-                    else
-                    {
-                        return parent.value.Substring(Position, delta);
-                    }
-                }
-			}
-
-			public bool MoveNext()
-			{
-				if(parent.value == null)
-				{
-					return false;
-				}
-
-				if(Position == -1)
-				{
-					Position = 0;
-					return true;
-				}
-
-				if((Position + OpenWireBinaryWriter.MAXSTRINGLEN) < parent.value.Length)
-				{
-					Position += OpenWireBinaryWriter.MAXSTRINGLEN;
-					return true;
-				}
-				else
-				{
-					return false;
-				}
-			}
-
-			public void Reset()
-			{
-				Position = -1;
-			}
-
-			#endregion
-		}
-
-		private String value;
-
-		#region IEnumerable Members
+        private uint CountUtf8Bytes(char[] chars)
+        {
+            uint utfLength = 0;
+            int c = 0;
+
+            for (int i = 0; i < chars.Length; i++)
+            {
+                c = chars[i];
+                if ((c >= 0x0001) && (c <= 0x007F))
+                {
+                    utfLength++;
+                }
+                else if (c > 0x07FF)
+                {
+                    utfLength += 3;
+                }
+                else
+                {
+                    utfLength += 2;
+                }
+            }
 
-		IEnumerator IEnumerable.GetEnumerator()
-		{
-			return new StringPackageSplitterEnumerator(this);
-		}
+            return utfLength;
+        }
 
-		#endregion
+        private void encodeUTF8toBuffer(char[] chars, byte[] buffer )
+        {
+            int c = 0;
+            int count = 0;
+
+            for (int i = 0; i < chars.Length; i++)
+            {
+                c = chars[i];
+                if ((c >= 0x0001) && (c <= 0x007F))
+                {
+                    buffer[count++] = (byte)c;
+                }
+                else if (c > 0x07FF)
+                {
+                    buffer[count++] = (byte)(0xE0 | ((c >> 12) & 0x0F));
+                    buffer[count++] = (byte)(0x80 | ((c >> 6) & 0x3F));
+                    buffer[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
+                }
+                else
+                {
+                    buffer[count++] = (byte)(0xC0 | ((c >> 6) & 0x1F));
+                    buffer[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
+                }
+            }
+        }
 	}
-
-	#endregion // END StringPackageSplitter
 }
 

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/PrimitiveMap.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/PrimitiveMap.cs?rev=767309&r1=767308&r2=767309&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/PrimitiveMap.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/OpenWire/PrimitiveMap.cs
Tue Apr 21 21:49:39 2009
@@ -455,12 +455,12 @@
 				if (s.Length > 8191)
 				{
 					dataOut.Write(BIG_STRING_TYPE);
-					dataOut.Write(s);
+					((OpenWireBinaryWriter)dataOut).WriteString32(s);
 				}
 				else
 				{
 					dataOut.Write(STRING_TYPE);
-					dataOut.Write(s);
+					((OpenWireBinaryWriter)dataOut).WriteString16(s);
 				}
 			}
 			else if (value is IDictionary)
@@ -519,10 +519,10 @@
 					value = data;
 					break;
 				case STRING_TYPE:
-					value = dataIn.ReadString();
+					value = ((OpenWireBinaryReader)dataIn).ReadString16();
 					break;
 				case BIG_STRING_TYPE:
-					value = dataIn.ReadString();
+					value = ((OpenWireBinaryReader)dataIn).ReadString32();
 					break;
 				case MAP_TYPE:
 					value = UnmarshalPrimitiveMap(dataIn);

Added: 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=767309&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQTextMessageTest.cs
Tue Apr 21 21:49:39 2009
@@ -0,0 +1,50 @@
+/*
+ * 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 Apache.NMS.ActiveMQ.Commands;
+using NUnit.Framework;
+using System;
+using System.Text;
+
+namespace Apache.NMS.ActiveMQ.Test.Commands
+{
+    [TestFixture]
+    public class ActiveMQTextMessageTest
+    {
+        [Test]
+        public void TestCommand()
+        {
+            ActiveMQTextMessage message = new ActiveMQTextMessage();
+
+            Assert.IsNull(message.Text);            
+
+            // Test with ASCII Data.
+            message.Text = "Hello World";
+            Assert.IsNotNull(message.Text);
+            Assert.AreEqual("Hello World", message.Text);
+
+            String unicodeString =
+                "This unicode string contains two characters " +
+                "with codes outside an 8-bit code range, " +
+                "Pi (\u03a0) and Sigma (\u03a3).";
+
+            message.Text = unicodeString;
+            Assert.IsNotNull(message.Text);
+            Assert.AreEqual(unicodeString, message.Text);
+        }
+    }
+}

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryReaderTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryReaderTest.cs?rev=767309&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryReaderTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryReaderTest.cs
Tue Apr 21 21:49:39 2009
@@ -0,0 +1,180 @@
+/*
+ * 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.Generic;
+using System.Text;
+using System.IO;
+using Apache.NMS.ActiveMQ;
+using Apache.NMS.ActiveMQ.OpenWire;
+using NUnit.Framework;
+
+namespace Apache.NMS.ActiveMQ.Test.OpenWire
+{
+    [TestFixture]
+    public class OpenWireBinaryReaderTest
+    {
+        public void readString16Helper( byte[] input, char[] expect )
+        {
+            MemoryStream stream = new MemoryStream(input);
+            OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+
+            char[] result = reader.ReadString16().ToCharArray();
+
+            for (int i = 0; i < expect.Length; ++i)
+            {
+                Assert.AreEqual(expect[i], result[i]);
+            }
+        }
+
+        [Test]
+        public void testReadString16()
+        {
+            // Test data with 1-byte UTF8 encoding.
+            {
+                char[] expect = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C',
'\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064' };
+                byte[] input = { 0x00, 0x0E, 0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F,
0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };
+
+                readString16Helper(input, expect);
+            }
+
+            // Test data with 2-byte UT8 encoding.
+            {
+                char[] expect = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' };
+                byte[] input = { 0x00, 0x0A, 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83,
0xC2, 0xA6 };
+                readString16Helper(input, expect);
+            }
+
+            // Test data with 1-byte and 2-byte encoding with embedded NULL's.
+            {
+                char[] expect = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000',
'\u00A6' };
+                byte[] input = { 0x00, 0x0D, 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xA9, 0xC3,
0x83, 0xC0, 0x80, 0xC2, 0xA6 };
+
+                readString16Helper(input, expect);
+            }
+
+            // Test with bad UTF-8 encoding, missing 2nd byte of two byte value
+            {
+                byte[] input = { 0x00, 0x0D, 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xC2, 0xC3,
0x83, 0xC0, 0x80, 0xC2, 0xA6 };
+
+                MemoryStream stream = new MemoryStream(input);
+                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+
+                try
+                {
+                    reader.ReadString16();
+                    Assert.Fail("Should Throw an IOException");
+                }
+                catch (NMSException)
+                {
+                }
+            }
+
+            // Test with three byte encode that's missing a last byte.
+            {
+                byte[] input = { 0x00, 0x02, 0xE8, 0xA8 };
+
+                MemoryStream stream = new MemoryStream(input);
+                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+
+                try
+                {
+                    reader.ReadString16();
+                    Assert.Fail("Should Throw an IOException");
+                }
+                catch (NMSException)
+                {
+                }
+            }
+        }
+
+        public void readString32Helper(byte[] input, char[] expect)
+        {
+            MemoryStream stream = new MemoryStream(input);
+            OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+
+            char[] result = reader.ReadString32().ToCharArray();
+
+            for (int i = 0; i < expect.Length; ++i)
+            {
+                Assert.AreEqual(expect[i], result[i]);
+            }
+        }
+
+        [Test]
+        public void testReadString32()
+        {
+            // Test data with 1-byte UTF8 encoding.
+            {
+                char[] expect = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C',
'\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064' };
+                byte[] input = { 0x00, 0x00, 0x00, 0x0E, 0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C,
0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };
+
+                readString32Helper(input, expect);
+            }
+
+            // Test data with 2-byte UT8 encoding.
+            {
+                char[] expect = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' };
+                byte[] input = { 0x00, 0x00, 0x00, 0x0A, 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9,
0xC3, 0x83, 0xC2, 0xA6 };
+                readString32Helper(input, expect);
+            }
+
+            // Test data with 1-byte and 2-byte encoding with embedded NULL's.
+            {
+                char[] expect = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000',
'\u00A6' };
+                byte[] input = { 0x00, 0x00, 0x00, 0x0D, 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2,
0xA9, 0xC3, 0x83, 0xC0, 0x80, 0xC2, 0xA6 };
+
+                readString32Helper(input, expect);
+            }
+
+            // Test with bad UTF-8 encoding, missing 2nd byte of two byte value
+            {
+                byte[] input = { 0x00, 0x00, 0x00, 0x0D, 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2,
0xC2, 0xC3, 0x83, 0xC0, 0x80, 0xC2, 0xA6 };
+
+                MemoryStream stream = new MemoryStream(input);
+                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+
+                try
+                {
+                    reader.ReadString32();
+                    Assert.Fail("Should Throw an IOException");
+                }
+                catch (NMSException)
+                {
+                }
+            }
+
+            // Test with three byte encode that's missing a last byte.
+            {
+                byte[] input = { 0x00, 0x00, 0x00, 0x02, 0xE8, 0xA8 };
+
+                MemoryStream stream = new MemoryStream(input);
+                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+
+                try
+                {
+                    reader.ReadString32();
+                    Assert.Fail("Should Throw an IOException");
+                }
+                catch (NMSException)
+                {
+                }
+            }
+        }
+
+    }
+}

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryWriterTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryWriterTest.cs?rev=767309&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryWriterTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/OpenWire/OpenWireBinaryWriterTest.cs
Tue Apr 21 21:49:39 2009
@@ -0,0 +1,206 @@
+/*
+ * 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.Generic;
+using System.Text;
+using System.IO;
+using Apache.NMS.ActiveMQ.OpenWire;
+using NUnit.Framework;
+
+namespace Apache.NMS.ActiveMQ.Test.OpenWire
+{
+    [TestFixture]
+    public class OpenWireBinaryWriterTest
+    {
+        void writeString16TestHelper(char[] input, byte[] expect)
+        {
+            MemoryStream stream = new MemoryStream();
+            OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+
+            String str = new String(input);
+
+            writer.WriteString16(str);
+
+            byte[] result = stream.GetBuffer();
+
+            Assert.AreEqual(result[0], 0x00);
+            Assert.AreEqual(result[1], expect.Length);
+
+            for (int i = 4; i < expect.Length; ++i)
+            {
+                Assert.AreEqual(result[i], expect[i - 2]);
+            }
+        }
+        [Test]
+        public void testWriteString16()
+        {
+            // Test data with 1-byte UTF8 encoding.
+            {
+                char[] input = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C',
'\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064' };
+                byte[] expect = { 0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57,
0x6F, 0x72, 0x6C, 0x64 };
+
+                writeString16TestHelper(input, expect);
+            }
+
+            // Test data with 2-byte UT8 encoding.
+            {
+                char[] input = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' };
+                byte[] expect = { 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC2, 0xA6
};
+
+                writeString16TestHelper(input, expect);
+            }
+
+            // Test data with 1-byte and 2-byte encoding with embedded NULL's.
+            {
+                char[] input = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000',
'\u00A6' };
+                byte[] expect = { 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC0,
0x80, 0xC2, 0xA6 };
+
+                writeString16TestHelper(input, expect);
+            }
+
+            // test that a null string writes no output.
+            {
+                MemoryStream stream = new MemoryStream();
+                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+                writer.WriteString16(null);
+                Assert.AreEqual(0, stream.Length);
+            }
+
+            // test that a null string writes no output.
+            {
+                MemoryStream stream = new MemoryStream();
+                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+                writer.WriteString16("");
+
+                stream.Seek(0, SeekOrigin.Begin);
+                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+                Assert.AreEqual(0, reader.ReadInt16());
+            }
+
+            // String of length 65536 of Null Characters.
+            {
+                MemoryStream stream = new MemoryStream();
+                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+                String testStr = new String( 'a', 65536 );
+                try{
+                    writer.Write(testStr);
+                    Assert.Fail( "Should throw an Exception" );
+                } 
+                catch( Exception ) 
+                {
+                }
+            }
+
+            // String of length 65535 of non Null Characters since Null encodes as UTF-8.
+            {
+                MemoryStream stream = new MemoryStream();
+                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+                String testStr = new String( 'a', 65535 );
+                try{
+                    writer.Write(testStr);
+                } 
+                catch( Exception ) 
+                {
+                    Assert.Fail( "Should not throw an Exception" );
+                }
+            }
+
+            // Set one of the 65535 bytes to a value that will result in a 2 byte UTF8 encoded
sequence.
+            // This will cause the string of length 65535 to have a utf length of 65536.
+            {
+                MemoryStream stream = new MemoryStream();
+                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+                String testStr = new String( 'a', 65535 );
+                char[] array = testStr.ToCharArray();
+                array[0] = '\u0000';
+                testStr = new String(array);
+                
+                try{
+                    writer.Write(testStr);
+                    Assert.Fail( "Should throw an Exception" );
+                } 
+                catch( Exception ) 
+                {
+                }
+            }
+           
+        }
+
+        void writeString32TestHelper(char[] input, byte[] expect) {
+
+            MemoryStream stream = new MemoryStream();
+            OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+
+            String str = new String(input);
+            
+            writer.WriteString32( str );
+
+            byte[] result = stream.GetBuffer();
+
+            Assert.AreEqual( result[0], 0x00 );
+            Assert.AreEqual( result[1], 0x00 );
+            Assert.AreEqual( result[2], 0x00 );
+            Assert.AreEqual( result[3], expect.Length );
+
+            for (int i = 4; i < expect.Length; ++i)
+            {
+                Assert.AreEqual( result[i], expect[i-4] );
+            }
+        }
+
+        [Test]
+        public void testWriteString32()
+        {
+            // Test data with 1-byte UTF8 encoding.
+            {
+                char[] input = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C',
'\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064'};
+                byte[] expect = {0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57,
0x6F, 0x72, 0x6C, 0x64};
+
+                writeString32TestHelper(input, expect);
+            }
+
+            // Test data with 2-byte UT8 encoding.
+            {
+                char[] input = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' };
+                byte[] expect = { 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC2, 0xA6
};
+
+                writeString32TestHelper(input, expect);
+            }
+
+            // Test data with 1-byte and 2-byte encoding with embedded NULL's.
+            {
+                char[] input = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000',
'\u00A6' };
+                byte[] expect = { 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC0,
0x80, 0xC2, 0xA6 };
+
+                writeString32TestHelper(input, expect);
+            }
+
+            // test that a null strings writes a -1
+            {
+                MemoryStream stream = new MemoryStream();
+                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
+                writer.WriteString32(null);
+
+                stream.Seek(0, SeekOrigin.Begin);
+                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
+                Assert.AreEqual(-1, reader.ReadInt32());
+            }
+        }
+
+    }
+}

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/vs2008-activemq-test.csproj
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/vs2008-activemq-test.csproj?rev=767309&r1=767308&r2=767309&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/vs2008-activemq-test.csproj (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/vs2008-activemq-test.csproj Tue Apr
21 21:49:39 2009
@@ -2,7 +2,7 @@
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProductVersion>9.0.30729</ProductVersion>
+    <ProductVersion>9.0.21022</ProductVersion>
     <SchemaVersion>2.0</SchemaVersion>
     <ProjectGuid>{EB943C69-2C9B-45E7-B95B-FB916E7057ED}</ProjectGuid>
     <OutputType>Library</OutputType>
@@ -62,6 +62,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="src\test\csharp\Commands\ActiveMQTextMessageTest.cs" />
     <Compile Include="src\test\csharp\Commands\CommandTest.cs">
       <SubType>Code</SubType>
     </Compile>
@@ -74,6 +75,8 @@
       <SubType>Code</SubType>
     </Compile>
     <Compile Include="src\test\csharp\OpenWire\MaxInactivityDurationTest.cs" />
+    <Compile Include="src\test\csharp\OpenWire\OpenWireBinaryReaderTest.cs" />
+    <Compile Include="src\test\csharp\OpenWire\OpenWireBinaryWriterTest.cs" />
     <Compile Include="src\test\csharp\OpenWire\PrefetchSizeZeroTest.cs" />
     <Compile Include="src\test\csharp\OpenWire\PrimitiveMapTest.cs">
       <SubType>Code</SubType>



Mime
View raw message