OleDbDataReader_GetBytes_IIBII.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_GetBytes_IIBII : GHTBase
  33. {
  34. int testTypesInvocations;
  35. Exception exp = null;
  36. public static void Main()
  37. {
  38. OleDbDataReader_GetBytes_IIBII tc = new OleDbDataReader_GetBytes_IIBII();
  39. Exception exp = null;
  40. try
  41. {
  42. tc.BeginTest("OleDbDataReader_GetBytes_IIBII");
  43. tc.run();
  44. }
  45. catch(Exception ex){exp = ex;}
  46. finally {tc.EndTest(exp);}
  47. }
  48. [Test]
  49. public void run()
  50. {
  51. DoTestTypes(ConnectedDataProvider.GetSimpleDbTypesParameters());
  52. DoTestTypes(ConnectedDataProvider.GetExtendedDbTypesParameters());
  53. }
  54. public void DoTestTypes(DbTypeParametersCollection row)
  55. {
  56. testTypesInvocations++;
  57. exp = null;
  58. string rowId = "43966_" + this.testTypesInvocations.ToString();
  59. OleDbConnection con =null;
  60. OleDbDataReader rdr = null;
  61. try
  62. {
  63. row.ExecuteInsert(rowId);
  64. row.ExecuteSelectReader(rowId, out rdr, out con);
  65. while (rdr.Read())
  66. {
  67. //Run over all the columns in the result set row.
  68. //For each column, try to read it as a byte array.
  69. for (int i=0; i<row.Count; i++)
  70. {
  71. if (row[i].Value.GetType() == typeof(byte[])) //The value in the result set should be a byte array.
  72. {
  73. try
  74. {
  75. BeginCase(string.Format("Calling GetBytes() on a field of dbtype {0}", row[i].DbTypeName));
  76. byte[] origBytes = (byte[])row[i].Value;
  77. byte[] retBytes = new byte[origBytes.Length];
  78. rdr.GetBytes(i, 0, retBytes, 0, origBytes.Length);
  79. Compare(origBytes, retBytes);
  80. }
  81. catch (Exception ex)
  82. {
  83. exp = ex;
  84. }
  85. finally
  86. {
  87. EndCase(exp);
  88. exp = null;
  89. }
  90. }
  91. else //The value in the result set should NOT be byte array. In this case an Invalid case exception should be thrown.
  92. {
  93. try
  94. {
  95. BeginCase(string.Format("Calling GetBytes() on a field of dbtype {0}", row[i].DbTypeName));
  96. byte[] retBytes = new byte[1];
  97. rdr.GetBytes(i, 0, retBytes, 0, 1);
  98. ExpectedExceptionNotCaught("InvalidCastException");
  99. }
  100. catch (InvalidCastException ex)
  101. {
  102. ExpectedExceptionCaught(ex);
  103. }
  104. catch (Exception ex)
  105. {
  106. exp = ex;
  107. }
  108. finally
  109. {
  110. EndCase(exp);
  111. exp = null;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. finally
  118. {
  119. row.ExecuteDelete(rowId);
  120. if (rdr != null && !rdr.IsClosed)
  121. {
  122. rdr.Close();
  123. }
  124. if (con != null && con.State != ConnectionState.Closed)
  125. {
  126. con.Close();
  127. }
  128. }
  129. }
  130. }
  131. }