[System*] Throw a PlatformNotSupported exception when using the managed networking...
[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                 [Test]
48                 public void UpdateBatchSize ()
49                 {
50                         MyAdapter da = new MyAdapter ();
51                         try {
52                                 da.UpdateBatchSize = 0;
53                                 Assert.Fail ("#A1");
54                         } catch (NotSupportedException ex) {
55                                 // Specified method is not supported
56                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
57                                 Assert.IsNull (ex.InnerException, "#A3");
58                                 Assert.IsNotNull (ex.Message, "#A4");
59                         }
60                         Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
61
62                         try {
63                                 da.UpdateBatchSize = int.MaxValue;
64                                 Assert.Fail ("#B1");
65                         } catch (NotSupportedException ex) {
66                                 // Specified method is not supported
67                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
68                                 Assert.IsNull (ex.InnerException, "#B3");
69                                 Assert.IsNotNull (ex.Message, "#B4");
70                         }
71                         Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
72
73                         da.UpdateBatchSize = 1;
74                         Assert.AreEqual (1, da.UpdateBatchSize, "#C");
75                 }
76
77                 [Test]
78                 public void UpdateBatchSize_Negative ()
79                 {
80                         MyAdapter da = new MyAdapter ();
81                         try {
82                                 da.UpdateBatchSize = -1;
83                                 Assert.Fail ("#1");
84                         } catch (NotSupportedException ex) {
85                                 // Specified method is not supported
86                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
87                                 Assert.IsNull (ex.InnerException, "#3");
88                                 Assert.IsNotNull (ex.Message, "#4");
89                         }
90                 }
91
92                 [Test]
93                 public void AddToBatch ()
94                 {
95                         MyAdapter da = new MyAdapter ();
96                         try {
97                                 da.AddToBatch (new SqlCommand ());
98                                 Assert.Fail ("#1");
99 #if FEATURE_NO_BSD_SOCKETS
100                         } catch (PlatformNotSupportedException) {
101 #else
102                         } catch (NotSupportedException ex) {
103                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
104                                 Assert.IsNull (ex.InnerException, "#3");
105                                 Assert.IsNotNull (ex.Message, "#4");
106 #endif
107                         }
108                 }
109
110                 [Test]
111                 public void ClearBatch ()
112                 {
113                         MyAdapter da = new MyAdapter ();
114                         try {
115                                 da.ClearBatch ();
116                                 Assert.Fail ("#1");
117                         } catch (NotSupportedException ex) {
118                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
119                                 Assert.IsNull (ex.InnerException, "#3");
120                                 Assert.IsNotNull (ex.Message, "#4");
121                         }
122                 }
123
124                 [Test]
125                 public void ExecuteBatch ()
126                 {
127                         MyAdapter da = new MyAdapter ();
128                         try {
129                                 da.ExecuteBatch ();
130                                 Assert.Fail ("#1");
131                         } catch (NotSupportedException ex) {
132                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
133                                 Assert.IsNull (ex.InnerException, "#3");
134                                 Assert.IsNotNull (ex.Message, "#4");
135                         }
136                 }
137
138                 [Test]
139                 public void GetBatchedParameter ()
140                 {
141                         MyAdapter da = new MyAdapter ();
142                         try {
143                                 da.GetBatchedParameter (1, 1);
144                                 Assert.Fail ("#1");
145                         } catch (NotSupportedException ex) {
146                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
147                                 Assert.IsNull (ex.InnerException, "#3");
148                                 Assert.IsNotNull (ex.Message, "#4");
149                         }
150                 }
151
152                 [Test]
153                 public void GetBatchedRecordsAffected ()
154                 {
155                         MyAdapter da = new MyAdapter ();
156                         int recordsAffected = 0;
157                         Exception error = null;
158
159                         Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
160                                 out recordsAffected, out error), "#1");
161                         Assert.AreEqual (1, recordsAffected, "#2");
162                         Assert.IsNull (error, "#3");
163                 }
164
165                 [Test]
166                 public void InitializeBatching ()
167                 {
168                         MyAdapter da = new MyAdapter ();
169                         try {
170                                 da.InitializeBatching ();
171                                 Assert.Fail ("#1");
172                         } catch (NotSupportedException ex) {
173                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
174                                 Assert.IsNull (ex.InnerException, "#3");
175                                 Assert.IsNotNull (ex.Message, "#4");
176                         }
177                 }
178
179                 [Test]
180                 public void TerminateBatching ()
181                 {
182                         MyAdapter da = new MyAdapter ();
183                         try {
184                                 da.TerminateBatching ();
185                                 Assert.Fail ("#1");
186                         } catch (NotSupportedException ex) {
187                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
188                                 Assert.IsNull (ex.InnerException, "#3");
189                                 Assert.IsNotNull (ex.Message, "#4");
190                         }
191                 }
192 #if !MOBILE     
193                 [Test]
194                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
195                 public void XimarinBugzillaBug853Test()
196                 {
197                         const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
198                         SqliteConnection dbConnection = new SqliteConnection(connectionString);
199                         dbConnection.Open();
200                         SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
201                         ClearTableEntry.ExecuteNonQuery();
202
203                         SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
204                         SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
205                         sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
206                         sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
207                         
208                         DataSet dataSet = new DataSet();
209
210                         sqliteDataAdapter.Fill(dataSet, "Primus");//reset
211
212                         DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
213                         rowToBeAdded["id"] = 123;
214                         rowToBeAdded["name"] = "Name";//not null primary key
215                         rowToBeAdded["value"] = 777;
216
217                         dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
218 sqliteDataAdapter.Update (dataSet, "Primus");
219
220                         //This would fail with NULL constraint violation in bug
221                         //report.  Because before the patch, it would create
222                         //a new record with all fields being null-- if the
223                         //exception rises, test fails
224                         sqliteDataAdapter.Update (dataSet, "Primus");
225
226                         dbConnection.Close();
227                         dbConnection = null;
228                 }
229
230                 [Test]
231                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
232                 public void UpdateResetRowErrorCorrectly ()
233                 {
234                         const string connectionString = "URI = file::memory:; Version = 3";
235                         using (var dbConnection = new SqliteConnection (connectionString)) {
236                                 dbConnection.Open ();
237
238                                 using (var cmd = dbConnection.CreateCommand ()) {
239                                         cmd.CommandText = "CREATE TABLE data (id PRIMARY KEY, name TEXT)";
240                                         cmd.ExecuteNonQuery ();
241                                 }
242
243
244                                 var ts = dbConnection.BeginTransaction ();
245                                 var da = new SqliteDataAdapter ("SELECT * FROM data", dbConnection);
246                                 var builder = new SqliteCommandBuilder (da);
247                                 da.UpdateCommand = builder.GetUpdateCommand ();
248                                 da.UpdateCommand.Transaction = ts;
249
250                                 var ds1 = new DataSet ();
251                                 da.Fill (ds1, "data");
252
253                                 var table = ds1.Tables [0];
254                                 var row = table.NewRow ();
255                                 row ["id"] = 10;
256                                 row ["name"] = "Bart";
257                                 table.Rows.Add (row);
258
259                                 var ds2 = ds1.GetChanges ();
260                                 da.Update (ds2, "data");
261                                 Assert.IsFalse (ds2.HasErrors);
262                         }
263                 }
264 #endif
265
266
267                 class MyAdapter : DbDataAdapter
268                 {
269
270                         public new int AddToBatch (IDbCommand command)
271                         {
272                                 return base.AddToBatch (command);
273                         }
274
275                         public new void ClearBatch ()
276                         {
277                                 base.ClearBatch ();
278                         }
279
280                         public new void ExecuteBatch ()
281                         {
282                                 base.ClearBatch ();
283                         }
284
285                         public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
286                         {
287                                 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
288                         }
289
290                         public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
291                         {
292                                 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
293                         }
294
295                         public new void InitializeBatching ()
296                         {
297                                 base.InitializeBatching ();
298                         }
299
300                         public new void TerminateBatching ()
301                         {
302                                 base.TerminateBatching ();
303                         }
304                 }
305         }
306 }