Merge pull request #3913 from omwok/master
[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
38 namespace MonoTests.System.Data
39 {
40 //      public class MyUniqueConstraint: UniqueConstraint {
41 //              public MyUniqueConstraint(DataColumn col, bool pk): base(col,pk){}
42 //              string _myval = "";
43 //              public override string ConstraintName {
44 //                      get{
45 //                              return _myval;
46 //                              return base.ConstraintName;
47 //                      }
48 //                      set{
49 //                              Console.WriteLine("NameSet = " + value);
50 //                              base.ConstraintName = value;
51 //                              _myval = value;
52 //                      }
53 //              }
54 //      }
55
56         [TestFixture]
57         public class ConstraintTest
58         {
59                 private DataTable _table;
60                 private Constraint _constraint1;
61                 private Constraint _constraint2;
62
63                 [SetUp]
64                 public void GetReady ()
65                 {
66
67                         //Setup DataTable
68                         _table = new DataTable ("TestTable");
69
70                         _table.Columns.Add ("Col1", typeof(int));
71                         _table.Columns.Add ("Col2", typeof(int));
72
73                         //Use UniqueConstraint to test Constraint Base Class
74                         _constraint1 = new UniqueConstraint (_table.Columns [0], false); 
75                         _constraint2 = new UniqueConstraint (_table.Columns [1], false); 
76
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 ();
81                 }  
82
83                 [Test]
84                 public void SetConstraintNameNullOrEmptyExceptions ()
85                 {
86                         bool exceptionCaught = false;
87                         string name = null;
88
89                         _table.Constraints.Add (_constraint1);  
90
91                         for (int i = 0; i <= 1; i++) {
92                                 exceptionCaught = false;
93                                 if (0 == i)
94                                         name = null;
95                                 if (1 == i)
96                                         name = String.Empty;
97         
98                                 try {
99                                 
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
103                                         //collection
104                                         _constraint1.ConstraintName = name; 
105                                 } catch (ArgumentException) { 
106                                         exceptionCaught = true;
107                                 } catch {
108                                         Assert.Fail ("Wrong exception type thrown.");
109                                 }
110                                 
111                                 Assert.That (exceptionCaught, Is.True, "Failed to throw exception.");
112                         }       
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (DuplicateNameException))]
117                 public void SetConstraintNameDuplicateException ()
118                 {
119                         _constraint1.ConstraintName = "Dog";
120                         _constraint2.ConstraintName = "Cat";
121
122                         _table.Constraints.Add (_constraint1);
123                         _table.Constraints.Add (_constraint2);
124
125                         //Should throw DuplicateNameException
126                         _constraint2.ConstraintName = "Dog";
127                 }
128
129                 [Test]
130                 public void ToStringTest ()
131                 {
132                         _constraint1.ConstraintName = "Test";
133                         Assert.That (_constraint1.ConstraintName, Is.EqualTo (_constraint1.ToString ()),
134                                 "ToString is the same as constraint name.");
135                         
136                         _constraint1.ConstraintName = null;
137                         Assert.That (_constraint1.ToString (), Is.Not.Null, "ToString should return empty.");
138                 }
139
140                 [Test]
141                 public void GetExtendedProperties ()
142                 {
143                         PropertyCollection col = _constraint1.ExtendedProperties as
144                                 PropertyCollection;
145
146                         Assert.That (col, Is.Not.Null, "ExtendedProperties returned null or didn't " +
147                                 "return the correct type");
148                 }
149         }
150 }