Merge pull request #601 from knocte/sock_improvements
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowTest.cs
1 // DataRowTest.cs - NUnit Test Cases for System.DataRow
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Daniel Morgan <danmorg@sc.rr.com>
6 //   Roopa Wilson (rowilson@novell.com)
7 //
8 // (C) Copyright 2002 Franklin Wise
9 // (C) Copyright 2003 Daniel Morgan
10 // (C) Copyright 2003 Martin Willemoes Hansen
11 // 
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36
37 using NUnit.Framework;
38 using System;
39 using System.Data;
40
41 namespace MonoTests.System.Data
42 {
43         [TestFixture]
44         public class DataRowTest : Assertion {
45                 private DataTable table;                                                
46                 private DataRow row;    
47
48                 [SetUp]
49                 public void GetReady() {
50                         table = MakeTable ();                                           
51                         row = table.NewRow ();                                          
52                         row ["FName"] = "Hello";                                        
53                         row ["LName"] = "World";                                        
54                         table.Rows.Add (row);  
55                 }
56                 
57                 private DataTable MakeTable ()
58                 {
59                         DataTable namesTable = new DataTable ("Names");
60                         DataColumn idColumn = new  DataColumn ();
61                                                                                                     
62                                                                                                     
63                         idColumn.DataType = Type.GetType ("System.Int32");
64                         idColumn.ColumnName = "Id";
65                         idColumn.AutoIncrement = true;
66                         namesTable.Columns.Add (idColumn);
67                                                                                                     
68                                                                                                     
69                         DataColumn fNameColumn = new DataColumn ();
70                         fNameColumn.DataType = Type.GetType ("System.String");
71                         fNameColumn.ColumnName = "Fname";
72                         fNameColumn.DefaultValue = "Fname";
73                         namesTable.Columns.Add (fNameColumn);
74                                                                                                     
75                         DataColumn lNameColumn = new DataColumn ();
76                         lNameColumn.DataType = Type.GetType ("System.String");
77                         lNameColumn.ColumnName = "LName";
78                         lNameColumn.DefaultValue="LName";
79                         namesTable.Columns.Add (lNameColumn);
80                                                                                                     
81                                                                                                     
82                         // Set the primary key for the table
83                         DataColumn [] keys = new DataColumn [1];
84                         keys [0] = idColumn;
85                         namesTable.PrimaryKey = keys;
86                         // Return the new DataTable.
87                         return namesTable;
88                 }
89
90                 [Test]
91                 public void SetColumnErrorTest ()
92                 {
93                         string errorString;
94                         errorString = "Some error!";
95                         // Set the error for the specified column of the row.
96                         row.SetColumnError (1, errorString);
97                         GetColumnErrorTest ();
98                         GetAllErrorsTest ();
99                 }
100
101                 private  void GetColumnErrorTest ()
102                 {
103                         // Print the error of a specified column.
104                         AssertEquals ("#A01", "Some error!", row.GetColumnError (1));
105                 }
106
107                 private void GetAllErrorsTest ()
108                 {
109                         DataColumn [] colArr;
110                                                                                                     
111                         if (row.HasErrors) {
112                                 colArr = row.GetColumnsInError ();
113                                                                                                     
114                                 for (int i = 0; i < colArr.Length; i++) {
115                                         AssertEquals ("#A02", table.Columns [1], colArr [i]);
116                                 }
117                                 row.ClearErrors ();
118                         }
119                 }
120
121                 [Test]
122                 public void DeleteRowTest ()
123                 {
124                         DataRow newRow;
125                                                                                                     
126                                                                                                     
127                         for (int i = 1; i <= 2; i++) {
128                                 newRow = table.NewRow ();
129                                 newRow ["FName"] = "Name " + i;
130                                 newRow ["LName"] = " Last Name" + i;
131                                 table.Rows.Add (newRow);
132                         }
133                         table.AcceptChanges ();
134                                                                                                     
135                         int cnt = 1;
136                         for (int i = 1; i < table.Rows.Count; i++) {
137                                 DataRow r = table.Rows [i];
138                                 AssertEquals ("#A03", "Name " + cnt, r ["fName"]);
139                                 cnt++;
140                         }
141                                                                                                     
142                                                                                                     
143                         // Create a DataView with the table.
144                         DataRowCollection rc = table.Rows;
145                         rc [0].Delete ();
146                         rc [2].Delete ();
147                                                                                                     
148                                                                                                     
149                         AssertEquals ("#A04", "Deleted", rc [0].RowState.ToString ());
150                         AssertEquals ("#A05", "Deleted", rc [2].RowState.ToString ());
151                                                                                                     
152                                                                                                     
153                         // Accept changes
154                         table.AcceptChanges ();
155                         AssertEquals ("#A06", "Name 1", (table.Rows [0]) [1]);
156                         try  {
157                                 object o = rc [2];
158                                 Fail ("#A07");
159                         }
160                         catch (Exception e) {
161                                 // Never premise English.
162                                 //AssertEquals ("#A08", "There is no row at position 2.", e.Message);
163                         }
164                 }
165
166                 [Test]
167                 public void EditModeTest ()
168                 {
169                         try {
170                                 //Clear all existing values from table
171                                 for (int i = 0; i < table.Rows.Count; i++) {
172                                         table.Rows[i].Delete ();
173                                 }
174                                 table.AcceptChanges ();
175                                 row = table.NewRow ();
176                                 row["FName"] = "My FName";
177                                 table.Rows.Add (row);
178                                                                                                     
179                                                                                                     
180                                 // Stage 1
181                                 //Initially: After Add (Row) But Before Accept Changes");
182                                 AssertEquals ("#A09", "My FName", row [1, DataRowVersion.Default]);
183                                 AssertEquals ("#A10", "LName", row [2, DataRowVersion.Default]);
184                                                                                                     
185                                 AssertEquals ("#A11", "My FName", row [1, DataRowVersion.Current]);
186                                 AssertEquals ("#A12", "LName", row [2, DataRowVersion.Current]);
187                                                                                                     
188                                 try {
189                                       object o = row [1, DataRowVersion.Original];
190                                       o = row [1, DataRowVersion.Proposed];
191                                         Fail ("#A13");
192                                 }
193                                 catch (Exception e) {
194                                         if (e.GetType () != typeof (AssertionException)) {
195                                                 AssertEquals ("#A14", typeof (VersionNotFoundException), e.GetType ());
196                                         }
197                                 }
198                                                                                                     
199                                 // Stage 2
200                                 //After Accept Changes
201                                 table.AcceptChanges ();
202                                 AssertEquals ("#A15", "My FName", row [1, DataRowVersion.Default]);
203                                 AssertEquals ("#A16", "LName", row [2, DataRowVersion.Default]);
204                                                                                                     
205                                                                                                     
206                                 AssertEquals ("#A17", "My FName", row [1, DataRowVersion.Current]);
207                                 AssertEquals ("#A18", "LName", row [2, DataRowVersion.Current]);
208                                 
209                                 try {
210                                       object o = row [1, DataRowVersion.Proposed];
211                                         Fail ("#A19");
212                                 }
213                                 catch (Exception e) {
214                                         if (e.GetType () != typeof (AssertionException)) {
215                                                 AssertEquals ("#A20", typeof (VersionNotFoundException), e.GetType ());
216                                         }
217                                 }
218                                                                                                     
219                                                                                                                                                                                                          
220                                 // Stage 3                                 // Edit Mode
221                                 table.Rows [0].BeginEdit ();
222                                 table.Rows [0] ["LName"] = "My LName";
223                                                                                                     
224                                 AssertEquals ("#A21", "My FName", row [1, DataRowVersion.Default]);
225                                 AssertEquals ("#A22", "My LName", row [2, DataRowVersion.Default]);
226                                                                                                                                                                                                          
227                                 AssertEquals ("#A23", "My FName", row [1, DataRowVersion.Current]);
228                                 AssertEquals ("#A24", "LName", row [2, DataRowVersion.Current]);
229                                                                                                     
230                                                                                                     
231                                 AssertEquals ("#A25", "My FName", row [1, DataRowVersion.Original]);                                AssertEquals ("#A26", "LName", row [2, DataRowVersion.Original]);
232                                                                                                     
233                                 AssertEquals ("#A26", "My FName", row [1, DataRowVersion.Proposed]);
234                                 AssertEquals ("#A27", "My LName", row [2, DataRowVersion.Proposed]);                                                                                                    
235                                                                                                     
236                                                                                                     
237                                 // Stage 4
238                                 //After Edit sessions
239                                 for (int i=0; i < table.Rows.Count;i++)
240                                         table.Rows [i].EndEdit ();
241                                 AssertEquals ("#A28", "My FName", row [1, DataRowVersion.Default]);
242                                 AssertEquals ("#A29", "My LName", row [2, DataRowVersion.Default]);
243                                                                                                                                                                                                          
244                                 AssertEquals ("#A30", "My FName", row [1, DataRowVersion.Original]);                                AssertEquals ("#A31", "LName", row [2, DataRowVersion.Original]);
245                                                                                                     
246                                                                                                     
247                                 AssertEquals ("#A32", "My FName", row [1, DataRowVersion.Current]);
248                                 AssertEquals ("#A33", "My LName", row [2, DataRowVersion.Current]);
249                                                                                                     
250                                 try {
251                                       object o = row [1, DataRowVersion.Proposed];
252                                         Fail ("#A34");
253                                 }
254                                 catch (Exception e) {
255                                         if (e.GetType ()!=typeof (AssertionException)) {
256                                                 AssertEquals ("#A35", typeof (VersionNotFoundException), e.GetType ());
257                                         }
258                                 }
259                                                                                                     
260                                 //Stage 5
261                                 //After Accept Changes
262                                 table.AcceptChanges ();
263                                 AssertEquals ("#A36", "My FName", row [1, DataRowVersion.Default]);
264                                 AssertEquals ("#A37", "My LName", row [2, DataRowVersion.Default]);
265                                                                                                     
266                                                                                                     
267                                 AssertEquals ("#A38", "My FName", row [1, DataRowVersion.Original]);                                AssertEquals ("#A39", "My LName", row [2, DataRowVersion.Original]);                                                                                                    
268                                                                                                     
269                                 AssertEquals ("#A40", "My FName", row [1, DataRowVersion.Current]);
270                                 AssertEquals ("#A41", "My LName", row [2, DataRowVersion.Current]);
271                                                                                                     
272                                                                                                     
273                                 try {
274                                       object o = row [1, DataRowVersion.Proposed];
275                                         Fail ("#A42");
276                                 }
277                                 catch (Exception e) {
278                                                 if (e.GetType () != typeof (AssertionException)) {
279                                                         AssertEquals ("#A43", typeof (VersionNotFoundException),
280                                                                 e.GetType ());
281                                                 }
282                                         }
283                                                                                                     
284                                                                                                     
285                         }
286                         catch (Exception e){
287 //                              Console.WriteLine (e + "" + e.StackTrace);
288                         }
289                 }                                                                                                     
290                 [Test]
291                 public void ParentRowTest ()
292                 {
293
294                         //Clear all existing values from table
295                         for (int i = 0; i < table.Rows.Count; i++) {
296                                         table.Rows[i].Delete ();
297                         }
298                         table.AcceptChanges ();
299                         row = table.NewRow ();
300                         row["FName"] = "My FName";
301                         row["Id"] = 0;
302                         table.Rows.Add (row);
303                                                                                                     
304                         DataTable tableC = new DataTable ("Child");
305                         DataColumn colC;
306                         DataRow rowC;
307                                                                                                     
308                         colC = new DataColumn ();
309                         colC.DataType = Type.GetType ("System.Int32");
310                         colC.ColumnName = "Id";
311                         colC.AutoIncrement=true;
312                         tableC.Columns.Add (colC);
313                                                                                                     
314                                                                                                     
315                         colC = new DataColumn ();
316                         colC.DataType = Type.GetType ("System.String");
317                         colC.ColumnName = "Name";
318                         tableC.Columns.Add (colC);
319                                                                                                     
320                         rowC = tableC.NewRow ();
321                         rowC["Name"] = "My FName";
322                         tableC.Rows.Add (rowC);
323                         DataSet ds = new DataSet ();
324                         ds.Tables.Add (table);
325                         ds.Tables.Add (tableC);
326                         DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
327                         ds.Relations.Add (dr);
328                                                                                                     
329                         rowC.SetParentRow (table.Rows [0], dr);
330                                                                                                     
331                         AssertEquals ("#PRT-01", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
332                         AssertEquals ("#PRT-02", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
333
334                         ds.Relations.Clear ();
335                         dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
336                         ds.Relations.Add (dr);
337                         rowC.SetParentRow (table.Rows [0], dr);
338                         AssertEquals ("#PRT-03", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
339                         AssertEquals ("#PRT-04", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
340
341                         ds.Relations.Clear ();
342                         dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
343                         tableC.ParentRelations.Add (dr);
344                         rowC.SetParentRow (table.Rows [0]);
345                         AssertEquals ("#PRT-05", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
346                         AssertEquals ("#PRT-06", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
347                                                 
348                 } 
349
350                 [Test]
351                 public void ParentRowTest2 ()
352                 {
353                         DataSet ds = new DataSet ();
354                         DataTable tableP = ds.Tables.Add ("Parent");
355                         DataTable tableC = ds.Tables.Add ("Child");
356                         DataColumn colC;
357                         DataRow rowC;
358                                                                                                     
359                         colC = new DataColumn ();
360                         colC.DataType = Type.GetType ("System.Int32");
361                         colC.ColumnName = "Id";
362                         colC.AutoIncrement = true;
363                         tableP.Columns.Add (colC);
364                         
365                         colC = new DataColumn ();
366                         colC.DataType = Type.GetType ("System.Int32");
367                         colC.ColumnName = "Id";
368                         tableC.Columns.Add (colC);
369  
370                         row = tableP.Rows.Add (new object [0]);
371                         rowC = tableC.NewRow ();
372  
373                         ds.EnforceConstraints = false;
374                         DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
375                         ds.Relations.Add (dr);
376
377                         rowC.SetParentRow (row, dr);
378                         DataRow [] rows = rowC.GetParentRows (dr);
379
380                         AssertEquals ("#A49", 1, rows.Length);
381                         AssertEquals ("#A50", tableP.Rows [0], rows [0]);
382
383                         try{
384                                 rows = row.GetParentRows (dr);
385                         }catch(InvalidConstraintException){
386                                 //Test done
387                                 return ;
388                         }catch(Exception e){
389                                 Fail("#A51, InvalidConstraintException expected, got : " + e);
390                         }
391                         
392                         Fail("#A52, InvalidConstraintException expected but got none.");
393                 }
394
395                 [Test]
396                 public void ChildRowTest ()
397                 {
398
399                         //Clear all existing values from table
400                         for (int i = 0; i < table.Rows.Count; i++) {
401                                         table.Rows [i].Delete ();
402                         }
403                         table.AcceptChanges ();
404                         row = table.NewRow ();
405                         row ["FName"] = "My FName";
406                         row ["Id"] = 0;
407                         table.Rows.Add (row);
408                                                                                                     
409                         DataTable tableC = new DataTable ("Child");
410                         DataColumn colC;
411                         DataRow rowC;
412
413                         colC = new DataColumn ();
414                         colC.DataType = Type.GetType ("System.Int32");
415                         colC.ColumnName = "Id";
416                         colC.AutoIncrement = true;
417                         tableC.Columns.Add (colC);
418                                                                                                     
419                         colC = new DataColumn ();
420                         colC.DataType = Type.GetType ("System.String");
421                         colC.ColumnName = "Name";
422                         tableC.Columns.Add (colC);
423                                                                                                     
424                         rowC = tableC.NewRow ();
425                         rowC ["Name"] = "My FName";
426                         tableC.Rows.Add (rowC);
427                         DataSet ds = new DataSet ();
428                         ds.Tables.Add (table);
429                         ds.Tables.Add (tableC);
430                         DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
431                         ds.Relations.Add (dr);
432                                                                                                     
433                         rowC.SetParentRow (table.Rows [0], dr);
434                                                                                                     
435                         DataRow [] rows = (table.Rows [0]).GetChildRows (dr);
436
437                         AssertEquals ("#A45", 1, rows.Length);
438                         AssertEquals ("#A46", tableC.Rows [0], rows [0]);
439                         
440                 } 
441
442                 [Test]
443                 public void ChildRowTest2 ()
444                 {
445                         DataSet ds = new DataSet ();
446                         DataTable tableP = ds.Tables.Add ("Parent");
447                         DataTable tableC = ds.Tables.Add ("Child");
448                         DataColumn colC;
449                         DataRow rowC;
450                                                                                                     
451                         colC = new DataColumn ();
452                         colC.DataType = Type.GetType ("System.Int32");
453                         colC.ColumnName = "Id";
454                         colC.AutoIncrement = true;
455                         tableP.Columns.Add (colC);
456                         
457                         colC = new DataColumn ();
458                         colC.DataType = Type.GetType ("System.Int32");
459                         colC.ColumnName = "Id";
460                         tableC.Columns.Add (colC);
461
462                         row = tableP.NewRow ();
463                         rowC = tableC.Rows.Add (new object [0]);
464
465                         ds.EnforceConstraints = false;
466                         DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
467                         ds.Relations.Add (dr);
468
469                         rowC.SetParentRow (row, dr);
470                         DataRow [] rows = row.GetChildRows (dr);
471                         
472                         AssertEquals ("#A47", 1, rows.Length);
473                         AssertEquals ("#A48", tableC.Rows [0], rows [0]);
474
475                         try{
476                             rows = rowC.GetChildRows (dr);
477                         }catch(InvalidConstraintException){
478                             //Test done
479                             return ;
480                         }catch(Exception e){
481                             Fail("#A53, InvalidConstraintException expected, got : " + e);
482                         }
483                         
484                         Fail("#A54, InvalidConstraintException expected but got none.");
485                 }
486
487                  [Category ("NotWorking")] //Mismatch in Exception namespace/class reference
488                 [Test]
489                 public void ParentChildRowVersionTest ()
490                 {
491                         DataSet ds = new DataSet ();
492                         DataTable tableP = ds.Tables.Add ("Parent");
493                         DataTable tableC = ds.Tables.Add ("Child");
494                         DataColumn colC;
495                         DataRow rowC;
496                                                                                                     
497                         colC = new DataColumn ();
498                         colC.DataType = Type.GetType ("System.Int32");
499                         colC.ColumnName = "Id";
500                         colC.AutoIncrement = true;
501                         tableP.Columns.Add (colC);
502                         
503                         colC = new DataColumn ();
504                         colC.DataType = Type.GetType ("System.Int32");
505                         colC.ColumnName = "Id";
506                         tableC.Columns.Add (colC);
507
508                         row = tableP.NewRow ();
509                         rowC = tableC.Rows.Add (new object [0]);
510
511                         ds.EnforceConstraints = false;
512                         DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
513                         ds.Relations.Add (dr);
514
515                         rowC.SetParentRow (row, dr);
516                         DataRow [] rows;
517
518                         try {
519                             rows = row.GetChildRows (dr, DataRowVersion.Current);
520                         }catch (VersionNotFoundException v) {
521                             //Check for GetParentRows
522                             try{
523                                 //Child Row should be in Detached state for the next test
524                                 rowC = tableC.NewRow();
525
526                                 rows = rowC.GetParentRows (dr, DataRowVersion.Current);
527                             }catch (VersionNotFoundException v2) {
528                                 //Test Done
529                                 return ;
530                             }catch (Exception e){
531                                 Fail ("#A55, VersionNotFoundException expected, got : " + e);
532                             }
533                             Fail ("#A56, VersionNotFoundException expected but got none.");
534                         }catch (Exception e){
535                             Fail ("#A57, VersionNotFoundException expected, got : " + e);
536                         }
537                         
538                         Fail("#A58, VersionNotFoundException expected but got none.");
539                 }
540
541                 // tests item at row, column in table to be DBNull.Value
542                 private void DBNullTest (string message, DataTable dt, int row, int column) 
543                 {
544                         object val = dt.Rows[row].ItemArray[column];
545                         AssertEquals(message, DBNull.Value, val);
546                 }
547
548                 // tests item at row, column in table to be null
549                 private void NullTest (string message, DataTable dt, int row, int column) 
550                 {
551                         object val = dt.Rows[row].ItemArray[column];
552                         AssertEquals(message, null, val);
553                 }
554
555                 // tests item at row, column in table to be 
556                 private void ValueTest (string message, DataTable dt, int row, int column, object value) 
557                 {
558                         object val = dt.Rows[row].ItemArray[column];
559                         AssertEquals(message, value, val);
560                 }
561
562                 // test set null, DBNull.Value, and ItemArray short count
563                 [Test]
564                 public void NullInItemArray () 
565                 {
566                         string zero = "zero";
567                         string one = "one";
568                         string two = "two";
569
570                         DataTable table = new DataTable();
571                         table.Columns.Add(new DataColumn(zero, typeof(string)));
572                         table.Columns.Add(new DataColumn(one, typeof(string)));
573                         table.Columns.Add(new DataColumn(two, typeof(string)));
574
575                         object[] obj = new object[3];
576                         // -- normal -----------------
577                         obj[0] = zero;
578                         obj[1] = one;
579                         obj[2] = two;
580                         // results:
581                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
582                         //   table.Rows[0].ItemArray.ItemArray[1] = "one"
583                         //   table.Rows[0].ItemArray.ItemArray[2] = "two"
584                         
585                         DataRow row = table.NewRow();
586                         
587                         try {
588                                 row.ItemArray = obj;
589                         }
590                         catch(Exception e1) {
591                                 Fail("DR1: Exception Caught: " + e1);
592                         }
593                         
594                         table.Rows.Add(row);
595
596                         // -- null ----------
597                         obj[1] = null;
598                         // results:
599                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
600                         //   table.Rows[1].ItemArray.ItemArray[1] = DBNull.Value
601                         //   table.Rows[1].ItemArray.ItemArray[2] = "two"
602
603                         row = table.NewRow();
604                         
605                         try {
606                                 row.ItemArray = obj;
607                         }
608                         catch(Exception e2) {
609                                 Fail("DR2: Exception Caught: " + e2);
610                         }
611                         
612                         table.Rows.Add(row);
613
614                         // -- DBNull.Value -------------
615                         obj[1] = DBNull.Value;
616                         // results:
617                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
618                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
619                         //   table.Rows[2].ItemArray.ItemArray[2] = "two"
620
621                         row = table.NewRow();
622                         
623                         try {
624                                 row.ItemArray = obj;
625                         }
626                         catch(Exception e3) {
627                                 Fail("DR3: Exception Caught: " + e3);
628                         }
629                         
630                         table.Rows.Add(row);
631
632                         // -- object array smaller than number of columns -----
633                         string abc = "abc";
634                         string def = "def";
635                         obj = new object[2];
636                         obj[0] = abc;
637                         obj[1] = def;
638                         // results:
639                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
640                         //   table.Rows[3].ItemArray.ItemArray[1] = "def"
641                         //   table.Rows[3].ItemArray.ItemArray[2] = DBNull.Value;
642                         
643                         row = table.NewRow();
644                         
645                         try {
646                                 row.ItemArray = obj;
647                         }
648                         catch(Exception e3) {
649                                 Fail("DR4: Exception Caught: " + e3);
650                         }
651                         
652                         table.Rows.Add(row);
653
654                         // -- normal -----------------
655                         ValueTest("DR5: normal value test", table, 0, 0, zero);
656                         ValueTest("DR6: normal value test", table, 0, 1, one);
657                         ValueTest("DR7: normal value test", table, 0, 2, two);
658
659                         // -- null ----------
660                         ValueTest("DR8: null value test", table, 1, 0, zero);
661                         ValueTest("DR9: null value test", table, 1, 1, DBNull.Value);
662                         ValueTest("DR10: null value test", table, 1, 2, two);
663
664                         // -- DBNull.Value -------------
665                         ValueTest("DR11: DBNull.Value value test", table, 2, 0, zero);
666                         ValueTest("DR12: DBNull.Value value test", table, 2, 1, DBNull.Value);
667                         ValueTest("DR13: DBNull.Value value test", table, 2, 2, two);
668
669                         // -- object array smaller than number of columns -----
670                         ValueTest("DR14: array smaller value test", table, 3, 0, abc);
671                         ValueTest("DR15: array smaller value test", table, 3, 1, def);
672                         ValueTest("DR16: array smaller value test", table, 3, 2, DBNull.Value);
673                 }
674         
675                 // test DefaultValue when setting ItemArray
676                 [Test]
677                 public void DefaultValueInItemArray () {                
678                         string zero = "zero";
679
680                         DataTable table = new DataTable();
681                         table.Columns.Add(new DataColumn("zero", typeof(string)));              
682                         
683                         DataColumn column = new DataColumn("num", typeof(int));
684                         column.DefaultValue = 15;
685                         table.Columns.Add(column);
686                         
687                         object[] obj = new object[2];
688                         // -- normal -----------------
689                         obj[0] = "zero";
690                         obj[1] = 8;
691                         // results:
692                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
693                         //   table.Rows[0].ItemArray.ItemArray[1] = 8
694                                                 
695                         DataRow row = table.NewRow();
696                         
697                         try {
698                                 row.ItemArray = obj;
699                         }
700                         catch(Exception e1) {
701                                 Fail("DR17: Exception Caught: " + e1);
702                         }
703                         
704                         table.Rows.Add(row);
705
706                         // -- null ----------
707                         obj[1] = null;
708                         // results:
709                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
710                         //   table.Rows[1].ItemArray.ItemArray[1] = 15
711                         
712                         row = table.NewRow();
713                         
714                         try {
715                                 row.ItemArray = obj;
716                         }
717                         catch(Exception e2) {
718                                 Fail("DR18: Exception Caught: " + e2);
719                         }
720                         
721                         table.Rows.Add(row);
722
723                         // -- DBNull.Value -------------
724                         obj[1] = DBNull.Value;
725                         // results:
726                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
727                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
728                         //      even though internally, the v
729                         
730                         row = table.NewRow();
731                         
732                         try {
733                                 row.ItemArray = obj;
734                         }
735                         catch(Exception e3) {
736                                 Fail("DR19: Exception Caught: " + e3);
737                         }
738                         
739                         table.Rows.Add(row);
740
741                         // -- object array smaller than number of columns -----
742                         string abc = "abc";
743                         string def = "def";
744                         obj = new object[2];
745                         obj[0] = abc;
746                         // results:
747                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
748                         //   table.Rows[3].ItemArray.ItemArray[1] = DBNull.Value
749                                                 
750                         row = table.NewRow();
751                         
752                         try {
753                                 row.ItemArray = obj;
754                         }
755                         catch(Exception e3) {
756                                 Fail("DR20: Exception Caught: " + e3);
757                         }
758                         
759                         table.Rows.Add(row);
760
761                         // -- normal -----------------
762                         ValueTest("DR20: normal value test", table, 0, 0, zero);
763                         ValueTest("DR21: normal value test", table, 0, 1, 8);
764                         
765                         // -- null ----------
766                         ValueTest("DR22: null value test", table, 1, 0, zero);
767                         ValueTest("DR23: null value test", table, 1, 1, 15);
768                         
769                         // -- DBNull.Value -------------
770                         ValueTest("DR24: DBNull.Value value test", table, 2, 0, zero);
771                         DBNullTest("DR25: DBNull.Value value test", table, 2, 1);
772                         
773                         // -- object array smaller than number of columns -----
774                         ValueTest("DR26: array smaller value test", table, 3, 0, abc);
775                         ValueTest("DR27: array smaller value test", table, 3, 1, 15);
776                 }
777
778                 // test AutoIncrement when setting ItemArray
779                 [Test]
780                 public void AutoIncrementInItemArray () {
781                         string zero = "zero";
782                         string num = "num";
783                         
784                         DataTable table = new DataTable();
785                         table.Columns.Add(new DataColumn(zero, typeof(string)));                
786                         
787                         DataColumn column = new DataColumn("num", typeof(int));
788                         column.AutoIncrement = true;
789                         table.Columns.Add(column);
790                         
791                         object[] obj = new object[2];
792                         // -- normal -----------------
793                         obj[0] = "zero";
794                         obj[1] = 8;
795                         // results:
796                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
797                         //   table.Rows[0].ItemArray.ItemArray[1] = 8
798                                                 
799                         DataRow row = table.NewRow();
800                         
801                         try {
802                                 row.ItemArray = obj;
803                         }
804                         catch(Exception e1) {
805                                 Fail("DR28:  Exception Caught: " + e1);
806                         }
807                         
808                         table.Rows.Add(row);
809
810                         // -- null 1----------
811                         obj[1] = null;
812                         // results:
813                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
814                         //   table.Rows[1].ItemArray.ItemArray[1] = 9
815                         
816                         row = table.NewRow();
817                         
818                         try {
819                                 row.ItemArray = obj;
820                         }
821                         catch(Exception e2) {
822                                 Fail("DR29:  Exception Caught: " + e2);
823                         }
824                         
825                         table.Rows.Add(row);
826
827                         // -- null 2----------
828                         obj[1] = null;
829                         // results:
830                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
831                         //   table.Rows[1].ItemArray.ItemArray[1] = 10
832                         
833                         row = table.NewRow();
834                         
835                         try {
836                                 row.ItemArray = obj;
837                         }
838                         catch(Exception e2) {
839                                 Fail("DR30: Exception Caught: " + e2);
840                         }
841                         
842                         table.Rows.Add(row);
843
844                         // -- null 3----------
845                         obj[1] = null;
846                         // results:
847                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
848                         //   table.Rows[1].ItemArray.ItemArray[1] = 11
849                         
850                         row = table.NewRow();
851                         
852                         try {
853                                 row.ItemArray = obj;
854                         }
855                         catch(Exception e2) {
856                                 Fail("DR31: Exception Caught: " + e2);
857                         }
858                         
859                         table.Rows.Add(row);
860
861                         // -- DBNull.Value -------------
862                         obj[1] = DBNull.Value;
863                         // results:
864                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
865                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
866                         //      even though internally, the AutoIncrement value
867                         //      is incremented
868                         
869                         row = table.NewRow();
870                         
871                         try {
872                                 row.ItemArray = obj;
873                         }
874                         catch(Exception e3) {
875                                 Fail("DR32: Exception Caught: " + e3);
876                         }
877                         
878                         table.Rows.Add(row);
879
880                         // -- null 4----------
881                         obj[1] = null;
882                         // results:
883                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
884                         //   table.Rows[1].ItemArray.ItemArray[1] = 13
885                         
886                         row = table.NewRow();
887                         
888                         try {
889                                 row.ItemArray = obj;
890                         }
891                         catch(Exception e2) {
892                                 Fail("DR48: Exception Caught: " + e2);
893                         }
894                         
895                         table.Rows.Add(row);
896
897                         // -- object array smaller than number of columns -----
898                         string abc = "abc";
899                         string def = "def";
900                         obj = new object[2];
901                         obj[0] = abc;
902                         // results:
903                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
904                         //   table.Rows[3].ItemArray.ItemArray[1] = 14
905                                                 
906                         row = table.NewRow();
907                         
908                         try {
909                                 row.ItemArray = obj;
910                         }
911                         catch(Exception e3) {
912                                 Fail("DR33: Exception Caught: " + e3);
913                         }
914                         
915                         table.Rows.Add(row);
916
917                         // -- normal -----------------
918                         ValueTest("DR34: normal value test", table, 0, 0, zero);
919                         ValueTest("DR35: normal value test", table, 0, 1, 8);
920                         
921                         // -- null 1----------
922                         ValueTest("DR36: null value test", table, 1, 0, zero);
923                         ValueTest("DR37: null value test", table, 1, 1, 9);
924
925                         // -- null 2----------
926                         ValueTest("DR38: null value test", table, 2, 0, zero);
927                         ValueTest("DR39: null value test", table, 2, 1, 10);
928
929                         // -- null 3----------
930                         ValueTest("DR40: null value test", table, 3, 0, zero);
931                         ValueTest("DR41: null value test", table, 3, 1, 11);
932
933                         // -- DBNull.Value -------------
934                         ValueTest("DR42: DBNull.Value value test", table, 4, 0, zero);
935                         ValueTest("DR43: DBNull.Value value test", table, 4, 1, DBNull.Value);
936
937                         // -- null 4----------
938                         ValueTest("DR44: null value test", table, 5, 0, zero);
939                         ValueTest("DR45: null value test", table, 5, 1, 13);
940
941                         // -- object array smaller than number of columns -----
942                         ValueTest("DR46: array smaller value test", table, 6, 0, abc);
943                         ValueTest("DR47: array smaller value test", table, 6, 1, 14);
944                 }
945
946                 [Test]
947                 public void AutoIncrementColumnIntegrity ()
948                 {
949                         // AutoIncrement-column shouldn't raise index out of range
950                         // exception because of size mismatch of internal itemarray.
951                         DataTable dt = new DataTable ();
952                         dt.Columns.Add ("foo");
953                         dt.Rows.Add (new object [] {"value"});
954                         DataColumn col = new DataColumn ("bar");
955                         col.AutoIncrement = true;
956                         dt.Columns.Add (col);
957                         dt.Rows [0] [0] = "test";
958                 }
959
960                 [Test]
961                 public void EnforceConstraint ()
962                 {
963                          int id = 100;
964                         // Setup stuff
965                         DataSet ds = new DataSet();
966                         DataTable parent = ds.Tables.Add("parent");
967                         parent.Columns.Add("id", typeof(int));
968                         DataTable child = ds.Tables.Add("child");
969                         child.Columns.Add("idref", typeof(int));
970                         Constraint uniqueId = null;
971                         parent.Constraints.Add(uniqueId = new UniqueConstraint("uniqueId",
972                                       new DataColumn[] {parent.Columns["id"]}, true));
973                         ForeignKeyConstraint fkc = new ForeignKeyConstraint("ParentChildConstraint",                                      new DataColumn[] { parent.Columns["id"] },
974                                       new DataColumn[] { child.Columns["idref"]});
975         
976                         child.Constraints.Add(fkc);
977         
978                         DataRelation relateParentChild = new DataRelation("relateParentChild",
979                                              new DataColumn[] {parent.Columns["id"] },
980                                              new DataColumn[] {child.Columns["idref"] },
981                                              false);
982                         ds.Relations.Add(relateParentChild);
983         
984                         ds.EnforceConstraints = false;
985                         DataRow parentRow = parent.Rows.Add(new object[] { id });
986                         DataRow childRow = child.Rows.Add(new object[] { id });
987                         if (parentRow == childRow.GetParentRow(relateParentChild)) {
988                             foreach(DataColumn dc in parent.Columns)
989                                 AssertEquals(100,parentRow[dc]);
990                             
991                         }
992                                         
993         
994                 }
995
996                 [Test]
997                 [ExpectedException (typeof (RowNotInTableException))]
998                 public void DetachedRowItemException ()
999                 {
1000                         DataTable dt = new DataTable ("table");
1001                         dt.Columns.Add ("col");
1002                         dt.Rows.Add ((new object [] {"val"}));
1003
1004                         DataRow dr = dt.NewRow ();
1005                         AssertEquals (DataRowState.Detached, dr.RowState);
1006                         dr.CancelEdit ();
1007                         AssertEquals (DataRowState.Detached, dr.RowState);
1008                         object o = dr ["col"];
1009                 }
1010
1011                 [Test]
1012                 public void SetParentRow_Null ()
1013                 {
1014                         DataSet ds = new DataSet();
1015
1016                         DataTable       child   = ds.Tables.Add("child");
1017                         child.Columns.Add("column1");
1018
1019                         DataRow r1 = child.NewRow();
1020
1021                         r1.SetParentRow(null);
1022                 }
1023
1024                 [Test]
1025                 public void SetParentRow_DataInheritance ()
1026                 {
1027                         var ds = new DataSet() ;
1028
1029                         var     child                   = ds.Tables.Add("child") ;
1030
1031                         var     childColumn1    = child.Columns.Add("column1");
1032                         var     childColumn2    = child.Columns.Add("column2");
1033
1034                         var     parent1                 = ds.Tables.Add("parent1");
1035                         var     parent1Column1  = parent1.Columns.Add("column1");
1036                         var     parent1Column2  = parent1.Columns.Add("column2");
1037
1038                         var     parent2                 = ds.Tables.Add("parent2");
1039                         var     parent2Column1  = parent2.Columns.Add("column1");
1040                         var     parent2Column2  = parent2.Columns.Add("column2");
1041
1042                         var relation1 = ds.Relations.Add("parent1-child", parent1Column1, childColumn1);
1043                                                         ds.Relations.Add("parent2-child", parent2Column2, childColumn2);
1044
1045                         var childRow1   = child.NewRow();
1046                         var parent1Row  = parent1.NewRow();
1047                         var parent2Row  = parent2.NewRow();
1048
1049                         parent1Row[parent1Column1] = "p1c1";
1050                         parent1Row[parent1Column2] = "p1c2";
1051                         parent2Row[parent2Column1] = "p2c1";
1052                         parent2Row[parent2Column2] = "p2c2";
1053
1054                         child.Rows.Add(childRow1);
1055                         parent1.Rows.Add(parent1Row);
1056                         parent2.Rows.Add(parent2Row);
1057
1058                         childRow1.SetParentRow(parent1Row);
1059                         AssertEquals ("p1c1", childRow1[childColumn1]);
1060                         AssertEquals (DBNull.Value, childRow1[childColumn2]);
1061
1062                         childRow1.SetParentRow(parent2Row);
1063                         AssertEquals ("p1c1", childRow1[childColumn1]);
1064                         AssertEquals ("p2c2", childRow1[childColumn2]);
1065
1066                         childRow1.SetParentRow(null);
1067                         AssertEquals (DBNull.Value, childRow1[childColumn1]);
1068                         AssertEquals (DBNull.Value, childRow1[childColumn2]);
1069
1070                         childRow1.SetParentRow(parent2Row);
1071                         AssertEquals (DBNull.Value, childRow1[childColumn1]);
1072                         AssertEquals ("p2c2", childRow1[childColumn2]);
1073                 }
1074
1075                 [Test]
1076                 public void SetParentRow_with_Relation ()
1077                 {
1078                         var ds = new DataSet() ;
1079
1080                         var     child                   = ds.Tables.Add("child") ;
1081
1082                         var     childColumn1    = child.Columns.Add("column1");
1083                         var     childColumn2    = child.Columns.Add("column2");
1084
1085                         var     parent1                 = ds.Tables.Add("parent1");
1086                         var     parent1Column1  = parent1.Columns.Add("column1");
1087                         var     parent1Column2  = parent1.Columns.Add("column2");
1088
1089                         var     parent2                 = ds.Tables.Add("parent2");
1090                         var     parent2Column1  = parent2.Columns.Add("column1");
1091                         var     parent2Column2  = parent2.Columns.Add("column2");
1092
1093                         var relation1 = ds.Relations.Add("parent1-child", parent1Column1, childColumn1) ; 
1094                         var relation2 = ds.Relations.Add("parent2-child", parent2Column2, childColumn2) ;
1095
1096                         var childRow1   = child.NewRow();
1097                         var parent1Row  = parent1.NewRow();
1098                         var parent2Row  = parent2.NewRow();
1099
1100                         parent1Row[parent1Column1] = "p1c1";
1101                         parent1Row[parent1Column2] = "p1c2";
1102                         parent2Row[parent2Column1] = "p2c1";
1103                         parent2Row[parent2Column2] = "p2c2";
1104
1105                         child.Rows.Add(childRow1);
1106                         parent1.Rows.Add(parent1Row);
1107                         parent2.Rows.Add(parent2Row);
1108
1109
1110                         childRow1.SetParentRow (null, relation2);
1111                         AssertEquals (DBNull.Value, childRow1[childColumn1]);
1112                         AssertEquals (DBNull.Value, childRow1[childColumn2]);
1113
1114                         try {
1115                                 childRow1.SetParentRow(parent1Row, relation2);
1116                                 Fail ("Must throw InvalidConstaintException");
1117                         } catch (InvalidConstraintException e) {
1118                         }
1119                         AssertEquals (DBNull.Value, childRow1[childColumn1]);
1120                         AssertEquals (DBNull.Value, childRow1[childColumn2]);
1121
1122                         childRow1.SetParentRow(parent1Row, relation1);
1123                         AssertEquals ("p1c1", childRow1[childColumn1]);
1124                         AssertEquals (DBNull.Value, childRow1[childColumn2]);
1125
1126
1127                         childRow1.SetParentRow (null, relation2);
1128                         AssertEquals ("p1c1", childRow1[childColumn1]);
1129                         AssertEquals (DBNull.Value, childRow1[childColumn2]);
1130
1131                         childRow1.SetParentRow (null, relation1);
1132                         AssertEquals (DBNull.Value, childRow1[childColumn1]);
1133                         AssertEquals (DBNull.Value, childRow1[childColumn2]);
1134                 }
1135
1136                 [Test]
1137                 public void SetParent_missing_ParentRow ()
1138                 {
1139                         var ds = new DataSet() ;
1140
1141                         var     child                   = ds.Tables.Add("child") ;
1142
1143                         var     childColumn1    = child.Columns.Add("column1");
1144                         var     childColumn2    = child.Columns.Add("column2");
1145
1146                         var     parent1                 = ds.Tables.Add("parent1");
1147                         var     parentColumn1   = parent1.Columns.Add("column1");
1148
1149                         var     parent2                 = ds.Tables.Add("parent2");
1150                         var     parentColumn2   = parent2.Columns.Add("column2");
1151
1152                         ds.Relations.Add("parent1-child", parentColumn1, childColumn1);
1153                         ds.Relations.Add("parent2-child", parentColumn2, childColumn2);
1154
1155                         var childRow = child.NewRow();
1156                         var parentRow = parent2.NewRow();
1157
1158                         parentRow[parentColumn2] = "value";
1159
1160                         child.Rows.Add(childRow);
1161                         parent2.Rows.Add(parentRow);
1162
1163                         childRow.SetParentRow(parentRow);
1164                         AssertEquals (DBNull.Value, childRow[childColumn1]);
1165                         AssertEquals ("value", childRow[childColumn2]);
1166                 }
1167
1168         }
1169 }