Skip to content
Permalink
Browse files
Add some tests for the XmlPrimitiveMapMarshaler and a couple fixes to…
… the marshaler. Can now read marshaled XML maps
  • Loading branch information
Timothy A. Bish committed Jan 14, 2010
1 parent 8e49e7a commit 28f5dd234e4413d0f6e48e37d7b808be18ff2437
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
@@ -28,6 +28,16 @@ public class MapMessage : Message, IMapMessage
private PrimitiveMap body;
private PrimitiveMapInterceptor typeConverter;

public MapMessage() : base()
{
}

public MapMessage(PrimitiveMap body) : base()
{
this.body = body;
this.typeConverter = new PrimitiveMapInterceptor(this, this.body);
}

public override byte GetDataStructureType()
{
return DataStructureTypes.MapMessageType;
@@ -17,6 +17,7 @@
using Apache.NMS.Stomp.Commands;
using Apache.NMS.Stomp.Transport;
using Apache.NMS;
using Apache.NMS.Util;
using System;
using System.Collections;
using System.IO;
@@ -192,11 +193,17 @@ protected virtual Object CreateCommand(StompFrame frame)
protected virtual Command ReadMessage(StompFrame frame)
{
Message message = null;
string transformation = frame.RemoveProperty("transformation");

if(frame.HasProperty("content-length"))
{
message = new BytesMessage();
message.Content = frame.Content;
}
else if(transformation == "jms-map-xml")
{
message = new MapMessage(this.mapMarshaler.Unmarshal(frame.Content) as PrimitiveMap);
}
else
{
message = new TextMessage(encoder.GetString(frame.Content, 0, frame.Content.Length));
@@ -26,7 +26,7 @@
namespace Apache.NMS.Stomp.Protocol
{
/// <summary>
/// Reads / Writes an IPrimitveMap as XML.
/// Reads / Writes an IPrimitveMap as XML compatible with XStream.
/// </summary>
public class XmlPrimitiveMapMarshaler : IPrimitiveMapMarshaler
{
@@ -121,60 +121,51 @@ public IPrimitiveMap Unmarshal(byte[] mapContent)
{
case "char":
value = Convert.ToChar(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "double":
value = Convert.ToDouble(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "float":
value = Convert.ToSingle(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "long":
value = Convert.ToInt64(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "int":
value = Convert.ToInt32(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "short":
value = Convert.ToInt16(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "byte":
value = Convert.ToByte(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "boolean":
value = Convert.ToBoolean(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
case "byte-array":

byte[] buffer = new byte[1024];
MemoryStream array = new MemoryStream();

int bytesRead = 0;

do
{
bytesRead = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length);
array.Write(buffer, 0, bytesRead);
}
while(bytesRead != 0);

array.Close();

value = array.ToArray();

// Jump out here since this one reads past the EndElement for us.
continue;
value = Convert.FromBase64String(reader.ReadElementContentAsString());
reader.ReadEndElement();
break;
default:
Console.WriteLine("Key = " + reader.ReadElementContentAsString());
value = reader.ReadElementContentAsString();
reader.ReadEndElement();
break;
};

// Now store the value into our new PrimitiveMap.
Console.WriteLine("result[{0}] = {1}", key, value);
result[key] = value;

reader.ReadEndElement();
}

reader.ReadEndElement();
reader.Close();

return result;
@@ -30,8 +30,12 @@ namespace Apache.NMS.Stomp.Test.Protocol
[TestFixture]
public class XmlPrimitiveMapMarshalerTest
{
private static String xmlString =
private const String xmlString =
@"<map>
<entry>
<string>BYTES</string>
<byte-array>CgoKFBQU</byte-array>
</entry>
<entry>
<string>CHAR</string>
<char>a</char>
@@ -64,10 +68,6 @@ public class XmlPrimitiveMapMarshalerTest
<string>STRING</string>
<string>FOO</string>
</entry>
<entry>
<string>BYTES</string>
<byte-array>CgoKFBQU</byte-array>
</entry>
<entry>
<string>BOOL</string>
<boolean>true</boolean>
@@ -105,7 +105,24 @@ public void TestMarshalPrimitiveMap()
public void TestUnmarshalPrimitiveMap()
{
XmlPrimitiveMapMarshaler marshaler = new XmlPrimitiveMapMarshaler();
}

byte[] rawBytes = Encoding.UTF8.GetBytes(xmlString);

IPrimitiveMap result = marshaler.Unmarshal(rawBytes);

Assert.IsNotNull(result);

Assert.IsTrue(result.Contains("BOOL"));
Assert.IsTrue(result.Contains("BYTES"));
Assert.IsTrue(result.Contains("STRING"));
Assert.IsTrue(result.Contains("LONG"));
Assert.IsTrue(result.Contains("FLOAT"));
Assert.IsTrue(result.Contains("INT"));
Assert.IsTrue(result.Contains("BYTE"));
Assert.IsTrue(result.Contains("SHORT"));
Assert.IsTrue(result.Contains("DOUBLE"));
Assert.IsTrue(result.Contains("CHAR"));

}
}
}

0 comments on commit 28f5dd2

Please sign in to comment.