System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System.Data / Test / System.Data / ConstraintCollectionTest.cs
1 // ConstraintCollection.cs - NUnit Test Cases for testing the ConstraintCollection 
2 //      class.
3 //      
4 // Authors:
5 //   Franklin Wise (gracenote@earthlink.net)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Roopa Wilson (rowilson@novell.com) 
8 //
9 // (C) Franklin Wise
10 // (C) 2003 Martin Willemoes Hansen
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34 using NUnit.Framework;
35 using NUnit.Framework.SyntaxHelpers;
36 using System;
37 using System.Data;
38
39 namespace MonoTests.System.Data
40 {
41         [TestFixture]
42         public class ConstraintCollectionTest
43         {
44                 private DataTable _table;
45                 private DataTable _table2;
46                 private Constraint _constraint1;
47                 private Constraint _constraint2;
48
49                 [SetUp]
50                 public void GetReady ()
51                 {
52                         //Setup DataTable
53                         _table = new DataTable ("TestTable");
54                         _table.Columns.Add ("Col1", typeof(int));
55                         _table.Columns.Add ("Col2", typeof(int));
56                         _table.Columns.Add ("Col3", typeof(int));
57
58                         _table2 = new DataTable ("TestTable");
59                         _table2.Columns.Add ("Col1", typeof(int));
60                         _table2.Columns.Add ("Col2", typeof(int));
61
62                         //Use UniqueConstraint to test Constraint Base Class
63                         _constraint1 = new UniqueConstraint (_table.Columns [0], false); 
64                         _constraint2 = new UniqueConstraint (_table.Columns [1], false); 
65
66                         // not sure why this is needed since a new _table was just created
67                         // for us, but this Clear() keeps the tests from throwing
68                         // an exception when the Add() is called.
69                         _table.Constraints.Clear ();
70                 }
71
72                 [Test]
73                 public void Add ()
74                 {
75                         ConstraintCollection col = _table.Constraints;
76                         col.Add (_constraint1);
77                         col.Add (_constraint2);
78
79                         Assert.That (col.Count, Is.EqualTo (2), "Count doesn't equal added.");
80                 }
81
82                 [Test]
83                 public void AddExceptions ()
84                 {
85                         ConstraintCollection col = _table.Constraints;
86                         
87                         //null
88                         try {
89                                 col.Add (null);
90                                 Assert.Fail ("B1: Failed to throw ArgumentNullException.");
91                         } catch (ArgumentNullException) {
92                         } catch (AssertionException exc) {
93                                 throw exc;
94                         } catch {
95                                 Assert.Fail ("A1: Wrong exception type");
96                         }
97
98                         //duplicate name
99                         try {
100                                 _constraint1.ConstraintName = "Dog";
101                                 _constraint2.ConstraintName = "dog"; //case insensitive
102                                 col.Add (_constraint1);
103                                 col.Add (_constraint2);
104 #if NET_1_1
105 #else
106                                 Assert.Fail ("Failed to throw Duplicate name exception.");
107 #endif
108                                 col.Remove (_constraint2); // only for !1.0
109                                 col.Remove (_constraint1);
110                         }
111 #if ! NET_1_1
112                         catch (DuplicateNameException) {
113                         }
114 #endif
115                         catch (AssertionException exc) {
116                                 throw exc;
117                         }
118 /* Don't use such catch. They cover our eyes from the exact exception location.
119                         catch (Exception exc)
120                         {
121                                 Assert.Fail("A2: Wrong exception type. " + exc.ToString());
122                         }
123 */
124                         //Constraint Already exists
125                         try {
126                                 col.Add (_constraint1);
127 #if NET_1_1
128 #else
129                                 Assert.Fail ("B2: Failed to throw ArgumentException.");
130 #endif
131                                 col.Remove (_constraint1);
132                         } catch (ArgumentException) {
133 #if NET_1_1
134 #else
135                                 throw;
136 #endif
137                         } catch (AssertionException exc) {
138                                 throw exc;
139                         } catch {
140                                 Assert.Fail ("A3: Wrong exception type");
141                         }
142                 }
143
144                 [Test]
145                 public void Indexer ()
146                 {
147                         Constraint c1 = new UniqueConstraint (_table.Columns [0]);
148                         Constraint c2 = new UniqueConstraint (_table.Columns [1]);
149
150                         c1.ConstraintName = "first";
151                         c2.ConstraintName = "second";
152
153
154                         _table.Constraints.Add (c1);
155                         _table.Constraints.Add (c2);
156
157                         Assert.AreSame (c1, _table.Constraints [0], "A1"); 
158                         Assert.AreSame (c2, _table.Constraints [1], "A2");
159
160                         Assert.AreSame (c1, _table.Constraints ["first"], "A3");
161                         Assert.AreSame (c2, _table.Constraints ["sEcond"], "A4"); //case insensitive
162
163                 }
164
165                 [Test]
166                 public void IndexOf ()
167                 {
168                         Constraint c1 = new UniqueConstraint (_table.Columns [0]);
169                         Constraint c2 = new UniqueConstraint (_table.Columns [1]);
170
171                         c1.ConstraintName = "first";
172                         c2.ConstraintName = "second";
173
174                         _table.Constraints.Add (c1);
175                         _table.Constraints.Add (c2);
176
177                         Assert.AreEqual (0, _table.Constraints.IndexOf (c1), "A1");
178                         Assert.AreEqual (1, _table.Constraints.IndexOf (c2), "A2");
179                         Assert.AreEqual (0, _table.Constraints.IndexOf ("first"), "A3");
180                         Assert.AreEqual (1, _table.Constraints.IndexOf ("second"), "A4");
181                 }
182
183                 [Test]
184                 public void Contains ()
185                 {
186                         Constraint c1 = new UniqueConstraint (_table.Columns [0]);
187                         Constraint c2 = new UniqueConstraint (_table.Columns [1]);
188
189                         c1.ConstraintName = "first";
190                         c2.ConstraintName = "second";
191
192                         _table.Constraints.Add (c1);
193
194                         Assert.That (_table.Constraints.Contains (c1.ConstraintName), Is.True);
195                         Assert.That (_table.Constraints.Contains (c2.ConstraintName), Is.False);
196                 }
197
198                 [Test]
199                 public void IndexerFailures ()
200                 {
201                         _table.Constraints.Add (new UniqueConstraint (_table.Columns [0]));
202
203                         //This doesn't throw
204                         Assert.That (_table.Constraints ["notInCollection"], Is.Null);
205                         
206                         //Index too high
207                         try {
208                                 Constraint c = _table.Constraints [_table.Constraints.Count];
209                                 Assert.Fail ("B1: Failed to throw IndexOutOfRangeException.");
210                         } catch (IndexOutOfRangeException) {
211                         } catch (AssertionException exc) {
212                                 throw exc;
213                         } catch {
214                                 Assert.Fail ("A1: Wrong exception type");
215                         }
216
217                         //Index too low
218                         try {
219                                 Constraint c = _table.Constraints [-1];
220                                 Assert.Fail ("B2: Failed to throw IndexOutOfRangeException.");
221                         } catch (IndexOutOfRangeException) {
222                         } catch (AssertionException exc) {
223                                 throw exc;
224                         } catch {
225                                 Assert.Fail ("A2: Wrong exception type");
226                         }       
227                 }
228
229                 [Test]
230                 public void AddFkException1 ()
231                 {
232                         DataSet ds = new DataSet ();
233                         ds.Tables.Add (_table);
234                         _table2.TableName = "TestTable2";
235                         ds.Tables.Add (_table2);
236
237                         _table.Rows.Add (new object [] {1});
238                         _table.Rows.Add (new object [] {1});
239
240                         //FKC: can't create unique constraint because duplicate values already exist
241                         try {
242                                 ForeignKeyConstraint fkc = new ForeignKeyConstraint (_table.Columns [0],
243                                                                                         _table2.Columns [0]);
244                                 
245                                 _table2.Constraints.Add (fkc);  //should throw                  
246                                 Assert.Fail ("B1: Failed to throw ArgumentException.");
247                         } catch (ArgumentException) {
248                         } catch (AssertionException exc) {
249                                 throw exc;
250                         } catch (Exception exc) {
251                                 Assert.Fail ("A1: Wrong Exception type. " + exc.ToString ());
252                         }
253                 }
254
255                 [Test]
256                 public void AddFkException2 ()
257                 {
258                         //Foreign key rules only work when the tables
259                         //are apart of the dataset
260                         DataSet ds = new DataSet ();
261                         ds.Tables.Add (_table);
262                         _table2.TableName = "TestTable2";
263                         ds.Tables.Add (_table2);
264
265                         _table.Rows.Add (new object [] {1});
266                         
267                         // will need a matching parent value in 
268                         // _table
269                         _table2.Rows.Add (new object [] {3}); 
270                                                                 
271
272                         //FKC: no matching parent value
273                         try {
274                                 ForeignKeyConstraint fkc = new ForeignKeyConstraint (_table.Columns [0],
275                                         _table2.Columns [0]);
276                                 
277                                 _table2.Constraints.Add (fkc);  //should throw                  
278                                 Assert.Fail ("B1: Failed to throw ArgumentException.");
279                         } catch (ArgumentException) {
280                         } catch (AssertionException exc) {
281                                 throw exc;
282                         } catch (Exception exc) {
283                                 Assert.Fail ("A1: Wrong Exception type. " + exc.ToString ());
284                         }
285                 }
286
287                 [Test]
288                 public void AddUniqueExceptions ()
289                 {
290                         
291
292                         //UC: can't create unique constraint because duplicate values already exist
293                         try {
294                                 _table.Rows.Add (new object [] {1});
295                                 _table.Rows.Add (new object [] {1});
296                                 UniqueConstraint uc = new UniqueConstraint (_table.Columns [0]);
297                                 
298                                 _table.Constraints.Add (uc);    //should throw                  
299                                 Assert.Fail ("B1: Failed to throw ArgumentException.");
300                         } catch (ArgumentException) {
301                         } catch (AssertionException exc) {
302                                 throw exc;
303                         } catch (Exception exc) {
304                                 Assert.Fail ("A1: Wrong Exception type. " + exc.ToString ());
305                         }
306                 }
307
308                 [Test]
309                 //Tests AddRange (), CanRemove (), RemoveAt (), Remove (), Exceptions of  Remove(), and Clear ()
310                 public void AddRemoveTest ()
311                 {
312                         AddRange ();
313                         //CanRemove (); This test is ignored
314                         Remove ();
315                         //RemoveAt (); This test is ignored
316
317                         // This test is expected to be failed, so don't reuse it.
318                         //RemoveExceptions ();
319                         _table.Constraints.Remove (_table.Constraints [0]);
320
321                         Clear ();
322                 }
323
324                 [Test]
325                 public void AddRange ()
326                 {
327                         _constraint1.ConstraintName = "UK1";
328                         _constraint2.ConstraintName = "UK12";
329                                                                                                     
330                         ForeignKeyConstraint _constraint3 = new ForeignKeyConstraint ("FK2", _table.Columns [0],
331                                         _table2.Columns [0]);
332                         UniqueConstraint _constraint4 = new UniqueConstraint ("UK2", _table2.Columns [1]);
333                                                                                                     
334                         // Add the constraints.
335                         Constraint [] constraints = {_constraint1, _constraint2};
336                         _table.Constraints.AddRange (constraints);
337                                                                                                                                                                                                          
338                         Constraint [] constraints1 = {_constraint3, _constraint4};
339                         _table2.Constraints.AddRange (constraints1);
340                                                                                                     
341                         Assert.AreEqual ("UK1", _table.Constraints [0].ConstraintName, "A1");
342                         Assert.AreEqual ("UK12", _table.Constraints [1].ConstraintName, "A2");
343                                                                                               
344                         Assert.AreEqual ("FK2", _table2.Constraints [0].ConstraintName, "A3");
345                         Assert.AreEqual ("UK2", _table2.Constraints [1].ConstraintName, "A4");
346                 }
347                 
348                 [Test]
349                 [Category ("NotDotNet")]
350                 // Even after EndInit(), MS.NET does not fill Table property
351                 // on UniqueConstraint.
352                 public void TestAddRange2 ()
353                 {
354                         DataTable table = new DataTable ("Table");
355                         DataColumn column1 = new DataColumn ("col1");
356                         DataColumn column2 = new DataColumn ("col2");
357                         DataColumn column3 = new DataColumn ("col3");
358                         table.Columns.Add (column1);
359                         table.Columns.Add (column2);
360                         table.Columns.Add (column3);
361                         string [] columnNames = {"col1", "col2", "col3"};
362                                                                                                     
363                         Constraint [] constraints = new Constraint[3];
364                         constraints [0] = new UniqueConstraint ("Unique1", column1);
365                         constraints [1] = new UniqueConstraint ("Unique2", column2);
366                         constraints [2] = new UniqueConstraint ("Unique3", columnNames, true);
367                                                                                                     
368                         table.BeginInit ();
369                         //Console.WriteLine(table.InitStatus == DataTable.initStatus.BeginInit);
370                         table.Constraints.AddRange (constraints);
371                                                                                                     
372                         //Check the table property of UniqueConstraint Object
373                         try {
374                                 Assert.That (constraints [2].Table, Is.Null, "#A01");
375                         } catch (Exception e) {
376                                 Assert.That (e, Is.TypeOf (typeof(NullReferenceException)), "#A02");
377                         }
378
379                         table.EndInit ();
380
381                         // After EndInit is called the constraints associated with most recent call to AddRange() must be
382                         // added to the ConstraintCollection
383                         Assert.That (constraints [2].Table.ToString (), Is.EqualTo ("Table"), "#A03");
384                         Assert.That (table.Constraints.Contains ("Unique1"), Is.True, "#A04");
385                         Assert.That (table.Constraints.Contains ("Unique3"), Is.True, "#A06");
386                         Assert.That (table.Constraints.Contains ("Unique2"), Is.True, "#A05");
387                 }
388
389                 [Test]
390                 public void Clear ()
391                 {
392                         try {
393                                 _table.Constraints.Clear (); //Clear all constraints
394                                 Assert.That (_table.Constraints.Count, Is.EqualTo (0), "A1"); //No constraints should remain
395                                 _table2.Constraints.Clear ();
396                                 Assert.That (_table2.Constraints.Count, Is.EqualTo (0), "A2"); //No constraints should remain
397                         } catch (Exception e) {
398                                 Console.WriteLine (e);
399                         }
400                 }
401
402                 [Test]
403                 [Ignore ("This never works on MS.NET (and it should not)")]
404                 public void CanRemove ()
405                 {
406                         Assert.That (_table.Constraints.CanRemove (_table.Constraints [0]), Is.False, "A1");
407                 }
408
409                 [Test]
410                 public void CollectionChanged ()
411                 {
412                 }
413 #if false
414                 //
415                 // If this fails on MS.NET and its supposed to fail, why do we have this as Ignore?
416                 //
417                 [Test]
418                 [Ignore ("MS.NET fails this test (and it should fail)")]
419                 public void RemoveAt()
420                 {
421                          _table2.Constraints.RemoveAt (1); //Remove constraint and again add it
422                          AssertEquals ("A1", 1, _table2.Constraints.Count);                                                  UniqueConstraint _constraint4  = new UniqueConstraint ("UK2", _table2.Columns [1]);
423                          // Add the constraints.
424                          Constraint [] constraints = {_constraint4};
425                          _table.Constraints.AddRange (constraints);
426
427                 }
428 #endif
429
430                 //[Test]
431                 [Ignore ("MS.NET fails this test (and it should fail)")]
432                 public void Remove ()
433                 {
434                         _table2.Constraints.Remove (_table2.Constraints [1]); //Remove constraint and again add it
435                         Assert.That (_table2.Constraints.Count, Is.EqualTo (1), "A1");
436                         UniqueConstraint _constraint4 = new UniqueConstraint ("UK2", _table2.Columns [1]);                                                                               
437                         // Add the constraints.
438                         Constraint [] constraints = {_constraint4};
439                         _table2.Constraints.AddRange (constraints);
440                 }
441
442                 [Test]
443                 public void RemoveExceptions ()
444                 {
445                         try {
446                                 //Remove constraint that cannot be removed
447                                 _table.Constraints.Remove (_table.Constraints [0]);
448                                 Assert.Fail ("A1");
449                         } catch (Exception e) {
450                                 Assert.That (e, Is.TypeOf (typeof(IndexOutOfRangeException)), "A2");
451                         }
452                 }
453         }
454 }