1 // ConstraintCollection.cs - NUnit Test Cases for testing the ConstraintCollection
5 // Franklin Wise (gracenote@earthlink.net)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Roopa Wilson (rowilson@novell.com)
10 // (C) 2003 Martin Willemoes Hansen
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using NUnit.Framework;
40 namespace MonoTests.System.Data
43 public class ConstraintCollectionTest : Assertion {
44 private DataTable _table;
45 private DataTable _table2;
46 private Constraint _constraint1;
47 private Constraint _constraint2;
50 public void GetReady()
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));
58 _table2 = new DataTable("TestTable");
59 _table2.Columns.Add("Col1",typeof(int));
60 _table2.Columns.Add("Col2",typeof(int));
62 //Use UniqueConstraint to test Constraint Base Class
63 _constraint1 = new UniqueConstraint(_table.Columns[0],false);
64 _constraint2 = new UniqueConstraint(_table.Columns[1],false);
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();
75 ConstraintCollection col = _table.Constraints;
76 col.Add(_constraint1);
77 col.Add(_constraint2);
79 AssertEquals("Count doesn't equal added.",2, col.Count);
83 public void AddExceptions()
85 ConstraintCollection col = _table.Constraints;
91 Fail("B1: Failed to throw ArgumentNullException.");
93 catch (ArgumentNullException) {}
94 catch (AssertionException exc) {throw exc;}
97 Fail("A1: Wrong exception type");
103 _constraint1.ConstraintName = "Dog";
104 _constraint2.ConstraintName = "dog"; //case insensitive
105 col.Add(_constraint1);
106 col.Add(_constraint2);
109 Fail("Failed to throw Duplicate name exception.");
111 col.Remove (_constraint2); // only for !1.0
112 col.Remove (_constraint1);
115 catch (DuplicateNameException) {
118 catch (AssertionException exc) {throw exc;}
119 /* Don't use such catch. They cover our eyes from the exact exception location.
120 catch (Exception exc)
122 Fail("A2: Wrong exception type. " + exc.ToString());
125 //Constraint Already exists
128 col.Add(_constraint1);
131 Fail("B2: Failed to throw ArgumentException.");
133 col.Remove (_constraint1);
135 catch (ArgumentException) {
141 catch (AssertionException exc) {throw exc;}
144 Fail("A3: Wrong exception type");
149 public void Indexer()
151 Constraint c1 = new UniqueConstraint(_table.Columns[0]);
152 Constraint c2 = new UniqueConstraint(_table.Columns[1]);
154 c1.ConstraintName = "first";
155 c2.ConstraintName = "second";
158 _table.Constraints.Add(c1);
159 _table.Constraints.Add(c2);
161 AssertSame("A1", c1, _table.Constraints[0]);
162 AssertSame("A2", c2, _table.Constraints[1]);
164 AssertSame("A3", c1, _table.Constraints["first"]);
165 AssertSame("A4", c2, _table.Constraints["sEcond"]); //case insensitive
170 public void IndexOf()
172 Constraint c1 = new UniqueConstraint(_table.Columns[0]);
173 Constraint c2 = new UniqueConstraint(_table.Columns[1]);
175 c1.ConstraintName = "first";
176 c2.ConstraintName = "second";
178 _table.Constraints.Add(c1);
179 _table.Constraints.Add(c2);
181 AssertEquals("A1", 0, _table.Constraints.IndexOf(c1));
182 AssertEquals("A2", 1, _table.Constraints.IndexOf(c2));
183 AssertEquals("A3", 0, _table.Constraints.IndexOf("first"));
184 AssertEquals("A4", 1, _table.Constraints.IndexOf("second"));
188 public void Contains()
190 Constraint c1 = new UniqueConstraint(_table.Columns[0]);
191 Constraint c2 = new UniqueConstraint(_table.Columns[1]);
193 c1.ConstraintName = "first";
194 c2.ConstraintName = "second";
196 _table.Constraints.Add(c1);
198 Assert("A1", _table.Constraints.Contains(c1.ConstraintName)); //true
199 Assert("A2", _table.Constraints.Contains(c2.ConstraintName) == false); //doesn't contain
203 public void IndexerFailures()
205 _table.Constraints.Add(new UniqueConstraint(_table.Columns[0]));
208 AssertNull(_table.Constraints["notInCollection"]);
213 Constraint c = _table.Constraints[_table.Constraints.Count];
214 Fail("B1: Failed to throw IndexOutOfRangeException.");
216 catch (IndexOutOfRangeException) {}
217 catch (AssertionException exc) {throw exc;}
220 Fail("A1: Wrong exception type");
226 Constraint c = _table.Constraints[-1];
227 Fail("B2: Failed to throw IndexOutOfRangeException.");
229 catch (IndexOutOfRangeException) {}
230 catch (AssertionException exc) {throw exc;}
233 Fail("A2: Wrong exception type");
239 public void AddFkException1()
241 DataSet ds = new DataSet();
242 ds.Tables.Add(_table);
243 _table2.TableName = "TestTable2";
244 ds.Tables.Add(_table2);
246 _table.Rows.Add(new object [] {1});
247 _table.Rows.Add(new object [] {1});
249 //FKC: can't create unique constraint because duplicate values already exist
252 ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
255 _table2.Constraints.Add(fkc); //should throw
256 Fail("B1: Failed to throw ArgumentException.");
258 catch (ArgumentException) {}
259 catch (AssertionException exc) {throw exc;}
260 catch (Exception exc)
262 Fail("A1: Wrong Exception type. " + exc.ToString());
270 public void AddFkException2()
272 //Foreign key rules only work when the tables
273 //are apart of the dataset
274 DataSet ds = new DataSet();
275 ds.Tables.Add(_table);
276 _table2.TableName = "TestTable2";
277 ds.Tables.Add(_table2);
279 _table.Rows.Add(new object [] {1});
281 // will need a matching parent value in
283 _table2.Rows.Add(new object [] {3});
286 //FKC: no matching parent value
289 ForeignKeyConstraint fkc = new ForeignKeyConstraint( _table.Columns[0],
292 _table2.Constraints.Add(fkc); //should throw
293 Fail("B1: Failed to throw ArgumentException.");
295 catch (ArgumentException) {}
296 catch (AssertionException exc) {throw exc;}
297 catch (Exception exc)
299 Fail("A1: Wrong Exception type. " + exc.ToString());
307 public void AddUniqueExceptions()
311 //UC: can't create unique constraint because duplicate values already exist
314 _table.Rows.Add(new object [] {1});
315 _table.Rows.Add(new object [] {1});
316 UniqueConstraint uc = new UniqueConstraint( _table.Columns[0]);
318 _table.Constraints.Add(uc); //should throw
319 Fail("B1: Failed to throw ArgumentException.");
321 catch (ArgumentException) {}
322 catch (AssertionException exc) {throw exc;}
323 catch (Exception exc)
325 Fail("A1: Wrong Exception type. " + exc.ToString());
330 //Tests AddRange (), CanRemove (), RemoveAt (), Remove (), Exceptions of Remove(), and Clear ()
331 public void AddRemoveTest ()
334 // CanRemove (); This test is ignored
336 // RemoveAt (); This test is ignored
338 // This test is expected to be failed, so don't reuse it.
339 // RemoveExceptions ();
340 _table.Constraints.Remove (_table.Constraints [0]);
346 public void AddRange()
348 _constraint1.ConstraintName = "UK1";
349 _constraint2.ConstraintName = "UK12";
351 ForeignKeyConstraint _constraint3 = new ForeignKeyConstraint ("FK2", _table.Columns [0],
352 _table2.Columns [0]);
353 UniqueConstraint _constraint4=new UniqueConstraint("UK2", _table2.Columns [1]);
355 // Add the constraints.
356 Constraint [] constraints = {_constraint1, _constraint2};
357 _table.Constraints.AddRange (constraints);
359 Constraint [] constraints1 = {_constraint3, _constraint4};
360 _table2.Constraints.AddRange (constraints1);
362 AssertEquals ("A1", "UK1", _table.Constraints [0].ConstraintName);
363 AssertEquals ("A2", "UK12", _table.Constraints [1].ConstraintName);
365 AssertEquals ("A3", "FK2", _table2.Constraints [0].ConstraintName);
366 AssertEquals ("A4", "UK2", _table2.Constraints [1].ConstraintName);
371 [Category ("NotDotNet")]
372 // Even after EndInit(), MS.NET does not fill Table property
373 // on UniqueConstraint.
374 public void TestAddRange2()
376 DataTable table = new DataTable ("Table");
377 DataColumn column1 = new DataColumn ("col1");
378 DataColumn column2 = new DataColumn ("col2");
379 DataColumn column3 = new DataColumn ("col3");
380 table.Columns.Add (column1);
381 table.Columns.Add (column2);
382 table.Columns.Add (column3);
383 string []columnNames = {"col1", "col2", "col3"};
385 Constraint []constraints = new Constraint[3];
386 constraints [0] = new UniqueConstraint ("Unique1",column1);
387 constraints [1] = new UniqueConstraint ("Unique2",column2);
388 constraints [2] = new UniqueConstraint ("Unique3", columnNames, true);
391 //Console.WriteLine(table.InitStatus == DataTable.initStatus.BeginInit);
392 table.Constraints.AddRange (constraints);
394 //Check the table property of UniqueConstraint Object
396 Assertion.AssertNull ("#01", constraints [2].Table);
398 catch (Exception e) {
399 Assertion.Assert ("#A02", "System.NullReferenceException".Equals (e.GetType().ToString()));
404 // After EndInit is called the constraints associated with most recent call to AddRange() must be
405 // added to the ConstraintCollection
406 Assertion.Assert ("#A03", constraints [2].Table.ToString().Equals ("Table"));
407 Assertion.Assert ("#A04", table.Constraints.Contains ("Unique1"));
408 Assertion.Assert ("#A05", table.Constraints.Contains ("Unique2"));
409 Assertion.Assert ("#A06", table.Constraints.Contains ("Unique3"));
419 _table.Constraints.Clear (); //Clear all constraints
420 AssertEquals ("A1", 0, _table.Constraints.Count); //No constraints should remain
421 _table2.Constraints.Clear ();
422 AssertEquals ("A2", 0, _table2.Constraints.Count);
424 catch (Exception e) {
425 Console.WriteLine (e);
431 [Ignore ("This never works on MS.NET (and it should not)")]
432 public void CanRemove()
434 AssertEquals ("A1", false, _table.Constraints.CanRemove (_table.Constraints [0]));
439 public void CollectionChanged()
444 // If this fails on MS.NET and its supposed to fail, why do we have this as Ignore?
447 [Ignore ("MS.NET fails this test (and it should fail)")]
448 public void RemoveAt()
450 _table2.Constraints.RemoveAt (1); //Remove constraint and again add it
451 AssertEquals ("A1", 1, _table2.Constraints.Count); UniqueConstraint _constraint4 = new UniqueConstraint ("UK2", _table2.Columns [1]);
452 // Add the constraints.
453 Constraint [] constraints = {_constraint4};
454 _table.Constraints.AddRange (constraints);
460 [Ignore ("MS.NET fails this test (and it should fail)")]
463 _table2.Constraints.Remove (_table2.Constraints [1]); //Remove constraint and again add it
464 AssertEquals ("A1", 1, _table2.Constraints.Count);
465 UniqueConstraint _constraint4 = new UniqueConstraint ("UK2", _table2.Columns [1]);
466 // Add the constraints.
467 Constraint [] constraints = {_constraint4};
468 _table2.Constraints.AddRange (constraints);
472 public void RemoveExceptions()
475 //Remove constraint that cannot be removed
476 _table.Constraints.Remove (_table.Constraints [0]);
478 } catch (Exception e) {
479 AssertEquals ("A2", typeof (IndexOutOfRangeException), e.GetType ());