SqlConnection_StateChange.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using MonoTests.System.Data.Utils;
  5. using NUnit.Framework;
  6. namespace MonoTests.System.Data.SqlClient
  7. {
  8. [TestFixture]
  9. public class SqlConnection_StateChange : GHTBase
  10. {
  11. public static void Main()
  12. {
  13. SqlConnection_StateChange tc = new SqlConnection_StateChange();
  14. Exception exp = null;
  15. try
  16. {
  17. tc.BeginTest("SqlConnection_StateChange");
  18. tc.run();
  19. }
  20. catch(Exception ex){exp = ex;}
  21. finally {tc.EndTest(exp);}
  22. }
  23. bool blnEventRaised = false;
  24. ConnectionState OriginalState,CurrentState;
  25. [Test]
  26. public void run()
  27. {
  28. Exception exp = null;
  29. SqlConnection con = new SqlConnection(ConnectedDataProvider.ConnectionStringSQLClient);
  30. // ----------- reserved for future versions of the product ---------------
  31. //Broken The connection to the data source is broken. This can occur only after the connection has been opened. A connection in this state may be closed and then re-opened. (This value is reserved for future versions of the product).
  32. //Connecting The connection object is connecting to the data source. (This value is reserved for future versions of the product.) 2
  33. //Executing The connection object is executing a command. (This value is reserved for future versions of the product.) 4
  34. //Fetching The connection object is retrieving data. (This value is reserved for future versions of the product.) 8
  35. //-------------- checking only the following: ----------------
  36. //Closed The connection is closed.
  37. //Open The connection is open.
  38. //add event handler
  39. con.StateChange +=new StateChangeEventHandler(con_StateChange);
  40. con.Open();
  41. try
  42. {
  43. BeginCase("ConnectionState Closed");
  44. Compare(blnEventRaised,true);
  45. }
  46. catch(Exception ex){exp = ex;}
  47. finally{EndCase(exp); exp = null;}
  48. try
  49. {
  50. BeginCase("OriginalState Closed");
  51. Compare(OriginalState,ConnectionState.Closed );
  52. }
  53. catch(Exception ex){exp = ex;}
  54. finally{EndCase(exp); exp = null;}
  55. try
  56. {
  57. BeginCase("CurrentState Open");
  58. Compare(CurrentState,ConnectionState.Open );
  59. }
  60. catch(Exception ex){exp = ex;}
  61. finally{EndCase(exp); exp = null;}
  62. blnEventRaised = false;
  63. con.Close();
  64. try
  65. {
  66. BeginCase("ConnectionState Open");
  67. Compare(blnEventRaised,true);
  68. }
  69. catch(Exception ex){exp = ex;}
  70. finally{EndCase(exp); exp = null;}
  71. try
  72. {
  73. BeginCase("OriginalState Open");
  74. Compare(OriginalState,ConnectionState.Open );
  75. }
  76. catch(Exception ex){exp = ex;}
  77. finally{EndCase(exp); exp = null;}
  78. try
  79. {
  80. BeginCase("CurrentState Close");
  81. Compare(CurrentState,ConnectionState.Closed );
  82. }
  83. catch(Exception ex){exp = ex;}
  84. finally{EndCase(exp); exp = null;}
  85. if (con.State == ConnectionState.Open) con.Close();
  86. }
  87. void con_StateChange(Object sender, StateChangeEventArgs e)
  88. {
  89. CurrentState = e.CurrentState ;
  90. OriginalState = e.OriginalState ;
  91. blnEventRaised = true;
  92. }
  93. //public TestClass():base(true){}
  94. //Activate this constructor to log Failures to a log file
  95. //public TestClass(System.IO.TextWriter tw):base(tw, false){}
  96. //Activate this constructor to log All to a log file
  97. //public TestClass(System.IO.TextWriter tw):base(tw, true){}
  98. //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
  99. }
  100. }