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 namespace MonoTests.System.Data
40 // public class MyUniqueConstraint: UniqueConstraint {
41 // public MyUniqueConstraint(DataColumn col, bool pk): base(col,pk){}
42 // string _myval = "";
43 // public override string ConstraintName {
46 // return base.ConstraintName;
49 // Console.WriteLine("NameSet = " + value);
50 // base.ConstraintName = value;
57 public class ConstraintTest
59 private DataTable _table;
60 private Constraint _constraint1;
61 private Constraint _constraint2;
64 public void GetReady ()
68 _table = new DataTable ("TestTable");
70 _table.Columns.Add ("Col1", typeof(int));
71 _table.Columns.Add ("Col2", typeof(int));
73 //Use UniqueConstraint to test Constraint Base Class
74 _constraint1 = new UniqueConstraint (_table.Columns [0], false);
75 _constraint2 = new UniqueConstraint (_table.Columns [1], false);
77 // not sure why this is needed since a new _table was just created
78 // for us, but this Clear() keeps the tests from throwing
79 // an exception when the Add() is called.
80 _table.Constraints.Clear ();
84 public void SetConstraintNameNullOrEmptyExceptions ()
86 bool exceptionCaught = false;
89 _table.Constraints.Add (_constraint1);
91 for (int i = 0; i <= 1; i++) {
92 exceptionCaught = false;
100 //Next line should throw ArgumentException
101 //Because ConstraintName can't be set to null
102 //or empty while the constraint is part of the
104 _constraint1.ConstraintName = name;
105 } catch (ArgumentException) {
106 exceptionCaught = true;
108 Assert.Fail ("Wrong exception type thrown.");
111 Assert.That (exceptionCaught, Is.True, "Failed to throw exception.");
116 [ExpectedException (typeof (DuplicateNameException))]
117 public void SetConstraintNameDuplicateException ()
119 _constraint1.ConstraintName = "Dog";
120 _constraint2.ConstraintName = "Cat";
122 _table.Constraints.Add (_constraint1);
123 _table.Constraints.Add (_constraint2);
125 //Should throw DuplicateNameException
126 _constraint2.ConstraintName = "Dog";
130 public void ToStringTest ()
132 _constraint1.ConstraintName = "Test";
133 Assert.That (_constraint1.ConstraintName, Is.EqualTo (_constraint1.ToString ()),
134 "ToString is the same as constraint name.");
136 _constraint1.ConstraintName = null;
137 Assert.That (_constraint1.ToString (), Is.Not.Null, "ToString should return empty.");
141 public void GetExtendedProperties ()
143 PropertyCollection col = _constraint1.ExtendedProperties as
146 Assert.That (col, Is.Not.Null, "ExtendedProperties returned null or didn't " +
147 "return the correct type");