[Mono-dev] [PATCH] Fix serialization of typed datasets

John Lenz jlenz2 at math.uiuc.edu
Mon Sep 14 21:45:52 EDT 2009


Nagappan Alagappan wrote:
> Hello John,
> 
> This patch looks okay to me. Can you add the test case as well to this 
> patch ?
> 

Here is a test.  I noticed the Tests directory already has a typed
dataset in DataSet1.Designer.cs so I used that one.

Also, I tried looking at mono's xsd.exe to get it to generate the correct
code in the constructor, but I could not figure out how (or in which file)
mono's xsd.exe actually produces the class code.

John

Index: class/System.Data/Test/System.Data/DataSetTypedDataSetTest.cs
===================================================================
--- class/System.Data/Test/System.Data/DataSetTypedDataSetTest.cs	(revision 141922)
+++ class/System.Data/Test/System.Data/DataSetTypedDataSetTest.cs	(working copy)
@@ -196,6 +196,58 @@
  			myTypedDataSet.Order_DetailsRow[] drArr1 = ds.Orders[0].GetOrder_DetailsRows();
  			DataRow[] drArr2 = ds.Orders[0].GetChildRows(ds.Relations[0]);
  			Assert.AreEqual(drArr1 ,drArr2,"TDS25");
+
+#if NET_2_0
+			//now test serialization of a typed dataset generated by microsoft's xsd.exe
+			DataSet1 ds1 = new DataSet1();
+			ds1.DataTable1.AddDataTable1Row("test");
+			ds1.DataTable1.AddDataTable1Row("test2");
+
+			global::System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
+			  new global::System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
+			MemoryStream stream = new MemoryStream();
+
+			formatter.Serialize(stream, ds1);
+
+			stream.Seek(0, SeekOrigin.Begin);
+
+			DataSet1 ds1load = (DataSet1) formatter.Deserialize(stream);
+
+			Assert.IsTrue(ds1load.Tables.Contains("DataTable1"));
+			Assert.AreEqual("DataTable1DataTable", ds1load.Tables["DataTable1"].GetType().Name);
+			Assert.AreEqual(2, ds1load.DataTable1.Rows.Count);
+			Assert.AreEqual("DataTable1Row", ds1load.DataTable1[0].GetType().Name);
+			if (ds1load.DataTable1[0].Column1 == "test") {
+				Assert.AreEqual("test2", ds1load.DataTable1[1].Column1);
+			} else if (ds1load.DataTable1[0].Column1 == "test2") {
+				Assert.AreEqual("test", ds1load.DataTable1[1].Column1);
+			} else {
+				Assert.Fail("Invalid entry for Column1");
+			}
+
+			//now test when the mode is exclude schema
+			ds1.SchemaSerializationMode = global::System.Data.SchemaSerializationMode.ExcludeSchema;
+
+			stream = new MemoryStream();
+
+			formatter.Serialize(stream, ds1);
+
+			stream.Seek(0, SeekOrigin.Begin);
+
+			ds1load = (DataSet1) formatter.Deserialize(stream);
+
+			Assert.IsTrue(ds1load.Tables.Contains("DataTable1"));
+			Assert.AreEqual("DataTable1DataTable", ds1load.Tables["DataTable1"].GetType().Name);
+			Assert.AreEqual(2, ds1load.DataTable1.Rows.Count);
+			Assert.AreEqual("DataTable1Row", ds1load.DataTable1[0].GetType().Name);
+			if (ds1load.DataTable1[0].Column1 == "test") {
+				Assert.AreEqual("test2", ds1load.DataTable1[1].Column1);
+			} else if (ds1load.DataTable1[0].Column1 == "test2") {
+				Assert.AreEqual("test", ds1load.DataTable1[1].Column1);
+			} else {
+				Assert.Fail("Invalid entry for Column1");
+			}
+#endif
  		}
  	
  		protected void T_Changing(object sender, myTypedDataSet.OrdersRowChangeEvent e)


More information about the Mono-devel-list mailing list