OdbcCommandBuilderTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // OdbcCommandBuilderTest.cs - NUnit Test Cases for testing the
  2. // OdbcCommandBuilder Test.
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // Copyright (c) 2008 Gert Driesen
  8. //
  9. // Permission is hereby granted, free of charge, to any person
  10. // obtaining a copy of this software and associated documentation
  11. // files (the "Software"), to deal in the Software without
  12. // restriction, including without limitation the rights to use, copy,
  13. // modify, merge, publish, distribute, sublicense, and/or sell copies
  14. // of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  24. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  25. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. // SOFTWARE.
  28. using System;
  29. using System.Data;
  30. using System.Data.Common;
  31. using System.Data.Odbc;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Data.Odbc
  34. {
  35. [TestFixture]
  36. public class OdbcCommandBuilderTest
  37. {
  38. [Test]
  39. public void QuotePrefix ()
  40. {
  41. OdbcCommandBuilder cb = new OdbcCommandBuilder ();
  42. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
  43. cb.QuotePrefix = "mono";
  44. Assert.AreEqual ("mono", cb.QuotePrefix, "#2");
  45. cb.QuotePrefix = null;
  46. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#3");
  47. cb.QuotePrefix = "'\"";
  48. Assert.AreEqual ("'\"", cb.QuotePrefix, "#4");
  49. cb.QuotePrefix = string.Empty;
  50. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#5");
  51. cb.QuotePrefix = " ";
  52. Assert.AreEqual (" ", cb.QuotePrefix, "#6");
  53. }
  54. [Test]
  55. public void QuoteSuffix ()
  56. {
  57. OdbcCommandBuilder cb = new OdbcCommandBuilder ();
  58. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
  59. cb.QuoteSuffix = "mono";
  60. Assert.AreEqual ("mono", cb.QuoteSuffix, "#2");
  61. cb.QuoteSuffix = null;
  62. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#3");
  63. cb.QuoteSuffix = "'\"";
  64. Assert.AreEqual ("'\"", cb.QuoteSuffix, "#4");
  65. cb.QuoteSuffix = string.Empty;
  66. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#5");
  67. }
  68. #if NET_2_0
  69. [Test] // QuoteIdentifier (String)
  70. [Category ("NotWorking")]
  71. public void QuoteIdentifier1 ()
  72. {
  73. OdbcCommandBuilder cb;
  74. cb = new OdbcCommandBuilder ();
  75. cb.QuotePrefix = "abc";
  76. Assert.AreEqual ("abcmono", cb.QuoteIdentifier ("mono"), "#A1");
  77. Assert.AreEqual ("abc", cb.QuoteIdentifier (string.Empty), "#A2");
  78. Assert.AreEqual ("abcZ", cb.QuoteIdentifier ("Z"), "#A3");
  79. Assert.AreEqual ("abcabc", cb.QuoteIdentifier ("abc"), "#A4");
  80. cb.QuoteSuffix = "def";
  81. Assert.AreEqual ("abcmonodef", cb.QuoteIdentifier ("mono"), "#A5");
  82. Assert.AreEqual ("abcdef", cb.QuoteIdentifier (string.Empty), "#A6");
  83. Assert.AreEqual ("abcZdef", cb.QuoteIdentifier ("Z"), "#A7");
  84. Assert.AreEqual ("abcabcdef", cb.QuoteIdentifier ("abc"), "#A8");
  85. cb = new OdbcCommandBuilder ();
  86. cb.QuotePrefix = "X";
  87. Assert.AreEqual ("Xmono", cb.QuoteIdentifier ("mono"), "#B1");
  88. Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty), "#B2");
  89. Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z"), "#B3");
  90. Assert.AreEqual ("XX", cb.QuoteIdentifier ("X"), "#B4");
  91. cb.QuoteSuffix = " ";
  92. Assert.AreEqual ("Xmono ", cb.QuoteIdentifier ("mono"), "#B5");
  93. Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty), "#B6");
  94. Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z"), "#B7");
  95. Assert.AreEqual ("X ", cb.QuoteIdentifier (" "), "#B8");
  96. cb = new OdbcCommandBuilder ();
  97. cb.QuotePrefix = " ";
  98. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono"), "#C1");
  99. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty), "#C2");
  100. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z"), "#C3");
  101. cb.QuoteSuffix = "def";
  102. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono"), "#C4");
  103. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty), "#C5");
  104. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z"), "#C6");
  105. }
  106. [Test] // QuoteIdentifier (String)
  107. public void QuoteIdentifier1_QuotePrefix_Empty ()
  108. {
  109. OdbcCommandBuilder cb = new OdbcCommandBuilder ();
  110. try {
  111. cb.QuoteIdentifier ("mono");
  112. Assert.Fail ("#1");
  113. } catch (InvalidOperationException ex) {
  114. // QuoteIdentifier requires open connection when
  115. // the quote prefix has not been set
  116. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  117. Assert.IsNull (ex.InnerException, "#3");
  118. Assert.IsNotNull (ex.Message, "#4");
  119. }
  120. }
  121. [Test] // QuoteIdentifier (String)
  122. public void QuoteIdentifier1_UnquotedIdentifier_Null ()
  123. {
  124. OdbcCommandBuilder cb = new OdbcCommandBuilder ();
  125. try {
  126. cb.QuoteIdentifier ((string) null);
  127. Assert.Fail ("#1");
  128. } catch (ArgumentNullException ex) {
  129. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  130. Assert.IsNull (ex.InnerException, "#3");
  131. Assert.IsNotNull (ex.Message, "#4");
  132. Assert.AreEqual ("unquotedIdentifier", ex.ParamName, "#5");
  133. }
  134. }
  135. [Test] // QuoteIdentifier (String, OdbcConnection)
  136. [Category ("NotWorking")]
  137. public void QuoteIdentifier2_Connection_Null ()
  138. {
  139. OdbcCommandBuilder cb;
  140. OdbcConnection conn = null;
  141. cb = new OdbcCommandBuilder ();
  142. try {
  143. cb.QuoteIdentifier ("mono", conn);
  144. Assert.Fail ("#A1");
  145. } catch (InvalidOperationException ex) {
  146. // QuoteIdentifier requires open connection when
  147. // the quote prefix has not been set
  148. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  149. Assert.IsNull (ex.InnerException, "#A3");
  150. Assert.IsNotNull (ex.Message, "#A4");
  151. }
  152. cb = new OdbcCommandBuilder ();
  153. cb.QuotePrefix = "abc";
  154. Assert.AreEqual ("abcmono", cb.QuoteIdentifier ("mono", conn), "#B1");
  155. Assert.AreEqual ("abcZ", cb.QuoteIdentifier ("Z", conn), "#B2");
  156. Assert.AreEqual ("abcabc", cb.QuoteIdentifier ("abc", conn), "#B3");
  157. Assert.AreEqual ("abc", cb.QuoteIdentifier (string.Empty, conn), "#B4");
  158. Assert.AreEqual ("abc ", cb.QuoteIdentifier (" ", conn), "#B5");
  159. Assert.AreEqual ("abc\r", cb.QuoteIdentifier ("\r", conn), "#B6");
  160. cb.QuoteSuffix = "def";
  161. Assert.AreEqual ("abcmonodef", cb.QuoteIdentifier ("mono", conn), "#B7");
  162. Assert.AreEqual ("abcZdef", cb.QuoteIdentifier ("Z", conn), "#B8");
  163. Assert.AreEqual ("abcabcdef", cb.QuoteIdentifier ("abc", conn), "#B9");
  164. Assert.AreEqual ("abcdef", cb.QuoteIdentifier (string.Empty, conn), "#B10");
  165. Assert.AreEqual ("abc def", cb.QuoteIdentifier (" ", conn), "#B11");
  166. Assert.AreEqual ("abc\rdef", cb.QuoteIdentifier ("\r", conn), "#B12");
  167. cb.QuotePrefix = string.Empty;
  168. try {
  169. cb.QuoteIdentifier ("mono", conn);
  170. Assert.Fail ("#C1");
  171. } catch (InvalidOperationException ex) {
  172. // QuoteIdentifier requires open connection when
  173. // the quote prefix has not been set
  174. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  175. Assert.IsNull (ex.InnerException, "#C3");
  176. Assert.IsNotNull (ex.Message, "#C4");
  177. }
  178. cb = new OdbcCommandBuilder ();
  179. cb.QuotePrefix = "X";
  180. Assert.AreEqual ("Xmono", cb.QuoteIdentifier ("mono", conn), "#D1");
  181. Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z", conn), "#D2");
  182. Assert.AreEqual ("XX", cb.QuoteIdentifier ("X", conn), "#D3");
  183. Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty, conn), "#D4");
  184. Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D5");
  185. Assert.AreEqual ("X\r", cb.QuoteIdentifier ("\r", conn), "#D6");
  186. cb.QuoteSuffix = " ";
  187. Assert.AreEqual ("Xmono ", cb.QuoteIdentifier ("mono", conn), "#D7");
  188. Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z", conn), "#D8");
  189. Assert.AreEqual ("XX ", cb.QuoteIdentifier ("X", conn), "#D9");
  190. Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty, conn), "#D10");
  191. Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D11");
  192. Assert.AreEqual ("X\r ", cb.QuoteIdentifier ("\r", conn), "#D12");
  193. cb = new OdbcCommandBuilder ();
  194. cb.QuotePrefix = " ";
  195. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E1");
  196. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E2");
  197. Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E3");
  198. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E4");
  199. Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E5");
  200. Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E6");
  201. cb.QuoteSuffix = "def";
  202. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E7");
  203. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E8");
  204. Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E9");
  205. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E10");
  206. Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E11");
  207. Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E12");
  208. }
  209. [Test] // QuoteIdentifier (String, OdbcConnection)
  210. [Category ("NotWorking")]
  211. public void QuoteIdentifier2_Connection_Closed ()
  212. {
  213. OdbcCommandBuilder cb;
  214. OdbcConnection conn = new OdbcConnection ();
  215. cb = new OdbcCommandBuilder ();
  216. try {
  217. cb.QuoteIdentifier ("mono", conn);
  218. Assert.Fail ("#A1");
  219. } catch (InvalidOperationException ex) {
  220. // QuoteIdentifier requires an open and available
  221. // Connection. The connection's current state is
  222. // closed
  223. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  224. Assert.IsNull (ex.InnerException, "#A3");
  225. Assert.IsNotNull (ex.Message, "#A4");
  226. }
  227. cb = new OdbcCommandBuilder ();
  228. cb.QuotePrefix = "abc";
  229. Assert.AreEqual ("abcmono", cb.QuoteIdentifier ("mono", conn), "#B1");
  230. Assert.AreEqual ("abcZ", cb.QuoteIdentifier ("Z", conn), "#B2");
  231. Assert.AreEqual ("abcabc", cb.QuoteIdentifier ("abc", conn), "#B3");
  232. Assert.AreEqual ("abc", cb.QuoteIdentifier (string.Empty, conn), "#B4");
  233. Assert.AreEqual ("abc ", cb.QuoteIdentifier (" ", conn), "#B5");
  234. Assert.AreEqual ("abc\r", cb.QuoteIdentifier ("\r", conn), "#B6");
  235. cb.QuoteSuffix = "def";
  236. Assert.AreEqual ("abcmonodef", cb.QuoteIdentifier ("mono", conn), "#B7");
  237. Assert.AreEqual ("abcZdef", cb.QuoteIdentifier ("Z", conn), "#B8");
  238. Assert.AreEqual ("abcabcdef", cb.QuoteIdentifier ("abc", conn), "#B9");
  239. Assert.AreEqual ("abcdef", cb.QuoteIdentifier (string.Empty, conn), "#B10");
  240. Assert.AreEqual ("abc def", cb.QuoteIdentifier (" ", conn), "#B11");
  241. Assert.AreEqual ("abc\rdef", cb.QuoteIdentifier ("\r", conn), "#B12");
  242. cb.QuotePrefix = string.Empty;
  243. try {
  244. cb.QuoteIdentifier ("mono");
  245. Assert.Fail ("#C1");
  246. } catch (InvalidOperationException ex) {
  247. // QuoteIdentifier requires an open and available
  248. // Connection. The connection's current state is
  249. // closed
  250. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  251. Assert.IsNull (ex.InnerException, "#C3");
  252. Assert.IsNotNull (ex.Message, "#C4");
  253. }
  254. cb = new OdbcCommandBuilder ();
  255. cb.QuotePrefix = "X";
  256. Assert.AreEqual ("Xmono", cb.QuoteIdentifier ("mono"), "#D1");
  257. Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z"), "#D2");
  258. Assert.AreEqual ("XX", cb.QuoteIdentifier ("X"), "#D3");
  259. Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty, conn), "#D4");
  260. Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D5");
  261. Assert.AreEqual ("X\r", cb.QuoteIdentifier ("\r", conn), "#D6");
  262. cb.QuoteSuffix = " ";
  263. Assert.AreEqual ("Xmono ", cb.QuoteIdentifier ("mono"), "#D7");
  264. Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z"), "#D8");
  265. Assert.AreEqual ("XX ", cb.QuoteIdentifier ("X"), "#D9");
  266. Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty, conn), "#D10");
  267. Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D11");
  268. Assert.AreEqual ("X\r ", cb.QuoteIdentifier ("\r", conn), "#D12");
  269. cb = new OdbcCommandBuilder ();
  270. cb.QuotePrefix = " ";
  271. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E1");
  272. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E2");
  273. Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E3");
  274. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E4");
  275. Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E5");
  276. Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E6");
  277. cb.QuoteSuffix = "def";
  278. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E7");
  279. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E8");
  280. Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E9");
  281. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E10");
  282. Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E11");
  283. Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E12");
  284. }
  285. [Test]
  286. public void DefaultPropertiesTest ()
  287. {
  288. OdbcCommandBuilder cb = new OdbcCommandBuilder ();
  289. Assert.AreEqual (".", cb.CatalogSeparator, "#2");
  290. Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#3");
  291. Assert.AreEqual (".", cb.SchemaSeparator, "#4");
  292. Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
  293. }
  294. #endif
  295. }
  296. }