activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r898996 - in /activemq/activemq-dotnet/Apache.NMS.Stomp/trunk: src/main/csharp/Protocol/XmlPrimitiveMapMarshaler.cs src/test/csharp/Protocol/ src/test/csharp/Protocol/XmlPrimitiveMapMarshalerTest.cs vs2008-stomp-test.csproj
Date Wed, 13 Jan 2010 23:26:16 GMT
Author: tabish
Date: Wed Jan 13 23:26:16 2010
New Revision: 898996

URL: http://svn.apache.org/viewvc?rev=898996&view=rev
Log:
Add some tests for the XmlPrimitiveMapMarshaler and a couple fixes to the marshaler.  Also
adds an initial attmept at the unmarshal code for the PrimtiveMap from XML.

Added:
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/test/csharp/Protocol/
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/test/csharp/Protocol/XmlPrimitiveMapMarshalerTest.cs
  (with props)
Modified:
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Protocol/XmlPrimitiveMapMarshaler.cs
    activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp-test.csproj

Modified: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Protocol/XmlPrimitiveMapMarshaler.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Protocol/XmlPrimitiveMapMarshaler.cs?rev=898996&r1=898995&r2=898996&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Protocol/XmlPrimitiveMapMarshaler.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/main/csharp/Protocol/XmlPrimitiveMapMarshaler.cs
Wed Jan 13 23:26:16 2010
@@ -18,6 +18,7 @@
 
 using System;
 using System.Text;
+using System.IO;
 using System.Xml;
 using System.Collections;
 using Apache.NMS.Util;
@@ -47,6 +48,11 @@
 
         public byte[] Marshal(IPrimitiveMap map)
         {
+            if(map == null)
+            {
+                return null;
+            }
+
             StringBuilder builder = new StringBuilder();
 
             XmlWriterSettings settings = new XmlWriterSettings();
@@ -77,8 +83,6 @@
             writer.WriteEndElement();
             writer.Close();
 
-            Console.WriteLine("XML Map = " + builder.ToString());
-
             return this.encoder.GetBytes(builder.ToString());
         }
 
@@ -93,6 +97,86 @@
                 return result;
             }
 
+            String xmlString = this.encoder.GetString(mapContent);
+
+            XmlReaderSettings settings = new XmlReaderSettings();
+
+            settings.IgnoreComments = true;
+            settings.IgnoreWhitespace = true;
+            settings.IgnoreProcessingInstructions = true;
+
+            XmlReader reader = XmlReader.Create(new StringReader(xmlString), settings);
+
+            reader.MoveToContent();
+            reader.ReadStartElement("map");
+
+            while(reader.Name == "entry")
+            {
+                reader.ReadStartElement();
+                string key = reader.ReadElementContentAsString("string", "");
+
+                Object value = null;
+
+                switch(reader.Name)
+                {
+                case "char":
+                    value = Convert.ToChar(reader.ReadElementContentAsString());
+                    break;
+                case "double":
+                    value = Convert.ToDouble(reader.ReadElementContentAsString());
+                    break;
+                case "float":
+                    value = Convert.ToSingle(reader.ReadElementContentAsString());
+                    break;
+                case "long":
+                    value = Convert.ToInt64(reader.ReadElementContentAsString());
+                    break;
+                case "int":
+                    value = Convert.ToInt32(reader.ReadElementContentAsString());
+                    break;
+                case "short":
+                    value = Convert.ToInt16(reader.ReadElementContentAsString());
+                    break;
+                case "byte":
+                    value = Convert.ToByte(reader.ReadElementContentAsString());
+                    break;
+                case "boolean":
+                    value = Convert.ToBoolean(reader.ReadElementContentAsString());
+                    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;
+                default:
+                    Console.WriteLine("Key = " + reader.ReadElementContentAsString());
+                    break;
+                };
+
+                // Now store the value into our new PrimitiveMap.
+                result[key] = value;
+
+                reader.ReadEndElement();
+            }
+
+            reader.ReadEndElement();
+            reader.Close();
+
             return result;
         }
 
@@ -103,6 +187,10 @@
                 Console.WriteLine("Null Map Value");
                 throw new NullReferenceException("PrimitiveMap values should not be Null");
             }
