ConstraintTest.cs 3.6 KB

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