Merge pull request #1200 from akoeplinger/remove-jvm
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DbDataAdapterTest.cs
1 //
2 // DbDataAdapterTest.cs - NUnit Test Cases for testing the DbDataAdapter class
3 //
4 // Author:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (c) 2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // 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 BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Data;
31 using System.Data.Common;
32 using System.Data.SqlClient;
33
34 /*--For Bug 853 Test Begin--*/
35 #if !MOBILE
36 using Mono.Data.Sqlite;
37 #endif
38 /*--For Bug 853 Test End--*/
39
40 using NUnit.Framework;
41
42 namespace MonoTests.System.Data.Common
43 {
44         [TestFixture]
45         public class DbDataAdapterTest
46         {
47 #if NET_2_0
48                 [Test]
49                 public void UpdateBatchSize ()
50                 {
51                         MyAdapter da = new MyAdapter ();
52                         try {
53                                 da.UpdateBatchSize = 0;
54                                 Assert.Fail ("#A1");
55                         } catch (NotSupportedException ex) {
56                                 // Specified method is not supported
57                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
58                                 Assert.IsNull (ex.InnerException, "#A3");
59                                 Assert.IsNotNull (ex.Message, "#A4");
60                         }
61                         Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
62
63                         try {
64                                 da.UpdateBatchSize = int.MaxValue;
65                                 Assert.Fail ("#B1");
66                         } catch (NotSupportedException ex) {
67                                 // Specified method is not supported
68                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
69                                 Assert.IsNull (ex.InnerException, "#B3");
70                                 Assert.IsNotNull (ex.Message, "#B4");
71                         }
72                         Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
73
74                         da.UpdateBatchSize = 1;
75                         Assert.AreEqual (1, da.UpdateBatchSize, "#C");
76                 }
77
78                 [Test]
79                 public void UpdateBatchSize_Negative ()
80                 {
81                         MyAdapter da = new MyAdapter ();
82                         try {
83                                 da.UpdateBatchSize = -1;
84                                 Assert.Fail ("#1");
85                         } catch (NotSupportedException ex) {
86                                 // Specified method is not supported
87                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
88                                 Assert.IsNull (ex.InnerException, "#3");
89                                 Assert.IsNotNull (ex.Message, "#4");
90                         }
91                 }
92
93                 [Test]
94                 public void AddToBatch ()
95                 {
96                         MyAdapter da = new MyAdapter ();
97                         try {
98                                 da.AddToBatch (new SqlCommand ());
99                                 Assert.Fail ("#1");
100                         } catch (NotSupportedException ex) {
101                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
102                                 Assert.IsNull (ex.InnerException, "#3");
103                                 Assert.IsNotNull (ex.Message, "#4");
104                         }
105                 }
106
107                 [Test]
108                 public void ClearBatch ()
109                 {
110                         MyAdapter da = new MyAdapter ();
111                         try {
112                                 da.ClearBatch ();
113                                 Assert.Fail ("#1");
114                         } catch (NotSupportedException ex) {
115                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
116                                 Assert.IsNull (ex.InnerException, "#3");
117                                 Assert.IsNotNull (ex.Message, "#4");
118                         }
119                 }
120
121                 [Test]
122                 public void ExecuteBatch ()
123                 {
124                         MyAdapter da = new MyAdapter ();
125                         try {
126                                 da.ExecuteBatch ();
127                                 Assert.Fail ("#1");
128                         } catch (NotSupportedException ex) {
129                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
130                                 Assert.IsNull (ex.InnerException, "#3");
131                                 Assert.IsNotNull (ex.Message, "#4");
132                         }
133                 }
134
135                 [Test]
136                 public void GetBatchedParameter ()
137                 {
138                         MyAdapter da = new MyAdapter ();
139                         try {
140                                 da.GetBatchedParameter (1, 1);
141                                 Assert.Fail ("#1");
142                         } catch (NotSupportedException ex) {
143                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
144                                 Assert.IsNull (ex.InnerException, "#3");
145                                 Assert.IsNotNull (ex.Message, "#4");
146                         }
147                 }
148
149                 [Test]
150                 public void GetBatchedRecordsAffected ()
151                 {
152                         MyAdapter da = new MyAdapter ();
153                         int recordsAffected = 0;
154                         Exception error = null;
155
156                         Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
157                                 out recordsAffected, out error), "#1");
158                         Assert.AreEqual (1, recordsAffected, "#2");
159                         Assert.IsNull (error, "#3");
160                 }
161
162                 [Test]
163                 public void InitializeBatching ()
164                 {
165                         MyAdapter da = new MyAdapter ();
166                         try {
167                                 da.InitializeBatching ();
168                                 Assert.Fail ("#1");
169                         } catch (NotSupportedException ex) {
170                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
171                                 Assert.IsNull (ex.InnerException, "#3");
172                                 Assert.IsNotNull (ex.Message, "#4");
173                         }
174                 }
175
176                 [Test]
177                 public void TerminateBatching ()
178                 {
179                         MyAdapter da = new MyAdapter ();
180                         try {
181                                 da.TerminateBatching ();
182                                 Assert.Fail ("#1");
183                         } catch (NotSupportedException ex) {
184                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
185                                 Assert.IsNull (ex.InnerException, "#3");
186                                 Assert.IsNotNull (ex.Message, "#4");
187                         }
188                 }
189 #if !MOBILE     
190                 [Test]
191                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
192                 public void XimarinBugzillaBug853Test()
193                 {
194                         const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
195                         SqliteConnection dbConnection = new SqliteConnection(connectionString);
196                         dbConnection.Open();
197                         SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
198                         ClearTableEntry.ExecuteNonQuery();
199
200                         SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
201                         SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
202                         sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
203                         sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
204                         
205                         DataSet dataSet = new DataSet();
206
207                         sqliteDataAdapter.Fill(dataSet, "Primus");//reset
208
209                         DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
210                         rowToBeAdded["id"] = 123;
211                         rowToBeAdded["name"] = "Name";//not null primary key
212                         rowToBeAdded["value"] = 777;
213
214                         dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
215 sqliteDataAdapter.Update (dataSet, "Primus");
216
217                         //This would fail with NULL constraint violation in bug
218                         //report.  Because before the patch, it would create
219                         //a new record with all fields being null-- if the
220                         //exception rises, test fails
221                         sqliteDataAdapter.Update (dataSet, "Primus");
222
223                         dbConnection.Close();
224                         dbConnection = null;
225                 }
226
227                 [Test]
228                 public void UpdateResetRowErrorCorrectly ()
229                 {
230                         const string connectionString = "URI = file::memory:; Version = 3";
231                         using (var dbConnection = new SqliteConnection (connectionString)) {
232                                 dbConnection.Open ();
233
234                                 using (var cmd = dbConnection.CreateCommand ()) {
235                                         cmd.CommandText = "CREATE TABLE data (id PRIMARY KEY, name TEXT)";
236                                         cmd.ExecuteNonQuery ();
237                                 }
238
239
240                                 var ts = dbConnection.BeginTransaction ();
241                                 var da = new SqliteDataAdapter ("SELECT * FROM data", dbConnection);
242                                 var builder = new SqliteCommandBuilder (da);
243                                 da.UpdateCommand = builder.GetUpdateCommand ();
244                                 da.UpdateCommand.Transaction = ts;
245
246                                 var ds1 = new DataSet ();
247                                 da.Fill (ds1, "data");
248
249                                 var table = ds1.Tables [0];
250                                 var row = table.NewRow ();
251                                 row ["id"] = 10;
252                                 row ["name"] = "Bart";
253                                 table.Rows.Add (row);
254
255                                 var ds2 = ds1.GetChanges ();
256                                 da.Update (ds2, "data");
257                                 Assert.IsFalse (ds2.HasErrors);
258                         }
259                 }
260 #endif
261
262 #endif
263
264                 class MyAdapter : DbDataAdapter
265                 {
266 #if ONLY_1_1
267                         protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
268                                                                                      StatementType statementType,
269                                                                                      DataTableMapping tableMapping)
270                         {
271                                 throw new NotImplementedException ();
272                         }
273
274                         protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
275                                                                                        StatementType statementType,
276                                                                                        DataTableMapping tableMapping)
277                         {
278                                 throw new NotImplementedException ();
279                         }
280
281                         protected override void OnRowUpdated (RowUpdatedEventArgs value)
282                         {
283                                 throw new NotImplementedException ();
284                         }
285
286                         protected override void OnRowUpdating (RowUpdatingEventArgs value)
287                         {
288                                 throw new NotImplementedException ();
289                         }
290 #endif
291
292 #if NET_2_0
293                         public new int AddToBatch (IDbCommand command)
294                         {
295                                 return base.AddToBatch (command);
296                         }
297
298                         public new void ClearBatch ()
299                         {
300                                 base.ClearBatch ();
301                         }
302
303                         public new void ExecuteBatch ()
304                         {
305                                 base.ClearBatch ();
306                         }
307
308                         public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
309                         {
310                                 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
311                         }
312
313                         public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
314                         {
315                                 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
316                         }
317
318                         public new void InitializeBatching ()
319                         {
320                                 base.InitializeBatching ();
321                         }
322
323                         public new void TerminateBatching ()
324                         {
325                                 base.TerminateBatching ();
326                         }
327 #endif
328                 }
329         }
330 }