+            else if(value is char)
+            {
+                writer.WriteElementString("char", value.ToString());
+            }
             else if(value is bool)
             {
                 writer.WriteElementString("boolean", value.ToString().ToLower());

Added: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/test/csharp/Protocol/XmlPrimitiveMapMarshalerTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/test/csharp/Protocol/XmlPrimitiveMapMarshalerTest.cs?rev=898996&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/test/csharp/Protocol/XmlPrimitiveMapMarshalerTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/test/csharp/Protocol/XmlPrimitiveMapMarshalerTest.cs
Wed Jan 13 23:26:16 2010
@@ -0,0 +1,111 @@
+// /*
+//  * 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.Text;
+using Apache.NMS;
+using Apache.NMS.Util;
+using Apache.NMS.Test;
+using Apache.NMS.Stomp.Protocol;
+using NUnit.Framework;
+using NUnit.Framework.Extensions;
+
+namespace Apache.NMS.Stomp.Test.Protocol
+{
+    [TestFixture]
+    public class XmlPrimitiveMapMarshalerTest
+    {
+        private static String xmlString =
+                @"<map>
+                      <entry>
+                        <string>CHAR</string>
+                        <char>a</char>
+                      </entry>
+                      <entry>
+                        <string>DOUBLE</string>
+                        <double>1.5</double>
+                      </entry>
+                      <entry>
+                        <string>BYTE</string>
+                        <byte>123</byte>
+                      </entry>
+                      <entry>
+                        <string>SHORT</string>
+                        <short>-32768</short>
+                      </entry>
+                      <entry>
+                        <string>INT</string>
+                        <int>5</int>
+                      </entry>
+                      <entry>
+                        <string>FLOAT</string>
+                        <float>1.1</float>
+                      </entry>
+                      <entry>
+                        <string>LONG</string>
+                        <long>256</long>
+                      </entry>
+                      <entry>
+                        <string>STRING</string>
+                        <string>FOO</string>
+                      </entry>
+                      <entry>
+                        <string>BYTES</string>
+                        <byte-array>CgoKFBQU</byte-array>
+                      </entry>
+                      <entry>
+                        <string>BOOL</string>
+                        <boolean>true</boolean>
+                      </entry>
+                    </map>";
+
+        [Test]
+        public void TestMarshalPrimitiveMap()
+        {
+            XmlPrimitiveMapMarshaler marshaler = new XmlPrimitiveMapMarshaler();
+
+            PrimitiveMap map = new PrimitiveMap();
+
+            map.SetBool("boolean", true);
+            map.SetByte("byte", (byte)1);
+            map["bytes1"] = new byte[1];
+            map.SetChar("char", 'a');
+            map.SetDouble("double", 1.5);
+            map.SetFloat("float", 1.5f);
+            map.SetInt("int", 1);
+            map.SetLong("long", 1);
+            map["object"] = "stringObj";
+            map.SetShort("short", (short)1);
+            map.SetString("string", "string");
+
+            byte[] result = marshaler.Marshal(map);
+            Assert.IsNotNull(result);
+
+            result = marshaler.Marshal(null);
+            Assert.IsNull(result);
+
+        }
+
+        [Test]
+        public void TestUnmarshalPrimitiveMap()
+        {
+            XmlPrimitiveMapMarshaler marshaler = new XmlPrimitiveMapMarshaler();
+        }
+
+    }
+}

Propchange: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/src/test/csharp/Protocol/XmlPrimitiveMapMarshalerTest.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp-test.csproj
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp-test.csproj?rev=898996&r1=898995&r2=898996&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp-test.csproj (original)
+++ activemq/activemq-dotnet/Apache.NMS.Stomp/trunk/vs2008-stomp-test.csproj Wed Jan 13 23:26:16
2010
@@ -101,5 +101,9 @@
     <Compile Include="src\test\csharp\NMSConnectionFactoryTest.cs" />
     <Compile Include="src\test\csharp\Threads\DedicatedTaskRunnerTest.cs" />
     <Compile Include="src\test\csharp\Commands\MapMessageTest.cs" />
+    <Compile Include="src\test\csharp\Protocol\XmlPrimitiveMapMarshalerTest.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="src\test\csharp\Protocol\" />
   </ItemGroup>
 </Project>
\ No newline at end of file



Mime
View raw message