Making sure mono_marshal_free is used instead of g_free in mono_string_builder_to_utf8
[mono.git] / mcs / class / System.Data / Test / System.Data / ConstraintCollectionTest2.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30 using MonoTests.System.Data.Utils;
31 using System;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Data;
35
36 namespace MonoTests.System.Data
37 {
38         [TestFixture] public class ConstraintCollectionTest2
39         {
40                 private bool CollectionChangedFlag = false;
41
42                 [Test] public void CanRemove_ParentForeign()
43                 {
44                         DataSet ds = DataProvider.CreateForigenConstraint();
45                         Assert.AreEqual(false, ds.Tables["parent"].Constraints.CanRemove(ds.Tables["parent"].Constraints[0]), "CN1");
46                 }
47
48                 [Test] public void CanRemove_ChildForeign()
49                 {
50                         DataSet ds = DataProvider.CreateForigenConstraint();
51                         Assert.AreEqual(true, ds.Tables["child"].Constraints.CanRemove(ds.Tables["child"].Constraints[0]), "CN2");
52                 }
53
54                 [Test] public void CanRemove_ParentAndChildForeign()
55                 {
56                         DataSet ds = DataProvider.CreateForigenConstraint();
57                         //remove the forigen and ask about the unique
58                         ds.Tables["child"].Constraints.Remove(ds.Tables["child"].Constraints[0]);
59                         Assert.AreEqual(true, ds.Tables["parent"].Constraints.CanRemove(ds.Tables["parent"].Constraints[0]), "CN3");
60                 }
61
62                 // FIXME. This test isn't complete.
63                 public void CanRemove_Unique()
64                 {
65                         DataTable dt = DataProvider.CreateUniqueConstraint();
66                         //remove the forigen and ask about the unique
67                         dt.Constraints.Remove(dt.Constraints[0]);
68                         Assert.AreEqual(true, dt.Constraints.CanRemove(dt.Constraints[0]), "CN4");
69                 }
70
71                 [Test] public void Clear_Foreign()
72                 {
73                         DataSet ds = DataProvider.CreateForigenConstraint();
74                         foreach(DataTable dt in ds.Tables)
75                         {
76                                 dt.Constraints.Clear();
77                         }
78                         Assert.AreEqual(0, ds.Tables[0].Constraints.Count, "CN5");
79                         Assert.AreEqual(0, ds.Tables[0].Constraints.Count, "CN6");
80
81                 }
82
83                 [Test] public void Clear_Unique()
84                 {
85                         DataTable dt = DataProvider.CreateUniqueConstraint();
86                         int rowsCount = dt.Rows.Count;
87                         dt.Constraints.Clear();
88                         DataRow dr = dt.NewRow();
89                         dr[0] = 1;
90                         dt.Rows.Add(dr);
91                         Assert.AreEqual(rowsCount+1, dt.Rows.Count, "CN7"); //Just checking that no expection ocuured
92                 }
93
94                 [Test] public void CollectionChanged()
95                 {
96                         DataTable dt = DataProvider.CreateParentDataTable();
97                         CollectionChangedFlag = false;
98                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);     
99                         dt = DataProvider.CreateUniqueConstraint(dt);
100                         Assert.AreEqual(true, CollectionChangedFlag, "CN8"); 
101                 }
102
103                 [Test] public void Contains_ByName()
104                 {
105                         DataSet ds =  DataProvider.CreateForigenConstraint();
106          
107                         //changing the constraints's name
108
109                         ds.Tables["child"].Constraints[0].ConstraintName = "name1";
110                         ds.Tables["parent"].Constraints[0].ConstraintName = "name2";
111
112
113                         Assert.AreEqual(true, ds.Tables["child"].Constraints.Contains("name1"), "CN9");
114                         Assert.AreEqual(false, ds.Tables["child"].Constraints.Contains("xxx"), "CN10");
115                         Assert.AreEqual(true, ds.Tables["parent"].Constraints.Contains("name2"), "CN11");
116                         Assert.AreEqual(false, ds.Tables["parent"].Constraints.Contains("xxx"), "CN12");
117
118                 }
119
120                 [Test] public void CopyTo()
121                 {
122                         DataTable dt = DataProvider.CreateUniqueConstraint();
123                         dt.Constraints.Add("constraint2",dt.Columns["String1"],true);
124
125                         object[] ar = new object[2];
126
127                         dt.Constraints.CopyTo(ar,0);
128                         Assert.AreEqual(2, ar.Length, "CN13");
129                 }
130
131                 [Test] public void Count()
132                 {
133                         DataTable dt = DataProvider.CreateUniqueConstraint();
134                         Assert.AreEqual(1, dt.Constraints.Count, "CN14");
135
136                         //Add
137
138                         dt.Constraints.Add("constraint2",dt.Columns["String1"],false);
139                         Assert.AreEqual(2, dt.Constraints.Count, "CN15");
140
141                         //Remove
142
143                         dt.Constraints.Remove("constraint2");
144                         Assert.AreEqual(1, dt.Constraints.Count, "CN16");
145                 }
146
147                 [Test] public void GetEnumerator()
148                 {
149                         DataTable dt = DataProvider.CreateUniqueConstraint();
150                         dt.Constraints.Add("constraint2",dt.Columns["String1"],false);
151
152                         int counter=0;
153                         IEnumerator myEnumerator = dt.Constraints.GetEnumerator();
154                         while (myEnumerator.MoveNext())
155                         {
156                                 counter++;
157
158                         }
159                         Assert.AreEqual(2, counter, "CN17");
160                 }
161
162                 [Test] public void IndexOf()
163                 {
164                         DataTable dt = DataProvider.CreateUniqueConstraint();
165                         Assert.AreEqual(0, dt.Constraints.IndexOf(dt.Constraints[0]), "CN18");
166
167                         //Add new constraint
168                         Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
169
170                         dt.Constraints.Add(con);
171                         Assert.AreEqual(1, dt.Constraints.IndexOf(con), "CN19");
172
173                         //Remove it and try to look for it 
174
175                         dt.Constraints.Remove(con);
176                         Assert.AreEqual(-1, dt.Constraints.IndexOf(con), "CN20");
177
178                 }
179
180                 [Test] public void IndexOf_ByName()
181                 {
182                         DataTable dt = DataProvider.CreateUniqueConstraint();
183                         dt.Constraints[0].ConstraintName="name1";
184                         Assert.AreEqual(0, dt.Constraints.IndexOf("name1"), "CN21");
185
186                         //Add new constraint
187                         Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
188                         con.ConstraintName="name2";
189
190                         dt.Constraints.Add(con);
191                         Assert.AreEqual(1, dt.Constraints.IndexOf("name2"), "CN22");
192
193                         //Remove it and try to look for it 
194
195                         dt.Constraints.Remove(con);
196                         Assert.AreEqual(-1, dt.Constraints.IndexOf("name2"), "CN23");
197
198                 }
199
200                 [Test] public void IndexOf_SameColumns ()
201                 {
202                         DataSet ds = new DataSet ();
203                         DataTable table1 = ds.Tables.Add ("table1");
204                         DataTable table2 = ds.Tables.Add ("table2");
205                         DataColumn pcol = table1.Columns.Add ("col1");
206                         DataColumn ccol = table2.Columns.Add ("col1");
207         
208                         ds.Relations.Add ("fk_rel", pcol, ccol); 
209
210                         ForeignKeyConstraint fk = new ForeignKeyConstraint ("fk", pcol, ccol);
211                         Assert.AreEqual (-1, ds.Tables [1].Constraints.IndexOf (fk), "#1");
212                 }
213                 
214                 [Test]
215                 public void Add_RelationFirst_ConstraintNext()
216                 {
217                         DataSet ds = new DataSet ();
218                         DataTable table1 = ds.Tables.Add ("table1");
219                         DataTable table2 = ds.Tables.Add ("table2");
220                         DataColumn pcol = table1.Columns.Add ("col1");
221                         DataColumn ccol = table2.Columns.Add ("col1");
222         
223                         ds.Relations.Add ("fk_rel", pcol, ccol); 
224
225                         try {
226                                 table2.Constraints.Add ("fk_cons", pcol, ccol);
227                                 Assert.Fail ("#1 Cannot add duplicate fk constraint");
228                         }catch (DataException e) {
229                         }
230
231                         try {
232                                 table1.Constraints.Add ("pk_cons", pcol, false);
233                                 Assert.Fail ("#2 Cannot add duplicate unique constraint");
234                         }catch (DataException e) {
235                         }
236                 }
237
238                 [Test]
239                 public void Add_ConstraintFirst_RelationNext ()
240                 {
241                         DataSet ds = new DataSet ();
242                         DataTable table1 = ds.Tables.Add ("table1");
243                         DataTable table2 = ds.Tables.Add ("table2");
244                         DataColumn pcol = table1.Columns.Add ("col1");
245                         DataColumn ccol = table2.Columns.Add ("col1");
246         
247                         table2.Constraints.Add ("fk_cons", pcol, ccol);
248
249                         // Should not throw DataException 
250                         ds.Relations.Add ("fk_rel", pcol, ccol);
251
252                         Assert.AreEqual (1, table2.Constraints.Count, "#1 duplicate constraint shudnt be added");
253                         Assert.AreEqual (1, table1.Constraints.Count, "#2 duplicate constraint shudnt be added");
254                         Assert.AreEqual ("fk_cons", table2.Constraints [0].ConstraintName, "#3 shouldnt be overwritten");
255                         Assert.AreEqual ("Constraint1", table1.Constraints [0].ConstraintName, "#4 shouldnt be overwritten");
256                 }
257
258                 [Test] public void IsReadOnly()
259                 {
260                         DataTable dt = DataProvider.CreateUniqueConstraint();
261                         Assert.AreEqual(false, dt.Constraints.IsReadOnly, "CN24"); 
262                 }
263
264                 [Test] public void IsSynchronized()
265                 {
266                         DataTable dt = DataProvider.CreateUniqueConstraint();
267                         Assert.AreEqual(false, dt.Constraints.IsSynchronized, "CN25");
268          
269                         ConstraintCollection col = (ConstraintCollection)dt.Constraints.SyncRoot;
270
271         //              lock(dt.Constraints.SyncRoot)
272         //              {
273                         //      Assert.AreEqual(true, col.IsSynchronized, "CN26");
274
275                         //}
276                 }
277
278                 [Test] public void Remove()
279                 {
280                         DataTable dt = DataProvider.CreateUniqueConstraint();
281                         dt.Constraints.Remove(dt.Constraints[0]);
282                         Assert.AreEqual(0, dt.Constraints.Count, "CN27");
283                 }
284
285                 [Test] public void Remove_CheckUnique ()
286                 {
287                         DataTable table = new DataTable ();
288                         DataColumn col1 = table.Columns.Add ("col1");
289                         DataColumn col2 = table.Columns.Add ("col2");
290
291                         Assert.IsFalse (col1.Unique, "#1");
292
293                         Constraint uc = table.Constraints.Add ("", col1, false);
294                         Assert.IsTrue (col1.Unique, "#2 col shud be set to unique");
295
296                         table.Constraints.Remove (uc);
297                         Assert.IsFalse (col1.Unique, "#3 col should no longer be unique");
298
299                         table.PrimaryKey = new DataColumn[] {col2};
300
301                         try {
302                                 table.Constraints.Remove (table.Constraints [0]);
303                                 Assert.Fail ("#4 Cannot Remove PrimaryKey");
304                         } catch (ArgumentException) {
305                         }
306                 }
307
308                 [Test] public void Remove_ByNameSimple()
309                 {
310                         DataTable dt = DataProvider.CreateUniqueConstraint();
311                         dt.Constraints[0].ConstraintName = "constraint1";
312                         dt.Constraints.Remove("constraint1");
313                         Assert.AreEqual(0, dt.Constraints.Count, "CN28");
314                 }
315
316                 [Test] public void Remove_ByNameWithAdd()
317                 {
318                         DataTable dt = DataProvider.CreateUniqueConstraint();
319                         dt.Constraints[0].ConstraintName = "constraint1";
320                         Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
321                         dt.Constraints.Add(con);
322                         dt.Constraints.Remove(con);
323
324                         Assert.AreEqual(1, dt.Constraints.Count, "CN29");
325                         Assert.AreEqual("constraint1", dt.Constraints[0].ConstraintName, "CN30");
326                 }
327
328                 [Test] public void Remove_CollectionChangedEvent()
329                 {
330                         DataTable dt = DataProvider.CreateUniqueConstraint();
331                         CollectionChangedFlag = false;
332                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
333                         dt.Constraints.Remove(dt.Constraints[0]);
334                         Assert.AreEqual(true, CollectionChangedFlag, "CN31"); //Checking that event has raised
335                 }
336
337                 [Test] public void Remove_ByNameCollectionChangedEvent()
338                 {
339                         DataTable dt = DataProvider.CreateUniqueConstraint();
340                         CollectionChangedFlag = false;
341                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);
342                         dt.Constraints.Remove("constraint1");
343                         Assert.AreEqual(true, CollectionChangedFlag, "CN32"); //Checking that event has raised
344
345                 }
346
347                 [Test] public void add_CollectionChanged()
348                 {
349                         DataTable dt = DataProvider.CreateParentDataTable();
350                         CollectionChangedFlag = false;
351                         dt.Constraints.CollectionChanged += new CollectionChangeEventHandler(Constraints_CollectionChangedHandler);     
352                         dt = DataProvider.CreateUniqueConstraint(dt);
353                         Assert.AreEqual(true, CollectionChangedFlag, "CN33"); 
354                 }
355
356                 private void Constraints_CollectionChangedHandler(object sender, CollectionChangeEventArgs e)
357                 {
358                         CollectionChangedFlag = true;
359                 }
360
361                 [Test]
362                 public void Remove_Constraint ()
363                 {
364                         DataTable table1 = new DataTable ("table1");
365                         DataTable table2 = new DataTable ("table2");
366
367                         DataColumn col1 = table1.Columns.Add ("col1", typeof (int));
368                         DataColumn col2 = table1.Columns.Add ("col2", typeof (int));
369                         DataColumn col3 = table2.Columns.Add ("col1", typeof (int));
370
371                         Constraint c1 = table1.Constraints.Add ("unique1", col1, false);
372                         Constraint c2 = table1.Constraints.Add ("unique2", col2, false);
373                         Constraint c3 = table2.Constraints.Add ("fk", col1, col3);
374
375                         table1.Constraints.Remove (c1);
376                         table1.Constraints.Remove (c2);
377                         table2.Constraints.Remove (c3);
378
379                         Assert.AreEqual (0, table1.Constraints.Count, "#1");
380                         Assert.AreEqual (0, table2.Constraints.Count, "#2");
381
382                         DataSet ds = new DataSet ();
383                         ds.Tables.Add (table1);
384                         ds.Tables.Add (table2);
385
386                         c1 = table1.Constraints.Add ("unique1", col1, false);
387                         c2 = table1.Constraints.Add ("unique2", col2, false);
388                         c3 = table2.Constraints.Add ("fk", col1, col3);
389
390                         try {
391                                 table1.Constraints.Remove (c1);
392                                 Assert.Fail ("#3");
393                         } catch (ArgumentException) {
394                         }
395
396                         Assert.AreEqual (2, table1.Constraints.Count, "#4");
397
398                         table1.Constraints.Remove (c2);
399                         Assert.AreEqual (1, table1.Constraints.Count, "#5");
400
401                         table2.Constraints.Remove (c3);
402                         Assert.AreEqual (1, table1.Constraints.Count, "#6");
403                         Assert.AreEqual (0, table2.Constraints.Count, "#7");
404
405                         table1.Constraints.Remove (c1);
406                         Assert.AreEqual (0, table1.Constraints.Count, "#8");
407                 }
408
409                 public delegate void  testExceptionMethodCallback();
410
411                 [Test]
412                 public void Add_Constraint()
413                 {
414                         DataTable dt = DataProvider.CreateUniqueConstraint();
415                         Assert.AreEqual(1,dt.Constraints.Count,"ccac#1"); 
416                         Assert.AreEqual("Constraint1",dt.Constraints[0].ConstraintName,"ccac#2");                       
417
418                         DataSet ds = DataProvider.CreateForigenConstraint();
419                         Assert.AreEqual(1,ds.Tables[1].Constraints.Count,"ccac#3");
420                         Assert.AreEqual(1,ds.Tables[0].Constraints.Count,"ccac#4");
421
422                         ArrayList arr = new ArrayList(1);
423                         arr.Add(new ConstraintException()); 
424                         TestException( new testExceptionMethodCallback(DataProvider.TryToBreakUniqueConstraint),arr);
425
426                         arr = new ArrayList(1);
427                         arr.Add(new InvalidConstraintException()); 
428                         TestException( new testExceptionMethodCallback(DataProvider.TryToBreakForigenConstraint),arr);                  
429                 }
430
431                 public void TestException(testExceptionMethodCallback dlg,IList exceptionList)
432                 {                               
433                         try {
434                                 dlg();
435                                 Assert.Fail("ccac#A", ((Exception)exceptionList[0]).ToString()); 
436                         }
437                         catch(Exception ex) {                                   
438                                 foreach(Exception expectedEx in exceptionList)
439                                         if ( (expectedEx.GetType()) == (ex.GetType()) )
440                                                 return;                         
441                                 Assert.Fail("ccac#B");
442                         }               
443                 }
444
445                 [Test]
446                 public void Add_SDB1()
447                 {
448                         DataTable dt = DataProvider.CreateParentDataTable();
449                         dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],true);
450                         Assert.AreEqual(1,dt.Constraints.Count,1); 
451                         Assert.AreEqual("UniqueConstraint",dt.Constraints[0].ConstraintName,"CN34");                    
452                 }
453                 
454                 [Test]
455                 public void Add_SDB2()
456                 {
457                         DataTable dt = DataProvider.CreateParentDataTable();
458                         dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],false);
459                         Assert.AreEqual(1,dt.Constraints.Count,"CN34"); 
460                         Assert.AreEqual("UniqueConstraint",dt.Constraints[0].ConstraintName,"CN35");                    
461                 }
462                 
463                 [Test]
464                 public void Add_SDB3()
465                 {
466                         DataTable dt = DataProvider.CreateParentDataTable();
467                         dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],true);
468                         //Break the constraint
469
470                         ArrayList arr = new ArrayList(1);
471                         arr.Add(new ConstraintException()); 
472                         TestException( new testExceptionMethodCallback(DataProvider.TryToBreakUniqueConstraint),arr);
473
474                 }
475                 
476                 [Test]
477                 [ExpectedException(typeof(ConstraintException))]
478                 public void Add_SDB4()
479                 {
480                         DataTable dt = DataProvider.CreateParentDataTable();
481                         dt.Constraints.Add("UniqueConstraint",dt.Columns["ParentId"],false);
482                         //Break the constraint --> but we shouldn't get the excption --> wrong assumpation
483                         //TODO:check the right thing
484                         DataProvider.TryToBreakUniqueConstraint();
485                         Assert.AreEqual(2,dt.Select("ParentId=1").Length,"CN36");
486                 }
487
488                 [Test]
489                 public void Add_Constraint_Column_Column()
490                 {
491                         DataTable parent = DataProvider.CreateParentDataTable();
492                         DataTable child = DataProvider.CreateChildDataTable();
493          
494                         child.Constraints.Add("ForigenConstraint",parent.Columns[0],child.Columns[0]);
495
496                         Assert.AreEqual(1,parent.Constraints.Count,"ccaccc#1"); 
497                         Assert.AreEqual(1,child.Constraints.Count,"ccaccc#2"); 
498                         Assert.AreEqual("ForigenConstraint",child.Constraints[0].ConstraintName,"ccaccc#3");
499
500                         parent = DataProvider.CreateParentDataTable();
501                         child = DataProvider.CreateChildDataTable();
502          
503                         child.Constraints.Add("ForigenConstraint",parent.Columns[0],child.Columns[0]);
504
505                         ArrayList arr = new ArrayList(1);
506                         arr.Add(new InvalidConstraintException()); 
507                         TestException( new testExceptionMethodCallback(DataProvider.TryToBreakForigenConstraint),arr);
508
509                         Assert.AreEqual(1,parent.Constraints.Count,"ccaccc#4"); 
510                         Assert.AreEqual(1,child.Constraints.Count,"ccaccc#5"); 
511                 }
512
513                 [Test]
514                 public void AddRange_C1()
515                 {
516                         DataTable dt = new DataTable();
517                         dt.Constraints.AddRange(null);
518                         Assert.AreEqual(0,dt.Constraints.Count,"ccarc#1");
519                 }
520
521                 [Test]
522                 public void AddRange_C2()
523                 {
524                         DataSet ds = new DataSet();
525                         ds.Tables.Add(DataProvider.CreateParentDataTable());
526                         ds.Tables.Add(DataProvider.CreateChildDataTable());
527                         ds.Tables[1].Constraints.AddRange(GetConstraintArray(ds)); //Cuz foreign key belongs to child table
528                         Assert.AreEqual(2,ds.Tables[1].Constraints.Count,"ccarc#2");
529                         Assert.AreEqual(1,ds.Tables[0].Constraints.Count,"ccarc#3");
530                 }
531                 [Test]
532                 [ExpectedException(typeof(ArgumentException))]
533                 public void AddRange_C3()
534                 {
535                         DataSet ds = new DataSet();
536                         ds.Tables.Add(DataProvider.CreateParentDataTable());
537                         ds.Tables.Add(DataProvider.CreateChildDataTable());
538                         Constraint badConstraint = new UniqueConstraint(ds.Tables[0].Columns[0]);
539
540                         ds.Tables[1].Constraints.AddRange(new Constraint[] {badConstraint}); //Cuz foreign key belongs to child table                   
541                 }
542                 
543                 [Test]
544                 public void AddRange_C4()
545                 {
546                         ArrayList arr = new ArrayList(1);
547                         arr.Add(new ArgumentException());
548                         TestException(new testExceptionMethodCallback(AddRange_C3),arr);
549                 }
550
551                 private Constraint[] GetConstraintArray(DataSet ds)
552                 {
553                         DataTable parent = ds.Tables[0]; 
554                         DataTable child =  ds.Tables[1]; 
555                         Constraint[] constArray = new Constraint[2];
556
557                         //Create unique 
558                         constArray[0] = new UniqueConstraint("Unique1",child.Columns["ChildDouble"]);
559                         //Create foreign 
560                         constArray[1] = new ForeignKeyConstraint(parent.Columns[0],child.Columns[1]);
561
562                         return constArray;
563                 }
564
565                 [Test]
566                 public void Item()
567                 {
568                         DataTable dt = DataProvider.CreateUniqueConstraint();
569                         dt.Constraints[0].ConstraintName = "constraint1";
570                         Assert.AreEqual("constraint1",dt.Constraints[0].ConstraintName,"cci#1");
571                         Assert.AreEqual("constraint1",dt.Constraints["constraint1"].ConstraintName,"cci#2");
572
573                         ArrayList arr = new ArrayList(1);
574                         arr.Add(new IndexOutOfRangeException()); 
575                         TestException(new testExceptionMethodCallback(Item2),arr);
576                 }
577
578                 private void Item2()
579                 {
580                         DataTable dt = DataProvider.CreateUniqueConstraint();
581                         dt.Constraints[1].ConstraintName = "error";
582                 }
583
584                 private bool collectionChanged=false;
585
586                 [Test]
587                 public void RemoveAt_Integer()
588                 {
589                         DataTable dt = DataProvider.CreateUniqueConstraint();
590                         dt.Constraints.RemoveAt(0);
591                         Assert.AreEqual(0,dt.Constraints.Count,"ccrai#1");
592
593                         dt = DataProvider.CreateUniqueConstraint();
594                         Constraint con = new UniqueConstraint(dt.Columns["String1"],false);
595                         dt.Constraints[0].ConstraintName = "constraint1";
596                         con.ConstraintName="constraint2";
597                         dt.Constraints.Add(con);
598                         dt.Constraints.RemoveAt(0);
599                         Assert.AreEqual(1,dt.Constraints.Count,"ccrai#2");
600                         Assert.AreEqual("constraint2",dt.Constraints[0].ConstraintName,"ccrai#3");
601
602                         dt = DataProvider.CreateUniqueConstraint();
603                         dt.Constraints.CollectionChanged+=new CollectionChangeEventHandler(Constraints_CollectionChanged);
604                         dt.Constraints.RemoveAt(0);
605                         Assert.AreEqual(true,collectionChanged,"ccrai#4"); //Checking that event has raised
606
607                         ArrayList arr = new ArrayList(1);
608                         arr.Add(new IndexOutOfRangeException());
609                         TestException(new testExceptionMethodCallback(RemoveAt_I),arr);
610                 }
611
612                 private void Constraints_CollectionChanged(object sender, CollectionChangeEventArgs e)
613                 {
614                         collectionChanged = true;
615                 }
616
617                 private void RemoveAt_I()
618                 {
619                         DataTable dt = DataProvider.CreateUniqueConstraint();
620                         dt.Constraints.RemoveAt(2);
621                 }
622
623                 [Test]
624                 public void RemoveTest ()
625                 {
626                         DataTable table = new DataTable ();
627                         table.Columns.Add ("col1");
628                         Constraint c = table.Constraints.Add ("c", table.Columns [0], false);
629                         try {
630                                 table.Constraints.Remove ("sdfs");
631                                 Assert.Fail ("#1");
632                         } catch (ArgumentException e) {
633                                 Assert.AreEqual ("Constraint 'sdfs' does not belong to this DataTable.", 
634                                                 e.Message, "#2");
635                         }
636                         
637                         table.Constraints.Remove (c);
638                         Assert.AreEqual (0, table.Constraints.Count, "#3");
639
640                         // No exception shud be raised
641                         table.Constraints.Add (c);
642                         Assert.AreEqual (1, table.Constraints.Count, "#4");
643                 }
644         }
645 }