4 using System.Data.Common;
5 using System.Globalization;
7 using System.Runtime.Serialization;
8 using System.Runtime.Serialization.Formatters;
9 using System.Runtime.Serialization.Formatters.Binary;
10 using System.Threading;
12 using NUnit.Framework;
14 namespace MonoTests.System.Data
17 public class BinarySerializationTest
19 private CultureInfo originalCulture;
24 originalCulture = Thread.CurrentThread.CurrentCulture;
25 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
29 public void TearDown ()
31 Thread.CurrentThread.CurrentCulture = originalCulture;
35 public void RemotingFormatDataTableTest ()
37 DataTable dt = new DataTable ();
38 //Test Default SerializationFormat
39 Assert.AreEqual (SerializationFormat.Xml, dt.RemotingFormat, "#1 Default value for RemotingFormat of DataTable is Xml");
40 //Test with Assigned value
41 dt.RemotingFormat = SerializationFormat.Binary;
42 Assert.AreEqual (SerializationFormat.Binary, dt.RemotingFormat, "#2 RemotingFormat Should be Binary");
45 public void RemotingFormatDataSetTest ()
47 DataSet ds = new DataSet ();
48 DataTable dt = new DataTable ();
50 //Test Default SerializationFormat
51 Assert.AreEqual (SerializationFormat.Xml, dt.RemotingFormat, "#1 Default value for RemotingFormat of DataTable is Xml");
52 Assert.AreEqual (SerializationFormat.Xml, ds.RemotingFormat, "#2 Default value for RemotingFormat of DataSet is Xml");
54 //Test with Assigned value
55 ds.RemotingFormat = SerializationFormat.Binary;
56 Assert.AreEqual (SerializationFormat.Binary, ds.RemotingFormat, "#3 RemotingFormat Should be Binary");
58 //Test After adding a Table to DataSet
60 Assert.AreEqual (SerializationFormat.Binary, ds.RemotingFormat, "#4 RemotingFormat should be Binary for DataSet");
61 Assert.AreEqual (SerializationFormat.Binary, dt.RemotingFormat, "#5 RemotingFormat should be Binary for DataTable");
63 ds.RemotingFormat = SerializationFormat.Xml;
64 Assert.AreEqual (SerializationFormat.Xml, ds.RemotingFormat, "#6 RemotingFormat should be Xml for DataSet");
65 Assert.AreEqual (SerializationFormat.Xml, dt.RemotingFormat, "#7 RemotingFormat should be Xml for DataTable");
67 //Test for Exception when try t modify RemotingFormat of DataTable which belongs to a DataSet
70 dt.RemotingFormat = SerializationFormat.Binary;
71 Assert.Fail ("#8 It should throw an ArgumentException");
72 } catch (ArgumentException e) {
73 Assert.AreEqual (e.GetType (), typeof (ArgumentException), "#9 Invalid Exception");
74 } catch (Exception e) {
75 Assert.AreEqual (e.GetType (), typeof (ArgumentException), "#10 Invalid Exception");
80 public void DataTableSerializationTest1 ()
83 DataTable tb1 = new DataTable ("Test");
84 tb1.Columns.Add ("id", typeof (int));
85 tb1.Columns.Add ("name", typeof (string));
86 tb1.Rows.Add (new object[] {1, "A"});
87 tb1.Rows.Add (new object[] {2, "B"});
89 UniqueConstraint uniqueConstraint = new UniqueConstraint (tb1.Columns ["id"]);
90 tb1.Constraints.Add (uniqueConstraint);
91 tb1.RemotingFormat = SerializationFormat.Binary;
97 MemoryStream ms = new MemoryStream ();
98 BinaryFormatter bf = new BinaryFormatter ();
99 bf.Serialize (ms,tb1);
100 byte [] serializedStream = ms.ToArray ();
103 ms = new MemoryStream (serializedStream);
104 DataTable dt = (DataTable)bf.Deserialize (ms);
107 //Test Properties of table
108 //Assert.AreEqual (tb1.RemotingFormat, dt.RemotingFormat, "#1 RemotingFormat property is Different");
109 Assert.AreEqual (tb1.Columns.Count, dt.Columns.Count, "#2 ColumnCount property is Different");
110 Assert.AreEqual (tb1.TableName, dt.TableName, "#3 TableName property is Different");
111 Assert.AreEqual (tb1.Prefix, dt.Prefix, "#4 Prefix propertyis Different");
112 Assert.AreEqual (tb1.Namespace, dt.Namespace, "#5 NameSpace property is Different");
113 Assert.AreEqual (tb1.CaseSensitive, dt.CaseSensitive, "#6 CaseSensitive property is Different");
114 Assert.AreEqual (tb1.Locale.LCID, dt.Locale.LCID, "#7 LocaleLCID property is Different");
115 Assert.AreEqual (tb1.MinimumCapacity, dt.MinimumCapacity, "#8 NameSpace property is Different");
117 Assert.AreEqual (tb1.Constraints.Count, dt.Constraints.Count, "#9 No. of Constraints is Different");
118 for (int i = 0; i < tb1.Constraints.Count; i++)
119 Assert.AreEqual (tb1.Constraints [i].GetType (), dt.Constraints [i].GetType (), "#10 Constraint : {0} is Different", tb1.Constraints [i]);
120 //Test for Table Data
121 Assert.AreEqual (tb1.Rows.Count, dt.Rows.Count, "#11 RowCount propertyis Different");
123 for (int i = 0; i < tb1.Rows.Count; i++) {
124 Assert.AreEqual (tb1.Rows [i].RowState, dt.Rows [i].RowState, "#12 RowState is Different");
127 for (int i = 0; i < tb1.Rows.Count; i++)
128 for (int j = 0; j < tb1.Columns.Count; j++) {
129 if (tb1.Rows[i].RowState != DataRowState.Deleted)
130 Assert.AreEqual (tb1.Rows [i][j], dt.Rows [i][j], "#13 Elements differ at Row :{0} Column :{1}", i, j);
132 dt.Rows[0].RejectChanges();
133 dt.Rows[1].RejectChanges();
136 public void DataTableSerializationTest2 ()
139 DataTable tb1 = new DataTable ("Test");
140 tb1.Columns.Add ("id", typeof (int));
141 tb1.Columns.Add ("name", typeof (string));
142 tb1.Rows.Add (new object[] {1, "A"});
143 tb1.Rows.Add (new object[] {2, "B"});
145 UniqueConstraint uniqueConstraint = new UniqueConstraint (tb1.Columns ["id"]);
146 tb1.Constraints.Add (uniqueConstraint);
147 tb1.RemotingFormat = SerializationFormat.Binary;
151 tb1.Rows[1].Delete();
153 BinaryFormatter bf = new BinaryFormatter ();
154 FileStream fs = new FileStream ("Test/System.Data/binserialize/BS-tb1.bin", FileMode.Open, FileAccess.Read);
155 BinaryReader r = new BinaryReader (fs);
156 byte [] serializedStream = r.ReadBytes ((int)fs.Length);
160 MemoryStream ms = new MemoryStream (serializedStream);
161 DataTable dt = (DataTable)bf.Deserialize (ms);
164 //Test Properties of table
165 //Assert.AreEqual (tb1.RemotingFormat, dt.RemotingFormat, "#1 RemotingFormat property is Different");
166 Assert.AreEqual (tb1.Columns.Count, dt.Columns.Count, "#2 ColumnCount property is Different");
167 Assert.AreEqual (tb1.TableName, dt.TableName, "#3 TableName property is Different");
168 Assert.AreEqual (tb1.Prefix, dt.Prefix, "#4 Prefix propertyis Different");
169 Assert.AreEqual (tb1.Namespace, dt.Namespace, "#5 NameSpace property is Different");
170 Assert.AreEqual (tb1.CaseSensitive, dt.CaseSensitive, "#6 CaseSensitive property is Different");
171 Assert.AreEqual (tb1.Locale.LCID, dt.Locale.LCID, "#7 LocaleLCID property is Different");
172 Assert.AreEqual (tb1.MinimumCapacity, dt.MinimumCapacity, "#8 NameSpace property is Different");
174 Assert.AreEqual (tb1.Constraints.Count, dt.Constraints.Count, "#9 No. of Constraints is Different");
175 for (int i = 0; i < tb1.Constraints.Count; i++)
176 Assert.AreEqual (tb1.Constraints [i].GetType (), dt.Constraints [i].GetType (), "#10 Constraint : {0} is Different", tb1.Constraints [i]);
177 //Test for Table Data
178 Assert.AreEqual (tb1.Rows.Count, dt.Rows.Count, "#11 RowCount propertyis Different");
180 for (int i = 0; i < tb1.Rows.Count; i++) {
181 Assert.AreEqual (tb1.Rows [i].RowState, dt.Rows [i].RowState, "#12 RowState is Different");
184 for (int i = 0; i < tb1.Rows.Count; i++)
185 for (int j = 0; j < tb1.Columns.Count; j++) {
186 if (tb1.Rows[i].RowState != DataRowState.Deleted)
187 Assert.AreEqual (tb1.Rows [i][j], dt.Rows [i][j], "#13 Elements differ at Row :{0} Column :{1}", i, j);
189 dt.Rows[0].RejectChanges();
190 dt.Rows[1].RejectChanges();
194 public void TestDefaultValues ()
197 DataTable tb1 = new DataTable ();
198 tb1.Columns.Add ("id", typeof (int));
199 tb1.Columns.Add ("Date", typeof (string));
200 tb1.Columns["id"].DefaultValue = 10;
201 tb1.Columns["Date"].DefaultValue = "9/15/2008";
202 tb1.Rows.Add (tb1.NewRow());
204 MemoryStream ms = new MemoryStream ();
205 BinaryFormatter bf = new BinaryFormatter ();
206 tb1.RemotingFormat = SerializationFormat.Binary;
207 bf.Serialize (ms,tb1);
208 byte [] serializedStream = ms.ToArray ();
211 ms = new MemoryStream (serializedStream);
212 DataTable dt = (DataTable)bf.Deserialize (ms);
216 for (int i = 0; i < tb1.Rows.Count; i++)
217 for (int j = 0; j < tb1.Columns.Count; j++) {
218 Assert.AreEqual (tb1.Columns[j].DefaultValue, dt.Rows [i][j], "#1 Element differs from DefaultValue at Row :{0} Column :{1}", i, j);
219 Assert.AreEqual (tb1.Rows [i][j], dt.Rows [i][j], "#2 Elements differ at Row :{0} Column :{1}", i, j);
224 public void TestEmptyTable ()
227 DataTable tb1 = new DataTable ();
228 tb1.Columns.Add ("id", typeof (int));
229 tb1.Columns.Add ("Date", typeof (string));
231 MemoryStream ms = new MemoryStream ();
232 BinaryFormatter bf = new BinaryFormatter ();
233 tb1.RemotingFormat = SerializationFormat.Binary;
234 bf.Serialize (ms,tb1);
235 byte [] serializedStream = ms.ToArray ();
238 ms = new MemoryStream (serializedStream);
239 DataTable dt = (DataTable)bf.Deserialize (ms);
242 Assert.AreEqual(tb1.Rows.Count, dt.Rows.Count);
246 public void Test_With_Null_Values1 ()
249 DataTable tb1 = new DataTable ();
250 tb1.Columns.Add ("id", typeof (int));
251 tb1.Columns.Add ("Date", typeof (string));
252 tb1.Rows.Add (new object[] {1, "A"});
253 tb1.Rows.Add (new object[] {2, null});
254 tb1.Rows.Add (new object[] {null, "B"});
255 tb1.Rows.Add (new object[] {null, null});
257 MemoryStream ms = new MemoryStream ();
258 BinaryFormatter bf = new BinaryFormatter ();
259 tb1.RemotingFormat = SerializationFormat.Binary;
260 bf.Serialize (ms,tb1);
261 byte [] serializedStream = ms.ToArray ();
264 ms = new MemoryStream (serializedStream);
265 DataTable dt = (DataTable)bf.Deserialize (ms);
268 for (int i = 0; i < tb1.Rows.Count; i++)
269 for (int j = 0; j < tb1.Columns.Count; j++) {
270 Assert.AreEqual (tb1.Rows [i][j], dt.Rows [i][j], "#1 Elements differ at Row :{0} Column :{1}", i, j);
275 public void Test_With_Null_Values2 ()
278 DataTable tb1 = new DataTable ();
279 tb1.Columns.Add ("id", typeof (int));
280 tb1.Columns.Add ("Date", typeof (string));
281 tb1.Rows.Add (new object[] {1, "A"});
282 tb1.Rows.Add (new object[] {2, null});
283 tb1.Rows.Add (new object[] {null, "B"});
284 tb1.Rows.Add (new object[] {null, null});
286 BinaryFormatter bf = new BinaryFormatter ();
287 tb1.RemotingFormat = SerializationFormat.Binary;
288 FileStream fs = new FileStream ("Test/System.Data/binserialize/BS-tb2.bin", FileMode.Open, FileAccess.Read);
289 BinaryReader r = new BinaryReader (fs);
290 byte [] serializedStream = r.ReadBytes ((int)fs.Length);
294 MemoryStream ms = new MemoryStream (serializedStream);
295 DataTable dt = (DataTable)bf.Deserialize (ms);
298 for (int i = 0; i < tb1.Rows.Count; i++)
299 for (int j = 0; j < tb1.Columns.Count; j++) {
300 Assert.AreEqual (tb1.Rows [i][j], dt.Rows [i][j], "#1 Elements differ at Row :{0} Column :{1}", i, j);
305 public void Test_With_DateTime_Values1 ()
308 DataTable tb1 = new DataTable ();
309 DateTime dateTime = DateTime.UtcNow;
310 tb1.Columns.Add ("id", typeof (int));
311 tb1.Columns.Add ("Date", typeof (DateTime));
312 tb1.Rows.Add (new object[] {1, "12-09-07"});
313 tb1.Rows.Add (new object[] {2, "12-09-06"});
314 //tb1.Rows.Add (new object[] {3, dateTime});
315 MemoryStream ms = new MemoryStream ();
316 BinaryFormatter bf = new BinaryFormatter ();
317 tb1.RemotingFormat = SerializationFormat.Binary;
318 bf.Serialize (ms,tb1);
319 byte [] serializedStream = ms.ToArray ();
322 ms = new MemoryStream (serializedStream);
323 DataTable dt = (DataTable)bf.Deserialize (ms);
326 for (int i = 0; i < tb1.Rows.Count; i++)
327 for (int j = 0; j < tb1.Columns.Count; j++) {
328 Assert.AreEqual (tb1.Rows [i][j], dt.Rows [i][j], "#1 Elements differ at Row :{0} Column :{1}", i, j);
332 [Category ("NotWorking")]
333 public void Test_With_DateTime_Values2 ()
336 DataTable tb1 = new DataTable ();
337 DateTime dateTime = DateTime.UtcNow;
338 tb1.Columns.Add ("id", typeof (int));
339 tb1.Columns.Add ("Date", typeof (DateTime));
340 tb1.Rows.Add (new object[] {1, "12-09-07"});
341 tb1.Rows.Add (new object[] {2, "12-09-06"});
342 //tb1.Rows.Add (new object[] {3, dateTime});
343 BinaryFormatter bf = new BinaryFormatter ();
344 tb1.RemotingFormat = SerializationFormat.Binary;
345 FileStream fs = new FileStream ("Test/System.Data/binserialize/BS-tb3.bin", FileMode.Open, FileAccess.Read);
346 BinaryReader r = new BinaryReader (fs);
347 byte [] serializedStream = r.ReadBytes ((int) fs.Length);
351 MemoryStream ms = new MemoryStream (serializedStream);
352 DataTable dt = (DataTable)bf.Deserialize (ms);
355 for (int i = 0; i < tb1.Rows.Count; i++)
356 for (int j = 0; j < tb1.Columns.Count; j++) {
357 Assert.AreEqual (tb1.Rows [i][j], dt.Rows [i][j], "#1 Elements differ at Row :{0} Column :{1}", i, j);
361 public void DataSetSerializationTest1 ()
363 DataSet ds = new DataSet ();
365 DataTable tb1 = new DataTable ();
366 tb1.Columns.Add ("id", typeof (int));
367 tb1.Columns.Add ("name", typeof (string));
368 tb1.Rows.Add (new object[] {1, "A"});
369 tb1.Rows.Add (new object[] {2, "B"});
372 DataTable tb2 = new DataTable ();
373 tb2.Columns.Add ("RollNO", typeof (int));
374 tb2.Columns.Add ("Name", typeof (string));
375 tb2.Rows.Add (new object[] {1, "A"});
376 tb2.Rows.Add (new object[] {2, "B"});
378 //Constraints and relations
379 ForeignKeyConstraint fKey = new ForeignKeyConstraint (tb2.Columns ["RollNO"],
381 tb1.Constraints.Add (fKey);
382 DataRelation rel = new DataRelation ("Relation1", tb1.Columns ["name"],
383 tb2.Columns ["Name"]);
384 ds.Relations.Add (rel);
386 MemoryStream ms = new MemoryStream ();
387 BinaryFormatter bf = new BinaryFormatter ();
388 ds.RemotingFormat = SerializationFormat.Binary;
389 bf.Serialize (ms,ds);
390 byte [] serializedStream = ms.ToArray ();
393 ms = new MemoryStream (serializedStream);
394 DataSet ds1 = (DataSet)bf.Deserialize (ms);
396 //Test DataSet Properties
397 //Assert.AreEqual (ds.RemotingFormat, ds1.RemotingFormat, "#1 RemotingFormat is different");
398 Assert.AreEqual (ds.DataSetName, ds1.DataSetName, "#2 DataSetName is different");
399 Assert.AreEqual (ds.Namespace, ds1.Namespace, "#3 Namespace is different");
400 Assert.AreEqual (ds.Prefix, ds1.Prefix, "#4 Prefix is different");
401 Assert.AreEqual (ds.CaseSensitive, ds1.CaseSensitive, "#5 CaseSensitive property value is different");
402 Assert.AreEqual (ds.Locale.LCID, ds1.Locale.LCID, "#6 DataSet LocaleLCID is different");
403 Assert.AreEqual (ds.EnforceConstraints, ds1.EnforceConstraints, "#7 EnforceConstraints property value is different");
404 Assert.AreEqual (ds.Tables.Count, ds1.Tables.Count, "#7 Table Count is different");
406 //Test Constraints & relations
409 Assert.AreEqual (ds.Tables [0].Constraints.Count, ds1.Tables [0].Constraints.Count, "#8 Number of constraint is different for Table :{0}", ds.Tables [0].TableName);
411 for (int i = 0; i < ds.Tables [0].Constraints.Count; i++)
412 Assert.AreEqual (ds.Tables [0].Constraints [i].GetType (),
413 ds1.Tables [0].Constraints [i].GetType (),
414 "#9 Constraint : {0} is Different", ds.Tables [0].Constraints [i]);
417 Assert.AreEqual (ds.Tables [1].Constraints.Count, ds1.Tables [1].Constraints.Count, "#10 Number of constraint is different for Table :{0}", ds.Tables [1].TableName);
419 for (int i = 0; i < ds.Tables [1].Constraints.Count; i++)
420 Assert.AreEqual (ds.Tables [1].Constraints [i].GetType (),
421 ds1.Tables [1].Constraints [i].GetType (),
422 "#11 Constraint : {0} is Different", ds.Tables [1].Constraints [i]);
424 Assert.AreEqual (ds.Relations.Count, ds1.Relations.Count, "#12 Relation count is different");
425 for (int i = 0; i < ds.Relations.Count; i++)
426 Assert.AreEqual (ds.Relations [i].RelationName, ds1.Relations [i].RelationName, "#13 Relation Name is different for relation :{0}", ds.Relations [i].RelationName);
428 for (int i = 0; i < ds.Relations.Count; i++)
429 Assert.AreEqual (ds.Relations [i].ParentTable.TableName,
430 ds1.Relations[i].ParentTable.TableName, "#14 Relation Name is different for relation :{0}", ds.Relations [i].ParentTable.TableName);
432 for (int i = 0; i < ds.Relations.Count; i++)
433 Assert.AreEqual (ds.Relations [i].ChildTable.TableName,
434 ds1.Relations[i].ChildTable.TableName, "#15 Relation Name is different for relation :{0}", ds.Relations [i].ChildTable.TableName);
438 for (int i = 0; i < ds.Tables [0].Rows.Count; i++)
439 for (int j = 0; j < ds.Tables [0].Columns.Count; j++) {
440 Assert.AreEqual (ds.Tables [0].Rows [i][j], ds1.Tables [0].Rows [i][j],
441 "#16 Elements differ at Row :{0} Column :{1}", i, j);
444 for (int i = 0; i < ds.Tables [0].Rows.Count; i++)
445 for (int j = 0; j < ds.Tables [1].Columns.Count; j++) {
446 Assert.AreEqual (ds.Tables [1].Rows [i][j], ds1.Tables [1].Rows [i][j],
447 "#17 Elements differ at Row :{0} Column :{1}", i, j);
452 public void DataSetSerializationTest2 ()
454 DataSet ds = new DataSet ();
456 DataTable tb1 = new DataTable ();
457 tb1.Columns.Add ("id", typeof (int));
458 tb1.Columns.Add ("name", typeof (string));
459 tb1.Rows.Add (new object[] {1, "A"});
460 tb1.Rows.Add (new object[] {2, "B"});
463 DataTable tb2 = new DataTable ();
464 tb2.Columns.Add ("RollNO", typeof (int));
465 tb2.Columns.Add ("Name", typeof (string));
466 tb2.Rows.Add (new object[] {1, "A"});
467 tb2.Rows.Add (new object[] {2, "B"});
469 //Constraints and relations
470 ForeignKeyConstraint fKey = new ForeignKeyConstraint (tb2.Columns ["RollNO"],
472 tb1.Constraints.Add (fKey);
473 DataRelation rel = new DataRelation ("Relation1", tb1.Columns ["name"],
474 tb2.Columns ["Name"]);
475 ds.Relations.Add (rel);
477 BinaryFormatter bf = new BinaryFormatter ();
478 ds.RemotingFormat = SerializationFormat.Binary;
479 FileStream fs = new FileStream ("Test/System.Data/binserialize/BS-tb4.bin", FileMode.Open, FileAccess.Read);
480 BinaryReader r = new BinaryReader (fs);
481 byte [] serializedStream = r.ReadBytes ((int) fs.Length);
485 MemoryStream ms = new MemoryStream (serializedStream);
486 DataSet ds1 = (DataSet)bf.Deserialize (ms);
488 //Test DataSet Properties
489 //Assert.AreEqual (ds.RemotingFormat, ds1.RemotingFormat, "#1 RemotingFormat is different");
490 Assert.AreEqual (ds.DataSetName, ds1.DataSetName, "#2 DataSetName is different");
491 Assert.AreEqual (ds.Namespace, ds1.Namespace, "#3 Namespace is different");
492 Assert.AreEqual (ds.Prefix, ds1.Prefix, "#4 Prefix is different");
493 Assert.AreEqual (ds.CaseSensitive, ds1.CaseSensitive, "#5 CaseSensitive property value is different");
494 Assert.AreEqual (ds.Locale.LCID, ds1.Locale.LCID, "#6 DataSet LocaleLCID is different");
495 Assert.AreEqual (ds.EnforceConstraints, ds1.EnforceConstraints, "#7 EnforceConstraints property value is different");
496 Assert.AreEqual (ds.Tables.Count, ds1.Tables.Count, "#7 Table Count is different");
498 //Test Constraints & relations
501 Assert.AreEqual (ds.Tables [0].Constraints.Count, ds1.Tables [0].Constraints.Count, "#8 Number of constraint is different for Table :{0}", ds.Tables [0].TableName);
503 for (int i = 0; i < ds.Tables [0].Constraints.Count; i++)
504 Assert.AreEqual (ds.Tables [0].Constraints [i].GetType (),
505 ds1.Tables [0].Constraints [i].GetType (),
506 "#9 Constraint : {0} is Different", ds.Tables [0].Constraints [i]);
509 Assert.AreEqual (ds.Tables [1].Constraints.Count, ds1.Tables [1].Constraints.Count, "#10 Number of constraint is different for Table :{0}", ds.Tables [1].TableName);
511 for (int i = 0; i < ds.Tables [1].Constraints.Count; i++)
512 Assert.AreEqual (ds.Tables [1].Constraints [i].GetType (),
513 ds1.Tables [1].Constraints [i].GetType (),
514 "#11 Constraint : {0} is Different", ds.Tables [1].Constraints [i]);
516 Assert.AreEqual (ds.Relations.Count, ds1.Relations.Count, "#12 Relation count is different");
517 for (int i = 0; i < ds.Relations.Count; i++)
518 Assert.AreEqual (ds.Relations [i].RelationName, ds1.Relations [i].RelationName, "#13 Relation Name is different for relation :{0}", ds.Relations [i].RelationName);
520 for (int i = 0; i < ds.Relations.Count; i++)
521 Assert.AreEqual (ds.Relations [i].ParentTable.TableName,
522 ds1.Relations[i].ParentTable.TableName, "#14 Relation Name is different for relation :{0}", ds.Relations [i].ParentTable.TableName);
524 for (int i = 0; i < ds.Relations.Count; i++)
525 Assert.AreEqual (ds.Relations [i].ChildTable.TableName,
526 ds1.Relations[i].ChildTable.TableName, "#15 Relation Name is different for relation :{0}", ds.Relations [i].ChildTable.TableName);
530 for (int i = 0; i < ds.Tables [0].Rows.Count; i++)
531 for (int j = 0; j < ds.Tables [0].Columns.Count; j++) {
532 Assert.AreEqual (ds.Tables [0].Rows [i][j], ds1.Tables [0].Rows [i][j],
533 "#16 Elements differ at Row :{0} Column :{1}", i, j);
536 for (int i = 0; i < ds.Tables [0].Rows.Count; i++)
537 for (int j = 0; j < ds.Tables [1].Columns.Count; j++) {
538 Assert.AreEqual (ds.Tables [1].Rows [i][j], ds1.Tables [1].Rows [i][j],
539 "#17 Elements differ at Row :{0} Column :{1}", i, j);
544 public void Constraint_Relations_Test1 ()
547 DataSet ds = new DataSet ();
549 DataTable tb1 = new DataTable ();
550 tb1.Columns.Add ("id", typeof (int));
551 tb1.Columns.Add ("name", typeof (string));
552 tb1.Rows.Add (new object[] {1, "A"});
553 tb1.Rows.Add (new object[] {2, "B"});
556 DataTable tb2 = new DataTable ();
557 tb2.Columns.Add ("eid", typeof (int));
558 tb2.Columns.Add ("SSN", typeof (int));
559 tb2.Columns.Add ("DOJ", typeof (DateTime));
560 tb2.Rows.Add (new object[] {1, 111, "07-25-06"});
561 tb2.Rows.Add (new object[] {2, 112, "07-19-06"});
562 tb2.Rows.Add (new object[] {3, 113, "07-22-06"});
565 DataTable tb3 = new DataTable ();
566 tb3.Columns.Add ("eid", typeof (int));
567 tb3.Columns.Add ("Salary", typeof (long));
568 tb3.Rows.Add (new object[] {1, 20000});
569 tb3.Rows.Add (new object[] {2, 30000});
572 DataTable tb4 = new DataTable ();
573 tb4.Columns.Add ("ssn", typeof (int));
574 tb4.Columns.Add ("Name", typeof (string));
575 tb4.Columns.Add ("DOB", typeof (DateTime));
576 tb4.Rows.Add (new object[] {112, "A", "09-12-81"});
577 tb4.Rows.Add (new object[] {113, "B", "09-12-82"});
581 UniqueConstraint uKey = new UniqueConstraint (tb1.Columns ["id"]);
583 DataColumn[] parentColumns = new DataColumn[2];
584 parentColumns[0] = tb1.Columns["id"];
585 parentColumns[1] = tb1.Columns["name"];
586 DataColumn[] childColumns = new DataColumn[2];
587 childColumns[0] = tb4.Columns["ssn"];
588 childColumns[1] = tb4.Columns["Name"];
589 ForeignKeyConstraint fKey1 = new ForeignKeyConstraint(childColumns,
592 ForeignKeyConstraint fKey1 = new ForeignKeyConstraint (tb2.Columns ["eid"],
594 ForeignKeyConstraint fKey2 = new ForeignKeyConstraint (tb2.Columns ["eid"],
595 tb3.Columns ["eid"]);
596 DataRelation rel = new DataRelation ("Relation1", tb1.Columns ["name"], tb4.Columns ["Name"]);
597 DataRelation rel1 = new DataRelation ("Relation2", tb2.Columns ["SSN"], tb4.Columns ["ssn"]);
598 tb1.Constraints.Add (uKey);
599 tb1.Constraints.Add (fKey1);
600 tb3.Constraints.Add (fKey2);
601 ds.Relations.Add (rel);
602 ds.Relations.Add (rel1);
606 MemoryStream ms = new MemoryStream ();
607 BinaryFormatter bf = new BinaryFormatter ();
608 ds.RemotingFormat = SerializationFormat.Binary;
609 bf.Serialize (ms,ds);
610 byte [] serializedStream = ms.ToArray ();
613 ms = new MemoryStream (serializedStream);
614 DataSet ds1 = (DataSet)bf.Deserialize (ms);
616 Assert.AreEqual (ds.Tables.Count, ds1.Tables.Count, "#1 Number of Table differs");
619 Assert.AreEqual (ds.Tables [0].Constraints.Count, ds1.Tables [0].Constraints.Count, "#2 Number of Constraints differs in Table :{0}", ds.Tables [0]);
620 for (int i = 0; i < ds.Tables [0].Constraints.Count; i++)
621 Assert.AreEqual (ds.Tables [0].Constraints [i].GetType (),
622 ds1.Tables [0].Constraints [i].GetType (),
623 "#3 Constraint : {0} is Different", ds.Tables [0].Constraints [i]);
625 Assert.AreEqual (ds.Tables [1].Constraints.Count, ds1.Tables [1].Constraints.Count, "#4 Number of Constraints differs in Table :{0}", ds.Tables [1]);
626 for (int i = 0; i < ds.Tables [1].Constraints.Count; i++)
627 Assert.AreEqual (ds.Tables [1].Constraints [i].GetType (),
628 ds1.Tables [1].Constraints [i].GetType (),
629 "#5 Constraint : {0} is Different", ds.Tables [1].Constraints [i]);
631 Assert.AreEqual (ds.Tables [2].Constraints.Count, ds1.Tables [2].Constraints.Count, "#5 Number of Constraints differs in Table :{0}", ds.Tables [2]);
632 for (int i = 0; i < ds.Tables [2].Constraints.Count; i++)
633 Assert.AreEqual (ds.Tables [2].Constraints [i].GetType (),
634 ds1.Tables [2].Constraints [i].GetType (),
635 "#6 Constraint : {0} is Different", ds.Tables [2].Constraints [i]);
637 Assert.AreEqual (ds.Tables [3].Constraints.Count, ds1.Tables [3].Constraints.Count, "#7 Number of Constraints differs in Table :{0}", ds.Tables [3]);
638 for (int i = 0; i < ds.Tables [3].Constraints.Count; i++)
639 Assert.AreEqual (ds.Tables [3].Constraints [i].GetType (),
640 ds1.Tables [3].Constraints [i].GetType (),
641 "#8 Constraint : {0} is Different", ds.Tables [3].Constraints [i]);
643 Assert.AreEqual (ds.Relations.Count, ds1.Relations.Count, "#8 Number of realtions differ");
644 for (int i = 0; i < ds.Relations.Count; i++)
645 Assert.AreEqual (ds.Relations [i].RelationName, ds.Relations [i].RelationName, "#9 Relation : {0} differs", ds.Relations [i]);
648 public void Constraint_Relations_Test2 ()
651 DataSet ds = new DataSet ();
653 DataTable tb1 = new DataTable ();
654 tb1.Columns.Add ("id", typeof (int));
655 tb1.Columns.Add ("name", typeof (string));
656 tb1.Rows.Add (new object[] {1, "A"});
657 tb1.Rows.Add (new object[] {2, "B"});
660 DataTable tb2 = new DataTable ();
661 tb2.Columns.Add ("eid", typeof (int));
662 tb2.Columns.Add ("SSN", typeof (int));
663 tb2.Columns.Add ("DOJ", typeof (DateTime));
664 tb2.Rows.Add (new object[] {1, 111, "07-25-06"});
665 tb2.Rows.Add (new object[] {2, 112, "07-19-06"});
666 tb2.Rows.Add (new object[] {3, 113, "07-22-06"});
669 DataTable tb3 = new DataTable ();
670 tb3.Columns.Add ("eid", typeof (int));
671 tb3.Columns.Add ("Salary", typeof (long));
672 tb3.Rows.Add (new object[] {1, 20000});
673 tb3.Rows.Add (new object[] {2, 30000});
676 DataTable tb4 = new DataTable ();
677 tb4.Columns.Add ("ssn", typeof (int));
678 tb4.Columns.Add ("Name", typeof (string));
679 tb4.Columns.Add ("DOB", typeof (DateTime));
680 tb4.Rows.Add (new object[] {112, "A", "09-12-81"});
681 tb4.Rows.Add (new object[] {113, "B", "09-12-82"});
685 UniqueConstraint uKey = new UniqueConstraint (tb1.Columns ["id"]);
687 DataColumn[] parentColumns = new DataColumn[2];
688 parentColumns[0] = tb1.Columns["id"];
689 parentColumns[1] = tb1.Columns["name"];
690 DataColumn[] childColumns = new DataColumn[2];
691 childColumns[0] = tb4.Columns["ssn"];
692 childColumns[1] = tb4.Columns["Name"];
693 ForeignKeyConstraint fKey1 = new ForeignKeyConstraint(childColumns,
696 ForeignKeyConstraint fKey1 = new ForeignKeyConstraint (tb2.Columns ["eid"],
698 ForeignKeyConstraint fKey2 = new ForeignKeyConstraint (tb2.Columns ["eid"],
699 tb3.Columns ["eid"]);
700 DataRelation rel = new DataRelation ("Relation1", tb1.Columns ["name"], tb4.Columns ["Name"]);
701 DataRelation rel1 = new DataRelation ("Relation2", tb2.Columns ["SSN"], tb4.Columns ["ssn"]);
702 tb1.Constraints.Add (uKey);
703 tb1.Constraints.Add (fKey1);
704 tb3.Constraints.Add (fKey2);
705 ds.Relations.Add (rel);
706 ds.Relations.Add (rel1);
710 BinaryFormatter bf = new BinaryFormatter ();
711 ds.RemotingFormat = SerializationFormat.Binary;
712 FileStream fs = new FileStream ("Test/System.Data/binserialize/BS-tb5.bin", FileMode.Open, FileAccess.Read);
713 BinaryReader r = new BinaryReader (fs);
714 byte [] serializedStream = r.ReadBytes ((int)fs.Length);
718 MemoryStream ms = new MemoryStream (serializedStream);
719 DataSet ds1 = (DataSet)bf.Deserialize (ms);
721 Assert.AreEqual (ds.Tables.Count, ds1.Tables.Count, "#1 Number of Table differs");
724 Assert.AreEqual (ds.Tables [0].Constraints.Count, ds1.Tables [0].Constraints.Count, "#2 Number of Constraints differs in Table :{0}", ds.Tables [0]);
725 for (int i = 0; i < ds.Tables [0].Constraints.Count; i++)
726 Assert.AreEqual (ds.Tables [0].Constraints [i].GetType (),
727 ds1.Tables [0].Constraints [i].GetType (),
728 "#3 Constraint : {0} is Different", ds.Tables [0].Constraints [i]);
730 Assert.AreEqual (ds.Tables [1].Constraints.Count, ds1.Tables [1].Constraints.Count, "#4 Number of Constraints differs in Table :{0}", ds.Tables [1]);
731 for (int i = 0; i < ds.Tables [1].Constraints.Count; i++)
732 Assert.AreEqual (ds.Tables [1].Constraints [i].GetType (),
733 ds1.Tables [1].Constraints [i].GetType (),
734 "#5 Constraint : {0} is Different", ds.Tables [1].Constraints [i]);
736 Assert.AreEqual (ds.Tables [2].Constraints.Count, ds1.Tables [2].Constraints.Count, "#5 Number of Constraints differs in Table :{0}", ds.Tables [2]);
737 for (int i = 0; i < ds.Tables [2].Constraints.Count; i++)
738 Assert.AreEqual (ds.Tables [2].Constraints [i].GetType (),
739 ds1.Tables [2].Constraints [i].GetType (),
740 "#6 Constraint : {0} is Different", ds.Tables [2].Constraints [i]);
742 Assert.AreEqual (ds.Tables [3].Constraints.Count, ds1.Tables [3].Constraints.Count, "#7 Number of Constraints differs in Table :{0}", ds.Tables [3]);
743 for (int i = 0; i < ds.Tables [3].Constraints.Count; i++)
744 Assert.AreEqual (ds.Tables [3].Constraints [i].GetType (),
745 ds1.Tables [3].Constraints [i].GetType (),
746 "#8 Constraint : {0} is Different", ds.Tables [3].Constraints [i]);
748 Assert.AreEqual (ds.Relations.Count, ds1.Relations.Count, "#8 Number of realtions differ");
749 for (int i = 0; i < ds.Relations.Count; i++)
750 Assert.AreEqual (ds.Relations [i].RelationName, ds.Relations [i].RelationName, "#9 Relation : {0} differs", ds.Relations [i]);