OracleDataReader_GetDecimal_I.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.OracleClient;
  26. using MonoTests.System.Data.Utils;
  27. using MonoTests.System.Data.Utils.Data;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Data.OracleClient
  30. {
  31. [TestFixture]
  32. public class OracleDataReader_GetDecimal_I : ADONetTesterClass
  33. {
  34. private int testTypesInvocations;
  35. private Exception exp = null;
  36. public static void Main()
  37. {
  38. OracleDataReader_GetDecimal_I tc = new OracleDataReader_GetDecimal_I();
  39. Exception exp = null;
  40. try
  41. {
  42. tc.BeginTest("OracleDataReader_GetDecimal_I");
  43. tc.run();
  44. }
  45. catch(Exception ex)
  46. {
  47. exp = ex;
  48. }
  49. finally
  50. {
  51. tc.EndTest(exp);
  52. }
  53. }
  54. [Test]
  55. public void run()
  56. {
  57. DoTestTypes(ConnectedDataProvider.GetSimpleDbTypesParameters());
  58. DoTestTypes(ConnectedDataProvider.GetExtendedDbTypesParameters());
  59. DoTestTextualFieldsThatContainNumbers(ConnectedDataProvider.GetSimpleDbTypesParameters());
  60. DoTestTextualFieldsThatContainNumbers(ConnectedDataProvider.GetExtendedDbTypesParameters());
  61. }
  62. public void DoTestTypes(DbTypeParametersCollection row)
  63. {
  64. testTypesInvocations++;
  65. exp = null;
  66. string rowId = "43968_" + this.testTypesInvocations.ToString();
  67. OracleDataReader rdr = null;
  68. OracleConnection con = null;
  69. try
  70. {
  71. row.ExecuteInsert(rowId);
  72. row.ExecuteSelectReader(rowId, out rdr, out con);
  73. while (rdr.Read())
  74. {
  75. //Run over all the columns in the result set row.
  76. //For each column, try to read it as a Decimal.
  77. for (int i=0; i<row.Count; i++)
  78. {
  79. if (row[i].Value.GetType() == typeof(decimal) ||
  80. row[i].Value.GetType() == typeof(double)) //The value in the result set should be a Decimal.
  81. {
  82. try
  83. {
  84. BeginCase(string.Format("Calling GetDecimal() on a field of dbtype {0}", row[i].DbTypeName));
  85. decimal retDecimal = rdr.GetDecimal(i);
  86. Compare(row[i].Value, retDecimal);
  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 Decimal. In this case an Invalid case exception should be thrown.
  99. {
  100. try
  101. {
  102. BeginCase(string.Format("Calling GetDecimal() on a field of dbtype {0}", row[i].DbTypeName));
  103. decimal retDecimal = rdr.GetDecimal(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. public void DoTestTextualFieldsThatContainNumbers(DbTypeParametersCollection row)
  137. {
  138. //Leave only textual fields in the collection, and set their value to the string "10"
  139. SetTextualFieldsWithNumericValues(ref row);
  140. if (row.Count < 1)
  141. {
  142. return;
  143. }
  144. testTypesInvocations++;
  145. exp = null;
  146. string rowId = "43968_" + this.testTypesInvocations.ToString();
  147. OracleDataReader rdr = null;
  148. OracleConnection con = null;
  149. try
  150. {
  151. row.ExecuteInsert(rowId);
  152. row.ExecuteSelectReader(rowId, out rdr, out con);
  153. while (rdr.Read())
  154. {
  155. //Run over all the columns in the result set row.
  156. //For each column, try to read it as a Decimal.
  157. //Because all the fields are textual, this should throw an InvalidCastException.
  158. for (int i=0; i<row.Count; i++)
  159. {
  160. try
  161. {
  162. BeginCase(string.Format("Calling GetDecimal() on a textual field of dbtype {0} with value '{1}'", row[i].DbTypeName, row[i].Value));
  163. decimal retDecimal = rdr.GetDecimal(i);
  164. ExpectedExceptionNotCaught(typeof(InvalidCastException).FullName);
  165. }
  166. catch (InvalidCastException ex)
  167. {
  168. ExpectedExceptionCaught(ex);
  169. }
  170. catch (Exception ex)
  171. {
  172. exp = ex;
  173. }
  174. finally
  175. {
  176. EndCase(exp);
  177. exp = null;
  178. }
  179. }
  180. }
  181. }
  182. finally
  183. {
  184. row.ExecuteDelete(rowId);
  185. if ( (rdr != null) && (!rdr.IsClosed) )
  186. {
  187. rdr.Close();
  188. }
  189. if ( (con != null) && (con.State != ConnectionState.Closed) )
  190. {
  191. con.Close();
  192. }
  193. }
  194. }
  195. private void SetTextualFieldsWithNumericValues(ref DbTypeParametersCollection row)
  196. {
  197. DbTypeParametersCollection newRow = new DbTypeParametersCollection(row.TableName);
  198. foreach (DbTypeParameter current in row)
  199. {
  200. if (current.Value is string)
  201. {
  202. newRow.Add(current.DbTypeName, "10");
  203. }
  204. }
  205. row = newRow;
  206. }
  207. }
  208. }