OleDbDataReader_GetTimeSpan_I.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // Copyright (c) 2006 Mainsoft Co.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Data;
  25. using System.Data.OleDb;
  26. using MonoTests.System.Data.Utils;
  27. using MonoTests.System.Data.Utils.Data;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Data.OleDb
  30. {
  31. [TestFixture]
  32. public class OleDbDataReader_GetTimeSpan_I : ADONetTesterClass
  33. {
  34. private Exception exp = null;
  35. private int testTypesInvocations;
  36. // public OleDbDataReader_GetTimeSpan_I() : base(true)
  37. // {
  38. // }
  39. public static void Main()
  40. {
  41. OleDbDataReader_GetTimeSpan_I tc = new OleDbDataReader_GetTimeSpan_I();
  42. tc.exp = null;
  43. try
  44. {
  45. tc.BeginTest("OleDbDataReader_GetTimeSpan_I");
  46. tc.run();
  47. }
  48. catch(Exception ex)
  49. {
  50. tc.exp = ex;
  51. }
  52. finally
  53. {
  54. tc.EndTest(tc.exp);
  55. }
  56. }
  57. [Test]
  58. public void run()
  59. {
  60. DoTestTypes(ConnectedDataProvider.GetSimpleDbTypesParameters());
  61. DoTestTypes(ConnectedDataProvider.GetExtendedDbTypesParameters());
  62. }
  63. public void DoTestTypes(DbTypeParametersCollection row)
  64. {
  65. testTypesInvocations++;
  66. exp = null;
  67. string rowId = "43967_" + this.testTypesInvocations.ToString();
  68. OleDbDataReader rdr = null;
  69. OleDbConnection con = null;
  70. try
  71. {
  72. row.ExecuteInsert(rowId);
  73. row.ExecuteSelectReader(rowId, out rdr, out con);
  74. while (rdr.Read())
  75. {
  76. //Run over all the columns in the result set row.
  77. //For each column, try to read it as a TimeSpan.
  78. for (int i=0; i<row.Count; i++)
  79. {
  80. if (row[i].Value.GetType() == typeof(TimeSpan)) //The value in the result set should be a TimeSpan.
  81. {
  82. try
  83. {
  84. BeginCase(string.Format("Calling GetTimeSpan() on a field of dbtype {0}", row[i].DbTypeName));
  85. TimeSpan retTimeSpan = rdr.GetTimeSpan(i);
  86. Compare(row[i].Value, retTimeSpan);
  87. }
  88. catch (Exception ex)
  89. {
  90. exp = ex;
  91. }
  92. finally
  93. {
  94. EndCase(exp);
  95. exp = null;
  96. }
  97. }
  98. else //The value in the result set should NOT be TimeSpan. In this case an Invalid case exception should be thrown.
  99. {
  100. try
  101. {
  102. BeginCase(string.Format("Calling GetTimeSpan() on a field of dbtype {0}", row[i].DbTypeName));
  103. TimeSpan retTimeSpan = rdr.GetTimeSpan(i);
  104. ExpectedExceptionNotCaught("InvalidCastException");
  105. }
  106. catch (InvalidCastException ex)
  107. {
  108. ExpectedExceptionCaught(ex);
  109. }
  110. catch (Exception ex)
  111. {
  112. exp = ex;
  113. }
  114. finally
  115. {
  116. EndCase(exp);
  117. exp = null;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. finally
  124. {
  125. row.ExecuteDelete(rowId);
  126. if ( (rdr != null) && (!rdr.IsClosed) )
  127. {
  128. rdr.Close();
  129. }
  130. if ( (con != null) && (con.State != ConnectionState.Closed) )
  131. {
  132. con.Close();
  133. }
  134. }
  135. }
  136. }
  137. }