Fixed the primitive map marhsalling problem that was breaking the tests.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@386703 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2006-03-17 20:11:02 +00:00
parent 257f16a0bb
commit 9224eeb0a2
8 changed files with 988 additions and 842 deletions

View File

@ -35,7 +35,6 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets"/>
<ItemGroup>
<Compile Include="src\main\csharp\ActiveMQ\BrokerException.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\AbstractCommand.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\ActiveMQBytesMessage.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\ActiveMQDestination.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\ActiveMQMapMessage.cs"/>
@ -210,6 +209,7 @@
<Compile Include="src\test\csharp\ActiveMQ\Commands\CommandTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\OpenWire\BooleanStreamTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\OpenWire\EndianTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\OpenWire\PrimitiveMapTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\TestMain.cs"/>
<Compile Include="src\test\csharp\NMS\AsyncConsumeTest.cs"/>
<Compile Include="src\test\csharp\NMS\BadConsumeTest.cs"/>

View File

@ -29,17 +29,6 @@ namespace ActiveMQ.OpenWire
/// </summary>
public abstract class BaseDataStreamMarshaller
{
public const byte NULL = 0;
public const byte BOOLEAN_TYPE = 1;
public const byte BYTE_TYPE = 2;
public const byte CHAR_TYPE = 3;
public const byte SHORT_TYPE = 4;
public const byte INTEGER_TYPE = 5;
public const byte LONG_TYPE = 6;
public const byte DOUBLE_TYPE = 7;
public const byte FLOAT_TYPE = 8;
public const byte STRING_TYPE = 9;
public const byte BYTE_ARRAY_TYPE = 10;
private static String[] HEX_TABLE = new String[]{
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f",
@ -713,183 +702,6 @@ namespace ActiveMQ.OpenWire
}
/// <summary>
/// Marshals the primitive type map to a byte array
/// </summary>
public static byte[] MarshalPrimitiveMap(IDictionary map)
{
if (map == null)
{
return null;
}
else
{
MemoryStream memoryStream = new MemoryStream();
MarshalPrimitiveMap(map, new OpenWireBinaryWriter(memoryStream));
return memoryStream.GetBuffer();
}
}
public static void MarshalPrimitiveMap(IDictionary map, BinaryWriter dataOut)
{
if (map == null)
{
dataOut.Write((int)-1);
}
else
{
dataOut.Write(map.Count);
foreach (DictionaryEntry entry in map)
{
String name = (String) entry.Key;
dataOut.Write(name);
Object value = entry.Value;
MarshalPrimitive(dataOut, value);
}
}}
/// <summary>
/// Unmarshals the primitive type map from the given byte array
/// </summary>
public static IDictionary UnmarshalPrimitiveMap(byte[] data)
{
if (data == null)
{
return new Hashtable();
}
else
{
return UnmarshalPrimitiveMap(new OpenWireBinaryReader(new MemoryStream(data)));
}
}
public static IDictionary UnmarshalPrimitiveMap(BinaryReader dataIn)
{
int size = dataIn.ReadInt32();
if (size < 0)
{
return null;
}
else
{
IDictionary answer = new Hashtable(size);
for (int i=0; i < size; i++)
{
String name = dataIn.ReadString();
answer[name] = UnmarshalPrimitive(dataIn);
}
return answer;
}
}
public static void MarshalPrimitive(BinaryWriter dataOut, Object value)
{
if (value == null)
{
dataOut.Write(NULL);
}
else if (value is bool)
{
dataOut.Write(BOOLEAN_TYPE);
dataOut.Write((bool) value);
}
else if (value is byte)
{
dataOut.Write(BYTE_TYPE);
dataOut.Write(((Byte)value));
}
else if (value is char)
{
dataOut.Write(CHAR_TYPE);
dataOut.Write((char) value);
}
else if (value is short)
{
dataOut.Write(SHORT_TYPE);
dataOut.Write((short) value);
}
else if (value is int)
{
dataOut.Write(INTEGER_TYPE);
dataOut.Write((int) value);
}
else if (value is long)
{
dataOut.Write(LONG_TYPE);
dataOut.Write((long) value);
}
else if (value is float)
{
dataOut.Write(FLOAT_TYPE);
dataOut.Write((float) value);
}
else if (value is double)
{
dataOut.Write(DOUBLE_TYPE);
dataOut.Write((double) value);
}
else if (value is byte[])
{
byte[] data = (byte[]) value;
dataOut.Write(BYTE_ARRAY_TYPE);
dataOut.Write(data.Length);
dataOut.Write(data);
}
else if (value is string)
{
dataOut.Write(STRING_TYPE);
dataOut.Write((string) value);
}
else
{
throw new IOException("Object is not a primitive: " + value);
}
}
public static Object UnmarshalPrimitive(BinaryReader dataIn)
{
Object value=null;
switch (dataIn.ReadByte())
{
case BYTE_TYPE:
value = dataIn.ReadByte();
break;
case BOOLEAN_TYPE:
value = dataIn.ReadBoolean();
break;
case CHAR_TYPE:
value = dataIn.ReadChar();
break;
case SHORT_TYPE:
value = dataIn.ReadInt16();
break;
case INTEGER_TYPE:
value = dataIn.ReadInt32();
break;
case LONG_TYPE:
value = dataIn.ReadInt64();
break;
case FLOAT_TYPE:
value = dataIn.ReadSingle();
break;
case DOUBLE_TYPE:
value = dataIn.ReadDouble();
break;
case BYTE_ARRAY_TYPE:
int size = dataIn.ReadInt32();
byte[] data = new byte[size];
dataIn.Read(data, 0, size);
value = data;
break;
case STRING_TYPE:
value = dataIn.ReadString();
break;
}
return value;
}
/// <summary>
/// Converts the object to a String

View File

@ -79,7 +79,12 @@ namespace ActiveMQ.OpenWire
/// <returns>A char</returns>
public override char ReadChar()
{
return EndianSupport.SwitchEndian(base.ReadChar());
return (char) (
(((char)( (byte)(base.ReadByte()) )) << 8 ) |
(((char)( (byte)(base.ReadByte()) )) )
);
// return EndianSupport.SwitchEndian(base.ReadChar());
}
/// <summary>

View File

@ -102,7 +102,8 @@ namespace ActiveMQ.OpenWire
/// <param name="ch">A char</param>
public override void Write(char ch)
{
base.Write(EndianSupport.SwitchEndian(ch));
base.Write( (byte)( ( ch>>8 ) & 0xFF ) );
base.Write( (byte)( ch & 0xFF ) );
}
/// <summary>

View File

@ -17,6 +17,7 @@
using NMS;
using System;
using System.Collections;
using System.IO;
namespace ActiveMQ.OpenWire
{
@ -25,26 +26,20 @@ namespace ActiveMQ.OpenWire
/// </summary>
public class PrimitiveMap : IPrimitiveMap
{
public const byte NULL = 0;
public const byte BOOLEAN_TYPE = 1;
public const byte BYTE_TYPE = 2;
public const byte CHAR_TYPE = 3;
public const byte SHORT_TYPE = 4;
public const byte INTEGER_TYPE = 5;
public const byte LONG_TYPE = 6;
public const byte DOUBLE_TYPE = 7;
public const byte FLOAT_TYPE = 8;
public const byte STRING_TYPE = 9;
public const byte BYTE_ARRAY_TYPE = 10;
private IDictionary dictionary = new Hashtable();
/// <summary>
/// Unmarshalls the map from the given data or if the data is null just
/// return an empty map
/// </summary>
public static PrimitiveMap Unmarshal(byte[] data)
{
PrimitiveMap answer = new PrimitiveMap();
answer.dictionary = BaseDataStreamMarshaller.UnmarshalPrimitiveMap(data);
return answer;
}
public byte[] Marshal()
{
return BaseDataStreamMarshaller.MarshalPrimitiveMap(dictionary);
}
public void Clear()
{
dictionary.Clear();
@ -235,5 +230,226 @@ namespace ActiveMQ.OpenWire
}
}
/// <summary>
/// Method ToString
/// </summary>
/// <returns>A string</returns>
public override String ToString()
{
String s="{";
bool first=true;
foreach (DictionaryEntry entry in dictionary)
{
if( !first ) {
s+=", ";
}
first=false;
String name = (String) entry.Key;
Object value = entry.Value;
s+=name+"="+value;
}
s += "}";
return s;
}
/// <summary>
/// Unmarshalls the map from the given data or if the data is null just
/// return an empty map
/// </summary>
public static PrimitiveMap Unmarshal(byte[] data)
{
PrimitiveMap answer = new PrimitiveMap();
answer.dictionary = UnmarshalPrimitiveMap(data);
return answer;
}
public byte[] Marshal()
{
return MarshalPrimitiveMap(dictionary);
}
/// <summary>
/// Marshals the primitive type map to a byte array
/// </summary>
public static byte[] MarshalPrimitiveMap(IDictionary map)
{
if (map == null)
{
return null;
}
else
{
MemoryStream memoryStream = new MemoryStream();
MarshalPrimitiveMap(map, new OpenWireBinaryWriter(memoryStream));
return memoryStream.GetBuffer();
}
}
public static void MarshalPrimitiveMap(IDictionary map, BinaryWriter dataOut)
{
if (map == null)
{
dataOut.Write((int)-1);
}
else
{
dataOut.Write(map.Count);
foreach (DictionaryEntry entry in map)
{
String name = (String) entry.Key;
dataOut.Write(name);
Object value = entry.Value;
MarshalPrimitive(dataOut, value);
}
}}
/// <summary>
/// Unmarshals the primitive type map from the given byte array
/// </summary>
public static IDictionary UnmarshalPrimitiveMap(byte[] data)
{
if (data == null)
{
return new Hashtable();
}
else
{
return UnmarshalPrimitiveMap(new OpenWireBinaryReader(new MemoryStream(data)));
}
}
public static IDictionary UnmarshalPrimitiveMap(BinaryReader dataIn)
{
int size = dataIn.ReadInt32();
if (size < 0)
{
return null;
}
else
{
IDictionary answer = new Hashtable(size);
for (int i=0; i < size; i++)
{
String name = dataIn.ReadString();
answer[name] = UnmarshalPrimitive(dataIn);
}
return answer;
}
}
public static void MarshalPrimitive(BinaryWriter dataOut, Object value)
{
if (value == null)
{
dataOut.Write(NULL);
}
else if (value is bool)
{
dataOut.Write(BOOLEAN_TYPE);
dataOut.Write((bool) value);
}
else if (value is byte)
{
dataOut.Write(BYTE_TYPE);
dataOut.Write(((byte)value));
}
else if (value is char)
{
dataOut.Write(CHAR_TYPE);
dataOut.Write((char) value);
}
else if (value is short)
{
dataOut.Write(SHORT_TYPE);
dataOut.Write((short) value);
}
else if (value is int)
{
dataOut.Write(INTEGER_TYPE);
dataOut.Write((int) value);
}
else if (value is long)
{
dataOut.Write(LONG_TYPE);
dataOut.Write((long) value);
}
else if (value is float)
{
dataOut.Write(FLOAT_TYPE);
dataOut.Write((float) value);
}
else if (value is double)
{
dataOut.Write(DOUBLE_TYPE);
dataOut.Write((double) value);
}
else if (value is byte[])
{
byte[] data = (byte[]) value;
dataOut.Write(BYTE_ARRAY_TYPE);
dataOut.Write(data.Length);
dataOut.Write(data);
}
else if (value is string)
{
dataOut.Write(STRING_TYPE);
dataOut.Write((string) value);
}
else
{
throw new IOException("Object is not a primitive: " + value);
}
}
public static Object UnmarshalPrimitive(BinaryReader dataIn)
{
Object value=null;
switch (dataIn.ReadByte())
{
case BYTE_TYPE:
value = dataIn.ReadByte();
break;
case BOOLEAN_TYPE:
value = dataIn.ReadBoolean();
break;
case CHAR_TYPE:
value = dataIn.ReadChar();
break;
case SHORT_TYPE:
value = dataIn.ReadInt16();
break;
case INTEGER_TYPE:
value = dataIn.ReadInt32();
break;
case LONG_TYPE:
value = dataIn.ReadInt64();
break;
case FLOAT_TYPE:
value = dataIn.ReadSingle();
break;
case DOUBLE_TYPE:
value = dataIn.ReadDouble();
break;
case BYTE_ARRAY_TYPE:
int size = dataIn.ReadInt32();
byte[] data = new byte[size];
dataIn.Read(data, 0, size);
value = data;
break;
case STRING_TYPE:
value = dataIn.ReadString();
break;
}
return value;
}
}
}

