1 // ConstraintTest.cs - NUnit Test Cases for testing the abstract class System.Data.Constraint
2 // The tests use an inherited class (UniqueConstraint) to test the Constraint class.
5 // Franklin Wise <gracenote@earthlink.net>
6 // Martin Willemoes Hansen <mwh@sysrq.dk>
8 // (C) 2002 Franklin Wise
9 // (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.
34 using NUnit.Framework;
38 using NUnit.Framework.SyntaxHelpers;
41 namespace MonoTests.System.Data
43 // public class MyUniqueConstraint: UniqueConstraint {
44 // public MyUniqueConstraint(DataColumn col, bool pk): base(col,pk){}
45 // string _myval = "";
46 // public override string ConstraintName {
49 // return base.ConstraintName;
52 // Console.WriteLine("NameSet = " + value);
53 // base.ConstraintName = value;
60 public class ConstraintTest
62 private DataTable _table;
63 private Constraint _constraint1;
64 private Constraint _constraint2;
67 public void GetReady ()
71 _table = new DataTable ("TestTable");
73 _table.Columns.Add ("Col1", typeof(int));
74 _table.Columns.Add ("Col2", typeof(int));
76 //Use UniqueConstraint to test Constraint Base Class
77 _constraint1 = new UniqueConstraint (_table.Columns [0], false);
78 _constraint2 = new UniqueConstraint (_table.Columns [1], false);
80 // not sure why this is needed since a new _table was just created
81 // for us, but this Clear() keeps the tests from throwing
82 // an exception when the Add() is called.
83 _table.Constraints.Clear ();
87 public void SetConstraintNameNullOrEmptyExceptions ()
89 bool exceptionCaught = false;
92 _table.Constraints.Add (_constraint1);
94 for (int i = 0; i <= 1; i++) {
95 exceptionCaught = false;
103 //Next line should throw ArgumentException
104 //Because ConstraintName can't be set to null
105 //or empty while the constraint is part of the
107 _constraint1.ConstraintName = name;
108 } catch (ArgumentException) {
109 exceptionCaught = true;
111 Assert.Fail ("Wrong exception type thrown.");
114 Assert.That (exceptionCaught, Is.True, "Failed to throw exception.");
119 [ExpectedException (typeof (DuplicateNameException))]
120 public void SetConstraintNameDuplicateException ()
122 _constraint1.ConstraintName = "Dog";
123 _constraint2.ConstraintName = "Cat";
125 _table.Constraints.Add (_constraint1);
126 _table.Constraints.Add (_constraint2);
128 //Should throw DuplicateNameException
129 _constraint2.ConstraintName = "Dog";
133 public void ToStringTest ()
135 _constraint1.ConstraintName = "Test";
136 Assert.That (_constraint1.ConstraintName, Is.EqualTo (_constraint1.ToString ()),
137 "ToString is the same as constraint name.");
139 _constraint1.ConstraintName = null;
140 Assert.That (_constraint1.ToString (), Is.Not.Null, "ToString should return empty.");
144 public void GetExtendedProperties ()
146 PropertyCollection col = _constraint1.ExtendedProperties as
149 Assert.That (col, Is.Not.Null, "ExtendedProperties returned null or didn't " +
150 "return the correct type");