[runtime] Updates comments.
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowCollectionTest.cs
1 // DataRowCollectionTest.cs - NUnit Test Cases for System.DataRowCollection
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) Copyright 2002 Franklin Wise
8 // (C) Copyright 2003 Martin Willemoes Hansen
9 // 
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34
35 using NUnit.Framework;
36 using System;
37 using System.Data;
38
39 namespace MonoTests.System.Data
40 {
41         [TestFixture]
42         public class DataRowCollectionTest
43         {
44                 private DataTable _tbl; 
45
46                 [SetUp]
47                 public void GetReady()
48                 {
49                         _tbl = new DataTable();
50                 }
51
52                 [Test]
53                 public void AutoIncrement()
54                 {
55                         DataColumn col = new DataColumn("Auto");
56                         col.AutoIncrement = true;
57                         col.AutoIncrementSeed = 0;
58                         col.AutoIncrementStep = 1;
59                         
60                         _tbl.Columns.Add(col);
61                         _tbl.Rows.Add(_tbl.NewRow());
62
63                         Assert.AreEqual (0, Convert.ToInt32(_tbl.Rows[0]["Auto"] ), "test#01" );
64                                 
65                         _tbl.Rows.Add(_tbl.NewRow());
66                         Assert.AreEqual (1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ), "test#02" );
67                         
68                         col.AutoIncrement = false;
69                         Assert.AreEqual (1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ), "test#03" );
70
71                         _tbl.Rows.Add(_tbl.NewRow());
72                         Assert.AreEqual (DBNull.Value, _tbl.Rows[2]["Auto"], "test#04" );
73
74                         col.AutoIncrement = true;
75                         col.AutoIncrementSeed = 10;
76                         col.AutoIncrementStep = 2;
77                         
78                         _tbl.Rows.Add(_tbl.NewRow());
79                         Assert.AreEqual (10, Convert.ToInt32(_tbl.Rows[3]["Auto"] ), "test#05" );
80                         _tbl.Rows.Add(_tbl.NewRow());
81                         Assert.AreEqual (12, Convert.ToInt32(_tbl.Rows[4]["Auto"] ), "test#06" );
82
83                         col = new DataColumn ("Auto2");
84                         col.DataType = typeof(string);
85                         col.AutoIncrement = true;
86                         col.AutoIncrementSeed = 0;
87                         col.AutoIncrementStep = 1;
88                         _tbl.Columns.Add (col);
89                         
90                         _tbl.Rows.Add(_tbl.NewRow());
91                         Assert.AreEqual (typeof (int), _tbl.Columns [1].DataType, "test#07");
92                         Assert.AreEqual (typeof (int), _tbl.Rows[5]["Auto2"].GetType (), "test#08" );
93
94                         col = new DataColumn ("Auto3");
95                         col.AutoIncrement = true;
96                         col.AutoIncrementSeed = 0;
97                         col.AutoIncrementStep = 1;
98                         col.DataType = typeof(string);
99                         Assert.AreEqual (typeof (string), col.DataType, "test#09");
100                         Assert.IsFalse (col.AutoIncrement, "test#10");
101                 }
102
103                 [Test]
104                 public void Add ()
105                 {
106                         _tbl.Columns.Add ();
107                         _tbl.Columns.Add ();
108                         DataRow Row = _tbl.NewRow ();
109                         DataRowCollection Rows = _tbl.Rows;
110                         
111                         Rows.Add (Row);
112                         Assert.AreEqual (1, Rows.Count, "test#01");
113                         Assert.IsFalse (Rows.IsReadOnly, "test#02");
114                         Assert.IsFalse (Rows.IsSynchronized, "test#03");
115                         Assert.AreEqual ("System.Data.DataRowCollection", Rows.ToString (), "test#04");
116                         
117                         string [] cols = new string [2];
118                         cols [0] = "first";
119                         cols [1] = "second";
120                         
121                         Rows.Add (cols);
122                         cols [0] = "something";
123                         cols [1] = "else";
124                         Rows.Add (cols);
125                         
126                         Assert.AreEqual (3, Rows.Count, "test#05");
127                         Assert.AreEqual ("System.Data.DataRow",  Rows [0].ToString (), "test#06");
128                         Assert.AreEqual (DBNull.Value, Rows [0] [0], "test#07");
129                         Assert.AreEqual (DBNull.Value, Rows [0] [1], "test#08");
130                         Assert.AreEqual ("first", Rows [1] [0], "test#09");
131                         Assert.AreEqual ("something", Rows [2] [0], "test#10");
132                         Assert.AreEqual ("second", Rows [1] [1], "test#11");
133                         Assert.AreEqual ("else", Rows [2] [1], "test#12");
134                         
135                         try {
136                                 Rows.Add (Row);
137                                 Assert.Fail ("test#13");
138                         } catch (Exception e) {
139                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#14");
140                                 // Never premise English.
141                                 //Assert.AreEqual ("This row already belongs to this table.", e.Message, "test#15");
142                         }
143                         
144                         try {
145                                 Row = null;
146                                 Rows.Add (Row);
147                                 Assert.Fail ("test#16");
148                         } catch (Exception e) {
149                                 Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "test#17");
150                                 //Assert.AreEqual ("'row' argument cannot be null.\r\nParameter name: row", e.Message, "test#18");
151                         }
152                         
153                         DataColumn Column = new DataColumn ("not_null");
154                         Column.AllowDBNull = false;
155                         _tbl.Columns.Add (Column);
156                         
157                         cols = new string [3];
158                         cols [0] = "first";
159                         cols [1] = "second";
160                         cols [2] = null;
161                         
162                         try {
163                                 Rows.Add (cols);
164                                 Assert.Fail ("test#19");
165                         } catch (Exception e) {
166                                 Assert.AreEqual (typeof (NoNullAllowedException), e.GetType (), "test#20");
167                                 //Assert.AreEqual ("Column 'not_null' does not allow nulls.", e.Message, "test#21");
168                         }
169                         
170                         Column = _tbl.Columns [0];                      
171                         Column.Unique = true;
172
173                         cols = new string [3];
174                         cols [0] = "first";
175                         cols [1] = "second";
176                         cols [2] = "blabal";
177                         
178                         try {
179                                 Rows.Add (cols);
180                                 Assert.Fail ("test#22");
181                         } catch (Exception e) {
182                                 Assert.AreEqual (typeof (ConstraintException), e.GetType (), "test#23");
183                                 // Never premise English.
184                                 //Assert.AreEqual ("Column 'Column1' is constrained to be unique.  Value 'first' is already present.", e.Message, "test#24");
185                         }
186                        
187                         Column = new DataColumn ("integer");
188                         Column.DataType = typeof (short);
189                         _tbl.Columns.Add (Column);
190                         
191                         object [] obs = new object [4];
192                         obs [0] = "_first";
193                         obs [1] = "second";
194                         obs [2] = "blabal";
195                         obs [3] = "ads";
196                         
197                         try {
198                                 Rows.Add (obs);
199                                 Assert.Fail ("test#25");
200                         } catch (ArgumentException e) {
201                                 // LAMESPEC: MSDN says this exception is InvalidCastException
202 //                              Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#26");
203                         }
204
205                         object [] obs1 = new object [5];
206                         obs1 [0] = "A";
207                         obs1 [1] = "B";
208                         obs1 [2] = "C";
209                         obs1 [3] = 38;
210                         obs1 [4] = "Extra";
211                         try {
212                                 Rows.Add (obs1);
213                                 Assert.Fail ("test#27");
214                         } catch (Exception e) {
215                                 Assert.AreEqual (typeof(ArgumentException), e.GetType (), "test#28");
216                         }
217                 }
218
219                 [Test]
220                 public void Add_ByValuesNullTest ()
221                 {
222                         DataTable t = new DataTable ("test");
223                         t.Columns.Add ("id", typeof (int));
224                         t.Columns.Add ("name", typeof (string));
225                         t.Columns.Add ("nullable", typeof (string));
226
227                         t.Columns [0].AutoIncrement = true;
228                         t.Columns [0].AutoIncrementSeed = 10;
229                         t.Columns [0].AutoIncrementStep = 5;
230
231                         t.Columns [1].DefaultValue = "testme";
232                         
233
234                         // null test & missing columns
235                         DataRow r = t.Rows.Add (new object [] { null, null});
236                         Assert.AreEqual (10, (int) r [0], "#ABV1");
237                         Assert.AreEqual ("testme", (string) r [1], "#ABV2");
238                         Assert.AreEqual (DBNull.Value, r [2], "#ABV3");
239
240                         // dbNull test
241                         r = t.Rows.Add (new object [] { DBNull.Value, DBNull.Value, DBNull.Value});
242                         Assert.AreEqual (DBNull.Value, r [0], "#ABV4");
243                         Assert.AreEqual (DBNull.Value, r [1], "#ABV5");
244                         Assert.AreEqual (DBNull.Value, r [2], "#ABV6");
245
246                         // ai test & no default value test
247                         r = t.Rows.Add (new object [] { null, null, null});
248                         Assert.AreEqual (15, (int) r [0], "#ABV7");
249                         Assert.AreEqual ("testme", (string) r [1], "#ABV8");
250                         Assert.AreEqual (DBNull.Value, r [2], "#ABV9");
251                 }
252                 
253                 [Test]
254                 public void Clear ()
255                 {
256                         DataRowCollection Rows = _tbl.Rows;
257                         DataTable Table = new DataTable ("child");
258                         Table.Columns.Add ("first", typeof (int));
259                         Table.Columns.Add ("second", typeof (string));
260                         
261                         _tbl.Columns.Add ("first", typeof (int));
262                         _tbl.Columns.Add ("second", typeof (float));
263
264                         string [] cols = new string [2];
265                         cols [0] = "1";
266                         cols [1] = "1,1";
267                         Rows.Add (cols);
268                         
269                         cols [0] = "2";
270                         cols [1] = "2,1";
271                         Rows.Add (cols);
272                         
273                         cols [0] = "3";
274                         cols [1] = "3,1";
275                         Rows.Add (cols);
276                         
277                         Assert.AreEqual (3, Rows.Count, "test#01");
278                         Rows.Clear ();
279                         
280                         // hmm... TODO: better tests
281                         Assert.AreEqual (0, Rows.Count, "test#02");
282                         
283                         cols [0] = "1";
284                         cols [1] = "1,1";
285                         Rows.Add (cols);
286                         
287                         cols [0] = "2";
288                         cols [1] = "2,1";
289                         Rows.Add (cols);
290                         
291                         cols [0] = "3";
292                         cols [1] = "3,1";
293                         Rows.Add (cols);
294
295                         cols [0] = "1";
296                         cols [1] = "test";
297                         Table.Rows.Add (cols);
298                         
299                         cols [0] = "2";
300                         cols [1] = "test2";
301                         Table.Rows.Add (cols);
302                         
303                         cols [0] = "3";
304                         cols [1] = "test3";
305                         Table.Rows.Add (cols);                  
306                         
307                         DataRelation Rel = new DataRelation ("REL", _tbl.Columns [0], Table.Columns [0]);
308                         DataSet Set = new DataSet ();
309                         Set.Tables.Add (_tbl);
310                         Set.Tables.Add (Table);
311                         Set.Relations.Add (Rel);
312                         
313                         try {
314                                 Rows.Clear ();
315                                 Assert.Fail ("test#03");
316                         } catch (InvalidConstraintException) {
317                         }
318                         
319                         Assert.AreEqual (3, Table.Rows.Count, "test#06");
320                         Table.Rows.Clear ();
321                         Assert.AreEqual (0, Table.Rows.Count, "test#07");
322                 }
323                 
324                 [Test]
325                 public void Contains ()
326                 {
327                         DataColumn C = new DataColumn ("key");
328                         C.Unique = true;                        
329                         C.DataType = typeof (int);
330                         C.AutoIncrement = true;
331                         C.AutoIncrementSeed = 0;
332                         C.AutoIncrementStep = 1;
333                         _tbl.Columns.Add (C);
334                         _tbl.Columns.Add ("first", typeof (string));
335                         _tbl.Columns.Add ("second", typeof (decimal));
336                         
337                         DataRowCollection Rows = _tbl.Rows;
338                         
339                         DataRow Row = _tbl.NewRow ();
340                         _tbl.Rows.Add (Row);
341                         Row = _tbl.NewRow ();
342                         _tbl.Rows.Add (Row);
343                         Row = _tbl.NewRow ();
344                         _tbl.Rows.Add (Row);
345                         Row = _tbl.NewRow ();
346                         _tbl.Rows.Add (Row);
347                         
348                         Rows [0] [1] = "test0";
349                         Rows [0] [2] = 0;
350                         Rows [1] [1] = "test1";
351                         Rows [1] [2] = 1;
352                         Rows [2] [1] = "test2";
353                         Rows [2] [2] = 2;
354                         Rows [3] [1] = "test3";
355                         Rows [3] [2] = 3;
356                         
357                         Assert.AreEqual (3, _tbl.Columns.Count, "test#01");
358                         Assert.AreEqual (4, _tbl.Rows.Count, "test#02");
359                         Assert.AreEqual (0, _tbl.Rows [0] [0], "test#03");
360                         Assert.AreEqual (1, _tbl.Rows [1] [0], "test#04");
361                         Assert.AreEqual (2, _tbl.Rows [2] [0], "test#05");
362                         Assert.AreEqual (3, _tbl.Rows [3] [0], "test#06");
363                         
364                         try {
365                                 Rows.Contains (1);
366                                 Assert.Fail ("test#07");
367                         } catch (Exception e) {
368                                 Assert.AreEqual (typeof (MissingPrimaryKeyException), e.GetType (), "test#08");
369                                 // Never premise English.
370                                 //Assert.AreEqual ("Table doesn't have a primary key.", e.Message, "test#09");                  
371                         }
372                         
373                         _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0]};
374                         Assert.IsTrue (Rows.Contains (1), "test#10");
375                         Assert.IsTrue (Rows.Contains (2), "test#11");
376                         Assert.IsFalse (Rows.Contains (4), "test#12");
377                         
378                         try {
379                                 Rows.Contains (new object [] {64, "test0"});
380                                 Assert.Fail ("test#13");
381                         } catch (Exception e) {
382                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#14");
383                                 // Never premise English.
384                                 //Assert.AreEqual ("Expecting 1 value(s) for the key being indexed, but received 2 value(s).", e.Message, "test#15");
385                         }
386                         
387                         _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0], _tbl.Columns [1]};
388                         Assert.IsFalse (Rows.Contains (new object [] {64, "test0"}), "test#16");
389                         Assert.IsFalse (Rows.Contains (new object [] {0, "test1"}), "test#17");
390                         Assert.IsTrue (Rows.Contains (new object [] {1, "test1"}), "test#18");
391                         Assert.IsTrue (Rows.Contains (new object [] {2, "test2"}), "test#19");
392                         
393                         try {
394                                 Rows.Contains (new object [] {2});
395                                 Assert.Fail ("test#20");
396                         } catch (Exception e) {
397                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#21");
398                                 // Never premise English.
399                                 //Assert.AreEqual ("Expecting 2 value(s) for the key being indexed, but received 1 value(s).", e.Message, "test#22");
400                         }
401                 }
402                 
403                 [Test]
404                 public void CopyTo ()
405                 {
406                         _tbl.Columns.Add ();
407                         _tbl.Columns.Add ();
408                         _tbl.Columns.Add ();
409                         
410                         DataRowCollection Rows = _tbl.Rows;
411                         
412                         Rows.Add (new object [] {"1", "1", "1"});
413                         Rows.Add (new object [] {"2", "2", "2"});
414                         Rows.Add (new object [] {"3", "3", "3"});
415                         Rows.Add (new object [] {"4", "4", "4"});
416                         Rows.Add (new object [] {"5", "5", "5"});
417                         Rows.Add (new object [] {"6", "6", "6"});
418                         Rows.Add (new object [] {"7", "7", "7"});
419                         
420                         DataRow [] dr = new DataRow [10];
421                         
422                         try {
423                                 Rows.CopyTo (dr, 4);
424                                 Assert.Fail ("test#01");
425                         } catch (Exception e) {                 
426                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#02");
427                                 //Assert.AreEqual ("Destination array was not long enough.  Check destIndex and length, and the array's lower bounds.", e.Message, "test#03");
428                         }
429                         
430                         dr = new DataRow [11];
431                         Rows.CopyTo (dr, 4);
432                         
433                         Assert.IsNull (dr [0], "test#04");
434                         Assert.IsNull (dr [1], "test#05");
435                         Assert.IsNull (dr [2], "test#06");
436                         Assert.IsNull (dr [3], "test#07");
437                         Assert.AreEqual ("1", dr [4] [0], "test#08");
438                         Assert.AreEqual ("2", dr [5] [0], "test#09");
439                         Assert.AreEqual ("3", dr [6] [0], "test#10");
440                         Assert.AreEqual ("4", dr [7] [0], "test#11");
441                         Assert.AreEqual ("5", dr [8] [0], "test#12");
442                         Assert.AreEqual ("6", dr [9] [0], "test#13");
443                 }
444                 
445                 [Test]
446                 public void Equals ()
447                 {
448                         _tbl.Columns.Add ();
449                         _tbl.Columns.Add ();
450                         _tbl.Columns.Add ();
451                         
452                         DataRowCollection Rows1 = _tbl.Rows;
453                         
454                         Rows1.Add (new object [] {"1", "1", "1"});
455                         Rows1.Add (new object [] {"2", "2", "2"});
456                         Rows1.Add (new object [] {"3", "3", "3"});
457                         Rows1.Add (new object [] {"4", "4", "4"});
458                         Rows1.Add (new object [] {"5", "5", "5"});
459                         Rows1.Add (new object [] {"6", "6", "6"});
460                         Rows1.Add (new object [] {"7", "7", "7"});
461                         
462                         DataRowCollection Rows2 = _tbl.Rows;
463                         
464                         Assert.IsTrue (Rows2.Equals (Rows1), "test#01");
465                         Assert.IsTrue (Rows1.Equals (Rows2), "test#02");
466                         Assert.IsTrue (Rows1.Equals (Rows1), "test#03");
467                         
468                         DataTable Table = new DataTable ();
469                         Table.Columns.Add ();
470                         Table.Columns.Add ();
471                         Table.Columns.Add ();
472                         DataRowCollection Rows3 = Table.Rows;
473
474                         Rows3.Add (new object [] {"1", "1", "1"});
475                         Rows3.Add (new object [] {"2", "2", "2"});
476                         Rows3.Add (new object [] {"3", "3", "3"});
477                         Rows3.Add (new object [] {"4", "4", "4"});
478                         Rows3.Add (new object [] {"5", "5", "5"});
479                         Rows3.Add (new object [] {"6", "6", "6"});
480                         Rows3.Add (new object [] {"7", "7", "7"});
481                         
482                         Assert.IsFalse (Rows3.Equals (Rows1), "test#04");
483                         Assert.IsFalse (Rows3.Equals (Rows2), "test#05");
484                         Assert.IsFalse (Rows1.Equals (Rows3), "test#06");
485                         Assert.IsFalse (Rows2.Equals (Rows3), "test#07");
486                 }
487                 
488                 [Test]
489                 public void Find ()
490                 {
491                         DataColumn Col = new DataColumn ("test_1");
492                         Col.AllowDBNull = false;
493                         Col.Unique = true;
494                         Col.DataType = typeof (long);
495                         _tbl.Columns.Add (Col);
496                         
497                         Col = new DataColumn ("test_2");
498                         Col.DataType = typeof (string);
499                         _tbl.Columns.Add (Col);
500                         
501                         DataRowCollection Rows = _tbl.Rows;
502                         
503                         Rows.Add (new object [] {1, "first"});
504                         Rows.Add (new object [] {2, "second"});
505                         Rows.Add (new object [] {3, "third"});
506                         Rows.Add (new object [] {4, "fourth"});
507                         Rows.Add (new object [] {5, "fifth"});
508                         
509                         try {
510                                 Rows.Find (1);
511                                 Assert.Fail ("test#01");
512                         } catch (Exception e) {
513                                 Assert.AreEqual (typeof (MissingPrimaryKeyException), e.GetType (), "test#02");
514                                 // Never premise English.
515                                 //Assert.AreEqual ("Table doesn't have a primary key.", e.Message, "test#03");              
516                         }
517                         
518                         _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0]};
519                         DataRow row = Rows.Find (1);
520                         Assert.AreEqual (1L, row [0], "test#04");
521                         row = Rows.Find (2);                    
522                         Assert.AreEqual (2L, row [0], "test#05");
523                         row = Rows.Find ("2");
524                         Assert.AreEqual (2L, row [0], "test#06");
525                         
526                         try {
527                                 row = Rows.Find ("test");
528                                 Assert.Fail ("test#07");
529                         } catch (Exception e) {
530                                 Assert.AreEqual (typeof (FormatException), e.GetType (), "test#08");
531                                 //Assert.AreEqual ("Input string was not in a correct format.", e.Message, "test#09");
532                         }
533                         
534                         String tes = null;                      
535                         row = Rows.Find (tes);                  
536                         Assert.IsNull (row, "test#10");
537                         _tbl.PrimaryKey = null;
538                         
539                         try {
540                                 Rows.Find (new object [] {1, "fir"});
541                                 Assert.Fail ("test#11");
542                         } catch (Exception e) {
543                                 Assert.AreEqual (typeof (MissingPrimaryKeyException), e.GetType (), "test#12");
544                                 // Never premise English.
545                                 //Assert.AreEqual ("Table doesn't have a primary key.", e.Message, "tets#13");
546                         }
547                         
548                         _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0], _tbl.Columns [1]};
549                         
550                         try {
551                                 Rows.Find (1);
552                                 Assert.Fail ("test#14");
553                         } catch (Exception e) {
554                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#15");
555                                 // Never premise English.
556                                 //Assert.AreEqual ("Expecting 2 value(s) for the key being indexed, but received 1 value(s).", e.Message, "test#16");
557                         }
558                         
559                         row = Rows.Find (new object [] {1, "fir"});
560                         Assert.IsNull (row, "test#16");
561                         row = Rows.Find (new object [] {1, "first"});
562                         Assert.AreEqual (1L, row [0], "test#17");
563                 }
564                 
565                 [Test]
566                 public void Find2 ()
567                 {
568                         DataSet ds = new DataSet ();
569                         ds.EnforceConstraints = false;
570
571                         DataTable dt = new DataTable ();
572                         ds.Tables.Add (dt);
573
574                         DataColumn dc = new DataColumn ("Column A");
575                         dt.Columns.Add (dc);
576
577                         dt.PrimaryKey = new DataColumn [] {dc};
578
579                         DataRow dr = dt.NewRow ();
580                         dr [0] = "a";
581                         dt.Rows.Add (dr);
582
583                         dr = dt.NewRow ();
584                         dr [0] = "b";
585                         dt.Rows.Add (dr);
586
587                         dr = dt.NewRow ();
588                         dr [0] = "c";
589                         dt.Rows.Add (dr);
590
591                         DataRow row = (DataRow) ds.Tables [0].Rows.Find (new object [] {"a"});
592                         
593                         Assert.IsNotNull (row);
594                 }
595                 
596                 [Test]
597                 public void InsertAt ()
598                 {
599                         _tbl.Columns.Add ();
600                         _tbl.Columns.Add ();
601                         _tbl.Columns.Add ();
602                         DataRowCollection Rows = _tbl.Rows;
603                         
604                         Rows.Add (new object [] {"a", "aa", "aaa"});
605                         Rows.Add (new object [] {"b", "bb", "bbb"});
606                         Rows.Add (new object [] {"c", "cc", "ccc"});
607                         Rows.Add (new object [] {"d", "dd", "ddd"});
608                         
609                         DataRow Row = _tbl.NewRow ();
610                         Row [0] = "e";
611                         Row [1] = "ee";
612                         Row [2] = "eee";
613                         
614                         try {
615                                 Rows.InsertAt (Row, -1);
616                                 Assert.Fail ("test#01");
617                         } catch (Exception e) {
618                                 Assert.AreEqual (typeof (IndexOutOfRangeException), e.GetType (), "test#02");
619                                 // Never premise English.
620                                 //Assert.AreEqual ("The row insert position -1 is invalid.", e.Message, "test#03");
621                         }
622                         
623                         Rows.InsertAt (Row, 0);
624                         Assert.AreEqual ("e", Rows [0][0], "test#04");
625                         Assert.AreEqual ("a", Rows [1][0], "test#05");
626                         
627                         Row = _tbl.NewRow ();
628                         Row [0] = "f";
629                         Row [1] = "ff";
630                         Row [2] = "fff";
631                         
632                         Rows.InsertAt (Row, 5);
633                         Assert.AreEqual ("f", Rows [5][0], "test#06");
634                         
635                         Row = _tbl.NewRow ();
636                         Row [0] = "g";
637                         Row [1] = "gg";
638                         Row [2] = "ggg";
639
640                         Rows.InsertAt (Row, 500);
641                         Assert.AreEqual ("g", Rows [6][0], "test#07");
642
643                         try {
644                                 Rows.InsertAt (Row, 6); //Row already belongs to the table
645                                 Assert.Fail ("test#08");
646                         }
647                         catch (Exception e) {
648                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#09");
649                                 // Never premise English.
650                                 //Assert.AreEqual ("This row already belongs to this table.", e.Message, "test#10");
651                         }
652
653                         DataTable table = new DataTable ();
654                         DataColumn col = new DataColumn ("Name");
655                         table.Columns.Add (col);
656                         Row = table.NewRow ();
657                         Row ["Name"] = "Abc";
658                         table.Rows.Add (Row);
659                         try {
660                                 Rows.InsertAt (Row, 6);
661                                 Assert.Fail ("test#11");
662                         }
663                         catch (Exception e) {
664                                 Assert.AreEqual (typeof (ArgumentException), e.GetType (), "test#12");
665                                 // Never premise English.
666                                 //Assert.AreEqual ("This row already belongs to another table.", e.Message, "test#13");
667                         }
668
669                         table = new DataTable ();
670                         col = new DataColumn ("Name");
671                         col.DataType = typeof (string);
672                         table.Columns.Add (col);
673                         UniqueConstraint uk = new UniqueConstraint (col);
674                         table.Constraints.Add (uk);
675                         
676                         Row = table.NewRow ();
677                         Row ["Name"] = "aaa";
678                         table.Rows.InsertAt (Row, 0);
679         
680                         Row = table.NewRow ();
681                         Row ["Name"] = "aaa";
682                         try {
683                                 table.Rows.InsertAt (Row, 1);
684                                 Assert.Fail ("test#14");
685                         }
686                         catch (Exception e) {
687                                 Assert.AreEqual (typeof (ConstraintException), e.GetType (), "test#15");
688                         }
689                         try {
690                                 table.Rows.InsertAt (null, 1);
691                         }
692                         catch (Exception e) {
693                                 Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "test#16");
694                         }
695                 }
696                 
697                 [Test]
698                 public void Remove ()
699                 {
700                         _tbl.Columns.Add ();
701                         _tbl.Columns.Add ();
702                         _tbl.Columns.Add ();
703                         DataRowCollection Rows = _tbl.Rows;
704                         
705                         Rows.Add (new object [] {"a", "aa", "aaa"});
706                         Rows.Add (new object [] {"b", "bb", "bbb"});
707                         Rows.Add (new object [] {"c", "cc", "ccc"});
708                         Rows.Add (new object [] {"d", "dd", "ddd"});
709                         
710                         Assert.AreEqual (4, _tbl.Rows.Count, "test#01");
711                         
712                         Rows.Remove (_tbl.Rows [1]);
713                         Assert.AreEqual (3, _tbl.Rows.Count, "test#02");
714                         Assert.AreEqual ("a", _tbl.Rows [0] [0], "test#03");
715                         Assert.AreEqual ("c", _tbl.Rows [1] [0], "test#04");
716                         Assert.AreEqual ("d", _tbl.Rows [2] [0], "test#05");
717                         
718                         try {
719                                 Rows.Remove (null);
720                                 Assert.Fail ("test#06");
721                         } catch (Exception e) {
722                                 Assert.AreEqual (typeof (IndexOutOfRangeException), e.GetType (), "test#07");
723                                 // Never premise English.
724                                 //Assert.AreEqual ("The given datarow is not in the current DataRowCollection.", e.Message, "test#08");
725                         }
726                         
727                         DataRow Row = new DataTable ().NewRow ();
728                         
729                         try {
730                                 Rows.Remove (Row);
731                                 Assert.Fail ("test#09");
732                         } catch (Exception e) {
733                                 Assert.AreEqual (typeof (IndexOutOfRangeException), e.GetType (), "test#10");
734                                 // Never premise English.
735                                 //Assert.AreEqual ("The given datarow is not in the current DataRowCollection.", e.Message, "test#11");
736                         }
737                         
738                         try {
739                                 Rows.RemoveAt (-1);
740                                 Assert.Fail ("test#12");
741                         } catch (Exception e) {
742                                 Assert.AreEqual (typeof (IndexOutOfRangeException), e.GetType (), "test#13");
743                                 // Never premise English.
744                                 //Assert.AreEqual ("There is no row at position -1.", e.Message, "test#14");
745                         }
746                         
747                         try { 
748                                 Rows.RemoveAt (64);
749                                 Assert.Fail ("test#15");
750                         } catch (Exception e) {
751                                 Assert.AreEqual (typeof (IndexOutOfRangeException), e.GetType (), "test#16");
752                                 // Never premise English.
753                                 //Assert.AreEqual ("There is no row at position 64.", e.Message, "test#17");
754                         }
755                         
756                         Rows.RemoveAt (0);
757                         Rows.RemoveAt (1);
758                         Assert.AreEqual (1, Rows.Count, "test#18");
759                         Assert.AreEqual ("c", Rows [0] [0], "test#19");
760                 }
761
762                 [Test]
763                 public void IndexOf () {
764                         DataSet ds = new DataSet ();
765
766                         DataTable dt = new DataTable ();
767                         ds.Tables.Add (dt);
768
769                         DataColumn dc = new DataColumn ("Column A");
770                         dt.Columns.Add (dc);
771
772                         dt.PrimaryKey = new DataColumn[] { dc };
773
774                         DataRow dr1 = dt.NewRow ();
775                         dr1[0] = "a";
776                         dt.Rows.Add (dr1);
777
778                         DataRow dr2 = dt.NewRow ();
779                         dr2[0] = "b";
780                         dt.Rows.Add (dr2);
781
782                         DataRow dr3 = dt.NewRow ();
783                         dr3[0] = "c";
784                         dt.Rows.Add (dr3);
785
786                         DataRow dr4 = dt.NewRow ();
787                         dr4[0] = "d";
788                         dt.Rows.Add (dr4);
789
790                         DataRow dr5 = dt.NewRow ();
791                         dr5[0] = "e";
792
793                         int index = ds.Tables[0].Rows.IndexOf (dr3);
794                         Assert.AreEqual (2, index, "IndexOf-Yes");
795                         
796                         index = ds.Tables[0].Rows.IndexOf (dr5);
797                         Assert.AreEqual (-1, index, "IndexOf-No");
798                 }
799                 [Test]
800                 public void IndexOfTest()
801                 {
802                         DataTable dt = new DataTable("TestWriteXmlSchema");
803                         dt.Columns.Add("Col1", typeof(int));
804                         dt.Columns.Add("Col2", typeof(int));
805                         DataRow dr = dt.NewRow();
806                         dr[0] = 10;
807                         dr[1] = 20;
808                         dt.Rows.Add(dr);
809                         DataRow dr1 = dt.NewRow();
810                         dr1[0] = 10;
811                         dr1[1] = 20;
812                         dt.Rows.Add(dr1);
813                         DataRow dr2 = dt.NewRow();
814                         dr2[0] = 10;
815                         dr2[1] = 20;
816                         dt.Rows.Add(dr2);
817                         Assert.AreEqual (1, dt.Rows.IndexOf (dr1));
818                         DataTable dt1 = new DataTable("HelloWorld");
819                         dt1.Columns.Add("T1", typeof(int));
820                         dt1.Columns.Add("T2", typeof(int));
821                         DataRow dr3 = dt1.NewRow();
822                         dr3[0] = 10;
823                         dr3[1] = 20;
824                         dt1.Rows.Add(dr3);
825                         Assert.AreEqual (-1, dt.Rows.IndexOf (dr3));
826                         Assert.AreEqual (-1, dt.Rows.IndexOf (null));
827                 }
828
829                 [Test]
830                 public void Find_DoesntThrowWithNullObjectInArray () // Novell bug 519648
831                 {
832                         var dt = new DataTable ("datatable");
833
834                         var column = new DataColumn ();
835                         dt.Columns.Add (column);
836                         var columns = new DataColumn [] { column };
837                         dt.PrimaryKey = columns;
838
839                         try {
840                                 Assert.AreEqual (null, dt.Rows.Find (new object [] { null }));
841                         } catch (IndexOutOfRangeException) {
842                                 Assert.Fail ("Bug #519648 (https://bugzilla.novell.com/show_bug.cgi?id=519648) is not fixed.");
843                         }
844                 }
845
846                 [Test]
847                 public void Find_DoesntThrowWithNullObject () // Novell bug 519648
848                 {
849                         var dt = new DataTable ("datatable");
850
851                         var column = new DataColumn ();
852                         dt.Columns.Add (column);
853                         var columns = new DataColumn [] { column };
854                         dt.PrimaryKey = columns;
855
856                         try {
857                                 Assert.AreEqual (null, dt.Rows.Find ( (object)null));
858                         } catch (IndexOutOfRangeException) {
859                                 Assert.Fail ("Bug #519648 (https://bugzilla.novell.com/show_bug.cgi?id=519648) is not fixed.");
860                         }
861                 }
862         }
863 }