ConstraintTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using NUnit.Framework;
  12. using System;
  13. using System.Data;
  14. namespace MonoTests.System.Data
  15. {
  16. // public class MyUniqueConstraint: UniqueConstraint {
  17. // public MyUniqueConstraint(DataColumn col, bool pk): base(col,pk){}
  18. // string _myval = "";
  19. // public override string ConstraintName {
  20. // get{
  21. // return _myval;
  22. // return base.ConstraintName;
  23. // }
  24. // set{
  25. // Console.WriteLine("NameSet = " + value);
  26. // base.ConstraintName = value;
  27. // _myval = value;
  28. // }
  29. // }
  30. // }
  31. [TestFixture]
  32. public class ConstraintTest : Assertion
  33. {
  34. private DataTable _table;
  35. private Constraint _constraint1;
  36. private Constraint _constraint2;
  37. [SetUp]
  38. public void GetReady() {
  39. //Setup DataTable
  40. _table = new DataTable("TestTable");
  41. _table.Columns.Add("Col1",typeof(int));
  42. _table.Columns.Add("Col2",typeof(int));
  43. //Use UniqueConstraint to test Constraint Base Class
  44. _constraint1 = new UniqueConstraint(_table.Columns[0],false);
  45. _constraint2 = new UniqueConstraint(_table.Columns[1],false);
  46. // not sure why this is needed since a new _table was just created
  47. // for us, but this Clear() keeps the tests from throwing
  48. // an exception when the Add() is called.
  49. _table.Constraints.Clear();
  50. }
  51. [Test]
  52. public void SetConstraintNameNullOrEmptyExceptions() {
  53. bool exceptionCaught = false;
  54. string name = null;
  55. _table.Constraints.Add (_constraint1);
  56. for (int i = 0; i <= 1; i++) {
  57. exceptionCaught = false;
  58. if (0 == i) name = null;
  59. if (1 == i) name = String.Empty;
  60. try {
  61. //Next line should throw ArgumentException
  62. //Because ConstraintName can't be set to null
  63. //or empty while the constraint is part of the
  64. //collection
  65. _constraint1.ConstraintName = name;
  66. }
  67. catch (ArgumentException){
  68. exceptionCaught = true;
  69. }
  70. catch {
  71. Fail("Wrong exception type thrown.");
  72. }
  73. Assert("Failed to throw exception.",
  74. true == exceptionCaught);
  75. }
  76. }
  77. [Test]
  78. [ExpectedException (typeof (DuplicateNameException))]
  79. public void SetConstraintNameDuplicateException ()
  80. {
  81. _constraint1.ConstraintName = "Dog";
  82. _constraint2.ConstraintName = "Cat";
  83. _table.Constraints.Add(_constraint1);
  84. _table.Constraints.Add(_constraint2);
  85. //Should throw DuplicateNameException
  86. _constraint2.ConstraintName = "Dog";
  87. }
  88. [Test]
  89. public void ToStringTest() {
  90. _constraint1.ConstraintName = "Test";
  91. Assert("ToString is the same as constraint name.", _constraint1.ConstraintName.CompareTo( _constraint1.ToString()) == 0);
  92. _constraint1.ConstraintName = null;
  93. AssertNotNull("ToString should return empty.",_constraint1.ToString());
  94. }
  95. [Test]
  96. public void GetExtendedProperties() {
  97. PropertyCollection col = _constraint1.ExtendedProperties as
  98. PropertyCollection;
  99. AssertNotNull("ExtendedProperties returned null or didn't " +
  100. "return the correct type", col);
  101. }
  102. }
  103. }