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/06 16:48:04 UTC
svn commit: r822299 - in
/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src:
main/csharp/Commands/ActiveMQStreamMessage.cs
test/csharp/Commands/ActiveMQStreamMessageTest.cs
Author: tabish
Date: Tue Oct 6 14:48:03 2009
New Revision: 822299
URL: http://svn.apache.org/viewvc?rev=822299&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQNET-116
Adds the NMS.ActiveMQ implementation of StreamMessage and associated unit tests.
Added:
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQStreamMessageTest.cs (with props)
Modified:
activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQStreamMessage.cs
Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQStreamMessage.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQStreamMessage.cs?rev=822299&r1=822298&r2=822299&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQStreamMessage.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Commands/ActiveMQStreamMessage.cs Tue Oct 6 14:48:03 2009
@@ -14,31 +14,904 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-using Apache.NMS.ActiveMQ.Commands;
+using System;
+using System.IO;
+using System.Collections;
+using Apache.NMS;
+using Apache.NMS.Util;
+using Apache.NMS.ActiveMQ.Commands;
namespace Apache.NMS.ActiveMQ.Commands
{
- public class ActiveMQStreamMessage : ActiveMQMessage
+ public class ActiveMQStreamMessage : ActiveMQMessage, IStreamMessage
{
+ private EndianBinaryReader dataIn = null;
+ private EndianBinaryWriter dataOut = null;
+ private MemoryStream byteBuffer = null;
+ private int bytesRemaining = -1;
+
public const byte ID_ACTIVEMQSTREAMMESSAGE = 27;
+ public override byte GetDataStructureType()
+ {
+ return ID_ACTIVEMQSTREAMMESSAGE;
+ }
+
+ public bool ReadBoolean()
+ {
+ InitializeReading();
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.BOOLEAN_TYPE)
+ {
+ return this.dataIn.ReadBoolean();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return Boolean.Parse(this.dataIn.ReadString16());
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a bool");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Boolean type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public byte ReadByte()
+ {
+ InitializeReading();
+
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.BYTE_TYPE)
+ {
+ return this.dataIn.ReadByte();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return Byte.Parse(this.dataIn.ReadString16());
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a byte");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Byte type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public char ReadChar()
+ {
+ InitializeReading();
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.CHAR_TYPE)
+ {
+ return this.dataIn.ReadChar();
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a char");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Char type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public short ReadInt16()
+ {
+ InitializeReading();
- // TODO generate Equals method
- // TODO generate GetHashCode method
- // TODO generate ToString method
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.SHORT_TYPE)
+ {
+ return this.dataIn.ReadInt16();
+ }
+ else if(type == PrimitiveMap.BYTE_TYPE)
+ {
+ return this.dataIn.ReadByte();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return Int16.Parse(this.dataIn.ReadString16());
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a short");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Int16 type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public int ReadInt32()
+ {
+ InitializeReading();
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.INTEGER_TYPE)
+ {
+ return this.dataIn.ReadInt32();
+ }
+ else if(type == PrimitiveMap.SHORT_TYPE)
+ {
+ return this.dataIn.ReadInt16();
+ }
+ else if(type == PrimitiveMap.BYTE_TYPE)
+ {
+ return this.dataIn.ReadByte();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return Int32.Parse(this.dataIn.ReadString16());
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a int");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Int32 type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
- public override byte GetDataStructureType()
+ public long ReadInt64()
{
- return ID_ACTIVEMQSTREAMMESSAGE;
+ InitializeReading();
+
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.LONG_TYPE)
+ {
+ return this.dataIn.ReadInt64();
+ }
+ else if(type == PrimitiveMap.INTEGER_TYPE)
+ {
+ return this.dataIn.ReadInt32();
+ }
+ else if(type == PrimitiveMap.SHORT_TYPE)
+ {
+ return this.dataIn.ReadInt16();
+ }
+ else if(type == PrimitiveMap.BYTE_TYPE)
+ {
+ return this.dataIn.ReadByte();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return Int64.Parse(this.dataIn.ReadString16());
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a long");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Int64 type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
}
+
+ public float ReadSingle()
+ {
+ InitializeReading();
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.FLOAT_TYPE)
+ {
+ return this.dataIn.ReadSingle();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return Single.Parse(this.dataIn.ReadString16());
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a float");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Single type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public double ReadDouble()
+ {
+ InitializeReading();
+
+ try
+ {
+ long startingPos = this.byteBuffer.Position;
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.DOUBLE_TYPE)
+ {
+ return this.dataIn.ReadDouble();
+ }
+ else if(type == PrimitiveMap.FLOAT_TYPE)
+ {
+ return this.dataIn.ReadSingle();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return Single.Parse(this.dataIn.ReadString16());
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new NMSException("Cannot convert Null type to a double");
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a Double type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public string ReadString()
+ {
+ InitializeReading();
+
+ long startingPos = this.byteBuffer.Position;
- // Properties
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.BIG_STRING_TYPE)
+ {
+ return this.dataIn.ReadString32();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return this.dataIn.ReadString16();
+ }
+ else if(type == PrimitiveMap.LONG_TYPE)
+ {
+ return this.dataIn.ReadInt64().ToString();
+ }
+ else if(type == PrimitiveMap.INTEGER_TYPE)
+ {
+ return this.dataIn.ReadInt32().ToString();
+ }
+ else if(type == PrimitiveMap.SHORT_TYPE)
+ {
+ return this.dataIn.ReadInt16().ToString();
+ }
+ else if(type == PrimitiveMap.FLOAT_TYPE)
+ {
+ return this.dataIn.ReadSingle().ToString();
+ }
+ else if(type == PrimitiveMap.DOUBLE_TYPE)
+ {
+ return this.dataIn.ReadDouble().ToString();
+ }
+ else if(type == PrimitiveMap.CHAR_TYPE)
+ {
+ return this.dataIn.ReadChar().ToString();
+ }
+ else if(type == PrimitiveMap.BYTE_TYPE)
+ {
+ return this.dataIn.ReadByte().ToString();
+ }
+ else if(type == PrimitiveMap.BOOLEAN_TYPE)
+ {
+ return this.dataIn.ReadBoolean().ToString();
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ return null;
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a known type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public int ReadBytes(byte[] value)
+ {
+ InitializeReading();
+
+ if(value == null)
+ {
+ throw new NullReferenceException("Passed Byte Array is null");
+ }
+
+ try
+ {
+ if(this.bytesRemaining == -1)
+ {
+ long startingPos = this.byteBuffer.Position;
+ byte type = this.dataIn.ReadByte();
+
+ if(type != PrimitiveMap.BYTE_ARRAY_TYPE)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Not a byte array");
+ }
+
+ this.bytesRemaining = this.dataIn.ReadInt32();
+ }
+ else if(this.bytesRemaining == 0)
+ {
+ this.bytesRemaining = -1;
+ return -1;
+ }
+
+ if(value.Length <= this.bytesRemaining)
+ {
+ // small buffer
+ this.bytesRemaining -= value.Length;
+ this.dataIn.Read(value, 0, value.Length);
+ return value.Length;
+ }
+ else
+ {
+ // big buffer
+ int rc = this.dataIn.Read(value, 0, this.bytesRemaining);
+ this.bytesRemaining = 0;
+ return rc;
+ }
+ }
+ catch(EndOfStreamException ex)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(ex);
+ }
+ catch(IOException ex)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(ex);
+ }
+ }
+
+ public Object ReadObject()
+ {
+ InitializeReading();
+ long startingPos = this.byteBuffer.Position;
+
+ try
+ {
+ int type = this.dataIn.ReadByte();
+
+ if(type == PrimitiveMap.BIG_STRING_TYPE)
+ {
+ return this.dataIn.ReadString32();
+ }
+ else if(type == PrimitiveMap.STRING_TYPE)
+ {
+ return this.dataIn.ReadString16();
+ }
+ else if(type == PrimitiveMap.LONG_TYPE)
+ {
+ return this.dataIn.ReadInt64();
+ }
+ else if(type == PrimitiveMap.INTEGER_TYPE)
+ {
+ return this.dataIn.ReadInt32();
+ }
+ else if(type == PrimitiveMap.SHORT_TYPE)
+ {
+ return this.dataIn.ReadInt16();
+ }
+ else if(type == PrimitiveMap.FLOAT_TYPE)
+ {
+ return this.dataIn.ReadSingle();
+ }
+ else if(type == PrimitiveMap.DOUBLE_TYPE)
+ {
+ return this.dataIn.ReadDouble();
+ }
+ else if(type == PrimitiveMap.CHAR_TYPE)
+ {
+ return this.dataIn.ReadChar();
+ }
+ else if(type == PrimitiveMap.BYTE_TYPE)
+ {
+ return this.dataIn.ReadByte();
+ }
+ else if(type == PrimitiveMap.BOOLEAN_TYPE)
+ {
+ return this.dataIn.ReadBoolean();
+ }
+ else if(type == PrimitiveMap.BYTE_ARRAY_TYPE)
+ {
+ int length = this.dataIn.ReadInt32();
+ byte[] data = new byte[length];
+ this.dataIn.Read(data, 0, length);
+ return data;
+ }
+ else if(type == PrimitiveMap.NULL)
+ {
+ return null;
+ }
+ else
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw new MessageFormatException("Value is not a known type.");
+ }
+ }
+ catch(FormatException e)
+ {
+ this.byteBuffer.Seek(startingPos, SeekOrigin.Begin);
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ catch(EndOfStreamException e)
+ {
+ throw NMSExceptionSupport.createMessageEOFException(e);
+ }
+ catch(IOException e)
+ {
+ throw NMSExceptionSupport.createMessageFormatException(e);
+ }
+ }
+
+ public void WriteBoolean(bool value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.BOOLEAN_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteByte(byte value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.BYTE_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteBytes(byte[] value)
+ {
+ InitializeWriting();
+ this.WriteBytes(value, 0, value.Length);
+ }
+
+ public void WriteBytes(byte[] value, int offset, int length)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.BYTE_ARRAY_TYPE);
+ this.dataOut.Write((int) length);
+ this.dataOut.Write(value, offset, length);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteChar(char value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.CHAR_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteInt16(short value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.SHORT_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteInt32(int value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.INTEGER_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteInt64(long value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.LONG_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteSingle(float value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.FLOAT_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteDouble(double value)
+ {
+ InitializeWriting();
+ try
+ {
+ this.dataOut.Write(PrimitiveMap.DOUBLE_TYPE);
+ this.dataOut.Write(value);
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteString(string value)
+ {
+ InitializeWriting();
+ try
+ {
+ if( value.Length > 8192 )
+ {
+ this.dataOut.Write(PrimitiveMap.BIG_STRING_TYPE);
+ this.dataOut.WriteString32(value);
+ }
+ else
+ {
+ this.dataOut.Write(PrimitiveMap.STRING_TYPE);
+ this.dataOut.WriteString16(value);
+ }
+ }
+ catch(IOException e)
+ {
+ NMSExceptionSupport.create(e);
+ }
+ }
+
+ public void WriteObject(Object value)
+ {
+ InitializeWriting();
+ if( value is System.Byte )
+ {
+ this.WriteByte( (byte) value );
+ }
+ else if( value is Char )
+ {
+ this.WriteChar( (char) value );
+ }
+ else if( value is Boolean )
+ {
+ this.WriteBoolean( (bool) value );
+ }
+ else if( value is Int16 )
+ {
+ this.WriteInt16( (short) value );
+ }
+ else if( value is Int32 )
+ {
+ this.WriteInt32( (int) value );
+ }
+ else if( value is Int64 )
+ {
+ this.WriteInt64( (long) value );
+ }
+ else if( value is Single )
+ {
+ this.WriteSingle( (float) value );
+ }
+ else if( value is Double )
+ {
+ this.WriteDouble( (double) value );
+ }
+ else if( value is byte[] )
+ {
+ this.WriteBytes( (byte[]) value );
+ }
+ else if( value is String )
+ {
+ this.WriteString( (string) value );
+ }
+ else
+ {
+ throw new MessageFormatException("Cannot write non-primitive type:" + value.GetType());
+ }
+ }
+
+ public override Object Clone()
+ {
+ StoreContent();
+ return base.Clone();
+ }
+
+ public override void OnSend()
+ {
+ base.OnSend();
+ StoreContent();
+ }
+
+ public override void ClearBody()
+ {
+ base.ClearBody();
+ this.byteBuffer = null;
+ this.dataIn = null;
+ this.dataOut = null;
+ this.bytesRemaining = -1;
+ }
+
+ public void Reset()
+ {
+ StoreContent();
+ this.dataIn = null;
+ this.dataOut = null;
+ this.byteBuffer = null;
+ this.bytesRemaining = -1;
+ this.ReadOnlyBody = true;
+ }
+
+ private void InitializeReading()
+ {
+ FailIfWriteOnlyBody();
+ if(this.dataIn == null)
+ {
+ // TODO - Add support for Message Compression.
+ this.byteBuffer = new MemoryStream(this.Content, false);
+ dataIn = new EndianBinaryReader(byteBuffer);
+ }
+ }
+
+ private void InitializeWriting()
+ {
+ FailIfReadOnlyBody();
+ if(this.dataOut == null)
+ {
+ // TODO - Add support for Message Compression.
+ this.byteBuffer = new MemoryStream();
+ this.dataOut = new EndianBinaryWriter(byteBuffer);
+ }
+ }
+
+ private void StoreContent()
+ {
+ if( dataOut != null)
+ {
+ dataOut.Close();
+ // TODO - Add support for Message Compression.
+
+ this.Content = byteBuffer.ToArray();
+ this.dataOut = null;
+ this.byteBuffer = null;
+ }
+ }
+
}
}
Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQStreamMessageTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQStreamMessageTest.cs?rev=822299&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQStreamMessageTest.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQStreamMessageTest.cs Tue Oct 6 14:48:03 2009
@@ -0,0 +1,1266 @@
+/*
+ * 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 Apache.NMS.ActiveMQ.Commands;
+
+namespace Apache.NMS.ActiveMQ.Test.Commands
+{
+ [TestFixture]
+ public class ActiveMQStreamMessageTest
+ {
+ [Test]
+ public void TestCommand()
+ {
+ ActiveMQStreamMessage message = new ActiveMQStreamMessage();
+
+ Assert.IsNull( message.Content );
+ Assert.IsTrue( !message.ReadOnlyBody );
+ Assert.IsTrue( !message.ReadOnlyProperties );
+ }
+
+ [Test]
+ public void TestReadBoolean() {
+
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ msg.WriteBoolean(true);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadBoolean());
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == "True");
+
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadInt32();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadInt64();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadDouble();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadByte()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ byte test = (byte)4;
+ msg.WriteByte(test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadByte() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt16() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt32() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt64() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == (test).ToString());
+
+ msg.Reset();
+ try
+ {
+ msg.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadDouble();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadInt16() {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ short test = (short)4;
+ msg.WriteInt16(test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt16() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt32() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt64() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == (test).ToString());
+ msg.Reset();
+ try
+ {
+ msg.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadDouble();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadChar() {
+
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ char test = 'z';
+ msg.WriteChar(test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadChar() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == (test).ToString());
+ msg.Reset();
+ try
+ {
+ msg.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt32();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt64();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadDouble();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadInt() {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ int test = 4;
+ msg.WriteInt32(test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt32() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt64() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == (test).ToString());
+ msg.Reset();
+ try
+ {
+ msg.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadDouble();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadLong()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ long test = 4L;
+ msg.WriteInt64(test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt64() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == (test).ToString());
+ msg.Reset();
+ try
+ {
+ msg.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt32();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadDouble();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg = new ActiveMQStreamMessage();
+ msg.WriteObject((Int64) 1);
+ // Reset so it's readable now
+ msg.Reset();
+ Assert.AreEqual( (Int64) 1, msg.ReadObject());
+ }
+
+ [Test]
+ public void TestReadFloat()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ float test = 4.4f;
+ msg.WriteSingle(test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadSingle() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadDouble() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == (test).ToString());
+ msg.Reset();
+ try
+ {
+ msg.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt32();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt64();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadDouble()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ double test = 4.4;
+ msg.WriteDouble(test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadDouble() == test);
+ msg.Reset();
+ Assert.IsTrue(msg.ReadString() == (test).ToString());
+ msg.Reset();
+ try
+ {
+ msg.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt32();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt64();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadString()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+
+ byte testByte = (byte)2;
+ msg.WriteString(((Byte)testByte).ToString());
+ msg.Reset();
+ Assert.IsTrue(msg.ReadByte() == testByte);
+ msg.ClearBody();
+ short testShort = 3;
+ msg.WriteString(((Int16)testShort).ToString());
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt16() == testShort);
+ msg.ClearBody();
+ int testInt = 4;
+ msg.WriteString(((Int32)testInt).ToString());
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt32() == testInt);
+ msg.ClearBody();
+ long testLong = 6L;
+ msg.WriteString(((Int64)testLong).ToString());
+ msg.Reset();
+ Assert.IsTrue(msg.ReadInt64() == testLong);
+ msg.ClearBody();
+ float testFloat = 6.6f;
+ msg.WriteString(((Single)testFloat).ToString());
+ msg.Reset();
+ Assert.IsTrue(msg.ReadSingle() == testFloat);
+ msg.ClearBody();
+ double testDouble = 7.7d;
+ msg.WriteString(((Double)testDouble).ToString());
+ msg.Reset();
+ Assert.AreEqual( testDouble, msg.ReadDouble(), 0.05 );
+ msg.ClearBody();
+ msg.WriteString("true");
+ msg.Reset();
+ Assert.IsTrue(msg.ReadBoolean());
+ msg.ClearBody();
+ msg.WriteString("a");
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.ClearBody();
+ msg.WriteString("777");
+ msg.Reset();
+ try
+ {
+ msg.ReadBytes(new byte[3]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadBigString()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+ // 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.WriteString(bigString);
+ msg.Reset();
+ Assert.AreEqual(bigString, msg.ReadString());
+ }
+
+ [Test]
+ public void TestReadBytes()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+ byte[] test = new byte[50];
+ for(int i = 0; i < test.Length; i++)
+ {
+ test[i] = (byte)i;
+ }
+ msg.WriteBytes(test);
+ msg.Reset();
+ byte[] valid = new byte[test.Length];
+ msg.ReadBytes(valid);
+ for(int i = 0; i < valid.Length; i++)
+ {
+ Assert.IsTrue(valid[i] == test[i]);
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt32();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadInt64();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ msg.Reset();
+ try
+ {
+ msg.ReadString();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadObject()
+ {
+ ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
+ byte testByte = (byte)2;
+ msg.WriteByte(testByte);
+ msg.Reset();
+ Assert.IsTrue((byte)msg.ReadObject() == testByte);
+ msg.ClearBody();
+
+ short testShort = 3;
+ msg.WriteInt16(testShort);
+ msg.Reset();
+ Assert.IsTrue((short)msg.ReadObject() == testShort);
+ msg.ClearBody();
+
+ int testInt = 4;
+ msg.WriteInt32(testInt);
+ msg.Reset();
+ Assert.IsTrue((int)msg.ReadObject() == testInt);
+ msg.ClearBody();
+
+ long testLong = 6L;
+ msg.WriteInt64(testLong);
+ msg.Reset();
+ Assert.IsTrue((long)msg.ReadObject() == testLong);
+ msg.ClearBody();
+
+ float testFloat = 6.6f;
+ msg.WriteSingle(testFloat);
+ msg.Reset();
+ Assert.IsTrue((float)msg.ReadObject() == testFloat);
+ msg.ClearBody();
+
+ double testDouble = 7.7d;
+ msg.WriteDouble(testDouble);
+ msg.Reset();
+ Assert.IsTrue((double)msg.ReadObject() == testDouble);
+ msg.ClearBody();
+
+ char testChar = 'z';
+ msg.WriteChar(testChar);
+ msg.Reset();
+ Assert.IsTrue((char)msg.ReadObject() == testChar);
+ msg.ClearBody();
+
+ byte[] data = new byte[50];
+ for(int i = 0; i < data.Length; i++)
+ {
+ data[i] = (byte)i;
+ }
+ msg.WriteBytes(data);
+ msg.Reset();
+ byte[] valid = (byte[])msg.ReadObject();
+ Assert.IsTrue(valid.Length == data.Length);
+ for(int i = 0; i < valid.Length; i++)
+ {
+ Assert.IsTrue(valid[i] == data[i]);
+ }
+ msg.ClearBody();
+ msg.WriteBoolean(true);
+ msg.Reset();
+ Assert.IsTrue((bool)msg.ReadObject());
+ }
+
+ [Test]
+ public void TestClearBody()
+ {
+ ActiveMQStreamMessage streamMessage = new ActiveMQStreamMessage();
+ try
+ {
+ streamMessage.WriteObject((Int64) 2);
+ streamMessage.ClearBody();
+ Assert.IsFalse(streamMessage.ReadOnlyBody);
+ streamMessage.WriteObject((Int64) 2);
+ streamMessage.ReadObject();
+ Assert.Fail("should throw exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ catch(MessageNotWriteableException)
+ {
+ Assert.Fail("should be writeable");
+ }
+ }
+
+ [Test]
+ public void TestReset()
+ {
+ ActiveMQStreamMessage streamMessage = new ActiveMQStreamMessage();
+ try
+ {
+ streamMessage.WriteDouble(24.5);
+ streamMessage.WriteInt64(311);
+ }
+ catch(MessageNotWriteableException)
+ {
+ Assert.Fail("should be writeable");
+ }
+ streamMessage.Reset();
+ try
+ {
+ Assert.IsTrue(streamMessage.ReadOnlyBody);
+ Assert.AreEqual(streamMessage.ReadDouble(), 24.5, 0);
+ Assert.AreEqual(streamMessage.ReadInt64(), 311);
+ }
+ catch(MessageNotReadableException)
+ {
+ Assert.Fail("should be readable");
+ }
+ try
+ {
+ streamMessage.WriteInt32(33);
+ Assert.Fail("should throw exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestReadOnlyBody()
+ {
+ ActiveMQStreamMessage message = new ActiveMQStreamMessage();
+ try
+ {
+ message.WriteBoolean(true);
+ message.WriteByte((byte)1);
+ message.WriteBytes(new byte[1]);
+ message.WriteBytes(new byte[3], 0, 2);
+ message.WriteChar('a');
+ message.WriteDouble(1.5);
+ message.WriteSingle((float)1.5);
+ message.WriteInt32(1);
+ message.WriteInt64(1);
+ message.WriteObject("stringobj");
+ message.WriteInt16((short)1);
+ message.WriteString("string");
+ }
+ catch(MessageNotWriteableException)
+ {
+ Assert.Fail("Should be writeable");
+ }
+ message.Reset();
+ try
+ {
+ message.ReadBoolean();
+ message.ReadByte();
+ Assert.AreEqual(1, message.ReadBytes(new byte[10]));
+ Assert.AreEqual(-1, message.ReadBytes(new byte[10]));
+ Assert.AreEqual(2, message.ReadBytes(new byte[10]));
+ Assert.AreEqual(-1, message.ReadBytes(new byte[10]));
+ message.ReadChar();
+ message.ReadDouble();
+ message.ReadSingle();
+ message.ReadInt32();
+ message.ReadInt64();
+ message.ReadString();
+ message.ReadInt16();
+ message.ReadString();
+ }
+ catch(MessageNotReadableException)
+ {
+ Assert.Fail("Should be readable");
+ }
+ try
+ {
+ message.WriteBoolean(true);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteByte((byte)1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteBytes(new byte[3], 0, 2);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try {
+ message.WriteChar('a');
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteDouble(1.5);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteSingle((float)1.5);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteInt32(1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteInt64(1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteObject("stringobj");
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteSingle((short)1);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ try
+ {
+ message.WriteString("string");
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotWriteableException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestWriteOnlyBody()
+ {
+ ActiveMQStreamMessage message = new ActiveMQStreamMessage();
+ message.ClearBody();
+ try
+ {
+ message.WriteBoolean(true);
+ message.WriteByte((byte)1);
+ message.WriteBytes(new byte[1]);
+ message.WriteBytes(new byte[3], 0, 2);
+ message.WriteChar('a');
+ message.WriteDouble(1.5);
+ message.WriteSingle((float)1.5);
+ message.WriteInt32(1);
+ message.WriteInt64(1);
+ message.WriteObject("stringobj");
+ message.WriteSingle((short)1);
+ message.WriteString("string");
+ }
+ catch(MessageNotWriteableException)
+ {
+ Assert.Fail("Should be writeable");
+ }
+ try
+ {
+ message.ReadBoolean();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadByte();
+ Assert.Fail("Should have thrown exception");
+ } catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadBytes(new byte[1]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadBytes(new byte[2]);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadChar();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadDouble();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadSingle();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException) {
+ }
+
+ try
+ {
+ message.ReadInt32();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadInt64();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadString();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadInt16();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ try
+ {
+ message.ReadString();
+ Assert.Fail("Should have thrown exception");
+ }
+ catch(MessageNotReadableException)
+ {
+ }
+ }
+
+ [Test]
+ public void TestWriteObject()
+ {
+ try
+ {
+ ActiveMQStreamMessage message = new ActiveMQStreamMessage();
+ message.ClearBody();
+ message.WriteObject("test");
+ message.WriteObject((Char) 'a');
+ message.WriteObject((Boolean) false);
+ message.WriteObject((Byte) ((byte) 2));
+ message.WriteObject((Int16) ((short) 2));
+ message.WriteObject((Int32) 2);
+ message.WriteObject((Int64) 2L);
+ message.WriteObject((Single) 2.0f);
+ message.WriteObject((Double) 2.0);
+ }
+ catch(Exception e)
+ {
+ Assert.Fail(e.Message);
+ }
+ try
+ {
+ ActiveMQStreamMessage message = new ActiveMQStreamMessage();
+ message.ClearBody();
+ message.WriteObject(new Object());
+ Assert.Fail("should throw an exception");
+ }
+ catch(MessageFormatException)
+ {
+ }
+ catch(Exception e)
+ {
+ Assert.Fail(e.Message);
+ }
+ }
+ }
+}
Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Commands/ActiveMQStreamMessageTest.cs
------------------------------------------------------------------------------
svn:eol-style = native