DbCommandBuilderTest.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // DbCommandBuilderTest.cs - NUnit Test Cases for DbCommandBuilder class
  2. //
  3. // Author:
  4. // Gert Driesen ([email protected])
  5. //
  6. // Copyright (C) 2008 Gert Driesen
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. #if NET_2_0
  28. using System;
  29. using System.Data;
  30. using System.Data.Common;
  31. using System.Globalization;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Data.Common
  34. {
  35. [TestFixture]
  36. public class DbCommandBuilderTest
  37. {
  38. [Test]
  39. public void CatalogLocationTest ()
  40. {
  41. MyCommandBuilder cb = new MyCommandBuilder ();
  42. Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
  43. cb.CatalogLocation = CatalogLocation.End;
  44. Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#2");
  45. }
  46. [Test]
  47. public void CatalogLocation_Value_Invalid ()
  48. {
  49. MyCommandBuilder cb = new MyCommandBuilder ();
  50. cb.CatalogLocation = CatalogLocation.End;
  51. try {
  52. cb.CatalogLocation = (CatalogLocation) 666;
  53. Assert.Fail ("#1");
  54. } catch (ArgumentOutOfRangeException ex) {
  55. // The CatalogLocation enumeration value, 666, is invalid
  56. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  57. Assert.IsNull (ex.InnerException, "#3");
  58. Assert.IsNotNull (ex.Message, "#4");
  59. Assert.IsTrue (ex.Message.IndexOf ("CatalogLocation") != -1, "#5:" + ex.Message);
  60. Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
  61. Assert.AreEqual ("CatalogLocation", ex.ParamName, "#7");
  62. }
  63. Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#8");
  64. }
  65. [Test]
  66. public void CatalogSeparator ()
  67. {
  68. MyCommandBuilder cb = new MyCommandBuilder ();
  69. Assert.AreEqual (".", cb.CatalogSeparator, "#1");
  70. cb.CatalogSeparator = "a";
  71. Assert.AreEqual ("a", cb.CatalogSeparator, "#2");
  72. cb.CatalogSeparator = null;
  73. Assert.AreEqual (".", cb.CatalogSeparator, "#3");
  74. cb.CatalogSeparator = "b";
  75. Assert.AreEqual ("b", cb.CatalogSeparator, "#4");
  76. cb.CatalogSeparator = string.Empty;
  77. Assert.AreEqual (".", cb.CatalogSeparator, "#5");
  78. cb.CatalogSeparator = " ";
  79. Assert.AreEqual (" ", cb.CatalogSeparator, "#6");
  80. }
  81. [Test]
  82. public void ConflictOptionTest ()
  83. {
  84. MyCommandBuilder cb = new MyCommandBuilder ();
  85. Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
  86. cb.ConflictOption = ConflictOption.CompareRowVersion;
  87. Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
  88. }
  89. [Test]
  90. public void ConflictOption_Value_Invalid ()
  91. {
  92. MyCommandBuilder cb = new MyCommandBuilder ();
  93. cb.ConflictOption = ConflictOption.CompareRowVersion;
  94. try {
  95. cb.ConflictOption = (ConflictOption) 666;
  96. Assert.Fail ("#1");
  97. } catch (ArgumentOutOfRangeException ex) {
  98. // The ConflictOption enumeration value, 666, is invalid
  99. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  100. Assert.IsNull (ex.InnerException, "#3");
  101. Assert.IsNotNull (ex.Message, "#4");
  102. Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
  103. Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
  104. Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
  105. }
  106. Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
  107. }
  108. [Test] // QuoteIdentifier (String)
  109. public void QuoteIdentifier ()
  110. {
  111. MyCommandBuilder cb = new MyCommandBuilder ();
  112. try {
  113. cb.QuoteIdentifier ((string) null);
  114. Assert.Fail ("#A1");
  115. } catch (NotSupportedException ex) {
  116. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
  117. Assert.IsNull (ex.InnerException, "#A3");
  118. Assert.AreEqual ((new NotSupportedException ()).Message, ex.Message, "#A4");
  119. }
  120. try {
  121. cb.QuoteIdentifier ("mono");
  122. Assert.Fail ("#B1");
  123. } catch (NotSupportedException ex) {
  124. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
  125. Assert.IsNull (ex.InnerException, "#B3");
  126. Assert.AreEqual ((new NotSupportedException ()).Message, ex.Message, "#B4");
  127. }
  128. }
  129. [Test]
  130. public void QuotePrefix ()
  131. {
  132. MyCommandBuilder cb = new MyCommandBuilder ();
  133. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
  134. cb.QuotePrefix = "mono";
  135. Assert.AreEqual ("mono", cb.QuotePrefix, "#2");
  136. cb.QuotePrefix = null;
  137. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#3");
  138. cb.QuotePrefix = "'\"";
  139. Assert.AreEqual ("'\"", cb.QuotePrefix, "#4");
  140. cb.QuotePrefix = string.Empty;
  141. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#5");
  142. cb.QuotePrefix = " ";
  143. Assert.AreEqual (" ", cb.QuotePrefix, "#6");
  144. }
  145. [Test]
  146. public void QuoteSuffix ()
  147. {
  148. MyCommandBuilder cb = new MyCommandBuilder ();
  149. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
  150. cb.QuoteSuffix = "mono";
  151. Assert.AreEqual ("mono", cb.QuoteSuffix, "#2");
  152. cb.QuoteSuffix = null;
  153. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#3");
  154. cb.QuoteSuffix = "'\"";
  155. Assert.AreEqual ("'\"", cb.QuoteSuffix, "#4");
  156. cb.QuoteSuffix = string.Empty;
  157. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#5");
  158. cb.QuoteSuffix = " ";
  159. Assert.AreEqual (" ", cb.QuoteSuffix, "#6");
  160. }
  161. [Test]
  162. public void SchemaSeparator ()
  163. {
  164. MyCommandBuilder cb = new MyCommandBuilder ();
  165. Assert.AreEqual (".", cb.SchemaSeparator, "#1");
  166. cb.SchemaSeparator = "a";
  167. Assert.AreEqual ("a", cb.SchemaSeparator, "#2");
  168. cb.SchemaSeparator = null;
  169. Assert.AreEqual (".", cb.SchemaSeparator, "#3");
  170. cb.SchemaSeparator = "b";
  171. Assert.AreEqual ("b", cb.SchemaSeparator, "#4");
  172. cb.SchemaSeparator = string.Empty;
  173. Assert.AreEqual (".", cb.SchemaSeparator, "#5");
  174. cb.SchemaSeparator = " ";
  175. Assert.AreEqual (" ", cb.SchemaSeparator, "#6");
  176. }
  177. private class MyCommandBuilder : DbCommandBuilder
  178. {
  179. protected override string GetParameterPlaceholder (int parameterOrdinal)
  180. {
  181. return string.Format (CultureInfo.InvariantCulture,
  182. "@PH:{0}@", parameterOrdinal);
  183. }
  184. protected override string GetParameterName (string parameterName)
  185. {
  186. return string.Format (CultureInfo.InvariantCulture,
  187. "@NAME:{0}@", parameterName);
  188. }
  189. protected override string GetParameterName (int parameterOrdinal)
  190. {
  191. return string.Format (CultureInfo.InvariantCulture,
  192. "@NAME:{0}@", parameterOrdinal);
  193. }
  194. protected override void ApplyParameterInfo (DbParameter parameter, DataRow row, StatementType statementType, bool whereClause)
  195. {
  196. }
  197. protected override void SetRowUpdatingHandler (DbDataAdapter adapter)
  198. {
  199. }
  200. }
  201. }
  202. }
  203. #endif // NET_2_0