1 // DataTableTest.cs - NUnit Test Cases for testing the DataTable
4 // Franklin Wise (gracenote@earthlink.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Hagit Yidov (hagity@mainsoft.com)
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using NUnit.Framework;
38 using System.Data.SqlTypes;
39 using System.Globalization;
41 using System.Runtime.Serialization.Formatters.Binary;
43 using MonoTests.System.Data.Utils;
44 using System.Collections;
46 namespace MonoTests.System.Data
49 public class DataTableTest : DataSetAssertion
51 string EOL = Environment.NewLine;
56 DataTable dt = new DataTable();
58 AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
59 Assert("Col",dt.Columns != null);
60 //Assert(dt.ChildRelations != null);
61 Assert("Const", dt.Constraints != null);
62 Assert("ds", dt.DataSet == null);
63 Assert("dv", dt.DefaultView != null);
64 Assert("de", dt.DisplayExpression == "");
65 Assert("ep", dt.ExtendedProperties != null);
66 Assert("he", dt.HasErrors == false);
67 Assert("lc", dt.Locale != null);
68 Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
69 Assert("ns", dt.Namespace == "");
70 //Assert(dt.ParentRelations != null);
71 Assert("pf", dt.Prefix == "");
72 Assert("pk", dt.PrimaryKey != null);
73 Assert("rows", dt.Rows != null);
74 Assert("Site", dt.Site == null);
75 Assert("tname", dt.TableName == "");
82 DataSet Set = new DataSet ();
83 DataTable Mom = new DataTable ("Mom");
84 DataTable Child = new DataTable ("Child");
86 Set.Tables.Add (Child);
88 DataColumn Col = new DataColumn ("Name");
89 DataColumn Col2 = new DataColumn ("ChildName");
90 Mom.Columns.Add (Col);
91 Mom.Columns.Add (Col2);
93 DataColumn Col3 = new DataColumn ("Name");
94 DataColumn Col4 = new DataColumn ("Age");
95 Col4.DataType = Type.GetType ("System.Int16");
96 Child.Columns.Add (Col3);
97 Child.Columns.Add (Col4);
99 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
100 Set.Relations.Add (Relation);
102 DataRow Row = Mom.NewRow ();
128 Row [0] = "'Jhon O'' Collenal'";
132 Row = Child.NewRow ();
135 Child.Rows.Add (Row);
137 Row = Child.NewRow ();
140 Child.Rows.Add (Row);
142 Row = Child.NewRow ();
145 Child.Rows.Add (Row);
147 Row = Child.NewRow ();
150 Child.Rows.Add (Row);
152 Row = Child.NewRow ();
155 Child.Rows.Add (Row);
157 Row = Child.NewRow ();
160 Child.Rows.Add (Row);
162 Row = Child.NewRow ();
165 Child.Rows.Add (Row);
167 DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
168 AssertEquals ("test#01", 2, Rows.Length);
170 // test with apos escaped
171 Rows = Mom.Select ("Name = '''Jhon O'''' Collenal'''");
172 AssertEquals ("test#01.1", 1, Rows.Length);
174 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
175 AssertEquals ("test#02", 0, Rows.Length);
177 Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
178 AssertEquals ("test#03", 1, Rows.Length);
180 Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
181 AssertEquals ("test#04", "Mack", Rows [0] [1]);
183 Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
184 AssertEquals ("test#05", 6, Rows.Length);
186 Rows = Child.Select ("age = 20 - 1");
187 AssertEquals ("test#06", 1, Rows.Length);
189 Rows = Child.Select ("age <= 20");
190 AssertEquals ("test#07", 3, Rows.Length);
192 Rows = Child.Select ("age >= 20");
193 AssertEquals ("test#08", 4, Rows.Length);
195 Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
196 AssertEquals ("test#09", 2, Rows.Length);
198 Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
199 AssertEquals ("test#10", 1, Rows.Length);
200 AssertEquals ("test#11", "Mack", Rows [0] [0]);
202 Rows = Child.Select ("not (Name = 'Jack')");
203 AssertEquals ("test#12", 6, Rows.Length);
207 public void Select2 ()
209 DataSet Set = new DataSet ();
210 DataTable Child = new DataTable ("Child");
212 Set.Tables.Add (Child);
214 DataColumn Col3 = new DataColumn ("Name");
215 DataColumn Col4 = new DataColumn ("Age");
216 Col4.DataType = Type.GetType ("System.Int16");
217 Child.Columns.Add (Col3);
218 Child.Columns.Add (Col4);
220 DataRow Row = Child.NewRow ();
223 Child.Rows.Add (Row);
225 Row = Child.NewRow ();
228 Child.Rows.Add (Row);
230 Row = Child.NewRow ();
233 Child.Rows.Add (Row);
235 Row = Child.NewRow ();
238 Child.Rows.Add (Row);
240 Row = Child.NewRow ();
243 Child.Rows.Add (Row);
245 Row = Child.NewRow ();
248 Child.Rows.Add (Row);
250 DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
251 AssertEquals ("test#01", 3, Rows.Length);
252 AssertEquals ("test#02", "Mack", Rows [0] [0]);
253 AssertEquals ("test#03", "Mick", Rows [1] [0]);
254 AssertEquals ("test#04", "Dick", Rows [2] [0]);
256 Rows = Child.Select ("age >= 20", "age asc");
257 AssertEquals ("test#05", 3, Rows.Length);
258 AssertEquals ("test#06", "Dick", Rows [0] [0]);
259 AssertEquals ("test#07", "Mick", Rows [1] [0]);
260 AssertEquals ("test#08", "Mack", Rows [2] [0]);
262 Rows = Child.Select ("age >= 20", "name asc");
263 AssertEquals ("test#09", 3, Rows.Length);
264 AssertEquals ("test#10", "Dick", Rows [0] [0]);
265 AssertEquals ("test#11", "Mack", Rows [1] [0]);
266 AssertEquals ("test#12", "Mick", Rows [2] [0]);
268 Rows = Child.Select ("age >= 20", "name desc");
269 AssertEquals ("test#09", 3, Rows.Length);
270 AssertEquals ("test#10", "Mick", Rows [0] [0]);
271 AssertEquals ("test#11", "Mack", Rows [1] [0]);
272 AssertEquals ("test#12", "Dick", Rows [2] [0]);
277 public void SelectParsing ()
279 DataTable T = new DataTable ("test");
280 DataColumn C = new DataColumn ("name");
282 C = new DataColumn ("age");
283 C.DataType = typeof (int);
285 C = new DataColumn ("id");
288 DataSet Set = new DataSet ("TestSet");
292 for (int i = 0; i < 100; i++) {
294 Row [0] = "human" + i;
306 AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
308 AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
311 T.Select ("name = 1human ");
313 } catch (Exception e) {
315 // missing operand after 'human' operand
316 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
320 T.Select ("name = 1");
322 } catch (Exception e) {
324 // Cannot perform '=' operation between string and Int32
325 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
328 AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
333 public void SelectEscaping () {
334 DataTable dt = new DataTable ();
335 dt.Columns.Add ("SomeCol");
336 dt.Rows.Add (new object [] {"\t"});
337 dt.Rows.Add (new object [] {"\\"});
339 AssertEquals ("test#01", 1, dt.Select (@"SomeCol='\t'").Length);
340 AssertEquals ("test#02", 1, dt.Select (@"SomeCol='\\'").Length);
343 dt.Select (@"SomeCol='\x'");
345 } catch (SyntaxErrorException) {}
349 public void SelectOperators ()
351 DataTable T = new DataTable ("test");
352 DataColumn C = new DataColumn ("name");
354 C = new DataColumn ("age");
355 C.DataType = typeof (int);
357 C = new DataColumn ("id");
360 DataSet Set = new DataSet ("TestSet");
364 for (int i = 0; i < 100; i++) {
366 Row [0] = "human" + i;
378 AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
379 AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);
380 AssertEquals ("test#03", 12, T.Select ("age< =10").Length);
381 AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
382 AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);
383 AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
384 AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
385 AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
386 // FIXME: Somebody explain how this can be possible.
387 // it seems that it is no matter between 10 - 30. The
388 // result is allways 25 :-P
389 //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
394 public void SelectExceptions ()
396 DataTable T = new DataTable ("test");
397 DataColumn C = new DataColumn ("name");
399 C = new DataColumn ("age");
400 C.DataType = typeof (int);
402 C = new DataColumn ("id");
405 for (int i = 0; i < 100; i++) {
406 DataRow Row = T.NewRow ();
407 Row [0] = "human" + i;
414 T.Select ("name = human1");
416 } catch (Exception e) {
418 // column name human not found
419 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
422 AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
423 AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
426 T.Select ("id = 1k3");
428 } catch (Exception e) {
430 // no operands after k3 operator
431 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
436 public void SelectStringOperators ()
438 DataTable T = new DataTable ("test");
439 DataColumn C = new DataColumn ("name");
441 C = new DataColumn ("age");
442 C.DataType = typeof (int);
444 C = new DataColumn ("id");
447 DataSet Set = new DataSet ("TestSet");
451 for (int i = 0; i < 100; i++) {
453 Row [0] = "human" + i;
464 AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
466 AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);
467 AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
468 AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);
469 AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
470 AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
472 AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
474 Set.CaseSensitive = true;
475 AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
477 T.CaseSensitive = false;
478 AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
480 T.CaseSensitive = true;
481 AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
483 Set.CaseSensitive = false;
484 AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
486 T.CaseSensitive = false;
487 AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
489 AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
490 AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
491 AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
494 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
496 } catch (Exception e) {
498 // 'h*an1' is invalid
499 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
503 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
505 } catch (Exception e) {
507 // 'h%an1' is invalid
508 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
511 AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
512 AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
517 public void SelectAggregates ()
519 DataTable T = new DataTable ("test");
520 DataColumn C = new DataColumn ("name");
522 C = new DataColumn ("age");
523 C.DataType = typeof (int);
525 C = new DataColumn ("id");
529 for (int i = 0; i < 1000; i++) {
531 Row [0] = "human" + i;
537 AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
538 AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
539 AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
540 AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
541 AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
542 AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
543 AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
547 public void SelectFunctions ()
549 DataTable T = new DataTable ("test");
550 DataColumn C = new DataColumn ("name");
552 C = new DataColumn ("age");
553 C.DataType = typeof (int);
555 C = new DataColumn ("id");
559 for (int i = 0; i < 1000; i++) {
561 Row [0] = "human" + i;
568 Row [0] = "human" + "test";
569 Row [1] = DBNull.Value;
570 Row [2] = DBNull.Value;
573 //TODO: How to test Convert-function
574 AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);
575 AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
576 AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
577 AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);
578 AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);
579 AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
580 AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
581 AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
582 AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
583 AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
588 public void SelectRelations ()
590 DataSet Set = new DataSet ();
591 DataTable Mom = new DataTable ("Mom");
592 DataTable Child = new DataTable ("Child");
594 Set.Tables.Add (Mom);
595 Set.Tables.Add (Child);
597 DataColumn Col = new DataColumn ("Name");
598 DataColumn Col2 = new DataColumn ("ChildName");
599 Mom.Columns.Add (Col);
600 Mom.Columns.Add (Col2);
602 DataColumn Col3 = new DataColumn ("Name");
603 DataColumn Col4 = new DataColumn ("Age");
604 Col4.DataType = Type.GetType ("System.Int16");
605 Child.Columns.Add (Col3);
606 Child.Columns.Add (Col4);
608 DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
609 Set.Relations.Add (Relation);
611 DataRow Row = Mom.NewRow ();
636 Row = Child.NewRow ();
639 Child.Rows.Add (Row);
641 Row = Child.NewRow ();
644 Child.Rows.Add (Row);
646 Row = Child.NewRow ();
649 Child.Rows.Add (Row);
651 Row = Child.NewRow ();
654 Child.Rows.Add (Row);
656 Row = Child.NewRow ();
659 Child.Rows.Add (Row);
661 Row = Child.NewRow ();
664 Child.Rows.Add (Row);
666 DataRow [] Rows = Child.Select ("name = Parent.Childname");
667 AssertEquals ("test#01", 6, Rows.Length);
668 Rows = Child.Select ("Parent.childname = 'Jack'");
669 AssertEquals ("test#02", 1, Rows.Length);
673 // FIXME: LAMESPEC: Why the exception is thrown why... why...
674 Mom.Select ("Child.Name = 'Jack'");
676 } catch (Exception e) {
677 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
678 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
682 Rows = Child.Select ("Parent.name = 'Laura'");
683 AssertEquals ("test#06", 3, Rows.Length);
685 DataTable Parent2 = new DataTable ("Parent2");
686 Col = new DataColumn ("Name");
687 Col2 = new DataColumn ("ChildName");
689 Parent2.Columns.Add (Col);
690 Parent2.Columns.Add (Col2);
691 Set.Tables.Add (Parent2);
693 Row = Parent2.NewRow ();
696 Parent2.Rows.Add (Row);
698 Row = Parent2.NewRow ();
701 Parent2.Rows.Add (Row);
703 Row = Parent2.NewRow ();
706 Parent2.Rows.Add (Row);
708 Row = Parent2.NewRow ();
711 Parent2.Rows.Add (Row);
713 Row = Parent2.NewRow ();
716 Parent2.Rows.Add (Row);
718 Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
719 Set.Relations.Add (Relation);
722 Rows = Child.Select ("Parent.ChildName = 'Jack'");
724 } catch (Exception e) {
725 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
726 //AssertEquals ("test#09", "The table [Child] involved in more than one relation. You must explicitly mention a relation name in the expression 'parent.[ChildName]'.", e.Message);
729 Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
730 AssertEquals ("test#10", 1, Rows.Length);
732 Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
733 AssertEquals ("test#10", 1, Rows.Length);
736 Mom.Select ("Parent.name = 'John'");
737 } catch (Exception e) {
738 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
739 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
745 public void SelectRowState()
747 DataTable d = new DataTable();
748 d.Columns.Add (new DataColumn ("aaa"));
749 DataRow [] rows = d.Select (null, null, DataViewRowState.Deleted);
750 AssertEquals(0, rows.Length);
751 d.Rows.Add (new object [] {"bbb"});
752 d.Rows.Add (new object [] {"bbb"});
753 rows = d.Select (null, null, DataViewRowState.Deleted);
754 AssertEquals(0, rows.Length);
758 public void ToStringTest()
760 DataTable dt = new DataTable();
761 dt.Columns.Add("Col1",typeof(int));
763 dt.TableName = "Mytable";
764 dt.DisplayExpression = "Col1";
767 string cmpr = dt.TableName + " + " + dt.DisplayExpression;
768 AssertEquals(cmpr,dt.ToString());
772 public void PrimaryKey ()
774 DataTable dt = new DataTable ();
775 DataColumn Col = new DataColumn ();
776 Col.AllowDBNull = false;
777 Col.DataType = typeof (int);
778 dt.Columns.Add (Col);
783 AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
785 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
786 AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
787 AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
789 dt.PrimaryKey = null;
790 AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
792 Col = new DataColumn ("failed");
795 dt.PrimaryKey = new DataColumn [] {Col};
797 } catch (Exception e) {
798 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
799 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
802 DataTable dt2 = new DataTable ();
806 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
808 } catch (Exception e) {
809 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
810 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
814 AssertEquals ("test#11", 0, dt.Constraints.Count);
816 dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
817 AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
818 AssertEquals ("test#13", 1, dt.Constraints.Count);
819 AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
820 AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
821 AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
826 public void PropertyExceptions ()
828 DataSet set = new DataSet ();
829 DataTable table = new DataTable ();
830 DataTable table1 = new DataTable ();
831 set.Tables.Add (table);
832 set.Tables.Add (table1);
834 DataColumn col = new DataColumn ();
835 col.ColumnName = "Id";
836 col.DataType = Type.GetType ("System.Int32");
837 table.Columns.Add (col);
838 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
839 table.Constraints.Add (uc);
840 table.CaseSensitive = false;
842 col = new DataColumn ();
843 col.ColumnName = "Name";
844 col.DataType = Type.GetType ("System.String");
845 table.Columns.Add (col);
847 col = new DataColumn ();
848 col.ColumnName = "Id";
849 col.DataType = Type.GetType ("System.Int32");
850 table1.Columns.Add (col);
851 col = new DataColumn ();
852 col.ColumnName = "Name";
853 col.DataType = Type.GetType ("System.String");
854 table1.Columns.Add (col);
856 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
857 set.Relations.Add (dr);
860 table.CaseSensitive = true;
861 table1.CaseSensitive = true;
864 catch (Exception e) {
865 if (e.GetType () != typeof (AssertionException))
866 AssertEquals ("#A02", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
868 Console.WriteLine (e);
871 CultureInfo cultureInfo = new CultureInfo ("en-gb");
872 table.Locale = cultureInfo;
873 table1.Locale = cultureInfo;
876 catch (Exception e) {
877 if (e.GetType () != typeof (AssertionException))
878 AssertEquals ("#A04", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
880 Console.WriteLine (e);
883 table.Prefix = "Prefix#1";
887 if (e.GetType () != typeof (AssertionException))
888 AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
890 Console.WriteLine (e);
896 public void GetErrors ()
898 DataTable table = new DataTable ();
900 DataColumn col = new DataColumn ();
901 col.ColumnName = "Id";
902 col.DataType = Type.GetType ("System.Int32");
903 table.Columns.Add (col);
905 col = new DataColumn ();
906 col.ColumnName = "Name";
907 col.DataType = Type.GetType ("System.String");
908 table.Columns.Add (col);
910 DataRow row = table.NewRow ();
912 row ["name"] = "Abc";
913 row.RowError = "Error#1";
914 table.Rows.Add (row);
916 AssertEquals ("#A01", 1, table.GetErrors ().Length);
917 AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
920 public void CloneCopyTest ()
922 DataTable table = new DataTable ();
923 table.TableName = "Table#1";
924 DataTable table1 = new DataTable ();
925 table1.TableName = "Table#2";
927 table.AcceptChanges ();
929 DataSet set = new DataSet ("Data Set#1");
930 set.DataSetName = "Dataset#1";
931 set.Tables.Add (table);
932 set.Tables.Add (table1);
934 DataColumn col = new DataColumn ();
935 col.ColumnName = "Id";
936 col.DataType = Type.GetType ("System.Int32");
937 table.Columns.Add (col);
938 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
939 table.Constraints.Add (uc);
941 col = new DataColumn ();
942 col.ColumnName = "Id";
943 col.DataType = Type.GetType ("System.Int32");
944 table1.Columns.Add (col);
946 col = new DataColumn ();
947 col.ColumnName = "Name";
948 col.DataType = Type.GetType ("System.String");
949 table.Columns.Add (col);
951 col = new DataColumn ();
952 col.ColumnName = "Name";
953 col.DataType = Type.GetType ("System.String");
954 table1.Columns.Add (col);
955 DataRow row = table.NewRow ();
957 row ["name"] = "Abc";
958 row.RowError = "Error#1";
959 table.Rows.Add (row);
961 row = table.NewRow ();
963 row ["name"] = "Efg";
964 table.Rows.Add (row);
965 table.AcceptChanges ();
967 table.CaseSensitive = true;
968 table1.CaseSensitive = true;
969 table.MinimumCapacity = 100;
970 table.Prefix = "PrefixNo:1";
971 table.Namespace = "Namespace#1";
972 table.DisplayExpression = "Id / Name + (Id * Id)";
973 DataColumn[] colArray = {table.Columns[0]};
974 table.PrimaryKey = colArray;
975 table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
976 #if NET_1_1 // This prevents further tests after .NET 1.1.
978 CultureInfo cultureInfo = new CultureInfo ("en-gb");
979 table.Locale = cultureInfo;
982 row = table1.NewRow ();
983 row ["Name"] = "Abc";
985 table1.Rows.Add (row);
987 row = table1.NewRow ();
989 row ["Name"] = "Efg";
990 table1.Rows.Add (row);
992 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
993 set.Relations.Add (dr);
995 //Testing properties of clone
996 DataTable cloneTable = table.Clone ();
997 AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
998 AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
999 AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
1000 AssertEquals ("#A04", 2, cloneTable.Columns.Count);
1001 AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
1002 AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
1003 AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
1004 AssertEquals ("#A08", false ,cloneTable.HasErrors);
1007 AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
1009 AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
1010 AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
1011 AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
1012 AssertEquals ("#A13", "Id", cloneTable.PrimaryKey[0].ColumnName);
1013 AssertEquals ("#A14",0 , cloneTable.Rows.Count );
1014 AssertEquals ("#A15", "Table#1", cloneTable.TableName);
1016 //Testing properties of copy
1017 DataTable copyTable = table.Copy ();
1018 AssertEquals ("#A16",true ,copyTable.CaseSensitive);
1019 AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
1020 AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
1021 AssertEquals ("#A19", 2, copyTable.Columns.Count);
1022 AssertEquals ("#A20", 1, copyTable.Constraints.Count);
1023 AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
1024 AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
1025 AssertEquals ("#A23", true ,copyTable.HasErrors);
1028 AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
1030 AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
1031 AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
1032 AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
1033 AssertEquals ("#A28", "Id", copyTable.PrimaryKey[0].ColumnName);
1034 AssertEquals ("#A29", 2 , copyTable.Rows.Count );
1035 AssertEquals ("#A30", "Table#1", copyTable.TableName);
1039 public void LoadDataException ()
1041 DataTable table = new DataTable ();
1042 DataColumn col = new DataColumn ();
1043 col.ColumnName = "Id";
1044 col.DataType = Type.GetType ("System.Int32");
1045 col.DefaultValue = 47;
1046 table.Columns.Add (col);
1047 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1048 table.Constraints.Add (uc);
1050 col = new DataColumn ();
1051 col.ColumnName = "Name";
1052 col.DataType = Type.GetType ("System.String");
1053 col.DefaultValue = "Hello";
1054 table.Columns.Add (col);
1056 table.BeginLoadData();
1057 object[] row = {147, "Abc"};
1058 DataRow newRow = table.LoadDataRow (row, true);
1060 object[] row1 = {147, "Efg"};
1061 DataRow newRow1 = table.LoadDataRow (row1, true);
1063 object[] row2 = {143, "Hij"};
1064 DataRow newRow2 = table.LoadDataRow (row2, true);
1067 table.EndLoadData ();
1070 catch (ConstraintException) {
1074 public void Changes () //To test GetChanges and RejectChanges
1076 DataTable table = new DataTable ();
1078 DataColumn col = new DataColumn ();
1079 col.ColumnName = "Id";
1080 col.DataType = Type.GetType ("System.Int32");
1081 table.Columns.Add (col);
1082 UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1083 table.Constraints.Add (uc);
1085 col = new DataColumn ();
1086 col.ColumnName = "Name";
1087 col.DataType = Type.GetType ("System.String");
1088 table.Columns.Add (col);
1090 DataRow row = table.NewRow ();
1092 row ["name"] = "Abc";
1093 table.Rows.Add (row);
1094 table.AcceptChanges ();
1096 row = table.NewRow ();
1098 row ["name"] = "Efg";
1099 table.Rows.Add (row);
1101 //Testing GetChanges
1102 DataTable changesTable = table.GetChanges ();
1103 AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1104 AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);
1105 table.AcceptChanges ();
1106 changesTable = table.GetChanges ();
1108 int cnt = changesTable.Rows.Count;
1110 catch(Exception e) {
1111 if (e.GetType () != typeof (AssertionException))
1112 AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1114 Console.WriteLine (e);
1117 //Testing RejectChanges
1118 row = table.NewRow ();
1120 row ["name"] = "Hij";
1121 table.Rows.Add (row);
1123 (table.Rows [0])["Name"] = "AaBbCc";
1124 table.RejectChanges ();
1125 AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1126 AssertEquals ("#A04", 2, table.Rows.Count);
1130 public void ImportRowTest ()
1132 // build source table
1133 DataTable src = new DataTable ();
1134 src.Columns.Add ("id", typeof (int));
1135 src.Columns.Add ("name", typeof (string));
1137 src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
1139 src.Rows.Add (new object [] { 1, "mono 1" });
1140 src.Rows.Add (new object [] { 2, "mono 2" });
1141 src.Rows.Add (new object [] { 3, "mono 3" });
1142 src.AcceptChanges ();
1144 src.Rows [0] [1] = "mono changed 1"; // modify 1st row
1145 src.Rows [1].Delete (); // delete 2nd row
1146 // 3rd row is unchanged
1147 src.Rows.Add (new object [] { 4, "mono 4" }); // add 4th row
1149 // build target table
1150 DataTable target = new DataTable ();
1151 target.Columns.Add ("id", typeof (int));
1152 target.Columns.Add ("name", typeof (string));
1154 target.PrimaryKey = new DataColumn [] {target.Columns [0]} ;
1157 target.ImportRow (src.Rows [0]); // import 1st row
1158 target.ImportRow (src.Rows [1]); // import 2nd row
1159 target.ImportRow (src.Rows [2]); // import 3rd row
1160 target.ImportRow (src.Rows [3]); // import 4th row
1163 target.ImportRow (src.Rows [2]); // import 3rd row again
1164 Fail ("#AA1 Should have thrown exception violativ PK");
1165 } catch (ConstraintException e) {}
1168 AssertEquals ("#A1", src.Rows [0].RowState, target.Rows [0].RowState);
1169 AssertEquals ("#A2", src.Rows [1].RowState, target.Rows [1].RowState);
1170 AssertEquals ("#A3", src.Rows [2].RowState, target.Rows [2].RowState);
1171 AssertEquals ("#A4", src.Rows [3].RowState, target.Rows [3].RowState);
1173 // check for modified row (1st row)
1174 AssertEquals ("#B1", (string) src.Rows [0] [1], (string) target.Rows [0] [1]);
1175 AssertEquals ("#B2", (string) src.Rows [0] [1, DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
1176 AssertEquals ("#B3", (string) src.Rows [0] [1, DataRowVersion.Original], (string) target.Rows [0] [1, DataRowVersion.Original]);
1177 AssertEquals ("#B4", (string) src.Rows [0] [1, DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
1178 AssertEquals ("#B5", false, target.Rows [0].HasVersion(DataRowVersion.Proposed));
1180 // check for deleted row (2nd row)
1181 AssertEquals ("#C1", (string) src.Rows [1] [1, DataRowVersion.Original], (string) target.Rows [1] [1, DataRowVersion.Original]);
1183 // check for unchanged row (3rd row)
1184 AssertEquals ("#D1", (string) src.Rows [2] [1], (string) target.Rows [2] [1]);
1185 AssertEquals ("#D2", (string) src.Rows [2] [1, DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
1186 AssertEquals ("#D3", (string) src.Rows [2] [1, DataRowVersion.Original], (string) target.Rows [2] [1, DataRowVersion.Original]);
1187 AssertEquals ("#D4", (string) src.Rows [2] [1, DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
1189 // check for newly added row (4th row)
1190 AssertEquals ("#E1", (string) src.Rows [3] [1], (string) target.Rows [3] [1]);
1191 AssertEquals ("#E2", (string) src.Rows [3] [1, DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
1192 AssertEquals ("#E3", (string) src.Rows [3] [1, DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
1196 public void ImportRowDetachedTest ()
1198 DataTable table = new DataTable ();
1199 DataColumn col = new DataColumn ();
1200 col.ColumnName = "Id";
1201 col.DataType = Type.GetType ("System.Int32");
1202 table.Columns.Add (col);
1204 table.PrimaryKey = new DataColumn [] {col};
1206 col = new DataColumn ();
1207 col.ColumnName = "Name";
1208 col.DataType = Type.GetType ("System.String");
1209 table.Columns.Add (col);
1211 DataRow row = table.NewRow ();
1213 row ["name"] = "Abc";
1215 // keep silent as ms.net ;-), though this is not useful.
1216 table.ImportRow (row);
1218 //if RowState is detached, then dont import the row.
1219 AssertEquals ("#1", 0, table.Rows.Count);
1223 public void ImportRowDeletedTest ()
1225 DataTable table = new DataTable ();
1226 table.Columns.Add ("col", typeof (int));
1227 table.Columns.Add ("col1", typeof (int));
1229 DataRow row = table.Rows.Add (new object[] {1,2});
1230 table.PrimaryKey = new DataColumn[] {table.Columns[0]};
1231 table.AcceptChanges ();
1233 // If row is in Deleted state, then ImportRow loads the
1236 table.ImportRow (row);
1237 AssertEquals ("#1", 2, table.Rows.Count);
1239 // Both the deleted rows shud be now gone
1240 table.AcceptChanges ();
1241 AssertEquals ("#2", 0, table.Rows.Count);
1243 //just add another row
1244 row = table.Rows.Add (new object[] {1,2});
1245 // no exception shud be thrown
1246 table.AcceptChanges ();
1248 // If row is in Deleted state, then ImportRow loads the
1249 // row and validate only on RejectChanges
1251 table.ImportRow (row);
1252 AssertEquals ("#3", 2, table.Rows.Count);
1253 AssertEquals ("#4", DataRowState.Deleted, table.Rows[1].RowState);
1256 table.RejectChanges ();
1258 } catch (ConstraintException e) {
1263 public void ClearReset () //To test Clear and Reset methods
1265 DataTable table = new DataTable ("table");
1266 DataTable table1 = new DataTable ("table1");
1268 DataSet set = new DataSet ();
1269 set.Tables.Add (table);
1270 set.Tables.Add (table1);
1272 table.Columns.Add ("Id", typeof (int));
1273 table.Columns.Add ("Name", typeof (string));
1274 table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
1275 table.CaseSensitive = false;
1277 table1.Columns.Add ("Id", typeof (int));
1278 table1.Columns.Add ("Name", typeof (string));
1280 DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1281 set.Relations.Add (dr);
1283 DataRow row = table.NewRow ();
1285 row ["name"] = "Roopa";
1286 table.Rows.Add (row);
1288 row = table.NewRow ();
1290 row ["Name"] = "roopa";
1291 table.Rows.Add (row);
1293 AssertEquals (2, table.Rows.Count);
1294 AssertEquals (1, table.ChildRelations.Count);
1297 Fail ("#A01, should have thrown ArgumentException");
1299 catch (ArgumentException) {
1301 AssertEquals ("#CT01", 0, table.Rows.Count);
1302 AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1303 AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1304 AssertEquals ("#CT04", 0, table.Constraints.Count);
1307 AssertEquals ("#A05", 0, table1.Rows.Count);
1308 AssertEquals ("#A06", 0, table1.Constraints.Count);
1309 AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1313 AssertEquals ("#A08", 0, table.Rows.Count);
1315 AssertEquals ("#A09", 0, table.Constraints.Count);
1317 AssertEquals ("#A09", 1, table.Constraints.Count);
1319 AssertEquals ("#A10", 0, table.ChildRelations.Count);
1324 public void ClearTest ()
1326 DataTable table = new DataTable ("test");
1327 table.Columns.Add ("id", typeof (int));
1328 table.Columns.Add ("name", typeof (string));
1330 table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
1332 table.Rows.Add (new object [] { 1, "mono 1" });
1333 table.Rows.Add (new object [] { 2, "mono 2" });
1334 table.Rows.Add (new object [] { 3, "mono 3" });
1335 table.Rows.Add (new object [] { 4, "mono 4" });
1337 table.AcceptChanges ();
1339 _tableClearedEventFired = false;
1340 table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
1345 AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
1348 DataRow r = table.Rows.Find (1);
1349 AssertEquals ("#1 should have cleared", true, r == null);
1351 // try adding new row. indexes should have cleared
1352 table.Rows.Add (new object [] { 2, "mono 2" });
1353 AssertEquals ("#2 should add row", 1, table.Rows.Count);
1356 private bool _tableClearedEventFired = false;
1357 private void OnTableCleared (object src, DataTableClearEventArgs args)
1359 _tableClearedEventFired = true;
1365 public void Serialize ()
1367 MemoryStream fs = new MemoryStream ();
1369 // Construct a BinaryFormatter and use it
1370 // to serialize the data to the stream.
1371 BinaryFormatter formatter = new BinaryFormatter();
1373 // Create an array with multiple elements refering to
1374 // the one Singleton object.
1375 DataTable dt = new DataTable();
1378 dt.Columns.Add(new DataColumn("Id", typeof(string)));
1379 dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1380 dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1381 dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1382 dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1385 loRowToAdd = dt.NewRow();
1386 loRowToAdd[0] = "a";
1387 loRowToAdd[1] = "b";
1388 loRowToAdd[2] = "c";
1389 loRowToAdd[3] = "d";
1390 loRowToAdd[4] = "e";
1392 dt.Rows.Add(loRowToAdd);
1394 DataTable[] dtarr = new DataTable[] {dt};
1396 // Serialize the array elements.
1397 formatter.Serialize(fs, dtarr);
1399 // Deserialize the array elements.
1401 DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1403 DataSet ds = new DataSet();
1404 ds.Tables.Add(a2[0]);
1406 StringWriter sw = new StringWriter ();
1408 XmlDocument doc = new XmlDocument ();
1409 doc.LoadXml (sw.ToString ());
1410 AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1414 [ExpectedException (typeof (DataException))]
1415 public void SetPrimaryKeyAssertsNonNull ()
1417 DataTable dt = new DataTable ("table");
1418 dt.Columns.Add ("col1");
1419 dt.Columns.Add ("col2");
1420 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1421 dt.Rows.Add (new object [] {1, 3});
1422 dt.Rows.Add (new object [] {DBNull.Value, 3});
1424 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1428 [ExpectedException (typeof (NoNullAllowedException))]
1429 public void PrimaryKeyColumnChecksNonNull ()
1431 DataTable dt = new DataTable ("table");
1432 dt.Columns.Add ("col1");
1433 dt.Columns.Add ("col2");
1434 dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1435 dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1436 dt.Rows.Add (new object [] {1, 3});
1437 dt.Rows.Add (new object [] {DBNull.Value, 3});
1441 public void PrimaryKey_CheckSetsAllowDBNull ()
1443 DataTable table = new DataTable ();
1444 DataColumn col1 = table.Columns.Add ("col1", typeof (int));
1445 DataColumn col2 = table.Columns.Add ("col2", typeof (int));
1447 AssertEquals ("#1" , true, col1.AllowDBNull);
1448 AssertEquals ("#2" , true, col2.AllowDBNull);
1449 AssertEquals ("#3" , false, col2.Unique);
1450 AssertEquals ("#4" , false, col2.Unique);
1452 table.PrimaryKey = new DataColumn[] {col1,col2};
1453 AssertEquals ("#5" , false, col1.AllowDBNull);
1454 AssertEquals ("#6" , false, col2.AllowDBNull);
1455 // LAMESPEC or bug ??
1456 AssertEquals ("#7" , false, col1.Unique);
1457 AssertEquals ("#8" , false, col2.Unique);
1460 void RowChanging (object o, DataRowChangeEventArgs e)
1462 AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1463 rowChangingRowChanging = true;
1466 void RowChanged (object o, DataRowChangeEventArgs e)
1468 AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1469 rowChangingRowChanged = true;
1472 bool rowChangingRowChanging, rowChangingRowChanged;
1473 DataRowAction rowChangingExpectedAction;
1476 public void RowChanging ()
1478 DataTable dt = new DataTable ("table");
1479 dt.Columns.Add ("col1");
1480 dt.Columns.Add ("col2");
1481 dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1482 dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1483 rowChangingExpectedAction = DataRowAction.Add;
1484 dt.Rows.Add (new object [] {1, 2});
1485 Assert ("changing,Added", rowChangingRowChanging);
1486 Assert ("changed,Added", rowChangingRowChanged);
1487 rowChangingExpectedAction = DataRowAction.Change;
1488 dt.Rows [0] [0] = 2;
1489 Assert ("changing,Changed", rowChangingRowChanging);
1490 Assert ("changed,Changed", rowChangingRowChanged);
1494 public void CloneSubClassTest()
1496 MyDataTable dt1 = new MyDataTable();
1497 MyDataTable dt = (MyDataTable)(dt1.Clone());
1498 AssertEquals("A#01",2,MyDataTable.count);
1501 DataRowAction rowActionChanging = DataRowAction.Nothing;
1502 DataRowAction rowActionChanged = DataRowAction.Nothing;
1504 public void AcceptChangesTest ()
1506 DataTable dt = new DataTable ("test");
1507 dt.Columns.Add ("id", typeof (int));
1508 dt.Columns.Add ("name", typeof (string));
1510 dt.Rows.Add (new object [] { 1, "mono 1" });
1512 dt.RowChanged += new DataRowChangeEventHandler (OnRowChanged);
1513 dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
1516 rowActionChanged = rowActionChanging = DataRowAction.Nothing;
1517 dt.AcceptChanges ();
1519 AssertEquals ("#1 should have fired event and set action to commit",
1520 DataRowAction.Commit, rowActionChanging);
1521 AssertEquals ("#2 should have fired event and set action to commit",
1522 DataRowAction.Commit, rowActionChanged);
1525 dt.RowChanged -= new DataRowChangeEventHandler (OnRowChanged);
1526 dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
1532 public void ColumnObjectTypeTest() {
1533 DataTable dt = new DataTable();
1534 dt.Columns.Add("Series Label", typeof(SqlInt32));
1535 dt.Rows.Add(new object[] {"sss"});
1536 AssertEquals(1, dt.Rows.Count);
1539 public void OnRowChanging (object src, DataRowChangeEventArgs args)
1541 rowActionChanging = args.Action;
1544 public void OnRowChanged (object src, DataRowChangeEventArgs args)
1546 rowActionChanged = args.Action;
1551 private DataTable dt;
1552 private void localSetup () {
1553 dt = new DataTable ("test");
1554 dt.Columns.Add ("id", typeof (int));
1555 dt.Columns.Add ("name", typeof (string));
1556 dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
1558 dt.Rows.Add (new object[] { 1, "mono 1" });
1559 dt.Rows.Add (new object[] { 2, "mono 2" });
1560 dt.Rows.Add (new object[] { 3, "mono 3" });
1562 dt.AcceptChanges ();
1565 #region DataTable.CreateDataReader Tests
1568 public void CreateDataReader1 () {
1570 DataTableReader dtr = dt.CreateDataReader ();
1571 Assert ("HasRows", dtr.HasRows);
1572 AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1574 while (dtr.Read ()) {
1575 for (int i = 0; i < dtr.FieldCount; i++) {
1576 AssertEquals ("RowData-" + ri + "-" + i, dt.Rows[ri][i],
1584 public void CreateDataReader2 () {
1586 DataTableReader dtr = dt.CreateDataReader ();
1587 Assert ("HasRows", dtr.HasRows);
1588 AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1590 AssertEquals ("RowData0-0", 1, dtr[0]);
1591 AssertEquals ("RowData0-1", "mono 1", dtr[1]);
1593 AssertEquals ("RowData1-0", 2, dtr[0]);
1594 AssertEquals ("RowData1-1", "mono 2", dtr[1]);
1596 AssertEquals ("RowData2-0", 3, dtr[0]);
1597 AssertEquals ("RowData2-1", "mono 3", dtr[1]);
1600 #endregion // DataTable.CreateDataReader Tests
1602 #region DataTable.Load Tests
1605 public void Load_Basic () {
1607 DataTable dtLoad = new DataTable ("LoadBasic");
1608 dtLoad.Columns.Add ("id", typeof (int));
1609 dtLoad.Columns.Add ("name", typeof (string));
1610 dtLoad.Columns["id"].ReadOnly = true;
1611 dtLoad.Columns["name"].ReadOnly = true;
1612 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1613 dtLoad.Rows.Add (new object[] { 1, "load 1" });
1614 dtLoad.Rows.Add (new object[] { 2, "load 2" });
1615 dtLoad.Rows.Add (new object[] { 3, "load 3" });
1616 dtLoad.AcceptChanges ();
1617 DataTableReader dtr = dt.CreateDataReader ();
1619 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1620 AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1621 AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1622 AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1623 AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1624 AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1625 AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1626 AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1630 public void Load_NoSchema () {
1632 DataTable dtLoad = new DataTable ("LoadNoSchema");
1633 DataTableReader dtr = dt.CreateDataReader ();
1635 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1636 AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1637 AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1638 AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1639 AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1640 AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1641 AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1642 AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1645 internal struct fillErrorStruct {
1646 internal string error;
1647 internal string tableName;
1648 internal int rowKey;
1649 internal bool contFlag;
1650 internal void init (string tbl, int row, bool cont, string err) {
1657 private fillErrorStruct[] fillErr = new fillErrorStruct[3];
1658 private int fillErrCounter;
1659 private void fillErrorHandler (object sender, FillErrorEventArgs e) {
1660 e.Continue = fillErr[fillErrCounter].contFlag;
1661 AssertEquals ("fillErr-T", fillErr[fillErrCounter].tableName, e.DataTable.TableName);
1662 AssertEquals ("fillErr-R", fillErr[fillErrCounter].rowKey, e.Values[0]);
1663 AssertEquals ("fillErr-C", fillErr[fillErrCounter].contFlag, e.Continue);
1664 AssertEquals ("fillErr-E", fillErr[fillErrCounter].error, e.Errors.Message);
1669 [ExpectedException (typeof (ArgumentException))]
1670 public void Load_Incompatible () {
1672 DataTable dtLoad = new DataTable ("LoadIncompatible");
1673 dtLoad.Columns.Add ("name", typeof (double));
1674 DataTableReader dtr = dt.CreateDataReader ();
1678 [Category ("NotWorking")]
1679 // Load doesn't have a third overload in System.Data
1680 // and is commented-out below
1681 public void Load_IncompatibleEHandlerT () {
1683 fillErr[0].init ("LoadIncompatible", 1, true,
1684 "Input string was not in a correct format.Couldn't store <mono 1> in name Column. Expected type is Double.");
1685 fillErr[1].init ("LoadIncompatible", 2, true,
1686 "Input string was not in a correct format.Couldn't store <mono 2> in name Column. Expected type is Double.");
1687 fillErr[2].init ("LoadIncompatible", 3, true,
1688 "Input string was not in a correct format.Couldn't store <mono 3> in name Column. Expected type is Double.");
1690 DataTable dtLoad = new DataTable ("LoadIncompatible");
1691 dtLoad.Columns.Add ("name", typeof (double));
1692 DataTableReader dtr = dt.CreateDataReader ();
1693 //dtLoad.Load (dtr,LoadOption.PreserveChanges,fillErrorHandler);
1696 [Category ("NotWorking")]
1697 // Load doesn't have a third overload in System.Data
1698 // and is commented-out below
1699 [ExpectedException (typeof (ArgumentException))]
1700 public void Load_IncompatibleEHandlerF () {
1702 fillErr[0].init ("LoadIncompatible", 1, false,
1703 "Input string was not in a correct format.Couldn't store <mono 1> in name Column. Expected type is Double.");
1705 DataTable dtLoad = new DataTable ("LoadIncompatible");
1706 dtLoad.Columns.Add ("name", typeof (double));
1707 DataTableReader dtr = dt.CreateDataReader ();
1708 //dtLoad.Load (dtr, LoadOption.PreserveChanges, fillErrorHandler);
1712 public void Load_ExtraColsEqualVal () {
1714 DataTable dtLoad = new DataTable ("LoadExtraCols");
1715 dtLoad.Columns.Add ("id", typeof (int));
1716 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1717 dtLoad.Rows.Add (new object[] { 1 });
1718 dtLoad.Rows.Add (new object[] { 2 });
1719 dtLoad.Rows.Add (new object[] { 3 });
1720 dtLoad.AcceptChanges ();
1721 DataTableReader dtr = dt.CreateDataReader ();
1723 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1724 AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1725 AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1726 AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1727 AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1728 AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1729 AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1730 AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1734 public void Load_ExtraColsNonEqualVal () {
1736 DataTable dtLoad = new DataTable ("LoadExtraCols");
1737 dtLoad.Columns.Add ("id", typeof (int));
1738 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1739 dtLoad.Rows.Add (new object[] { 4 });
1740 dtLoad.Rows.Add (new object[] { 5 });
1741 dtLoad.Rows.Add (new object[] { 6 });
1742 dtLoad.AcceptChanges ();
1743 DataTableReader dtr = dt.CreateDataReader ();
1745 AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1746 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1747 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1748 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1749 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1750 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1751 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1752 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1753 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1754 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1755 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1759 [ExpectedException (typeof (ConstraintException))]
1760 public void Load_MissingColsNonNullable () {
1762 DataTable dtLoad = new DataTable ("LoadMissingCols");
1763 dtLoad.Columns.Add ("id", typeof (int));
1764 dtLoad.Columns.Add ("name", typeof (string));
1765 dtLoad.Columns.Add ("missing", typeof (string));
1766 dtLoad.Columns["missing"].AllowDBNull = false;
1767 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1768 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1769 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1770 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1771 dtLoad.AcceptChanges ();
1772 DataTableReader dtr = dt.CreateDataReader ();
1777 public void Load_MissingColsDefault () {
1779 DataTable dtLoad = new DataTable ("LoadMissingCols");
1780 dtLoad.Columns.Add ("id", typeof (int));
1781 dtLoad.Columns.Add ("name", typeof (string));
1782 dtLoad.Columns.Add ("missing", typeof (string));
1783 dtLoad.Columns["missing"].AllowDBNull = false;
1784 dtLoad.Columns["missing"].DefaultValue = "DefaultValue";
1785 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1786 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1787 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1788 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1789 dtLoad.AcceptChanges ();
1790 DataTableReader dtr = dt.CreateDataReader ();
1792 AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1793 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1794 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1795 AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1796 AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1797 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1798 AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1799 AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1800 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1801 AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1802 AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1803 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1804 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1805 AssertEquals ("RowData3-2", "DefaultValue", dtLoad.Rows[3][2]);
1806 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1807 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1808 AssertEquals ("RowData4-2", "DefaultValue", dtLoad.Rows[4][2]);
1809 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1810 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1811 AssertEquals ("RowData5-2", "DefaultValue", dtLoad.Rows[5][2]);
1815 public void Load_MissingColsNullable () {
1817 DataTable dtLoad = new DataTable ("LoadMissingCols");
1818 dtLoad.Columns.Add ("id", typeof (int));
1819 dtLoad.Columns.Add ("name", typeof (string));
1820 dtLoad.Columns.Add ("missing", typeof (string));
1821 dtLoad.Columns["missing"].AllowDBNull = true;
1822 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1823 dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1824 dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1825 dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1826 dtLoad.AcceptChanges ();
1827 DataTableReader dtr = dt.CreateDataReader ();
1829 AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1830 AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1831 AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1832 AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1833 AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1834 AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1835 AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1836 AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1837 AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1838 AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1839 AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1840 AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1841 AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1842 //AssertEquals ("RowData3-2", null, dtLoad.Rows[3][2]);
1843 AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1844 AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1845 //AssertEquals ("RowData4-2", null, dtLoad.Rows[4][2]);
1846 AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1847 AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1848 //AssertEquals ("RowData5-2", null, dtLoad.Rows[5][2]);
1851 private DataTable setupRowState () {
1852 DataTable tbl = new DataTable ("LoadRowStateChanges");
1853 tbl.RowChanged += new DataRowChangeEventHandler (dtLoad_RowChanged);
1854 tbl.RowChanging += new DataRowChangeEventHandler (dtLoad_RowChanging);
1855 tbl.Columns.Add ("id", typeof (int));
1856 tbl.Columns.Add ("name", typeof (string));
1857 tbl.PrimaryKey = new DataColumn[] { tbl.Columns["id"] };
1858 tbl.Rows.Add (new object[] { 1, "RowState 1" });
1859 tbl.Rows.Add (new object[] { 2, "RowState 2" });
1860 tbl.Rows.Add (new object[] { 3, "RowState 3" });
1861 tbl.AcceptChanges ();
1862 // Update Table with following changes: Row0 unmodified,
1863 // Row1 modified, Row2 deleted, Row3 added, Row4 not-present.
1864 tbl.Rows[1]["name"] = "Modify 2";
1865 tbl.Rows[2].Delete ();
1866 DataRow row = tbl.NewRow ();
1868 row["name"] = "Add 4";
1873 private DataRowAction[] rowChangeAction = new DataRowAction[5];
1874 private bool checkAction = false;
1875 private int rowChagedCounter, rowChangingCounter;
1876 private void rowActionInit (DataRowAction[] act) {
1878 rowChagedCounter = 0;
1879 rowChangingCounter = 0;
1880 for (int i = 0; i < 5; i++)
1881 rowChangeAction[i] = act[i];
1883 private void rowActionEnd () {
1884 checkAction = false;
1886 private void dtLoad_RowChanged (object sender, DataRowChangeEventArgs e) {
1888 AssertEquals ("RowChanged" + rowChagedCounter,
1889 rowChangeAction[rowChagedCounter], e.Action);
1893 private void dtLoad_RowChanging (object sender, DataRowChangeEventArgs e) {
1895 AssertEquals ("RowChanging" + rowChangingCounter,
1896 rowChangeAction[rowChangingCounter], e.Action);
1897 rowChangingCounter++;
1902 [Category ("NotWorking")]
1903 public void Load_RowStateChangesDefault () {
1905 dt.Rows.Add (new object[] { 4, "mono 4" });
1906 dt.Rows.Add (new object[] { 5, "mono 5" });
1907 dt.AcceptChanges ();
1908 DataTableReader dtr = dt.CreateDataReader ();
1909 DataTable dtLoad = setupRowState ();
1910 DataRowAction[] dra = new DataRowAction[] {
1911 DataRowAction.ChangeCurrentAndOriginal,
1912 DataRowAction.ChangeOriginal,
1913 DataRowAction.ChangeOriginal,
1914 DataRowAction.ChangeOriginal,
1915 DataRowAction.ChangeCurrentAndOriginal};
1916 rowActionInit (dra);
1919 // asserting Unchanged Row0
1920 AssertEquals ("RowData0-C", "mono 1",
1921 dtLoad.Rows[0][1,DataRowVersion.Current]);
1922 AssertEquals ("RowData0-O", "mono 1",
1923 dtLoad.Rows[0][1,DataRowVersion.Original]);
1924 AssertEquals ("RowState0", DataRowState.Unchanged,
1925 dtLoad.Rows[0].RowState);
1926 // asserting Modified Row1
1927 AssertEquals ("RowData1-C", "Modify 2",
1928 dtLoad.Rows[1][1, DataRowVersion.Current]);
1929 AssertEquals ("RowData1-O", "mono 2",
1930 dtLoad.Rows[1][1, DataRowVersion.Original]);
1931 AssertEquals ("RowState1", DataRowState.Modified,
1932 dtLoad.Rows[1].RowState);
1933 // asserting Deleted Row2
1934 AssertEquals ("RowData1-O", "mono 3",
1935 dtLoad.Rows[2][1, DataRowVersion.Original]);
1936 AssertEquals ("RowState2", DataRowState.Deleted,
1937 dtLoad.Rows[2].RowState);
1938 // asserting Added Row3
1939 AssertEquals ("RowData3-C", "Add 4",
1940 dtLoad.Rows[3][1, DataRowVersion.Current]);
1941 AssertEquals ("RowData3-O", "mono 4",
1942 dtLoad.Rows[3][1, DataRowVersion.Original]);
1943 AssertEquals ("RowState3", DataRowState.Modified,
1944 dtLoad.Rows[3].RowState);
1945 // asserting Unpresent Row4
1946 AssertEquals ("RowData4-C", "mono 5",
1947 dtLoad.Rows[4][1, DataRowVersion.Current]);
1948 AssertEquals ("RowData4-O", "mono 5",
1949 dtLoad.Rows[4][1, DataRowVersion.Original]);
1950 AssertEquals ("RowState4", DataRowState.Unchanged,
1951 dtLoad.Rows[4].RowState);
1955 [ExpectedException (typeof (VersionNotFoundException))]
1956 [Category ("NotWorking")]
1957 public void Load_RowStateChangesDefaultDelete () {
1959 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1960 dtLoad.Columns.Add ("id", typeof (int));
1961 dtLoad.Columns.Add ("name", typeof (string));
1962 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1963 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1964 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1965 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1966 dtLoad.AcceptChanges ();
1967 dtLoad.Rows[2].Delete ();
1968 DataTableReader dtr = dt.CreateDataReader ();
1970 AssertEquals ("RowData2-C", " ",
1971 dtLoad.Rows[2][1, DataRowVersion.Current]);
1975 [Category ("NotWorking")]
1976 public void Load_RowStatePreserveChanges () {
1978 dt.Rows.Add (new object[] { 4, "mono 4" });
1979 dt.Rows.Add (new object[] { 5, "mono 5" });
1980 dt.AcceptChanges ();
1981 DataTableReader dtr = dt.CreateDataReader ();
1982 DataTable dtLoad = setupRowState ();
1983 DataRowAction[] dra = new DataRowAction[] {
1984 DataRowAction.ChangeCurrentAndOriginal,
1985 DataRowAction.ChangeOriginal,
1986 DataRowAction.ChangeOriginal,
1987 DataRowAction.ChangeOriginal,
1988 DataRowAction.ChangeCurrentAndOriginal};
1989 rowActionInit (dra);
1990 dtLoad.Load (dtr, LoadOption.PreserveChanges);
1992 // asserting Unchanged Row0
1993 AssertEquals ("RowData0-C", "mono 1",
1994 dtLoad.Rows[0][1, DataRowVersion.Current]);
1995 AssertEquals ("RowData0-O", "mono 1",
1996 dtLoad.Rows[0][1, DataRowVersion.Original]);
1997 AssertEquals ("RowState0", DataRowState.Unchanged,
1998 dtLoad.Rows[0].RowState);
1999 // asserting Modified Row1
2000 AssertEquals ("RowData1-C", "Modify 2",
2001 dtLoad.Rows[1][1, DataRowVersion.Current]);
2002 AssertEquals ("RowData1-O", "mono 2",
2003 dtLoad.Rows[1][1, DataRowVersion.Original]);
2004 AssertEquals ("RowState1", DataRowState.Modified,
2005 dtLoad.Rows[1].RowState);
2006 // asserting Deleted Row2
2007 AssertEquals ("RowData1-O", "mono 3",
2008 dtLoad.Rows[2][1, DataRowVersion.Original]);
2009 AssertEquals ("RowState2", DataRowState.Deleted,
2010 dtLoad.Rows[2].RowState);
2011 // asserting Added Row3
2012 AssertEquals ("RowData3-C", "Add 4",
2013 dtLoad.Rows[3][1, DataRowVersion.Current]);
2014 AssertEquals ("RowData3-O", "mono 4",
2015 dtLoad.Rows[3][1, DataRowVersion.Original]);
2016 AssertEquals ("RowState3", DataRowState.Modified,
2017 dtLoad.Rows[3].RowState);
2018 // asserting Unpresent Row4
2019 AssertEquals ("RowData4-C", "mono 5",
2020 dtLoad.Rows[4][1, DataRowVersion.Current]);
2021 AssertEquals ("RowData4-O", "mono 5",
2022 dtLoad.Rows[4][1, DataRowVersion.Original]);
2023 AssertEquals ("RowState4", DataRowState.Unchanged,
2024 dtLoad.Rows[4].RowState);
2028 [ExpectedException (typeof (VersionNotFoundException))]
2029 [Category ("NotWorking")]
2030 public void Load_RowStatePreserveChangesDelete () {
2032 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2033 dtLoad.Columns.Add ("id", typeof (int));
2034 dtLoad.Columns.Add ("name", typeof (string));
2035 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2036 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2037 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2038 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2039 dtLoad.AcceptChanges ();
2040 dtLoad.Rows[2].Delete ();
2041 DataTableReader dtr = dt.CreateDataReader ();
2042 dtLoad.Load (dtr,LoadOption.PreserveChanges);
2043 AssertEquals ("RowData2-C", " ",
2044 dtLoad.Rows[2][1, DataRowVersion.Current]);
2048 [Category ("NotWorking")]
2049 public void Load_RowStateOverwriteChanges () {
2051 dt.Rows.Add (new object[] { 4, "mono 4" });
2052 dt.Rows.Add (new object[] { 5, "mono 5" });
2053 dt.AcceptChanges ();
2054 DataTableReader dtr = dt.CreateDataReader ();
2055 DataTable dtLoad = setupRowState ();
2056 DataRowAction[] dra = new DataRowAction[] {
2057 DataRowAction.ChangeCurrentAndOriginal,
2058 DataRowAction.ChangeCurrentAndOriginal,
2059 DataRowAction.ChangeCurrentAndOriginal,
2060 DataRowAction.ChangeCurrentAndOriginal,
2061 DataRowAction.ChangeCurrentAndOriginal};
2062 rowActionInit (dra);
2063 dtLoad.Load (dtr, LoadOption.OverwriteChanges);
2065 // asserting Unchanged Row0
2066 AssertEquals ("RowData0-C", "mono 1",
2067 dtLoad.Rows[0][1, DataRowVersion.Current]);
2068 AssertEquals ("RowData0-O", "mono 1",
2069 dtLoad.Rows[0][1, DataRowVersion.Original]);
2070 AssertEquals ("RowState0", DataRowState.Unchanged,
2071 dtLoad.Rows[0].RowState);
2072 // asserting Modified Row1
2073 AssertEquals ("RowData1-C", "mono 2",
2074 dtLoad.Rows[1][1, DataRowVersion.Current]);
2075 AssertEquals ("RowData1-O", "mono 2",
2076 dtLoad.Rows[1][1, DataRowVersion.Original]);
2077 AssertEquals ("RowState1", DataRowState.Unchanged,
2078 dtLoad.Rows[1].RowState);
2079 // asserting Deleted Row2
2080 AssertEquals ("RowData1-C", "mono 3",
2081 dtLoad.Rows[2][1, DataRowVersion.Current]);
2082 AssertEquals ("RowData1-O", "mono 3",
2083 dtLoad.Rows[2][1, DataRowVersion.Original]);
2084 AssertEquals ("RowState2", DataRowState.Unchanged,
2085 dtLoad.Rows[2].RowState);
2086 // asserting Added Row3
2087 AssertEquals ("RowData3-C", "mono 4",
2088 dtLoad.Rows[3][1, DataRowVersion.Current]);
2089 AssertEquals ("RowData3-O", "mono 4",
2090 dtLoad.Rows[3][1, DataRowVersion.Original]);
2091 AssertEquals ("RowState3", DataRowState.Unchanged,
2092 dtLoad.Rows[3].RowState);
2093 // asserting Unpresent Row4
2094 AssertEquals ("RowData4-C", "mono 5",
2095 dtLoad.Rows[4][1, DataRowVersion.Current]);
2096 AssertEquals ("RowData4-O", "mono 5",
2097 dtLoad.Rows[4][1, DataRowVersion.Original]);
2098 AssertEquals ("RowState4", DataRowState.Unchanged,
2099 dtLoad.Rows[4].RowState);
2103 [Category ("NotWorking")]
2104 public void Load_RowStateUpsert () {
2106 dt.Rows.Add (new object[] { 4, "mono 4" });
2107 dt.Rows.Add (new object[] { 5, "mono 5" });
2108 dt.AcceptChanges ();
2109 DataTableReader dtr = dt.CreateDataReader ();
2110 DataTable dtLoad = setupRowState ();
2111 // Notice rowChange-Actions only occur 5 times, as number
2112 // of actual rows, ignoring row duplication of the deleted row.
2113 DataRowAction[] dra = new DataRowAction[] {
2114 DataRowAction.Change,
2115 DataRowAction.Change,
2117 DataRowAction.Change,
2119 rowActionInit (dra);
2120 dtLoad.Load (dtr, LoadOption.Upsert);
2122 // asserting Unchanged Row0
2123 AssertEquals ("RowData0-C", "mono 1",
2124 dtLoad.Rows[0][1, DataRowVersion.Current]);
2125 AssertEquals ("RowData0-O", "RowState 1",
2126 dtLoad.Rows[0][1, DataRowVersion.Original]);
2127 AssertEquals ("RowState0", DataRowState.Modified,
2128 dtLoad.Rows[0].RowState);
2129 // asserting Modified Row1
2130 AssertEquals ("RowData1-C", "mono 2",
2131 dtLoad.Rows[1][1, DataRowVersion.Current]);
2132 AssertEquals ("RowData1-O", "RowState 2",
2133 dtLoad.Rows[1][1, DataRowVersion.Original]);
2134 AssertEquals ("RowState1", DataRowState.Modified,
2135 dtLoad.Rows[1].RowState);
2136 // asserting Deleted Row2 and "Deleted-Added" Row4
2137 AssertEquals ("RowData2-O", "RowState 3",
2138 dtLoad.Rows[2][1, DataRowVersion.Original]);
2139 AssertEquals ("RowState2", DataRowState.Deleted,
2140 dtLoad.Rows[2].RowState);
2141 AssertEquals ("RowData4-C", "mono 3",
2142 dtLoad.Rows[4][1, DataRowVersion.Current]);
2143 AssertEquals ("RowState4", DataRowState.Added,
2144 dtLoad.Rows[4].RowState);
2145 // asserting Added Row3
2146 AssertEquals ("RowData3-C", "mono 4",
2147 dtLoad.Rows[3][1, DataRowVersion.Current]);
2148 AssertEquals ("RowState3", DataRowState.Added,
2149 dtLoad.Rows[3].RowState);
2150 // asserting Unpresent Row5
2151 // Notice row4 is used for added row of deleted row2 and so
2152 // unpresent row4 moves to row5
2153 AssertEquals ("RowData5-C", "mono 5",
2154 dtLoad.Rows[5][1, DataRowVersion.Current]);
2155 AssertEquals ("RowState5", DataRowState.Added,
2156 dtLoad.Rows[5].RowState);
2160 [Category ("NotWorking")]
2161 public void Load_RowStateUpsertDuplicateKey1 () {
2163 dt.Rows.Add (new object[] { 4, "mono 4" });
2164 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2165 dtLoad.Columns.Add ("id", typeof (int));
2166 dtLoad.Columns.Add ("name", typeof (string));
2167 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2168 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2169 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2170 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2171 dtLoad.AcceptChanges ();
2172 dtLoad.Rows[2].Delete ();
2173 DataTableReader dtr = dt.CreateDataReader ();
2174 dtLoad.Load (dtr, LoadOption.Upsert);
2175 dtLoad.Rows[3][1] = "NEWVAL";
2176 AssertEquals ("A-RowState2", DataRowState.Deleted,
2177 dtLoad.Rows[2].RowState);
2178 AssertEquals ("A-RowData2-id", 3,
2179 dtLoad.Rows[2][0, DataRowVersion.Original]);
2180 AssertEquals ("A-RowData2-name", "RowState 3",
2181 dtLoad.Rows[2][1, DataRowVersion.Original]);
2182 AssertEquals ("A-RowState3", DataRowState.Added,
2183 dtLoad.Rows[3].RowState);
2184 AssertEquals ("A-RowData3-id", 3,
2185 dtLoad.Rows[3][0, DataRowVersion.Current]);
2186 AssertEquals ("A-RowData3-name", "NEWVAL",
2187 dtLoad.Rows[3][1, DataRowVersion.Current]);
2188 AssertEquals ("A-RowState4", DataRowState.Added,
2189 dtLoad.Rows[4].RowState);
2190 AssertEquals ("A-RowData4-id", 4,
2191 dtLoad.Rows[4][0, DataRowVersion.Current]);
2192 AssertEquals ("A-RowData4-name", "mono 4",
2193 dtLoad.Rows[4][1, DataRowVersion.Current]);
2195 dtLoad.AcceptChanges ();
2197 AssertEquals ("B-RowState2", DataRowState.Unchanged,
2198 dtLoad.Rows[2].RowState);
2199 AssertEquals ("B-RowData2-id", 3,
2200 dtLoad.Rows[2][0, DataRowVersion.Current]);
2201 AssertEquals ("B-RowData2-name", "NEWVAL",
2202 dtLoad.Rows[2][1, DataRowVersion.Current]);
2203 AssertEquals ("B-RowState3", DataRowState.Unchanged,
2204 dtLoad.Rows[3].RowState);
2205 AssertEquals ("B-RowData3-id", 4,
2206 dtLoad.Rows[3][0, DataRowVersion.Current]);
2207 AssertEquals ("B-RowData3-name", "mono 4",
2208 dtLoad.Rows[3][1, DataRowVersion.Current]);
2212 [ExpectedException (typeof (IndexOutOfRangeException))]
2213 [Category ("NotWorking")]
2214 public void Load_RowStateUpsertDuplicateKey2 () {
2216 dt.Rows.Add (new object[] { 4, "mono 4" });
2217 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2218 dtLoad.Columns.Add ("id", typeof (int));
2219 dtLoad.Columns.Add ("name", typeof (string));
2220 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2221 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2222 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2223 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2224 dtLoad.AcceptChanges ();
2225 dtLoad.Rows[2].Delete ();
2226 DataTableReader dtr = dt.CreateDataReader ();
2227 dtLoad.Load (dtr, LoadOption.Upsert);
2228 dtLoad.AcceptChanges ();
2229 AssertEquals ("RowData4", " ", dtLoad.Rows[4][1]);
2233 [ExpectedException (typeof (VersionNotFoundException))]
2234 [Category ("NotWorking")]
2235 public void Load_RowStateUpsertDelete1 () {
2237 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2238 dtLoad.Columns.Add ("id", typeof (int));
2239 dtLoad.Columns.Add ("name", typeof (string));
2240 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2241 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2242 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2243 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2244 dtLoad.AcceptChanges ();
2245 dtLoad.Rows[2].Delete ();
2246 DataTableReader dtr = dt.CreateDataReader ();
2247 dtLoad.Load (dtr, LoadOption.Upsert);
2248 AssertEquals ("RowData2-C", " ",
2249 dtLoad.Rows[2][1, DataRowVersion.Current]);
2253 [ExpectedException (typeof (VersionNotFoundException))]
2254 [Category ("NotWorking")]
2255 public void Load_RowStateUpsertDelete2 () {
2257 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2258 dtLoad.Columns.Add ("id", typeof (int));
2259 dtLoad.Columns.Add ("name", typeof (string));
2260 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2261 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2262 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2263 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2264 dtLoad.AcceptChanges ();
2265 dtLoad.Rows[2].Delete ();
2266 DataTableReader dtr = dt.CreateDataReader ();
2267 dtLoad.Load (dtr, LoadOption.Upsert);
2268 AssertEquals ("RowData3-O", " ",
2269 dtLoad.Rows[3][1, DataRowVersion.Original]);
2273 [ExpectedException (typeof (VersionNotFoundException))]
2274 public void Load_RowStateUpsertAdd () {
2276 dt.Rows.Add (new object[] { 4, "mono 4" });
2277 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2278 dtLoad.Columns.Add ("id", typeof (int));
2279 dtLoad.Columns.Add ("name", typeof (string));
2280 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2281 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2282 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2283 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2284 dtLoad.AcceptChanges ();
2285 DataRow row = dtLoad.NewRow ();
2287 row["name"] = "Add 4";
2288 dtLoad.Rows.Add (row);
2289 DataTableReader dtr = dt.CreateDataReader ();
2290 dtLoad.Load (dtr, LoadOption.Upsert);
2291 AssertEquals ("RowData3-O", " ",
2292 dtLoad.Rows[3][1, DataRowVersion.Original]);
2296 [ExpectedException (typeof (VersionNotFoundException))]
2297 public void Load_RowStateUpsertUnpresent () {
2299 dt.Rows.Add (new object[] { 4, "mono 4" });
2300 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2301 dtLoad.Columns.Add ("id", typeof (int));
2302 dtLoad.Columns.Add ("name", typeof (string));
2303 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2304 dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2305 dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2306 dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2307 dtLoad.AcceptChanges ();
2308 DataTableReader dtr = dt.CreateDataReader ();
2309 dtLoad.Load (dtr, LoadOption.Upsert);
2310 AssertEquals ("RowData3-O", " ",
2311 dtLoad.Rows[3][1, DataRowVersion.Original]);
2315 public void Load_RowStateUpsertUnchangedEqualVal () {
2317 DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2318 dtLoad.Columns.Add ("id", typeof (int));
2319 dtLoad.Columns.Add ("name", typeof (string));
2320 dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2321 dtLoad.Rows.Add (new object[] { 1, "mono 1" });
2322 dtLoad.AcceptChanges ();
2323 DataTableReader dtr = dt.CreateDataReader ();
2324 DataRowAction[] dra = new DataRowAction[] {
2325 DataRowAction.Nothing,// REAL action
2326 DataRowAction.Nothing,// dummy
2327 DataRowAction.Nothing,// dummy
2328 DataRowAction.Nothing,// dummy
2329 DataRowAction.Nothing};// dummy
2330 rowActionInit (dra);
2331 dtLoad.Load (dtr, LoadOption.Upsert);
2333 AssertEquals ("RowData0-C", "mono 1",
2334 dtLoad.Rows[0][1, DataRowVersion.Current]);
2335 AssertEquals ("RowData0-O", "mono 1",
2336 dtLoad.Rows[0][1, DataRowVersion.Original]);
2337 AssertEquals ("RowState0", DataRowState.Unchanged,
2338 dtLoad.Rows[0].RowState);
2342 public void LoadDataRow_LoadOptions () {
2343 // LoadDataRow is covered in detail (without LoadOptions) in DataTableTest2
2344 // LoadOption tests are covered in detail in DataTable.Load().
2345 // Therefore only minimal tests of LoadDataRow with LoadOptions are covered here.
2348 dt = CreateDataTableExample ();
2349 dt.PrimaryKey = new DataColumn[] { dt.Columns[0] }; //add ParentId as Primary Key
2350 dt.Columns["String1"].DefaultValue = "Default";
2352 dr = dt.Select ("ParentId=1")[0];
2354 //Update existing row with LoadOptions = OverwriteChanges
2355 dt.BeginLoadData ();
2356 dt.LoadDataRow (new object[] { 1, null, "Changed" },
2357 LoadOption.OverwriteChanges);
2360 // LoadDataRow(update1) - check column String2
2361 AssertEquals ("DT72-C", "Changed",
2362 dr["String2", DataRowVersion.Current]);
2363 AssertEquals ("DT72-O", "Changed",
2364 dr["String2", DataRowVersion.Original]);
2366 // LoadDataRow(update1) - check row state
2367 AssertEquals ("DT73-LO", DataRowState.Unchanged, dr.RowState);
2369 //Add New row with LoadOptions = Upsert
2370 dt.BeginLoadData ();
2371 dt.LoadDataRow (new object[] { 99, null, "Changed" },
2375 // LoadDataRow(insert1) - check column String2
2376 dr = dt.Select ("ParentId=99")[0];
2377 AssertEquals ("DT75-C", "Changed",
2378 dr["String2", DataRowVersion.Current]);
2380 // LoadDataRow(insert1) - check row state
2381 AssertEquals ("DT76-LO", DataRowState.Added, dr.RowState);
2384 public static DataTable CreateDataTableExample () {
2385 DataTable dtParent = new DataTable ("Parent");
2387 dtParent.Columns.Add ("ParentId", typeof (int));
2388 dtParent.Columns.Add ("String1", typeof (string));
2389 dtParent.Columns.Add ("String2", typeof (string));
2391 dtParent.Columns.Add ("ParentDateTime", typeof (DateTime));
2392 dtParent.Columns.Add ("ParentDouble", typeof (double));
2393 dtParent.Columns.Add ("ParentBool", typeof (bool));
2395 dtParent.Rows.Add (new object[] { 1, "1-String1", "1-String2", new DateTime (2005, 1, 1, 0, 0, 0, 0), 1.534, true });
2396 dtParent.Rows.Add (new object[] { 2, "2-String1", "2-String2", new DateTime (2004, 1, 1, 0, 0, 0, 1), -1.534, true });
2397 dtParent.Rows.Add (new object[] { 3, "3-String1", "3-String2", new DateTime (2003, 1, 1, 0, 0, 1, 0), double.MinValue * 10000, false });
2398 dtParent.Rows.Add (new object[] { 4, "4-String1", "4-String2", new DateTime (2002, 1, 1, 0, 1, 0, 0), double.MaxValue / 10000, true });
2399 dtParent.Rows.Add (new object[] { 5, "5-String1", "5-String2", new DateTime (2001, 1, 1, 1, 0, 0, 0), 0.755, true });
2400 dtParent.Rows.Add (new object[] { 6, "6-String1", "6-String2", new DateTime (2000, 1, 1, 0, 0, 0, 0), 0.001, false });
2401 dtParent.AcceptChanges ();
2405 #endregion // DataTable.Load Tests
2407 #region Read/Write XML Tests
2410 [Category ("NotWorking")]
2411 public void ReadXmlSchema () {
2412 DataTable Table = new DataTable ();
2413 Table.ReadXmlSchema ("Test/System.Data/own_schema1.xsd");
2415 AssertEquals ("test#02", "test_table", Table.TableName);
2416 AssertEquals ("test#03", "", Table.Namespace);
2417 AssertEquals ("test#04", 2, Table.Columns.Count);
2418 AssertEquals ("test#05", 0, Table.Rows.Count);
2419 AssertEquals ("test#06", false, Table.CaseSensitive);
2420 AssertEquals ("test#07", 1, Table.Constraints.Count);
2421 AssertEquals ("test#08", "", Table.Prefix);
2423 Constraint cons = Table.Constraints[0];
2424 AssertEquals ("test#09", "Constraint1", cons.ConstraintName.ToString ());
2425 AssertEquals ("test#10", "Constraint1", cons.ToString ());
2427 DataColumn column = Table.Columns[0];
2428 AssertEquals ("test#11", true, column.AllowDBNull);
2429 AssertEquals ("test#12", false, column.AutoIncrement);
2430 AssertEquals ("test#13", 0L, column.AutoIncrementSeed);
2431 AssertEquals ("test#14", 1L, column.AutoIncrementStep);
2432 AssertEquals ("test#15", "test", column.Caption);
2433 AssertEquals ("test#16", "Element", column.ColumnMapping.ToString ());
2434 AssertEquals ("test#17", "first", column.ColumnName);
2435 AssertEquals ("test#18", "System.String", column.DataType.ToString ());
2436 AssertEquals ("test#19", "test_default_value", column.DefaultValue.ToString ());
2437 AssertEquals ("test#20", false, column.DesignMode);
2438 AssertEquals ("test#21", "", column.Expression);
2439 AssertEquals ("test#22", 100, column.MaxLength);
2440 AssertEquals ("test#23", "", column.Namespace);
2441 AssertEquals ("test#24", 0, column.Ordinal);
2442 AssertEquals ("test#25", "", column.Prefix);
2443 AssertEquals ("test#26", false, column.ReadOnly);
2444 AssertEquals ("test#27", true, column.Unique);
2446 DataColumn column2 = Table.Columns[1];
2447 AssertEquals ("test#28", true, column2.AllowDBNull);
2448 AssertEquals ("test#29", false, column2.AutoIncrement);
2449 AssertEquals ("test#30", 0L, column2.AutoIncrementSeed);
2450 AssertEquals ("test#31", 1L, column2.AutoIncrementStep);
2451 AssertEquals ("test#32", "second", column2.Caption);
2452 AssertEquals ("test#33", "Element", column2.ColumnMapping.ToString ());
2453 AssertEquals ("test#34", "second", column2.ColumnName);
2454 AssertEquals ("test#35", "System.Data.SqlTypes.SqlGuid", column2.DataType.ToString ());
2455 AssertEquals ("test#36", "Null", column2.DefaultValue.ToString ());
2456 AssertEquals ("test#37", false, column2.DesignMode);
2457 AssertEquals ("test#38", "", column2.Expression);
2458 AssertEquals ("test#39", -1, column2.MaxLength);
2459 AssertEquals ("test#40", "", column2.Namespace);
2460 AssertEquals ("test#41", 1, column2.Ordinal);
2461 AssertEquals ("test#42", "", column2.Prefix);
2462 AssertEquals ("test#43", false, column2.ReadOnly);
2463 AssertEquals ("test#44", false, column2.Unique);
2465 DataTable Table2 = new DataTable ();
2466 Table2.ReadXmlSchema ("Test/System.Data/own_schema2.xsd");
2468 AssertEquals ("test#45", "second_test_table", Table2.TableName);
2469 AssertEquals ("test#46", "", Table2.Namespace);
2470 AssertEquals ("test#47", 1, Table2.Columns.Count);
2471 AssertEquals ("test#48", 0, Table2.Rows.Count);
2472 AssertEquals ("test#49", false, Table2.CaseSensitive);
2473 AssertEquals ("test#50", 1, Table2.Constraints.Count);
2474 AssertEquals ("test#51", "", Table2.Prefix);
2476 DataColumn column3 = Table2.Columns[0];
2477 AssertEquals ("test#52", true, column3.AllowDBNull);
2478 AssertEquals ("test#53", false, column3.AutoIncrement);
2479 AssertEquals ("test#54", 0L, column3.AutoIncrementSeed);
2480 AssertEquals ("test#55", 1L, column3.AutoIncrementStep);
2481 AssertEquals ("test#56", "second_first", column3.Caption);
2482 AssertEquals ("test#57", "Element", column3.ColumnMapping.ToString ());
2483 AssertEquals ("test#58", "second_first", column3.ColumnName);
2484 AssertEquals ("test#59", "System.String", column3.DataType.ToString ());
2485 AssertEquals ("test#60", "default_value", column3.DefaultValue.ToString ());
2486 AssertEquals ("test#61", false, column3.DesignMode);
2487 AssertEquals ("test#62", "", column3.Expression);
2488 AssertEquals ("test#63", 100, column3.MaxLength);
2489 AssertEquals ("test#64", "", column3.Namespace);
2490 AssertEquals ("test#65", 0, column3.Ordinal);
2491 AssertEquals ("test#66", "", column3.Prefix);
2492 AssertEquals ("test#67", false, column3.ReadOnly);
2493 AssertEquals ("test#68", true, column3.Unique);
2497 public void ReadXmlSchema_2 () {
2498 DataTable dt = new DataTable ();
2499 string xmlData = string.Empty;
2500 xmlData += "<?xml version=\"1.0\"?>";
2501 xmlData += "<xs:schema id=\"SiteConfiguration\" targetNamespace=\"http://tempuri.org/PortalCfg.xsd\" xmlns:mstns=\"http://tempuri.org/PortalCfg.xsd\" xmlns=\"http://tempuri.org/PortalCfg.xsd\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" attributeFormDefault=\"qualified\" elementFormDefault=\"qualified\">";
2502 xmlData += "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
2503 xmlData += "<xs:complexType>";
2504 xmlData += "<xs:choice maxOccurs=\"unbounded\">";
2505 xmlData += "<xs:element name=\"Tab\">";
2506 xmlData += "<xs:complexType>";
2507 xmlData += "<xs:sequence>";
2508 xmlData += "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
2509 xmlData += "<xs:complexType>";
2510 xmlData += "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
2511 xmlData += "</xs:complexType>";
2512 xmlData += "</xs:element>";
2513 xmlData += "</xs:sequence>";
2514 xmlData += "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
2515 xmlData += "</xs:complexType>";
2516 xmlData += "</xs:element>";
2517 xmlData += "</xs:choice>";
2518 xmlData += "</xs:complexType>";
2519 xmlData += "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
2520 xmlData += "<xs:selector xpath=\".//mstns:Tab\" />";
2521 xmlData += "<xs:field xpath=\"@TabId\" />";
2522 xmlData += "</xs:key>";
2523 xmlData += "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
2524 xmlData += "<xs:selector xpath=\".//mstns:Module\" />";
2525 xmlData += "<xs:field xpath=\"@ModuleID\" />";
2526 xmlData += "</xs:key>";
2527 xmlData += "</xs:element>";
2528 xmlData += "</xs:schema>";
2529 dt.ReadXmlSchema (new StringReader (xmlData));
2533 public void ReadXmlSchema_ByStream () {
2534 DataSet ds1 = new DataSet ();
2535 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2536 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2538 MemoryStream ms1 = new MemoryStream ();
2539 MemoryStream ms2 = new MemoryStream ();
2540 //write xml schema only
2541 //ds1.WriteXmlSchema (ms);
2542 ds1.Tables[0].WriteXmlSchema (ms1);
2543 ds1.Tables[1].WriteXmlSchema (ms2);
2545 MemoryStream ms11 = new MemoryStream (ms1.GetBuffer ());
2546 MemoryStream ms22 = new MemoryStream (ms2.GetBuffer ());
2548 //DataSet ds2 = new DataSet ();
2549 DataTable dt1 = new DataTable ();
2550 DataTable dt2 = new DataTable ();
2552 //ds2.ReadXmlSchema (ms1);
2553 dt1.ReadXmlSchema (ms11);
2554 dt2.ReadXmlSchema (ms22);
2557 // ReadXmlSchema - Tables count
2558 //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS269");
2560 // ReadXmlSchema - Tables 0 Col count
2561 AssertEquals ("DS270", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2563 // ReadXmlSchema - Tables 1 Col count
2564 AssertEquals ("DS271", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2566 //check some colummns types
2567 // ReadXmlSchema - Tables 0 Col type
2568 AssertEquals ("DS272", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2570 // ReadXmlSchema - Tables 1 Col type
2571 AssertEquals ("DS273", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2573 //check that no data exists
2574 // ReadXmlSchema - Table 1 row count
2575 AssertEquals ("DS274",0, dt1.Rows.Count);
2577 // ReadXmlSchema - Table 2 row count
2578 AssertEquals ("DS275",0, dt2.Rows.Count);
2582 [Category ("NotWorking")]
2583 public void ReadWriteXmlSchema_ByFileName () {
2584 string sTempFileName1 = "tmpDataSet_ReadWriteXml_43899-1.xml";
2585 string sTempFileName2 = "tmpDataSet_ReadWriteXml_43899-2.xml";
2587 DataSet ds1 = new DataSet ();
2588 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2589 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2591 ds1.Tables[0].WriteXmlSchema (sTempFileName1);
2592 ds1.Tables[1].WriteXmlSchema (sTempFileName2);
2594 DataTable dt1 = new DataTable ();
2595 DataTable dt2 = new DataTable ();
2597 dt1.ReadXmlSchema (sTempFileName1);
2598 dt2.ReadXmlSchema (sTempFileName2);
2600 AssertEquals ("DS277", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2601 AssertEquals ("DS278", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2602 AssertEquals ("DS279", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2603 AssertEquals ("DS280", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2604 AssertEquals ("DS281", 0, dt1.Rows.Count);
2605 AssertEquals ("DS282", 0, dt2.Rows.Count);
2607 File.Delete (sTempFileName1);
2608 File.Delete (sTempFileName2);
2612 public void ReadXmlSchema_ByTextReader () {
2613 DataSet ds1 = new DataSet ();
2614 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2615 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2617 StringWriter sw1 = new StringWriter ();
2618 StringWriter sw2 = new StringWriter ();
2619 //write xml file, schema only
2620 //ds1.WriteXmlSchema (sw);
2621 ds1.Tables[0].WriteXmlSchema (sw1);
2622 ds1.Tables[1].WriteXmlSchema (sw2);
2624 StringReader sr1 = new StringReader (sw1.GetStringBuilder ().ToString ());
2625 StringReader sr2 = new StringReader (sw2.GetStringBuilder ().ToString ());
2626 //copy both data and schema
2627 //DataSet ds2 = new DataSet ();
2628 DataTable dt1 = new DataTable ();
2629 DataTable dt2 = new DataTable ();
2631 //ds2.ReadXmlSchema (sr);
2632 dt1.ReadXmlSchema (sr1);
2633 dt2.ReadXmlSchema (sr2);
2636 // ReadXmlSchema - Tables count
2637 //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS283");
2639 // ReadXmlSchema - Tables 0 Col count
2640 AssertEquals ("DS284", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2642 // ReadXmlSchema - Tables 1 Col count
2643 AssertEquals ("DS285", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2645 //check some colummns types
2646 // ReadXmlSchema - Tables 0 Col type
2647 AssertEquals ("DS286", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2649 // ReadXmlSchema - Tables 1 Col type
2650 AssertEquals ("DS287", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2652 //check that no data exists
2653 // ReadXmlSchema - Table 1 row count
2654 AssertEquals ("DS288", 0, dt1.Rows.Count);
2656 // ReadXmlSchema - Table 2 row count
2657 AssertEquals ("DS289", 0, dt2.Rows.Count);
2661 public void ReadXmlSchema_ByXmlReader () {
2662 DataSet ds1 = new DataSet ();
2663 ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2664 ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2666 StringWriter sw1 = new StringWriter ();
2667 XmlTextWriter xmlTW1 = new XmlTextWriter (sw1);
2668 StringWriter sw2 = new StringWriter ();
2669 XmlTextWriter xmlTW2 = new XmlTextWriter (sw2);
2671 //write xml file, schema only
2672 ds1.Tables[0].WriteXmlSchema (xmlTW1);
2674 ds1.Tables[1].WriteXmlSchema (xmlTW2);
2677 StringReader sr1 = new StringReader (sw1.ToString ());
2678 XmlTextReader xmlTR1 = new XmlTextReader (sr1);
2679 StringReader sr2 = new StringReader (sw2.ToString ());
2680 XmlTextReader xmlTR2 = new XmlTextReader (sr2);
2682 //copy both data and schema
2683 //DataSet ds2 = new DataSet ();
2684 DataTable dt1 = new DataTable ();
2685 DataTable dt2 = new DataTable ();
2687 //ds2.ReadXmlSchema (xmlTR);
2688 dt1.ReadXmlSchema (xmlTR1);
2689 dt2.ReadXmlSchema (xmlTR2);
2692 // ReadXmlSchema - Tables count
2693 //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS290");
2695 // ReadXmlSchema - Tables 0 Col count
2696 AssertEquals ("DS291", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2698 // ReadXmlSchema - Tables 1 Col count
2699 AssertEquals ("DS292", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2701 //check some colummns types
2702 // ReadXmlSchema - Tables 0 Col type
2703 AssertEquals ("DS293", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2705 // ReadXmlSchema - Tables 1 Col type
2706 AssertEquals ("DS294", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2708 //check that no data exists
2709 // ReadXmlSchema - Table 1 row count
2710 AssertEquals ("DS295", 0, dt1.Rows.Count);
2712 // ReadXmlSchema - Table 2 row count
2713 AssertEquals ("DS296", 0, dt2.Rows.Count);
2717 [Category ("NotWorking")]
2718 public void WriteXmlSchema () {
2719 DataSet ds = new DataSet ();
2720 ds.ReadXml ("Test/System.Data/region.xml");
2721 TextWriter writer = new StringWriter ();
2722 ds.Tables[0].WriteXmlSchema (writer);
2725 string TextString = GetNormalizedSchema (writer.ToString ());
2726 //string TextString = writer.ToString ();
2728 string substring = TextString.Substring (0, TextString.IndexOf (EOL));
2729 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2730 AssertEquals ("test#01", "<?xml version=\"1.0\" encoding=\"utf-16\"?>", substring);
2732 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2733 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2734 AssertEquals ("test#02", "<xs:schema id=\"Root\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">", substring);
2736 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2737 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2738 AssertEquals ("test#03", " <xs:element msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Region\" msdata:UseCurrentLocale=\"true\" name=\"Root\">", substring);
2740 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2741 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2742 AssertEquals ("test#04", " <xs:complexType>", substring);
2744 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2745 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2746 AssertEquals ("test#05", " <xs:choice maxOccurs=\"unbounded\" minOccurs=\"0\">", substring);
2748 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2749 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2750 AssertEquals ("test#06", " <xs:element name=\"Region\">", substring);
2752 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2753 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2754 AssertEquals ("test#07", " <xs:complexType>", substring);
2756 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2757 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2758 AssertEquals ("test#08", " <xs:sequence>", substring);
2760 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2761 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2762 AssertEquals ("test#09", " <xs:element minOccurs=\"0\" name=\"RegionID\" type=\"xs:string\" />", substring);
2764 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2765 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2766 AssertEquals ("test#10", " <xs:element minOccurs=\"0\" name=\"RegionDescription\" type=\"xs:string\" />", substring);
2768 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2769 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2770 AssertEquals ("test#11", " </xs:sequence>", substring);
2772 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2773 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2774 AssertEquals ("test#12", " </xs:complexType>", substring);
2776 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2777 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2778 AssertEquals ("test#13", " </xs:element>", substring);
2780 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2781 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2782 AssertEquals ("test#14", " </xs:choice>", substring);
2784 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2785 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2786 AssertEquals ("test#15", " </xs:complexType>", substring);
2788 substring = TextString.Substring (0, TextString.IndexOf (EOL));
2789 TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2790 AssertEquals ("test#16", " </xs:element>", substring);
2792 AssertEquals ("test#17", "</xs:schema>", TextString);
2796 [Category ("NotWorking")]
2797 public void WriteXmlSchema2 () {
2798 string xml = @"<myDataSet xmlns='NetFrameWork'><myTable><id>0</id><item>item 0</item></myTable><myTable><id>1</id><item>item 1</item></myTable><myTable><id>2</id><item>item 2</item></myTable><myTable><id>3</id><item>item 3</item></myTable><myTable><id>4</id><item>item 4</item></myTable><myTable><id>5</id><item>item 5</item></myTable><myTable><id>6</id><item>item 6</item></myTable><myTable><id>7</id><item>item 7</item></myTable><myTable><id>8</id><item>item 8</item></myTable><myTable><id>9</id><item>item 9</item></myTable></myDataSet>";
2799 string schema = @"<?xml version='1.0' encoding='utf-16'?>
2800 <xs:schema id='myDataSet' targetNamespace='NetFrameWork' xmlns:mstns='NetFrameWork' xmlns='NetFrameWork' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified'>
2801 <xs:element name='myDataSet' msdata:IsDataSet='true' msdata:MainDataTable='NetFrameWork_x003A_myTable' msdata:UseCurrentLocale='true'>
2803 <xs:choice minOccurs='0' maxOccurs='unbounded'>
2804 <xs:element name='myTable'>
2807 <xs:element name='id' msdata:AutoIncrement='true' type='xs:int' minOccurs='0' />
2808 <xs:element name='item' type='xs:string' minOccurs='0' />
2816 DataSet OriginalDataSet = new DataSet ("myDataSet");
2817 OriginalDataSet.Namespace = "NetFrameWork";
2818 DataTable myTable = new DataTable ("myTable");
2819 DataColumn c1 = new DataColumn ("id", typeof (int));
2820 c1.AutoIncrement = true;
2821 DataColumn c2 = new DataColumn ("item");
2822 myTable.Columns.Add (c1);
2823 myTable.Columns.Add (c2);
2824 OriginalDataSet.Tables.Add (myTable);
2827 for (int i = 0; i < 10; i++) {
2828 newRow = myTable.NewRow ();
2829 newRow["item"] = "item " + i;
2830 myTable.Rows.Add (newRow);
2832 OriginalDataSet.AcceptChanges ();
2834 StringWriter sw = new StringWriter ();
2835 XmlTextWriter xtw = new XmlTextWriter (sw);
2836 xtw.QuoteChar = '\'';
2837 OriginalDataSet.WriteXml (xtw);
2838 string result = sw.ToString ();
2840 AssertEquals (xml, result);
2842 sw = new StringWriter ();
2843 xtw = new XmlTextWriter (sw);
2844 xtw.Formatting = Formatting.Indented;
2845 OriginalDataSet.Tables[0].WriteXmlSchema (xtw);
2846 result = sw.ToString ();
2848 result = result.Replace ("\r\n", "\n").Replace ('"', '\'');
2849 AssertEquals (schema.Replace ("\r\n", "\n"), result);
2853 [Category ("NotWorking")]
2854 public void WriteXmlSchema3 () {
2855 string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2856 <xs:schema id=""ExampleDataSet"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2857 <xs:element name=""ExampleDataSet"" msdata:IsDataSet=""true"" msdata:MainDataTable=""ExampleDataTable"" msdata:UseCurrentLocale=""true"">
2859 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2860 <xs:element name=""ExampleDataTable"">
2862 <xs:attribute name=""PrimaryKeyColumn"" type=""xs:int"" use=""required"" />
2867 <xs:unique name=""PK_ExampleDataTable"" msdata:PrimaryKey=""true"">
2868 <xs:selector xpath="".//ExampleDataTable"" />
2869 <xs:field xpath=""@PrimaryKeyColumn"" />
2873 DataSet ds = new DataSet ("ExampleDataSet");
2875 ds.Tables.Add (new DataTable ("ExampleDataTable"));
2876 ds.Tables["ExampleDataTable"].Columns.Add (
2877 new DataColumn ("PrimaryKeyColumn", typeof (int), "", MappingType.Attribute));
2878 ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"].AllowDBNull = false;
2880 ds.Tables["ExampleDataTable"].Constraints.Add (
2881 "PK_ExampleDataTable",
2882 ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"],
2885 ds.AcceptChanges ();
2886 StringWriter sw = new StringWriter ();
2887 ds.Tables[0].WriteXmlSchema (sw);
2889 string result = sw.ToString ();
2891 AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2892 //AssertEquals (xmlschema, result.Replace ("\r\n", "\n"));
2896 [Category ("NotWorking")]
2897 public void WriteXmlSchema4 () {
2898 string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2899 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2900 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
2902 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2903 <xs:element name=""MyType"">
2905 <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2906 <xs:attribute name=""Desc"" type=""xs:string"" />
2913 DataSet ds = new DataSet ("Example");
2915 // Add MyType DataTable
2916 DataTable dt = new DataTable ("MyType");
2919 dt.Columns.Add (new DataColumn ("ID", typeof (int), "",
2920 MappingType.Attribute));
2921 dt.Columns["ID"].AllowDBNull = false;
2923 dt.Columns.Add (new DataColumn ("Desc", typeof
2924 (string), "", MappingType.Attribute));
2926 ds.AcceptChanges ();
2928 StringWriter sw = new StringWriter ();
2929 ds.Tables[0].WriteXmlSchema (sw);
2931 string result = sw.ToString ();
2933 AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2937 [Category ("NotWorking")]
2938 public void WriteXmlSchema5 () {
2939 string xmlschema1 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2940 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2941 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""StandAlone"" msdata:UseCurrentLocale=""true"">
2943 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2944 <xs:element name=""StandAlone"">
2946 <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2947 <xs:attribute name=""Desc"" type=""xs:string"" use=""required"" />
2954 string xmlschema2 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2955 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2956 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Dimension"" msdata:UseCurrentLocale=""true"">
2958 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2959 <xs:element name=""Dimension"">
2961 <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2962 <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2967 <xs:unique name=""PK_Dimension"" msdata:PrimaryKey=""true"">
2968 <xs:selector xpath="".//Dimension"" />
2969 <xs:field xpath=""@Number"" />
2973 string xmlschema3 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2974 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2975 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Element"" msdata:UseCurrentLocale=""true"">
2977 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2978 <xs:element name=""Element"">
2980 <xs:attribute name=""Dimension"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2981 <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2982 <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2987 <xs:unique name=""PK_Element"" msdata:PrimaryKey=""true"">
2988 <xs:selector xpath="".//Element"" />
2989 <xs:field xpath=""@Dimension"" />
2990 <xs:field xpath=""@Number"" />
2994 DataSet ds = new DataSet ("Example");
2996 // Add a DataTable with no ReadOnly columns
2997 DataTable dt1 = new DataTable ("StandAlone");
2998 ds.Tables.Add (dt1);
3000 // Add a ReadOnly column
3001 dt1.Columns.Add (new DataColumn ("ID", typeof (int), "",
3002 MappingType.Attribute));
3003 dt1.Columns["ID"].AllowDBNull = false;
3005 dt1.Columns.Add (new DataColumn ("Desc", typeof
3006 (string), "", MappingType.Attribute));
3007 dt1.Columns["Desc"].AllowDBNull = false;
3009 // Add related DataTables with ReadOnly columns
3010 DataTable dt2 = new DataTable ("Dimension");
3011 ds.Tables.Add (dt2);
3012 dt2.Columns.Add (new DataColumn ("Number", typeof
3013 (int), "", MappingType.Attribute));
3014 dt2.Columns["Number"].AllowDBNull = false;
3015 dt2.Columns["Number"].ReadOnly = true;
3017 dt2.Columns.Add (new DataColumn ("Title", typeof
3018 (string), "", MappingType.Attribute));
3019 dt2.Columns["Title"].AllowDBNull = false;
3021 dt2.Constraints.Add ("PK_Dimension", dt2.Columns["Number"], true);
3023 DataTable dt3 = new DataTable ("Element");
3024 ds.Tables.Add (dt3);
3026 dt3.Columns.Add (new DataColumn ("Dimension", typeof
3027 (int), "", MappingType.Attribute));
3028 dt3.Columns["Dimension"].AllowDBNull = false;
3029 dt3.Columns["Dimension"].ReadOnly = true;
3031 dt3.Columns.Add (new DataColumn ("Number", typeof
3032 (int), "", MappingType.Attribute));
3033 dt3.Columns["Number"].AllowDBNull = false;
3034 dt3.Columns["Number"].ReadOnly = true;
3036 dt3.Columns.Add (new DataColumn ("Title", typeof
3037 (string), "", MappingType.Attribute));
3038 dt3.Columns["Title"].AllowDBNull = false;
3040 dt3.Constraints.Add ("PK_Element", new DataColumn[] {
3041 dt3.Columns ["Dimension"],
3042 dt3.Columns ["Number"] }, true);
3044 ds.AcceptChanges ();
3046 StringWriter sw1 = new StringWriter ();
3047 ds.Tables[0].WriteXmlSchema (sw1);
3048 string result1 = sw1.ToString ();
3049 AssertEquals (xmlschema1.Replace ("\r\n", "\n"), result1.Replace ("\r\n", "\n"));
3051 StringWriter sw2 = new StringWriter ();
3052 ds.Tables[1].WriteXmlSchema (sw2);
3053 string result2 = sw2.ToString ();
3054 AssertEquals (xmlschema2.Replace ("\r\n", "\n"), result2.Replace ("\r\n", "\n"));
3056 StringWriter sw3 = new StringWriter ();
3057 ds.Tables[2].WriteXmlSchema (sw3);
3058 string result3 = sw3.ToString ();
3059 AssertEquals (xmlschema3.Replace ("\r\n", "\n"), result3.Replace ("\r\n", "\n"));
3063 [Category ("NotWorking")]
3064 public void WriteXmlSchema6 () {
3065 string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
3066 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
3067 <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
3069 <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
3070 <xs:element name=""MyType"">
3072 <xs:attribute name=""Desc"">
3074 <xs:restriction base=""xs:string"">
3075 <xs:maxLength value=""32"" />
3085 DataSet ds = new DataSet ("Example");
3087 // Add MyType DataTable
3088 ds.Tables.Add ("MyType");
3090 ds.Tables["MyType"].Columns.Add (new DataColumn (
3091 "Desc", typeof (string), "", MappingType.Attribute));
3092 ds.Tables["MyType"].Columns["Desc"].MaxLength = 32;
3094 ds.AcceptChanges ();
3096 StringWriter sw = new StringWriter ();
3097 ds.Tables[0].WriteXmlSchema (sw);
3099 string result = sw.ToString ();
3101 AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
3105 public void WriteXmlSchema7 () {
3106 DataSet ds = new DataSet ();
3107 DataTable dt = new DataTable ("table");
3108 dt.Columns.Add ("col1");
3109 dt.Columns.Add ("col2");
3111 dt.Rows.Add (new object[] { "foo", "bar" });
3112 StringWriter sw = new StringWriter ();
3113 ds.Tables[0].WriteXmlSchema (sw);
3114 Assert (sw.ToString ().IndexOf ("xmlns=\"\"") > 0);
3118 public void WriteXmlSchema_ConstraintNameWithSpaces () {
3119 DataSet ds = new DataSet ();
3120 DataTable table1 = ds.Tables.Add ("table1");
3121 DataTable table2 = ds.Tables.Add ("table2");
3123 table1.Columns.Add ("col1", typeof (int));
3124 table2.Columns.Add ("col1", typeof (int));
3126 table1.Constraints.Add ("uc 1", table1.Columns[0], false);
3127 table2.Constraints.Add ("fc 1", table1.Columns[0], table2.Columns[0]);
3129 StringWriter sw1 = new StringWriter ();
3130 StringWriter sw2 = new StringWriter ();
3132 //should not throw an exception
3133 ds.Tables[0].WriteXmlSchema (sw1);
3134 ds.Tables[1].WriteXmlSchema (sw2);
3138 public void WriteXmlSchema_ForignKeyConstraint () {
3139 DataSet ds1 = new DataSet ();
3141 DataTable table1 = ds1.Tables.Add ();
3142 DataTable table2 = ds1.Tables.Add ();
3144 DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
3145 DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
3147 table2.Constraints.Add ("fk", col1_1, col2_1);
3149 StringWriter sw1 = new StringWriter ();
3150 ds1.Tables[0].WriteXmlSchema (sw1);
3151 String xml1 = sw1.ToString ();
3152 Assert ("#1", xml1.IndexOf (@"<xs:unique name=""Constraint1"">") != -1);
3154 StringWriter sw2 = new StringWriter ();
3155 ds1.Tables[1].WriteXmlSchema (sw2);
3156 String xml2 = sw2.ToString ();
3157 Assert ("#2", xml2.IndexOf (@"<xs:unique name=""Constraint1"">") == -1);
3161 [Category ("NotWorking")]
3162 public void WriteXmlSchema_Relations_ForeignKeys () {
3163 MemoryStream ms1 = null;
3164 MemoryStream ms2 = null;
3165 MemoryStream msA = null;
3166 MemoryStream msB = null;
3168 DataSet ds1 = new DataSet ();
3170 DataTable table1 = ds1.Tables.Add ("Table 1");
3171 DataTable table2 = ds1.Tables.Add ("Table 2");
3173 DataColumn col1_1 = table1.Columns.Add ("col 1", typeof (int));
3174 DataColumn col1_2 = table1.Columns.Add ("col 2", typeof (int));
3175 DataColumn col1_3 = table1.Columns.Add ("col 3", typeof (int));
3176 DataColumn col1_4 = table1.Columns.Add ("col 4", typeof (int));
3177 DataColumn col1_5 = table1.Columns.Add ("col 5", typeof (int));
3178 DataColumn col1_6 = table1.Columns.Add ("col 6", typeof (int));
3179 DataColumn col1_7 = table1.Columns.Add ("col 7", typeof (int));
3181 DataColumn col2_1 = table2.Columns.Add ("col 1", typeof (int));
3182 DataColumn col2_2 = table2.Columns.Add ("col 2", typeof (int));
3183 DataColumn col2_3 = table2.Columns.Add ("col 3", typeof (int));
3184 DataColumn col2_4 = table2.Columns.Add ("col 4", typeof (int));
3185 DataColumn col2_5 = table2.Columns.Add ("col 5", typeof (int));
3186 DataColumn col2_6 = table2.Columns.Add ("col 6", typeof (int));
3187 DataColumn col2_7 = table2.Columns.Add ("col 7", typeof (int));
3189 ds1.Relations.Add ("rel 1",
3190 new DataColumn[] { col1_1, col1_2 },
3191 new DataColumn[] { col2_1, col2_2 },
3193 ds1.Relations.Add ("rel 2",
3194 new DataColumn[] { col1_3, col1_4 },
3195 new DataColumn[] { col2_3, col2_4 },
3197 table2.Constraints.Add ("fk 1",
3198 new DataColumn[] { col1_5, col1_6 },
3199 new DataColumn[] { col2_5, col2_6 });
3200 table1.Constraints.Add ("fk 2",
3201 new DataColumn[] { col2_5, col2_6 },
3202 new DataColumn[] { col1_5, col1_6 });
3204 table1.Constraints.Add ("pk 1", col1_7, true);
3205 table2.Constraints.Add ("pk 2", col2_7, true);
3207 ms1 = new MemoryStream ();
3208 ds1.Tables[0].WriteXmlSchema (ms1);
3209 ms2 = new MemoryStream ();
3210 ds1.Tables[1].WriteXmlSchema (ms2);
3212 msA = new MemoryStream (ms1.GetBuffer ());
3213 DataTable dtA = new DataTable ();
3214 dtA.ReadXmlSchema (msA);
3216 msB = new MemoryStream (ms2.GetBuffer ());
3217 DataTable dtB = new DataTable ();
3218 dtB.ReadXmlSchema (msB);
3220 AssertEquals ("#2", 3, dtA.Constraints.Count);
3221 AssertEquals ("#3", 2, dtB.Constraints.Count);
3223 Assert ("#5", dtA.Constraints.Contains ("pk 1"));
3224 Assert ("#6", dtA.Constraints.Contains ("Constraint1"));
3225 Assert ("#7", dtA.Constraints.Contains ("Constraint2"));
3226 Assert ("#9", dtB.Constraints.Contains ("pk 2"));
3227 Assert ("#10", dtB.Constraints.Contains ("Constraint1"));
3231 [Category ("NotWorking")]
3232 public void WriteXmlSchema_DifferentNamespace () {
3233 string schema = @"<xs:schema id='NewDataSet' targetNamespace='urn:bar' xmlns:mstns='urn:bar' xmlns='urn:bar' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app1='urn:baz' xmlns:app2='urn:foo' msdata:schemafragmentcount='3'>
3234 <xs:import namespace='urn:foo' />
3235 <xs:import namespace='urn:baz' />
3236 <xs:element name='NewDataSet' msdata:IsDataSet='true' msdata:MainDataTable='urn_x003A_foo_x003A_NS1Table' msdata:UseCurrentLocale='true'>
3238 <xs:choice minOccurs='0' maxOccurs='unbounded'>
3239 <xs:element ref='app2:NS1Table' />
3244 <xs:schema targetNamespace='urn:baz' xmlns:mstns='urn:bar' xmlns='urn:baz' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app1='urn:baz' xmlns:app2='urn:foo'>
3245 <xs:import namespace='urn:foo' />
3246 <xs:import namespace='urn:bar' />
3247 <xs:element name='column2' type='xs:string' />
3249 <xs:schema targetNamespace='urn:foo' xmlns:mstns='urn:bar' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app2='urn:foo' xmlns:app1='urn:baz'>
3250 <xs:import namespace='urn:bar' />
3251 <xs:import namespace='urn:baz' />
3252 <xs:element name='NS1Table'>
3255 <xs:element name='column1' type='xs:string' minOccurs='0' />
3256 <xs:element ref='app1:column2' minOccurs='0' />
3261 DataSet ds = new DataSet ();
3262 DataTable dt = new DataTable ();
3263 dt.TableName = "NS1Table";
3264 dt.Namespace = "urn:foo";
3265 dt.Columns.Add ("column1");
3266 dt.Columns.Add ("column2");
3267 dt.Columns[1].Namespace = "urn:baz";
3269 DataTable dt2 = new DataTable ();
3270 dt2.TableName = "NS2Table";
3271 dt2.Namespace = "urn:bar";
3272 ds.Tables.Add (dt2);
3273 ds.Namespace = "urn:bar";
3275 StringWriter sw1 = new StringWriter ();
3276 XmlTextWriter xw1 = new XmlTextWriter (sw1);
3277 xw1.Formatting = Formatting.Indented;
3278 xw1.QuoteChar = '\'';
3279 ds.Tables[0].WriteXmlSchema (xw1);
3280 string result1 = sw1.ToString ();
3281 AssertEquals ("#1", schema, result1);
3283 StringWriter sw2 = new StringWriter ();
3284 XmlTextWriter xw2 = new XmlTextWriter (sw2);
3285 xw2.Formatting = Formatting.Indented;
3286 xw2.QuoteChar = '\'';
3287 ds.Tables[0].WriteXmlSchema (xw2);
3288 string result2 = sw2.ToString ();
3289 AssertEquals ("#2", schema, result2);
3293 [Category ("NotWorking")]
3294 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3295 // and is commented-out TWICE below
3296 public void WriteXmlSchema_Hierarchy () {
3297 DataSet ds = new DataSet ();
3298 DataTable table1 = new DataTable ();
3299 DataColumn idColumn = table1.Columns.Add ("ID", typeof (Int32));
3300 table1.Columns.Add ("Name", typeof (String));
3301 table1.PrimaryKey = new DataColumn[] { idColumn };
3302 DataTable table2 = new DataTable ();
3303 table2.Columns.Add (new DataColumn ("OrderID", typeof (Int32)));
3304 table2.Columns.Add (new DataColumn ("CustomerID", typeof (Int32)));
3305 table2.Columns.Add (new DataColumn ("OrderDate", typeof (DateTime)));
3306 table2.PrimaryKey = new DataColumn[] { table2.Columns[0] };
3307 ds.Tables.Add (table1);
3308 ds.Tables.Add (table2);
3309 ds.Relations.Add ("CustomerOrder",
3310 new DataColumn[] { table1.Columns[0] },
3311 new DataColumn[] { table2.Columns[1] }, true);
3313 StringWriter writer1 = new StringWriter ();
3314 //table1.WriteXmlSchema (writer1, false);
3315 string expected1 = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n <xs:complexType>\r\n <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\r\n <xs:element name=\"Table1\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"ID\" type=\"xs:int\" />\r\n <xs:element name=\"Name\" type=\"xs:string\" minOccurs=\"0\" />\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n </xs:choice>\r\n </xs:complexType>\r\n <xs:unique name=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n <xs:selector xpath=\".//Table1\" />\r\n <xs:field xpath=\"ID\" />\r\n </xs:unique>\r\n </xs:element>\r\n</xs:schema>";
3316 AssertEquals ("#1", expected1, writer1.ToString());
3318 StringWriter writer2 = new StringWriter ();
3319 //table1.WriteXmlSchema (writer2, true);
3320 string expected2 = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n <xs:complexType>\r\n <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\r\n <xs:element name=\"Table1\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"ID\" type=\"xs:int\" />\r\n <xs:element name=\"Name\" type=\"xs:string\" minOccurs=\"0\" />\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n <xs:element name=\"Table2\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"OrderID\" type=\"xs:int\" />\r\n <xs:element name=\"CustomerID\" type=\"xs:int\" minOccurs=\"0\" />\r\n <xs:element name=\"OrderDate\" type=\"xs:dateTime\" minOccurs=\"0\" />\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n </xs:choice>\r\n </xs:complexType>\r\n <xs:unique name=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n <xs:selector xpath=\".//Table1\" />\r\n <xs:field xpath=\"ID\" />\r\n </xs:unique>\r\n <xs:unique name=\"Table2_Constraint1\" msdata:ConstraintName=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n <xs:selector xpath=\".//Table2\" />\r\n <xs:field xpath=\"OrderID\" />\r\n </xs:unique>\r\n <xs:keyref name=\"CustomerOrder\" refer=\"Constraint1\">\r\n <xs:selector xpath=\".//Table2\" />\r\n <xs:field xpath=\"CustomerID\" />\r\n </xs:keyref>\r\n </xs:element>\r\n</xs:schema>";
3321 AssertEquals ("#2", expected2, writer2.ToString ());
3325 [Category ("NotWorking")]
3326 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3327 // and is commented-out TWICE below
3328 public void ReadWriteXmlSchema () {
3329 DataSet ds = new DataSet ();
3330 ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3331 // check dataset properties before testing write
3332 AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3333 AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3334 AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3335 AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3336 // FIXME: currently order is not compatible. Use name as index
3337 AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3338 AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3340 ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.InferSchema);
3341 ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3343 TextWriter writer1 = new StringWriter ();
3344 ds.Tables[0].WriteXmlSchema (writer1);
3345 string TextString1 = GetNormalizedSchema (writer1.ToString ());
3346 string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3347 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3348 @"<xs:complexType name=""bookstoreType"">" +
3349 @"</xs:complexType>" +
3350 @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3351 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3352 @"<xs:complexType>" +
3353 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3354 @"<xs:element ref=""bookstore"" />" +
3356 @"</xs:complexType>" +
3359 AssertEquals ("#1", expected1, TextString1.Replace("\r\n","").Replace(" ",""));
3361 TextWriter writer2 = new StringWriter ();
3362 //ds.Tables[1].WriteXmlSchema (writer2,false);
3363 string TextString2 = GetNormalizedSchema (writer2.ToString ());
3364 string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3365 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3366 @"<xs:complexType name=""bookType"">" +
3368 @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3369 @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3371 @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3372 @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3373 @"</xs:complexType>" +
3374 @"<xs:element name=""book"" type=""bookType"" />" +
3375 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3376 @"<xs:complexType>" +
3377 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3378 @"<xs:element ref=""book"" />" +
3380 @"</xs:complexType>" +
3383 AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace (" ", ""));
3385 TextWriter writer3 = new StringWriter ();
3386 ds.Tables[2].WriteXmlSchema (writer3);
3387 string TextString3 = GetNormalizedSchema (writer3.ToString ());
3388 string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3389 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3390 @"<xs:complexType name=""authorName"">" +
3392 @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3393 @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3395 @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3396 @"</xs:complexType>" +
3397 @"<xs:element name=""author"" type=""authorName"" />" +
3398 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3399 @"<xs:complexType>" +
3400 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3401 @"<xs:element ref=""author"" />" +
3403 @"</xs:complexType>" +
3406 AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace (" ", ""));
3408 TextWriter writer4 = new StringWriter ();
3409 ds.Tables[3].WriteXmlSchema (writer4);
3410 string TextString4 = GetNormalizedSchema (writer4.ToString ());
3411 string expected4 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3412 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3413 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""Region"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3414 @"<xs:complexType>" +
3415 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3416 @"<xs:element name=""Region"">" +
3417 @"<xs:complexType>" +
3419 @"<xs:element minOccurs=""0"" name=""RegionID"" type=""xs:string"" />" +
3420 @"<xs:element minOccurs=""0"" name=""RegionDescription"" type=""xs:string"" />" +
3422 @"</xs:complexType>" +
3425 @"</xs:complexType>" +
3428 AssertEquals ("#4", expected4, TextString4.Replace ("\r\n", "").Replace (" ", ""));
3432 [Category ("NotWorking")]
3433 public void ReadWriteXmlSchema_IgnoreSchema () {
3434 DataSet ds = new DataSet ();
3435 ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3436 // check dataset properties before testing write
3437 AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3438 AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3439 AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3440 AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3441 // FIXME: currently order is not compatible. Use name as index
3442 AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3443 AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3445 ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.IgnoreSchema);
3446 ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3448 TextWriter writer1 = new StringWriter ();
3449 ds.Tables[0].WriteXmlSchema (writer1);
3450 string TextString1 = GetNormalizedSchema (writer1.ToString ());
3451 string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3452 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3453 @"<xs:complexType name=""bookstoreType"">" +
3454 @"</xs:complexType>" +
3455 @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3456 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3457 @"<xs:complexType>" +
3458 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3459 @"<xs:element ref=""bookstore"" />" +
3461 @"</xs:complexType>" +
3464 AssertEquals ("#1", expected1, TextString1.Replace ("\r\n", "").Replace (" ", ""));
3466 TextWriter writer2 = new StringWriter ();
3467 //ds.Tables[1].WriteXmlSchema (writer2, false);
3468 string TextString2 = GetNormalizedSchema (writer2.ToString ());
3469 string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3470 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3471 @"<xs:complexType name=""bookType"">" +
3473 @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3474 @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3476 @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3477 @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3478 @"</xs:complexType>" +
3479 @"<xs:element name=""book"" type=""bookType"" />" +
3480 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3481 @"<xs:complexType>" +
3482 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3483 @"<xs:element ref=""book"" />" +
3485 @"</xs:complexType>" +
3488 AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace (" ", ""));
3490 TextWriter writer3 = new StringWriter ();
3491 ds.Tables[2].WriteXmlSchema (writer3);
3492 string TextString3 = GetNormalizedSchema (writer3.ToString ());
3493 string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3494 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3495 @"<xs:complexType name=""authorName"">" +
3497 @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3498 @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3500 @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3501 @"</xs:complexType>" +
3502 @"<xs:element name=""author"" type=""authorName"" />" +
3503 @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3504 @"<xs:complexType>" +
3505 @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3506 @"<xs:element ref=""author"" />" +
3508 @"</xs:complexType>" +
3511 AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace (" ", ""));
3513 TextWriter writer4 = new StringWriter ();
3516 ds.Tables[3].WriteXmlSchema (writer4);
3518 catch (Exception ex) {
3519 expStr = ex.Message;
3521 AssertEquals ("#4", "Cannot find table 3.", expStr);
3525 [Category ("NotWorking")]
3526 public void ReadWriteXmlSchema_2 () {
3527 DataSet ds = new DataSet ("dataset");
3528 ds.Tables.Add ("table1");
3529 ds.Tables.Add ("table2");
3530 ds.Tables[0].Columns.Add ("col");
3531 ds.Tables[1].Columns.Add ("col");
3532 ds.Relations.Add ("rel", ds.Tables[0].Columns[0], ds.Tables[1].Columns[0], true);
3534 MemoryStream ms1 = new MemoryStream ();
3535 ds.Tables[0].WriteXmlSchema (ms1);
3536 MemoryStream ms2 = new MemoryStream ();
3537 ds.Tables[1].WriteXmlSchema (ms2);
3539 DataSet ds1 = new DataSet ();
3542 ds1.Tables[0].ReadXmlSchema (new MemoryStream (ms1.GetBuffer ()));
3543 ds1.Tables[1].ReadXmlSchema (new MemoryStream (ms2.GetBuffer ()));
3545 AssertEquals ("#1", 0, ds1.Relations.Count);
3546 AssertEquals ("#2", 1, ds1.Tables[0].Columns.Count);
3547 AssertEquals ("#3", 1, ds1.Tables[1].Columns.Count);
3551 [ExpectedException (typeof (XmlException))]
3552 public void ReadWriteXmlSchemaExp_NoRootElmnt () {
3553 MemoryStream ms = new MemoryStream ();
3554 DataTable dtr = new DataTable ();
3555 dtr.ReadXmlSchema (ms);
3559 [Category ("NotWorking")]
3560 [ExpectedException (typeof (InvalidOperationException))]
3561 public void ReadWriteXmlSchemaExp_NoTableName () {
3562 DataTable dtw = new DataTable ();
3563 MemoryStream ms = new MemoryStream ();
3564 dtw.WriteXmlSchema (ms);
3568 [ExpectedException (typeof (ArgumentException))]
3569 public void ReadWriteXmlSchemaExp_TableNameConflict () {
3570 DataTable dtw = new DataTable ("Table1");
3571 StringWriter writer1 = new StringWriter ();
3572 dtw.WriteXmlSchema (writer1);
3573 DataTable dtr = new DataTable ("Table2");
3574 StringReader reader1 = new StringReader (writer1.ToString());
3575 dtr.ReadXmlSchema (reader1);
3578 #endregion // Read/Write XML Tests
3585 public class MyDataTable:DataTable {
3587 public static int count = 0;
3589 public MyDataTable() {
3598 public class AppDomainsAndFormatInfo
3600 public void Remote ()
3602 int n = (int) Convert.ChangeType ("5", typeof (int));
3603 Assertion.AssertEquals ("n", 5, n);
3607 public void NFIFromBug55978 ()
3609 AppDomain domain = AppDomain.CreateDomain ("testdomain");
3610 AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
3612 domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
3613 AppDomain.Unload (domain);
3618 public void Bug55978 ()
3620 DataTable dt = new DataTable ();
3621 dt.Columns.Add ("StartDate", typeof (DateTime));
3624 DateTime date = DateTime.Now;
3626 for (int i = 0; i < 10; i++) {
3628 dr ["StartDate"] = date.AddDays (i);
3632 DataView dv = dt.DefaultView;
3633 dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
3634 Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
3635 Assertion.AssertEquals ("View", 2, dv.Count);