column name and ordinal fix...tested on 10.1
[mono.git] / mcs / class / System.Data / Test / System.Data / ConstraintTest.cs
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.
3 //
4 // Authors:
5 //   Franklin Wise <gracenote@earthlink.net>
6 //   Martin Willemoes Hansen <mwh@sysrq.dk>
7 //
8 // (C) 2002 Franklin Wise
9 // (C) 2003 Martin Willemoes Hansen
10 // 
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 System;
36 using System.Data;
37 #if !MOBILE
38 using NUnit.Framework.SyntaxHelpers;
39 #endif
40
41 namespace MonoTests.System.Data
42 {
43 //      public class MyUniqueConstraint: UniqueConstraint {
44 //              public MyUniqueConstraint(DataColumn col, bool pk): base(col,pk){}
45 //              string _myval = "";
46 //              public override string ConstraintName {
47 //                      get{
48 //                              return _myval;
49 //                              return base.ConstraintName;
50 //                      }
51 //                      set{
52 //                              Console.WriteLine("NameSet = " + value);
53 //                              base.ConstraintName = value;
54 //                              _myval = value;
55 //                      }
56 //              }
57 //      }
58
59         [TestFixture]
60         public class ConstraintTest
61         {
62                 private DataTable _table;
63                 private Constraint _constraint1;
64                 private Constraint _constraint2;
65
66                 [SetUp]
67                 public void GetReady ()
68                 {
69
70                         //Setup DataTable
71                         _table = new DataTable ("TestTable");
72
73                         _table.Columns.Add ("Col1", typeof(int));
74                         _table.Columns.Add ("Col2", typeof(int));
75
76                         //Use UniqueConstraint to test Constraint Base Class
77                         _constraint1 = new UniqueConstraint (_table.Columns [0], false); 
78                         _constraint2 = new UniqueConstraint (_table.Columns [1], false); 
79
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 ();
84                 }  
85
86                 [Test]
87                 public void SetConstraintNameNullOrEmptyExceptions ()
88                 {
89                         bool exceptionCaught = false;
90                         string name = null;
91
92                         _table.Constraints.Add (_constraint1);  
93
94                         for (int i = 0; i <= 1; i++) {
95                                 exceptionCaught = false;
96                                 if (0 == i)
97                                         name = null;
98                                 if (1 == i)
99                                         name = String.Empty;
100         
101                                 try {
102                                 
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
106                                         //collection
107                                         _constraint1.ConstraintName = name; 
108                                 } catch (ArgumentException) { 
109                                         exceptionCaught = true;
110                                 } catch {
111                                         Assert.Fail ("Wrong exception type thrown.");
112                                 }
113                                 
114                                 Assert.That (exceptionCaught, Is.True, "Failed to throw exception.");
115                         }       
116                 }
117
118                 [Test]
119                 [ExpectedException (typeof (DuplicateNameException))]
120                 public void SetConstraintNameDuplicateException ()
121                 {
122                         _constraint1.ConstraintName = "Dog";
123                         _constraint2.ConstraintName = "Cat";
124
125                         _table.Constraints.Add (_constraint1);
126                         _table.Constraints.Add (_constraint2);
127
128                         //Should throw DuplicateNameException
129                         _constraint2.ConstraintName = "Dog";
130                 }
131
132                 [Test]
133                 public void ToStringTest ()
134                 {
135                         _constraint1.ConstraintName = "Test";
136                         Assert.That (_constraint1.ConstraintName, Is.EqualTo (_constraint1.ToString ()),
137                                 "ToString is the same as constraint name.");
138                         
139                         _constraint1.ConstraintName = null;
140                         Assert.That (_constraint1.ToString (), Is.Not.Null, "ToString should return empty.");
141                 }
142
143                 [Test]
144                 public void GetExtendedProperties ()
145                 {
146                         PropertyCollection col = _constraint1.ExtendedProperties as
147                                 PropertyCollection;
148
149                         Assert.That (col, Is.Not.Null, "ExtendedProperties returned null or didn't " +
150                                 "return the correct type");
151                 }
152         }
153 }