SqlDataSourceDerbyTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // Tests for System.Web.UI.WebControls.SqlDataSource
  3. // This test uses Derby, java embedded database.
  4. //
  5. // Author:
  6. // Vladimir Krasnov ([email protected])
  7. //
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0 && TARGET_JVM
  30. using NUnit.Framework;
  31. using System;
  32. using System.Configuration;
  33. using System.Data;
  34. using System.Data.OleDb;
  35. using System.Data.Common;
  36. using System.IO;
  37. using System.Globalization;
  38. using System.Web;
  39. using System.Web.UI;
  40. using System.Web.UI.WebControls;
  41. using System.Collections.Specialized;
  42. namespace MonoTests.System.Web.UI.WebControls
  43. {
  44. [TestFixture]
  45. public class SqlDataSourceDerbyTest {
  46. [TestFixtureSetUp]
  47. public void setup ()
  48. {
  49. if (Directory.Exists (_dataDir))
  50. Directory.Delete (_dataDir, true);
  51. string initSql = @"CREATE TABLE Table1 (
  52. UserId int NOT NULL PRIMARY KEY,
  53. UserName varchar(256) NOT NULL,
  54. Description varchar(256)
  55. )";
  56. OleDbConnection connection = new OleDbConnection (_connectionString);
  57. try {
  58. connection.Open ();
  59. DbCommand cmd = connection.CreateCommand ();
  60. cmd.CommandText = initSql;
  61. cmd.CommandType = CommandType.Text;
  62. cmd.ExecuteNonQuery ();
  63. }
  64. catch (Exception) {
  65. }
  66. finally {
  67. connection.Close ();
  68. }
  69. }
  70. [Test]
  71. public void SelectTest1 ()
  72. {
  73. SqlDataSource ds = CreateDataSource ();
  74. ds.SelectCommand = "SELECT * FROM Table1";
  75. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ());
  76. Assert.AreEqual (10, dataView.Count);
  77. }
  78. [Test]
  79. public void SelectTest2 ()
  80. {
  81. SqlDataSource ds = CreateDataSource ();
  82. ds.SelectCommand = "SELECT * FROM Table1";
  83. ds.FilterExpression = "UserId > 5";
  84. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ());
  85. Assert.AreEqual (4, dataView.Count);
  86. }
  87. [Test]
  88. public void SelectTest3 ()
  89. {
  90. SqlDataSource ds = CreateDataSource ();
  91. ds.SelectCommand = "SELECT * FROM Table1";
  92. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ("Description"));
  93. Assert.AreEqual ("Description", dataView.Sort);
  94. }
  95. [Test]
  96. public void SelectTest4 ()
  97. {
  98. SqlDataSource ds = CreateDataSource ();
  99. ds.SelectCommand = "SELECT * FROM Table1";
  100. try {
  101. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments (1, 2));
  102. }
  103. catch (NotSupportedException) {
  104. Assert.AreEqual (true, true);
  105. }
  106. }
  107. [Test]
  108. public void SelectTest5 ()
  109. {
  110. SqlDataSource ds = CreateDataSource ();
  111. ds.SelectCommand = "SELECT * FROM Table1 WHERE UserId = ?";
  112. ds.SelectParameters.Add (new Parameter ("UserId", TypeCode.Int32, "5"));
  113. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ());
  114. Assert.AreEqual (1, dataView.Count);
  115. }
  116. [Test]
  117. public void UpdateTest1 ()
  118. {
  119. SqlDataSource ds = CreateDataSource ();
  120. ds.SelectCommand = "SELECT * FROM Table1 WHERE UserName = ?";
  121. ds.SelectParameters.Add (new Parameter ("UserName", TypeCode.String, "superuser"));
  122. ds.UpdateCommand = "UPDATE Table1 SET UserName = ? WHERE UserId = ?";
  123. ds.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "superuser"));
  124. ds.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "5"));
  125. int records = ds.Update ();
  126. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ());
  127. Assert.AreEqual (1, dataView.Count);
  128. Assert.AreEqual (1, records);
  129. }
  130. [Test]
  131. public void UpdateTest2 ()
  132. {
  133. SqlDataSource ds = CreateDataSource ();
  134. ds.SelectCommand = "SELECT * FROM Table1 WHERE UserName = ?";
  135. ds.SelectParameters.Add (new Parameter ("UserName", TypeCode.String, "SimpleUser"));
  136. ds.UpdateCommand = "UPDATE Table1 SET UserName = ? WHERE UserId = ?";
  137. ds.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "superuser"));
  138. ds.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "5"));
  139. ds.OldValuesParameterFormatString = "original_{0}";
  140. SqlDataSourceView view = (SqlDataSourceView) ((IDataSource) ds).GetView ("");
  141. OrderedDictionary keys = new OrderedDictionary ();
  142. keys.Add ("UserId", 7);
  143. OrderedDictionary values = new OrderedDictionary ();
  144. values.Add ("UserName", "SimpleUser");
  145. OrderedDictionary oldvalues = new OrderedDictionary ();
  146. oldvalues.Add ("UserName", "user7");
  147. int records = view.Update (keys, values, oldvalues);
  148. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ());
  149. Assert.AreEqual (1, dataView.Count);
  150. Assert.AreEqual (1, records);
  151. }
  152. [Test]
  153. public void InsertTest1 ()
  154. {
  155. SqlDataSource ds = CreateDataSource ();
  156. ds.SelectCommand = "SELECT * FROM Table1 WHERE UserName = ?";
  157. ds.SelectParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser"));
  158. ds.InsertCommand = "INSERT INTO Table1 (UserId, UserName, Description) VALUES (?, ?, ?)";
  159. ds.InsertParameters.Add (new Parameter ("UserId", TypeCode.Int32, "15"));
  160. ds.InsertParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser"));
  161. ds.InsertParameters.Add (new Parameter ("Description", TypeCode.String, "newuser"));
  162. int records = ds.Insert ();
  163. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ());
  164. Assert.AreEqual (1, dataView.Count);
  165. Assert.AreEqual (1, records);
  166. }
  167. [Test]
  168. public void InsertTest2 ()
  169. {
  170. SqlDataSource ds = CreateDataSource ();
  171. ds.SelectCommand = "SELECT * FROM Table1 WHERE UserName = ?";
  172. ds.SelectParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser2"));
  173. ds.InsertCommand = "INSERT INTO Table1 (UserId, UserName, Description) VALUES (?, ?, ?)";
  174. ds.InsertParameters.Add (new Parameter ("UserId", TypeCode.Int32, "5"));
  175. ds.InsertParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser"));
  176. ds.InsertParameters.Add (new Parameter ("Description", TypeCode.String, "newuser"));
  177. SqlDataSourceView view = (SqlDataSourceView) ((IDataSource) ds).GetView ("");
  178. OrderedDictionary values = new OrderedDictionary ();
  179. values.Add ("UserId", "17");
  180. values.Add ("UserName", "newuser2");
  181. values.Add ("Description", "newuser2");
  182. int records = view.Insert (values);
  183. DataView dataView = (DataView) ds.Select (new DataSourceSelectArguments ());
  184. Assert.AreEqual (1, dataView.Count);
  185. Assert.AreEqual (1, records);
  186. }
  187. const string _dataDir = "DataDir";
  188. const string _connectionString = "JdbcDriverClassName=org.apache.derby.jdbc.EmbeddedDriver;JdbcURL=jdbc:derby:" + _dataDir + ";create=true";
  189. private SqlDataSource CreateDataSource ()
  190. {
  191. SqlDataSource ds = new SqlDataSource ();
  192. ds.ConnectionString = _connectionString;
  193. ds.ProviderName = "System.Data.OleDb";
  194. ds.DataSourceMode = SqlDataSourceMode.DataSet;
  195. return ds;
  196. }
  197. [SetUp]
  198. public void RestoreData ()
  199. {
  200. string insertSql = @"INSERT INTO Table1 VALUES ({0}, '{1}', '{2}')";
  201. string deleteSql = @"DELETE FROM Table1";
  202. OleDbConnection connection = new OleDbConnection (_connectionString);
  203. connection.Open ();
  204. try {
  205. DbCommand dc = connection.CreateCommand ();
  206. dc.CommandText = deleteSql;
  207. dc.CommandType = CommandType.Text;
  208. dc.ExecuteNonQuery ();
  209. for (int i = 0; i < 10; i++) {
  210. DbCommand ic = connection.CreateCommand ();
  211. ic.CommandText = string.Format (insertSql, i.ToString (), "user" + i.ToString (), (9 - i).ToString ());
  212. ic.CommandType = CommandType.Text;
  213. ic.ExecuteNonQuery ();
  214. }
  215. }
  216. catch (Exception) {
  217. }
  218. finally {
  219. connection.Close ();
  220. }
  221. }
  222. }
  223. }
  224. #endif