View File

@ -0,0 +1,112 @@
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed 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.IO;
using ActiveMQ.OpenWire;
namespace ActiveMQ.OpenWire
{
[TestFixture]
public class PrimitiveMapTest
{
bool a = true;
byte b = 123;
char c = 'c';
short d = 0x1234;
int e = 0x12345678;
long f = 0x1234567812345678;
string g = "Hello World!";
bool h = false;
byte i = 0xFF;
short j = -0x1234;
int k = -0x12345678;
long l = -0x1234567812345678;
[Test]
public void TestNotMarshalled()
{
PrimitiveMap map = CreatePrimitiveMap();
AssertPrimitiveMap(map);
}
[Test]
public void TestMarshalled()
{
PrimitiveMap map = CreatePrimitiveMap();
Console.WriteLine("data: "+map);
byte[] data = map.Marshal();
map = PrimitiveMap.Unmarshal(data);
Console.WriteLine("data: "+map);
AssertPrimitiveMap(map);
}
protected PrimitiveMap CreatePrimitiveMap()
{
PrimitiveMap map = new PrimitiveMap();
map["a"] = a;
map["b"] = b;
map["c"] = c;
map["d"] = d;
map["e"] = e;
map["f"] = f;
map["g"] = g;
map["h"] = h;
map["i"] = i;
map["j"] = j;
map["k"] = k;
map["l"] = l;
return map;
}
protected void AssertPrimitiveMap(PrimitiveMap map) {
// use generic API to access entries
Assert.AreEqual(a, map["a"], "generic map entry: a");
Assert.AreEqual(b, map["b"], "generic map entry: b");
Assert.AreEqual(c, map["c"], "generic map entry: c");
Assert.AreEqual(d, map["d"], "generic map entry: d");
Assert.AreEqual(e, map["e"], "generic map entry: e");
Assert.AreEqual(f, map["f"], "generic map entry: f");
Assert.AreEqual(g, map["g"], "generic map entry: g");
Assert.AreEqual(h, map["h"], "generic map entry: h");
Assert.AreEqual(i, map["i"], "generic map entry: i");
Assert.AreEqual(j, map["j"], "generic map entry: j");
Assert.AreEqual(k, map["k"], "generic map entry: k");
Assert.AreEqual(l, map["l"], "generic map entry: l");
// use type safe APIs
Assert.AreEqual(a, map.GetBool("a"), "map entry: a");
Assert.AreEqual(b, map.GetByte("b"), "map entry: b");
Assert.AreEqual(c, map.GetChar("c"), "map entry: c");
Assert.AreEqual(d, map.GetShort("d"), "map entry: d");
Assert.AreEqual(e, map.GetInt("e"), "map entry: e");
Assert.AreEqual(f, map.GetLong("f"), "map entry: f");
Assert.AreEqual(g, map.GetString("g"), "map entry: g");
Assert.AreEqual(h, map.GetBool("h"), "map entry: h");
Assert.AreEqual(i, map.GetByte("i"), "map entry: i");
Assert.AreEqual(j, map.GetShort("j"), "map entry: j");
Assert.AreEqual(k, map.GetInt("k"), "map entry: k");
Assert.AreEqual(l, map.GetLong("l"), "map entry: l");
}
}
}