SqlConnection_StateChange.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer) {
  29. //All tests in this class are only for MSSQLServer.
  30. Log(string.Format("All tests in this class are only for MSSQLServer and cannot be tested on {0}", ConnectedDataProvider.GetDbType()));
  31. return;
  32. }
  33. Exception exp = null;
  34. SqlConnection con = new SqlConnection(ConnectedDataProvider.ConnectionStringSQLClient);
  35. // ----------- reserved for future versions of the product ---------------
  36. //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).
  37. //Connecting The connection object is connecting to the data source. (This value is reserved for future versions of the product.) 2
  38. //Executing The connection object is executing a command. (This value is reserved for future versions of the product.) 4
  39. //Fetching The connection object is retrieving data. (This value is reserved for future versions of the product.) 8
  40. //-------------- checking only the following: ----------------
  41. //Closed The connection is closed.
  42. //Open The connection is open.
  43. //add event handler
  44. con.StateChange +=new StateChangeEventHandler(con_StateChange);
  45. con.Open();
  46. try
  47. {
  48. BeginCase("ConnectionState Closed");
  49. Compare(blnEventRaised,true);
  50. }
  51. catch(Exception ex){exp = ex;}
  52. finally{EndCase(exp); exp = null;}
  53. try
  54. {
  55. BeginCase("OriginalState Closed");
  56. Compare(OriginalState,ConnectionState.Closed );
  57. }
  58. catch(Exception ex){exp = ex;}
  59. finally{EndCase(exp); exp = null;}
  60. try
  61. {
  62. BeginCase("CurrentState Open");
  63. Compare(CurrentState,ConnectionState.Open );
  64. }
  65. catch(Exception ex){exp = ex;}
  66. finally{EndCase(exp); exp = null;}
  67. blnEventRaised = false;
  68. con.Close();
  69. try
  70. {
  71. BeginCase("ConnectionState Open");
  72. Compare(blnEventRaised,true);
  73. }
  74. catch(Exception ex){exp = ex;}
  75. finally{EndCase(exp); exp = null;}
  76. try
  77. {
  78. BeginCase("OriginalState Open");
  79. Compare(OriginalState,ConnectionState.Open );
  80. }
  81. catch(Exception ex){exp = ex;}
  82. finally{EndCase(exp); exp = null;}
  83. try
  84. {
  85. BeginCase("CurrentState Close");
  86. Compare(CurrentState,ConnectionState.Closed );
  87. }
  88. catch(Exception ex){exp = ex;}
  89. finally{EndCase(exp); exp = null;}
  90. if (con.State == ConnectionState.Open) con.Close();
  91. }
  92. void con_StateChange(Object sender, StateChangeEventArgs e)
  93. {
  94. CurrentState = e.CurrentState ;
  95. OriginalState = e.OriginalState ;
  96. blnEventRaised = true;
  97. }
  98. //public TestClass():base(true){}
  99. //Activate this constructor to log Failures to a log file
  100. //public TestClass(System.IO.TextWriter tw):base(tw, false){}
  101. //Activate this constructor to log All to a log file
  102. //public TestClass(System.IO.TextWriter tw):base(tw, true){}
  103. //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
  104. }
  105. }