ConstraintTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <[email protected]>
  6. // Martin Willemoes Hansen <[email protected]>
  7. //
  8. // (C) 2002 Franklin Wise
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using NUnit.Framework;
  34. using System;
  35. using System.Data;
  36. namespace MonoTests.System.Data
  37. {
  38. // public class MyUniqueConstraint: UniqueConstraint {
  39. // public MyUniqueConstraint(DataColumn col, bool pk): base(col,pk){}
  40. // string _myval = "";
  41. // public override string ConstraintName {
  42. // get{
  43. // return _myval;
  44. // return base.ConstraintName;
  45. // }
  46. // set{
  47. // Console.WriteLine("NameSet = " + value);
  48. // base.ConstraintName = value;
  49. // _myval = value;
  50. // }
  51. // }
  52. // }
  53. [TestFixture]
  54. public class ConstraintTest : Assertion
  55. {
  56. private DataTable _table;
  57. private Constraint _constraint1;
  58. private Constraint _constraint2;
  59. [SetUp]
  60. public void GetReady() {
  61. //Setup DataTable
  62. _table = new DataTable("TestTable");
  63. _table.Columns.Add("Col1",typeof(int));
  64. _table.Columns.Add("Col2",typeof(int));
  65. //Use UniqueConstraint to test Constraint Base Class
  66. _constraint1 = new UniqueConstraint(_table.Columns[0],false);
  67. _constraint2 = new UniqueConstraint(_table.Columns[1],false);
  68. // not sure why this is needed since a new _table was just created
  69. // for us, but this Clear() keeps the tests from throwing
  70. // an exception when the Add() is called.
  71. _table.Constraints.Clear();
  72. }
  73. [Test]
  74. public void SetConstraintNameNullOrEmptyExceptions() {
  75. bool exceptionCaught = false;
  76. string name = null;
  77. _table.Constraints.Add (_constraint1);
  78. for (int i = 0; i <= 1; i++) {
  79. exceptionCaught = false;
  80. if (0 == i) name = null;
  81. if (1 == i) name = String.Empty;
  82. try {
  83. //Next line should throw ArgumentException
  84. //Because ConstraintName can't be set to null
  85. //or empty while the constraint is part of the
  86. //collection
  87. _constraint1.ConstraintName = name;
  88. }
  89. catch (ArgumentException){
  90. exceptionCaught = true;
  91. }
  92. catch {
  93. Fail("Wrong exception type thrown.");
  94. }
  95. Assert("Failed to throw exception.",
  96. true == exceptionCaught);
  97. }
  98. }
  99. [Test]
  100. [ExpectedException (typeof (DuplicateNameException))]
  101. public void SetConstraintNameDuplicateException ()
  102. {
  103. _constraint1.ConstraintName = "Dog";
  104. _constraint2.ConstraintName = "Cat";
  105. _table.Constraints.Add(_constraint1);
  106. _table.Constraints.Add(_constraint2);
  107. //Should throw DuplicateNameException
  108. _constraint2.ConstraintName = "Dog";
  109. }
  110. [Test]
  111. public void ToStringTest() {
  112. _constraint1.ConstraintName = "Test";
  113. Assert("ToString is the same as constraint name.", _constraint1.ConstraintName.CompareTo( _constraint1.ToString()) == 0);
  114. _constraint1.ConstraintName = null;
  115. AssertNotNull("ToString should return empty.",_constraint1.ToString());
  116. }
  117. [Test]
  118. public void GetExtendedProperties() {
  119. PropertyCollection col = _constraint1.ExtendedProperties as
  120. PropertyCollection;
  121. AssertNotNull("ExtendedProperties returned null or didn't " +
  122. "return the correct type", col);
  123. }
  124. }
  125. }