2009-11-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ObjectDataSourceViewTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.ObjectDataSourceView
3 //
4 // Author:
5 //      Chris Toshok (toshok@novell.com)
6 //      Konstantin Triger (kostat@mainsoft.com)
7 //      Yoni Klain (yonik@mainsoft.com)
8 //
9
10 //
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 // WARNING NOTES  : ALL NUNITWEB TESTS DOING UNLOAD BETWEEN TESTS FOR RELOAD
33 // OBJECT DATA SOURCE DEFAULT DATA 
34
35
36 #if NET_2_0
37
38 using NUnit.Framework;
39 using System;
40 using Sys = System;
41 using System.Configuration;
42 using System.Data.Common;
43 using System.IO;
44 using System.Globalization;
45 using System.Web;
46 using System.Web.UI;
47 using System.Web.UI.WebControls;
48 using System.Data;
49 using System.Collections;
50 using MonoTests.SystemWeb.Framework;
51 using MonoTests.stand_alone.WebHarness;
52 using System.Threading;
53
54 namespace MonoTests.System.Web.UI.WebControls
55 {
56         class ObjectViewPoker : ObjectDataSourceView
57         {
58                 public ObjectViewPoker (ObjectDataSource ds, string name, HttpContext context)
59                         : base (ds, name, context)
60                 {
61                         TrackViewState ();
62                 }
63
64                 public bool GetIsTrackingViewState ()
65                 {
66                         return base.IsTrackingViewState;
67                 }
68
69                 public object SaveToViewState ()
70                 {
71                         return SaveViewState ();
72                 }
73
74                 public void LoadFromViewState (object savedState)
75                 {
76                         LoadViewState (savedState);
77                 }
78
79                 public void DoOnDeleting (ObjectDataSourceMethodEventArgs e)
80                 {
81                         base.OnDeleting (e);
82                 }
83
84                 public void DoOnInserting (ObjectDataSourceMethodEventArgs e)
85                 {
86                         base.OnInserting (e);
87                 }
88
89                 public void DoOnFiltering (ObjectDataSourceFilteringEventArgs e)
90                 {
91                         base.OnFiltering (e);
92                 }
93
94                 public void DoOnObjectCreating (ObjectDataSourceEventArgs e)
95                 {
96                         base.OnObjectCreating (e);
97                 }
98
99                 public void DoOnObjectCreated (ObjectDataSourceEventArgs e)
100                 {
101                         base.OnObjectCreated (e);
102                 }
103
104                 public void DoOnObjectDisposing (ObjectDataSourceDisposingEventArgs e)
105                 {
106                         base.OnObjectDisposing (e);
107                 }
108
109                 public void DoOnSelecting (ObjectDataSourceSelectingEventArgs e)
110                 {
111                         base.OnSelecting (e);
112                 }
113
114                 public void DoOnUpdating (ObjectDataSourceMethodEventArgs e)
115                 {
116                         base.OnUpdating (e);
117                 }
118
119                 public void DoOnUpdated (ObjectDataSourceStatusEventArgs e)
120                 {
121                         base.OnUpdated (e);
122                 }
123
124                 public void DoOnDeleted (ObjectDataSourceStatusEventArgs e)
125                 {
126                         base.OnDeleted (e);
127                 }
128
129                 public void DoOnInserted (ObjectDataSourceStatusEventArgs e)
130                 {
131                         base.OnInserted (e);
132                 }
133
134                 public void DoOnSelected (ObjectDataSourceStatusEventArgs e)
135                 {
136                         base.OnSelected (e);
137                 }
138
139                 public int DoExecuteDelete (IDictionary keys, IDictionary oldValues)
140                 {
141                         return base.ExecuteDelete (keys, oldValues);
142                 }
143
144                 public void DoOnDataSourceViewChanged ()
145                 {
146                         base.OnDataSourceViewChanged (new EventArgs ());
147                 }
148
149         }
150
151         [TestFixture]
152         public class ObjectDataSourceViewTest
153         {
154
155                 [TestFixtureTearDown]
156                 public void TearDown ()
157                 {
158                         WebTest.Unload ();
159                 }
160
161                 [SetUp()]
162                 public void Setup () 
163                 {
164                         eventsCalled = null;
165                 }
166
167                 [Test]
168                 public void Defaults ()
169                 {
170                         ObjectDataSource ds = new ObjectDataSource ();
171                         ObjectViewPoker sql = new ObjectViewPoker (ds, "DefaultView", null);
172
173                         Assert.IsFalse (sql.CanDelete, "CanDelete");
174                         Assert.IsFalse (sql.CanInsert, "CanInsert");
175                         Assert.IsFalse (sql.CanPage, "CanPage");
176                         Assert.IsTrue (sql.CanSort, "CanSort");
177                         Assert.IsFalse (sql.CanUpdate, "CanUpdate");
178                         Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "ConflictDetection");
179                         Assert.IsFalse (sql.ConvertNullToDBNull, "ConvertNullToDBNull");
180                         Assert.AreEqual ("", sql.DataObjectTypeName, "DataObjectTypeName");
181                         Assert.AreEqual ("", sql.DeleteMethod, "DeleteMethod");
182                         Assert.IsNotNull (sql.DeleteParameters, "DeleteParameters");
183                         Assert.AreEqual (0, sql.DeleteParameters.Count, "DeleteParameters.Count");
184                         Assert.IsFalse (sql.EnablePaging, "EnablePaging");
185                         Assert.AreEqual ("", sql.InsertMethod, "InsertMethod");
186                         Assert.IsNotNull (sql.InsertParameters, "InsertParameters");
187                         Assert.AreEqual (0, sql.InsertParameters.Count, "InsertParameters.Count");
188                         Assert.AreEqual ("", sql.FilterExpression, "FilterExpression");
189                         Assert.IsNotNull (sql.FilterParameters, "FilterParameters");
190                         Assert.AreEqual (0, sql.FilterParameters.Count, "FilterParameters.Count");
191                         Assert.AreEqual ("maximumRows", sql.MaximumRowsParameterName, "MaximumRowsParameterName");
192                         Assert.AreEqual ("", sql.SelectCountMethod, "SelectCountMethod");
193                         Assert.AreEqual ("", sql.SelectMethod, "SelectMethod");
194                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "OldValuesParameterFormatString");
195                         Assert.IsNotNull (sql.SelectParameters, "SelectParameters");
196                         Assert.AreEqual (0, sql.SelectParameters.Count, "SelectParameters.Count");
197                         Assert.AreEqual ("", sql.SortParameterName, "SortParameterName");
198                         Assert.IsNotNull (sql.UpdateParameters, "UpdateParameters");
199                         Assert.AreEqual (0, sql.UpdateParameters.Count, "UpdateParameters.Count");
200                         Assert.AreEqual ("startRowIndex", sql.StartRowIndexParameterName, "StartRowIndexParameterName");
201                         Assert.AreEqual ("", sql.TypeName, "TypeName");
202                         Assert.AreEqual ("", sql.UpdateMethod, "UpdateMethod");
203                         Assert.AreEqual (true, sql.GetIsTrackingViewState (), "IsTrackingViewState");
204                         Assert.IsTrue (sql.CanRetrieveTotalRowCount, "CanRetrieveTotalRowCount");
205                 }
206
207                 [Test]
208                 public void DefaultsAssignProperties ()
209                 {
210
211                         ObjectDataSource ds = new ObjectDataSource ();
212                         ObjectViewPoker sql = new ObjectViewPoker (ds, "DefaultView", null);
213
214                         sql.ConflictDetection = ConflictOptions.CompareAllValues;
215                         Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "ConflictDetection");
216
217                         sql.ConvertNullToDBNull = true;
218                         Assert.IsTrue (sql.ConvertNullToDBNull, "ConvertNullToDBNull");
219
220                         sql.DataObjectTypeName = "test";
221                         Assert.AreEqual ("test", sql.DataObjectTypeName, "DataObjectTypeName");
222
223                         sql.DeleteMethod = "test";
224                         Assert.AreEqual ("test", sql.DeleteMethod, "DeleteMethod");
225
226                         sql.EnablePaging = true;
227                         Assert.IsTrue (sql.EnablePaging, "EnablePaging");
228
229                         sql.InsertMethod = "test";
230                         Assert.AreEqual ("test", sql.InsertMethod, "InsertMethod");
231
232                         sql.FilterExpression = "test";
233                         Assert.AreEqual ("test", sql.FilterExpression, "FilterExpression");
234
235                         sql.MaximumRowsParameterName = "test";
236                         Assert.AreEqual ("test", sql.MaximumRowsParameterName, "MaximumRowsParameterName");
237
238                         sql.SelectCountMethod = "test";
239                         Assert.AreEqual ("test", sql.SelectCountMethod, "SelectCountMethod");
240
241                         sql.SelectMethod = "test";
242                         Assert.AreEqual ("test", sql.SelectMethod, "SelectMethod");
243
244                         sql.OldValuesParameterFormatString = "test";
245                         Assert.AreEqual ("test", sql.OldValuesParameterFormatString, "OldValuesParameterFormatString");
246
247                         sql.StartRowIndexParameterName = "test";
248                         Assert.AreEqual ("test", sql.StartRowIndexParameterName, "StartRowIndexParameterName");
249
250                         sql.TypeName = "test";
251                         Assert.AreEqual ("test", sql.TypeName, "TypeName");
252
253                         sql.UpdateMethod = "test";
254                         Assert.AreEqual ("test", sql.UpdateMethod, "UpdateMethod");
255
256                         Assert.AreEqual ("DefaultView", sql.Name, "Name");
257
258                 }
259
260                 [Test]
261                 public void ViewStateSupport () 
262                 {
263                         ObjectDataSourceView view;
264                         MyDataSource ds = new MyDataSource ();
265
266                         ds.ID = "ObjectDataSource2";
267                         ds.TypeName = "MonoTests.System.Web.UI.WebControls.DataSourceObject";
268                         ds.SelectMethod = "Select";
269                         ds.SelectCountMethod = "SelectCount";
270
271                         view = (ObjectDataSourceView) ds.DoGetView ("DefaultView");
272                         ((IStateManager) view).TrackViewState ();
273
274                         Parameter p1 = new Parameter ("test", TypeCode.String);
275
276                         Assert.IsTrue (((IStateManager) view).IsTrackingViewState, "IsTrackingViewState");
277                         Assert.IsTrue (((IStateManager) view.FilterParameters).IsTrackingViewState, "FilterParameters.IsTrackingViewState");
278                         Assert.IsTrue (((IStateManager) view.SelectParameters).IsTrackingViewState, "SelecteParameters.IsTrackingViewState");
279                         Assert.IsFalse (((IStateManager) view.DeleteParameters).IsTrackingViewState, "DeleteParameters.IsTrackingViewState");
280                         Assert.IsFalse (((IStateManager) view.InsertParameters).IsTrackingViewState, "InsertParameters.IsTrackingViewState");
281                         Assert.IsFalse (((IStateManager) view.UpdateParameters).IsTrackingViewState, "UpdateParameters.IsTrackingViewState");
282
283                         object state = ((IStateManager) view).SaveViewState ();
284                         Assert.IsNull (state, "view ViewState not null");
285
286                         view.DeleteParameters.Add (p1);
287                         view.InsertParameters.Add (p1);
288                         //view.UpdateParameters.Add (p1);
289
290                         state = ((IStateManager) view).SaveViewState ();
291                         Assert.IsNull (state, "view ViewState not null");
292
293                         view.FilterParameters.Add (p1);
294                         //view.SelectParameters.Add (p1);
295
296                         state = ((IStateManager) view).SaveViewState ();
297                         Assert.IsNotNull (state, "view ViewState not null");
298
299                         state = ((IStateManager) view.FilterParameters).SaveViewState ();
300                         Assert.IsNotNull (state, "FilterParamenters ViewState not null");
301                         state = ((IStateManager) view.SelectParameters).SaveViewState ();
302                         Assert.IsNull (state, "SelectParameters ViewState not null");
303
304                         state = ((IStateManager) view.DeleteParameters).SaveViewState ();
305                         Assert.IsNotNull (state, "DeleteParameters ViewState not null");
306                         state = ((IStateManager) view.InsertParameters).SaveViewState ();
307                         Assert.IsNotNull (state, "InsertParameters ViewState not null");
308                         state = ((IStateManager) view.UpdateParameters).SaveViewState ();
309                         Assert.IsNull (state, "UpdateParameters ViewState not null");
310                 }
311
312                 [Test]
313                 public void ViewState ()
314                 {
315                         // Note :
316                         // IStateManager implementation allows public access to control state
317                         // Nothing added to viewstate
318
319                         ObjectDataSourceView view;
320                         MyDataSource ds = new MyDataSource ();
321
322                         ds.ID = "ObjectDataSource2";
323                         ds.TypeName = "MonoTests.System.Web.UI.WebControls.DataSourceObject";
324                         ds.SelectMethod = "Select";
325                         ds.SelectCountMethod = "SelectCount";
326
327                         view = (ObjectDataSourceView) ds.DoGetView ("DefaultView");
328                         ((IStateManager) view).TrackViewState ();
329
330                         view.ConflictDetection = ConflictOptions.CompareAllValues;
331                         view.ConvertNullToDBNull = true;
332                         view.DataObjectTypeName = "test";
333                         view.DeleteMethod = "test";
334                         view.EnablePaging = true;
335                         view.InsertMethod = "test";
336                         view.FilterExpression = "test";
337                         view.MaximumRowsParameterName = "test";
338                         view.SelectCountMethod = "test";
339                         view.SelectMethod = "test";
340                         view.OldValuesParameterFormatString = "test";
341                         view.StartRowIndexParameterName = "test";
342                         view.TypeName = "test";
343                         view.UpdateMethod = "test";
344
345                         object state = ((IStateManager) view).SaveViewState ();
346                         Assert.IsNull (state, "ViewState#1");
347
348                         ObjectDataSourceView copy = new ObjectDataSourceView (ds, "DefaultView", null);
349                         ((IStateManager) copy).LoadViewState (state);
350
351                 }
352
353                 [Test]
354                 public void CanDelete ()
355                 {
356                         ObjectDataSource ds = new ObjectDataSource ();
357                         ObjectViewPoker sql = new ObjectViewPoker (ds, "DefaultView", null);
358
359                         sql.DeleteMethod = "DeleteMethod";
360                         Assert.IsTrue (sql.CanDelete, "A1");
361
362                         sql.DeleteMethod = "";
363                         Assert.IsFalse (sql.CanDelete, "A2");
364
365                         sql.DeleteMethod = null;
366                         Assert.IsFalse (sql.CanDelete, "A3");
367                 }
368
369                 [Test]
370                 public void CanInsert ()
371                 {
372                         ObjectDataSource ds = new ObjectDataSource ();
373                         ObjectViewPoker sql = new ObjectViewPoker (ds, "DefaultView", null);
374
375                         sql.InsertMethod = "InsertMethod";
376                         Assert.IsTrue (sql.CanInsert, "A1");
377
378                         sql.InsertMethod = "";
379                         Assert.IsFalse (sql.CanInsert, "A2");
380
381                         sql.InsertMethod = null;
382                         Assert.IsFalse (sql.CanInsert, "A3");
383                 }
384
385                 [Test]
386                 public void CanUpdate ()
387                 {
388                         ObjectDataSource ds = new ObjectDataSource ();
389                         ObjectViewPoker sql = new ObjectViewPoker (ds, "DefaultView", null);
390
391                         sql.UpdateMethod = "UpdateMethod";
392                         Assert.IsTrue (sql.CanUpdate, "A1");
393
394                         sql.UpdateMethod = "";
395                         Assert.IsFalse (sql.CanUpdate, "A2");
396
397                         sql.UpdateMethod = null;
398                         Assert.IsFalse (sql.CanUpdate, "A3");
399                 }
400
401                 [Test]
402                 public void CanRetrieveTotalRowCount () 
403                 {
404                         ObjectDataSource ds = new ObjectDataSource ();
405                         ObjectDataSourceView view = new ObjectDataSourceView (ds, "DefaultView", null);
406
407                         Assert.IsFalse (view.CanPage, "CanPage#1");
408                         Assert.IsTrue (view.CanRetrieveTotalRowCount, "CanRetrieveTotalRowCount#1");
409
410                         view.EnablePaging = true;
411                         Assert.IsTrue (view.CanPage, "CanPage#2");
412                         Assert.IsFalse (view.CanRetrieveTotalRowCount, "CanRetrieveTotalRowCount#2");
413
414                         view.SelectCountMethod = "SelectCountMethod";
415                         Assert.IsTrue (view.CanPage, "CanPage#3");
416                         Assert.IsTrue (view.CanRetrieveTotalRowCount, "CanRetrieveTotalRowCount#3");
417
418                         view.EnablePaging = false;
419                         Assert.IsFalse (view.CanPage, "CanPage#4");
420                         Assert.IsTrue (view.CanRetrieveTotalRowCount, "CanRetrieveTotalRowCount#4");
421                 }
422
423                 [Test]
424                 public void OldValuesParameterFormatString ()
425                 {
426                         ObjectDataSource ds = new ObjectDataSource ();
427
428                         Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A1");
429
430                         ds.OldValuesParameterFormatString = "hi {0}";
431
432                         ObjectViewPoker sql = new ObjectViewPoker (ds, "DefaultView", null);
433
434                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A2");
435
436                         ds.OldValuesParameterFormatString = "hi {0}";
437
438                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A3");
439
440                         ds.OldValuesParameterFormatString = "{0}";
441                         sql.OldValuesParameterFormatString = "hi {0}";
442
443                         Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A4");
444                 }
445
446                 [Test]
447                 [Category("NunitWeb")]
448                 public void DeleteMethod ()
449                 {
450                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (delete));
451                         string html = t.Run ();
452                         Assert.AreEqual (-1, html.IndexOf("Yonik"), "ObjectDataSourceViewDelete");
453                 }
454
455                 public static void delete (Page p)
456                 {
457                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
458                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
459                         ObjectDataSourceView view;
460                         GridView grid = new GridView ();
461                         MyDataSource ds = new MyDataSource ();
462                         ds.ID = "ObjectDataSource2";
463
464                         ds.TypeName = "MonoTests.System.Web.UI.WebControls.DataSourceObject";
465                         ds.SelectMethod = "Select";
466                         ds.DeleteMethod = "Delete";
467                         ds.InsertMethod = "Insert";
468                         ds.UpdateMethod = "Update";
469                         Parameter p1 = new Parameter ("ID", TypeCode.String);
470                         Parameter p2 = new Parameter ("FName", TypeCode.String);
471                         Parameter p3 = new Parameter ("LName", TypeCode.String);
472                         ds.DeleteParameters.Add (p1);
473                         ds.DeleteParameters.Add (p2);
474                         ds.DeleteParameters.Add (p3);
475                         grid.ID = "Grid";
476                         grid.DataKeyNames = new string[] { "ID", "FName", "LName" };
477                         grid.DataSourceID = "ObjectDataSource2";
478                         p.Form.Controls.Add (lcb);
479                         p.Form.Controls.Add (ds);
480                         p.Form.Controls.Add (grid);
481                         p.Form.Controls.Add (lce);
482                         view = (ObjectDataSourceView) ds.DoGetView ("DefaultView");
483                         view.Deleting += new ObjectDataSourceMethodEventHandler (Event);
484
485                         DataSourceObject.InitDS ();
486
487                         Hashtable table = new Hashtable ();
488                         table.Add ("ID", "1001");
489                         table.Add ("FName", "Yonik");
490                         table.Add ("LName", "Laim");
491                         view.Delete (table, null);
492                         Assert.AreEqual (true, view.CanDelete, "CanDelete");
493                         Assert.AreEqual ("Delete", view.DeleteMethod, "DeleteMethod");
494                         Assert.AreEqual (3, view.DeleteParameters.Count, "DeleteParameters.Count");
495                         Assert.AreEqual ("ID", view.DeleteParameters[0].Name, "DeleteParametersName#1");
496                         Assert.AreEqual ("FName", view.DeleteParameters[1].Name, "DeleteParametersName#2");
497                         Assert.AreEqual ("LName", view.DeleteParameters[2].Name, "DeleteParametersName#3");
498                         ObjectDataSourceViewTest.Eventassert ("Delete event has not fired");
499                 }
500
501
502                 [Test]
503                 [Category ("NunitWeb")]
504                 public void SelectMethod ()
505                 {
506                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (select));
507                         string html = t.Run ();
508                         string origin = @"<div>
509                                                 <table cellspacing=""0"" rules=""all"" border=""1"" id=""Grid"" style=""border-collapse:collapse;"">
510                                                         <tr>
511                                                                 <th scope=""col"">ID</th><th scope=""col"">FName</th><th scope=""col"">LName</th>
512                                                         </tr><tr>
513                                                                 <td>1001</td><td>Mahesh</td><td>Chand</td>
514                                                         </tr>
515                                                 </table>
516                                         </div>";
517                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (html), "ObjectDataSourceViewSelect");
518                 }
519
520                 public static void select (Page p)
521                 {
522                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
523                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
524                         ObjectDataSourceView view;
525                         GridView grid = new GridView ();
526                         MyDataSource ds = new MyDataSource ();
527                         ds.ID = "ObjectDataSource2";
528                         ds.TypeName = "MonoTests.System.Web.UI.WebControls.DataSourceObject";
529                         ds.SelectMethod = "Select";
530
531                         grid.ID = "Grid";
532                         grid.DataKeyNames = new string[] { "ID" };
533                         grid.DataSourceID = "ObjectDataSource2";
534                         p.Form.Controls.Add (lcb);
535                         p.Form.Controls.Add (ds);
536                         p.Form.Controls.Add (grid);
537                         p.Form.Controls.Add (lce);
538                         view = (ObjectDataSourceView) ds.DoGetView ("DefaultView");
539                         view.Selecting += new ObjectDataSourceSelectingEventHandler (view_Selecting);
540
541                         DataSourceObject.InitDS ();
542
543                         DataView view1 = (DataView) view.Select (new DataSourceSelectArguments ());
544                         Assert.AreEqual (1, view1.Count, "SelectedRowsCount");
545                         Assert.AreEqual (1001, view1[0].Row["ID"], "SelectedRowsValue#1");
546                         Assert.AreEqual ("Mahesh", view1[0].Row["FName"], "SelectedRowsValue#2");
547                         Assert.AreEqual ("Chand", view1[0].Row["LName"], "SelectedRowsValue#3");
548                         ObjectDataSourceViewTest.Eventassert ("Select event has not fired");
549                 }
550
551                 [Test]
552                 [Category ("NunitWeb")]
553                 public void SelectCountMethod ()
554                 {
555                         // This method will render grid view with paging 
556                         // Note : ObjectDataSource will return page counter 5 hard coded
557                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (selectcount));
558                         string html = t.Run ();
559                         string origin = @"<div>
560                                                 <table cellspacing=""0"" rules=""all"" border=""1"" id=""Grid"" style=""border-collapse:collapse;"">
561                                                         <tr>
562                                                                 <th scope=""col"">ID</th><th scope=""col"">FName</th><th scope=""col"">LName</th>
563                                                         </tr><tr>
564                                                                 <td>1001</td><td>Mahesh</td><td>Chand</td>
565                                                         </tr><tr>
566                                                                 <td colspan=""3""><table border=""0"">
567                                                                         <tr>
568                                                                                 <td><span>1</span></td><td><a href=""javascript:__doPostBack('Grid','Page$2')"">2</a></td><td><a href=""javascript:__doPostBack('Grid','Page$3')"">3</a></td><td><a href=""javascript:__doPostBack('Grid','Page$4')"">4</a></td><td><a href=""javascript:__doPostBack('Grid','Page$5')"">5</a></td>
569                                                                         </tr>
570                                                                 </table></td>
571                                                         </tr>
572                                                 </table>
573                                         </div>";
574                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (html), "ObjectDataSourceViewSelectCount");
575                 }
576
577                 public static void selectcount (Page p)
578                 {
579                         // This method will render grid view with paging 
580                         // Note : ObjectDataSource will return page counter 5 hard coded
581
582                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
583                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
584                         ObjectDataSourceView view;
585                         GridView grid = new GridView ();
586                         MyDataSource ds = new MyDataSource ();
587                         ds.ID = "ObjectDataSource2";
588                         ds.EnablePaging = true;
589                         ds.TypeName = "MonoTests.System.Web.UI.WebControls.DataSourceObject";
590                         ds.SelectMethod = "Select";
591                         ds.SelectCountMethod = "SelectCount";
592
593
594                         grid.ID = "Grid";
595                         grid.DataKeyNames = new string[] { "ID" };
596                         grid.DataSourceID = "ObjectDataSource2";
597                         grid.AllowPaging = true;
598                         grid.PageSize = 1;
599
600                         p.Form.Controls.Add (lcb);
601                         p.Form.Controls.Add (ds);
602                         p.Form.Controls.Add (grid);
603                         p.Form.Controls.Add (lce);
604                         view = (ObjectDataSourceView) ds.DoGetView ("DefaultView");
605                         Assert.IsTrue (view.CanRetrieveTotalRowCount, "CanRetrieveTotalRowCount");
606                 }
607
608
609                 [Test]
610                 [Category ("NunitWeb")]
611                 public void InsertMethod ()
612                 {
613                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (insert));
614                         string html = t.Run ();
615                         string origin = @"<div>
616                                                 <table cellspacing=""0"" rules=""all"" border=""1"" id=""Grid"" style=""border-collapse:collapse;"">
617                                                         <tr>
618                                                                 <th scope=""col"">ID</th><th scope=""col"">FName</th><th scope=""col"">LName</th>
619                                                         </tr><tr>
620                                                                 <td>1001</td><td>Mahesh</td><td>Chand</td>
621                                                         </tr><tr>
622                                                                 <td>1000</td><td>Yonik</td><td>Laim</td>
623                                                         </tr>
624                                                 </table>
625                                         </div>";
626                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (html), "ObjectDataSourceViewInsert");
627                 }
628
629                 public static void insert (Page p)
630                 {
631                         DataSourceObject.InitDS ();
632
633                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
634                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
635                         ObjectDataSourceView view;
636                         GridView grid = new GridView ();
637                         MyDataSource ds = new MyDataSource ();
638                         ds.ID = "ObjectDataSource1";
639                         ds.TypeName = "MonoTests.System.Web.UI.WebControls.DataSourceObject";
640                         ds.SelectMethod = "Select";
641                         ds.InsertMethod = "Insert";
642
643
644                         Parameter p1 = new Parameter ("ID", TypeCode.String);
645                         Parameter p2 = new Parameter ("FName", TypeCode.String);
646                         Parameter p3 = new Parameter ("LName", TypeCode.String);
647
648                         ds.InsertParameters.Add (p1);
649                         ds.InsertParameters.Add (p2);
650                         ds.InsertParameters.Add (p3);
651
652                         grid.ID = "Grid";
653                         grid.DataSourceID = "ObjectDataSource1";
654                         p.Form.Controls.Add (lcb);
655                         p.Form.Controls.Add (ds);
656                         p.Form.Controls.Add (grid);
657                         p.Form.Controls.Add (lce);
658                         view = (ObjectDataSourceView) ds.DoGetView ("DefaultView");
659                         view.Inserting += new ObjectDataSourceMethodEventHandler (Event);
660
661                         Hashtable table = new Hashtable ();
662                         table.Add ("ID", "1000");
663                         table.Add ("FName", "Yonik");
664                         table.Add ("LName", "Laim");
665                         view.Insert (table);
666                         Assert.AreEqual (true, view.CanInsert, "CanInsert");
667                         Assert.AreEqual ("Insert", view.InsertMethod, "InsertMethod");
668                         Assert.AreEqual (3, view.InsertParameters.Count, "InsertParameters.Count");
669                         Assert.AreEqual ("ID", view.InsertParameters[0].Name, "InsertParametersName#1");
670                         Assert.AreEqual ("FName", view.InsertParameters[1].Name, "InsertParametersName#2");
671                         Assert.AreEqual ("LName", view.InsertParameters[2].Name, "InsertParametersName#3");
672                         ObjectDataSourceViewTest.Eventassert ("Insert event has not fired");
673                 }
674
675                 [Test]
676                 [Category ("NunitWeb")]
677                 public void UpdateMethod ()
678                 {
679                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (update));
680                         string html = t.Run ();
681                         string origin = @"<div>
682                                                 <table cellspacing=""0"" rules=""all"" border=""1"" id=""Grid"" style=""border-collapse:collapse;"">
683                                                         <tr>
684                                                                 <th scope=""col"">ID</th><th scope=""col"">FName</th><th scope=""col"">LName</th>
685                                                         </tr><tr>
686                                                                 <td>1001</td><td>Yonik</td><td>Laim</td>
687                                                         </tr>
688                                                 </table>
689                                         </div>";
690                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (html), "ObjectDataSourceViewUpdate");
691                 }
692
693                 public static void update (Page p)
694                 {
695                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
696                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
697                         ObjectDataSourceView view;
698                         GridView grid = new GridView ();
699                         MyDataSource ds = new MyDataSource ();
700                         ds.ID = "ObjectDataSource1";
701                         ds.TypeName = "MonoTests.System.Web.UI.WebControls.DataSourceObject";
702                         
703                         ds.SelectMethod = "Select";
704                         ds.DeleteMethod = "Delete";
705                         ds.InsertMethod = "Insert";
706                         ds.UpdateMethod = "Update";
707
708                         Parameter p1 = new Parameter ("ID", TypeCode.String);
709                         Parameter p2 = new Parameter ("FName", TypeCode.String);
710                         Parameter p3 = new Parameter ("LName", TypeCode.String);
711
712                         ds.UpdateParameters.Add (p1);
713                         ds.UpdateParameters.Add (p2);
714                         ds.UpdateParameters.Add (p3);
715
716                         grid.ID = "Grid";
717                         grid.DataSourceID = "ObjectDataSource1";
718                         p.Form.Controls.Add (lcb);
719                         p.Form.Controls.Add (ds);
720                         p.Form.Controls.Add (grid);
721                         p.Form.Controls.Add (lce);
722                         view = (ObjectDataSourceView) ds.DoGetView ("defaultView");
723                         view.Updating += new ObjectDataSourceMethodEventHandler (Event);
724
725                         DataSourceObject.InitDS ();
726
727                         Hashtable table = new Hashtable ();
728                         table.Add ("ID", "1001");
729                         table.Add ("FName", "Yonik");
730                         table.Add ("LName", "Laim");
731                         view.Update (null, table, null);
732                         Assert.AreEqual (true, view.CanUpdate, "CanUpdate");
733                         Assert.AreEqual ("Update", view.UpdateMethod, "UpdateMethod");
734                         Assert.AreEqual (3, view.UpdateParameters.Count, "UpdateParameters.Count");
735                         Assert.AreEqual ("ID", view.UpdateParameters[0].Name, "UpdateParametersName#1");
736                         Assert.AreEqual ("FName", view.UpdateParameters[1].Name, "UpdateParametersName#2");
737                         Assert.AreEqual ("LName", view.UpdateParameters[2].Name, "UpdateParametersName#3");
738                         ObjectDataSourceViewTest.Eventassert ("Update event has not fired");
739                 }
740
741
742                 //Events
743                 [Test]
744                 public void UpdateEvent ()
745                 {
746                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
747                         view.Updating += new ObjectDataSourceMethodEventHandler (Event);
748                         view.DoOnUpdating (new ObjectDataSourceMethodEventArgs (null));
749                         Eventassert ("UpdateEvent");
750                         view.Updated += new ObjectDataSourceStatusEventHandler (view_Status);
751                         view.DoOnUpdated (new ObjectDataSourceStatusEventArgs (null, null));
752                         Eventassert ("UpdateEvent");
753                 }
754
755                 [Test]
756                 public void SelectEvent ()
757                 {
758                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
759                         view.Selecting += new ObjectDataSourceSelectingEventHandler (view_Selecting);
760                         view.DoOnSelecting (new ObjectDataSourceSelectingEventArgs (null, new DataSourceSelectArguments (), false));
761                         Eventassert ("SelectingEvent");
762                         view.Selected += new ObjectDataSourceStatusEventHandler (view_Status);
763                         view.DoOnSelected (new ObjectDataSourceStatusEventArgs (null, null));
764                         Eventassert ("SelectedEvent");
765                 }
766
767                 [Test]
768                 public void InsertEvent ()
769                 {
770                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
771                         view.Inserting += new ObjectDataSourceMethodEventHandler (Event);
772                         view.DoOnInserting (new ObjectDataSourceMethodEventArgs (null));
773                         Eventassert ("InsertingEvent");
774                         view.Inserted += new ObjectDataSourceStatusEventHandler (view_Status);
775                         view.DoOnInserted (new ObjectDataSourceStatusEventArgs (null, null));
776                         Eventassert ("InsertedEvent");
777                 }
778
779                 [Test]
780                 public void DeleteEvent ()
781                 {
782                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
783                         view.Deleting += new ObjectDataSourceMethodEventHandler (Event);
784                         view.DoOnDeleting (new ObjectDataSourceMethodEventArgs (null));
785                         Eventassert ("DeletingEvent");
786                         view.Deleted += new ObjectDataSourceStatusEventHandler (view_Status);
787                         view.DoOnDeleted (new ObjectDataSourceStatusEventArgs (null, null));
788                         Eventassert ("DeletedEvent");
789                 }
790
791                 [Test]
792                 public void FilterEvent ()
793                 {
794                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
795                         view.Filtering += new ObjectDataSourceFilteringEventHandler (view_Filtering);
796                         view.DoOnFiltering (new ObjectDataSourceFilteringEventArgs (null));
797                         Eventassert ("FilterEvent");
798                 }
799
800                 [Test]
801                 public void ObjectCreatingEvent ()
802                 {
803                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
804                         view.ObjectCreating += new ObjectDataSourceObjectEventHandler (view_ObjectCreate);
805                         view.DoOnObjectCreating (new ObjectDataSourceEventArgs (null));
806                         Eventassert ("ObjectCreatingEvent");
807                         view.ObjectCreated += new ObjectDataSourceObjectEventHandler (view_ObjectCreate);
808                         view.DoOnObjectCreated (new ObjectDataSourceEventArgs (null));
809                         Eventassert ("ObjectCreatedEvent");
810                         view.ObjectDisposing += new ObjectDataSourceDisposingEventHandler (view_ObjectDisposing);
811                 }
812
813                 [Test]
814                 public void ObjectDisposing ()
815                 {
816                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
817                         view.ObjectDisposing += new ObjectDataSourceDisposingEventHandler (view_ObjectDisposing);
818                         view.DoOnObjectDisposing (new ObjectDataSourceDisposingEventArgs (null));
819                         Eventassert ("ObjectDisposing");
820                 }
821
822                 IEnumerable returnedData;
823                 void SelectCallback (IEnumerable data)
824                 {
825                         returnedData = data;
826                 }
827                 
828                 [Test] // bug #471767
829                 public void SelectReturnsObjectArray ()
830                 {
831                         ObjectDataSource ds = new ObjectDataSource ();
832                         ds.TypeName=typeof(DataSourceObject).AssemblyQualifiedName;
833                         ds.SelectMethod="SelectObject";
834
835                         DataSourceView dsv = ((IDataSource)ds).GetView (String.Empty);
836                         dsv.Select (DataSourceSelectArguments.Empty, new DataSourceViewSelectCallback (SelectCallback));
837                         Assert.IsTrue (returnedData != null, "#A1");
838                         Assert.AreEqual (typeof (object[]), returnedData.GetType (), "#A2");
839
840                         object[] data = returnedData as object[];
841                         Assert.AreEqual (1, data.Length, "#A3");
842                         Assert.AreEqual (typeof (MyCustomDataObject), data [0].GetType (), "#A4");
843                 }
844                 
845                 enum InitViewType
846                 {
847                         MatchParamsToValues,
848                         MatchParamsToOldValues,
849                         DontMatchParams,
850                 }
851
852                 public class DummyDataSourceObject
853                 {
854                         public static IEnumerable Select (string filter) 
855                         {
856                                 if (eventsCalled == null) {
857                                         eventsCalled = new ArrayList ();
858                                 }
859                                 eventsCalled.Add (String.Format ("Select(filter = {0})", filter));
860                                 return new string [] { "one", "two", "three" };
861                         }
862
863                         public static int Update (string ID) 
864                         {
865                                 if (eventsCalled == null) {
866                                         eventsCalled = new ArrayList ();
867                                 }
868                                 eventsCalled.Add (String.Format ("Update(ID = {0})", ID));
869                                 return 1;
870                         }
871
872                         public static int Update (string ID, string oldvalue_ID) 
873                         {
874                                 if (eventsCalled == null) {
875                                         eventsCalled = new ArrayList ();
876                                 }
877                                 eventsCalled.Add (String.Format ("Update(ID = {0}, oldvalue_ID = {1})", ID, oldvalue_ID));
878                                 return 1;
879                         }
880
881                         public static int UpdateOther (string ID, string OtherValue, string oldvalue_ID) 
882                         {
883                                 if (eventsCalled == null) {
884                                         eventsCalled = new ArrayList ();
885                                 }
886                                 eventsCalled.Add (String.Format ("UpdateOther(ID = {0}, OtherValue = {1}, oldvalue_ID = {2})", ID, OtherValue, oldvalue_ID));
887                                 return 1;
888                         }
889
890                         public static int Insert (string ID) 
891                         {
892                                 if (eventsCalled == null) {
893                                         eventsCalled = new ArrayList ();
894                                 }
895                                 eventsCalled.Add (String.Format ("Insert(ID = {0})", ID));
896                                 return 1;
897                         }
898
899                         public static int Insert (string ID, string oldvalue_ID) 
900                         {
901                                 if (eventsCalled == null) {
902                                         eventsCalled = new ArrayList ();
903                                 }
904                                 eventsCalled.Add (String.Format ("Insert(ID = {0}, oldvalue_ID = {1})", ID, oldvalue_ID));
905                                 return 1;
906                         }
907
908                         public static int InsertOther (string ID, string OtherValue) 
909                         {
910                                 if (eventsCalled == null) {
911                                         eventsCalled = new ArrayList ();
912                                 }
913                                 eventsCalled.Add (String.Format ("InsertOther(ID = {0}, OtherValue = {1})", ID, OtherValue));
914                                 return 1;
915                         }
916
917                         public static int Delete (string ID, string oldvalue_ID) 
918                         {
919                                 if (eventsCalled == null) {
920                                         eventsCalled = new ArrayList ();
921                                 }
922                                 eventsCalled.Add (String.Format ("Delete(ID = {0}, oldvalue_ID = {1})", ID, oldvalue_ID));
923                                 return 1;
924                         }
925
926                         public static int Delete (string oldvalue_ID) 
927                         {
928                                 if (eventsCalled == null) {
929                                         eventsCalled = new ArrayList ();
930                                 }
931                                 eventsCalled.Add (String.Format ("Delete(oldvalue_ID = {0})", oldvalue_ID));
932                                 return 1;
933                         }
934
935                         public static int DeleteOther (string oldvalue_ID, string OtherValue) 
936                         {
937                                 if (eventsCalled == null) {
938                                         eventsCalled = new ArrayList ();
939                                 }
940                                 eventsCalled.Add (String.Format ("DeleteOther(oldvalue_ID = {0}, OtherValue = {1})", oldvalue_ID, OtherValue));
941                                 return 1;
942                         }
943                 }
944
945                 public class AlwaysChangingParameter : Parameter
946                 {
947                         int evaluateCount;
948
949                         public AlwaysChangingParameter (string name, TypeCode type, string defaultValue)
950                                 : base (name, type, defaultValue) 
951                         {
952                                 evaluateCount = 0;
953                         }
954
955                         protected override object Evaluate (HttpContext context, Control control) 
956                         {
957                                 evaluateCount++;
958                                 return String.Format ("{0}{1}", DefaultValue, evaluateCount);
959                         }
960                 }
961
962                 #region help_results
963                 class eventAssert
964                 {
965                         private static int _testcounter;
966                         private static bool _eventChecker;
967                         private eventAssert ()
968                         {
969                                 _testcounter = 0;
970                         }
971
972                         public static bool eventChecker
973                         {
974                                 get
975                                 {
976                                         throw new NotImplementedException ();
977                                 }
978                                 set
979                                 {
980                                         _eventChecker = value;
981                                 }
982                         }
983
984                         static private void testAdded ()
985                         {
986                                 _testcounter++;
987                                 _eventChecker = false;
988                         }
989
990                         public static void IsTrue (string msg)
991                         {
992                                 Assert.IsTrue (_eventChecker, msg + "#" + _testcounter);
993                                 testAdded ();
994                         
995                         }
996
997                         public static void IsFalse (string msg)
998                         {
999                                 Assert.IsFalse (_eventChecker, msg + "#" + _testcounter);
1000                                 testAdded ();
1001                         }
1002                 }
1003                 #endregion
1004
1005                 [Test]
1006                 public void ObjectDataSourceView_DataSourceViewChanged ()
1007                 {
1008                         ObjectDataSource ds = new ObjectDataSource ();
1009                         ObjectViewPoker sql = new ObjectViewPoker (ds, "DefaultView", null);
1010                         sql.DataSourceViewChanged += new EventHandler (sql_DataSourceViewChanged);
1011                         
1012                         sql.DoOnDataSourceViewChanged ();
1013                         eventAssert.IsTrue ("DataSourceViewChanged");
1014
1015                         sql.ConflictDetection = ConflictOptions.CompareAllValues;
1016                         eventAssert.IsTrue ("DataSourceViewChanged");
1017
1018                         sql.ConvertNullToDBNull = true;
1019                         eventAssert.IsFalse ("DataSourceViewChanged");
1020
1021                         sql.DataObjectTypeName = "test";
1022                         eventAssert.IsTrue ("DataSourceViewChanged");
1023
1024                         sql.DeleteMethod = "test";
1025                         eventAssert.IsFalse ("DataSourceViewChanged");
1026
1027                         sql.EnablePaging = true;
1028                         eventAssert.IsTrue ("DataSourceViewChanged");
1029
1030                         sql.InsertMethod = "test";
1031                         eventAssert.IsFalse ("DataSourceViewChanged");
1032
1033                         sql.FilterExpression = "test";
1034                         eventAssert.IsTrue ("DataSourceViewChanged");
1035
1036                         sql.MaximumRowsParameterName = "test";
1037                         eventAssert.IsTrue ("DataSourceViewChanged");
1038
1039                         sql.SelectCountMethod = "test";
1040                         eventAssert.IsTrue ("DataSourceViewChanged");
1041
1042                         sql.SelectMethod = "test";
1043                         eventAssert.IsTrue ("DataSourceViewChanged");
1044
1045                         sql.OldValuesParameterFormatString = "test";
1046                         eventAssert.IsTrue ("DataSourceViewChanged");
1047
1048                         sql.StartRowIndexParameterName = "test";
1049                         eventAssert.IsTrue ("DataSourceViewChanged");
1050
1051                         sql.TypeName = "test";
1052                         eventAssert.IsTrue ("DataSourceViewChanged");
1053
1054                         sql.UpdateMethod = "test";
1055                         eventAssert.IsFalse ("DataSourceViewChanged");
1056                 }
1057
1058                 void sql_DataSourceViewChanged (object sender, EventArgs e)
1059                 {
1060                         eventAssert.eventChecker = true;
1061                 }
1062
1063                 [Test]
1064                 public void SelectCountMethod_DataSourceViewChanged ()
1065                 {
1066                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1067                         view.DataSourceViewChanged += new EventHandler (view_DataSourceViewChanged);
1068
1069                         Assert.AreEqual ("", view.SelectCountMethod);
1070                         view.SelectCountMethod = null;
1071                         Assert.AreEqual (1, eventsCalled.Count);
1072                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [0]);
1073                         Assert.AreEqual ("", view.SelectCountMethod);
1074
1075                         view.SelectCountMethod = null;
1076                         Assert.AreEqual (2, eventsCalled.Count);
1077                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [1]);
1078                         Assert.AreEqual ("", view.SelectCountMethod);
1079
1080                         view.SelectCountMethod = "";
1081                         Assert.AreEqual (2, eventsCalled.Count);
1082                 }
1083
1084                 [Test]
1085                 public void SelectMethod_DataSourceViewChanged2 ()
1086                 {
1087                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1088                         view.DataSourceViewChanged += new EventHandler (view_DataSourceViewChanged);
1089
1090                         Assert.AreEqual ("", view.SelectMethod);
1091                         view.SelectMethod = null;
1092                         Assert.AreEqual (1, eventsCalled.Count);
1093                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [0]);
1094                         Assert.AreEqual ("", view.SelectMethod);
1095
1096                         view.SelectMethod = null;
1097                         Assert.AreEqual (2, eventsCalled.Count);
1098                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [1]);
1099                         Assert.AreEqual ("", view.SelectMethod);
1100
1101                         view.SelectMethod = "";
1102                         Assert.AreEqual (2, eventsCalled.Count);
1103                 }
1104
1105                 [Test]
1106                 public void SelectMethod_DataSourceViewChanged1 ()
1107                 {
1108                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1109                         view.DataSourceViewChanged+=new EventHandler(view_DataSourceViewChanged);
1110
1111                         view.SelectMethod = "select_1";
1112                         Assert.AreEqual (1, eventsCalled.Count);
1113                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [0]);
1114
1115                         view.SelectMethod = "select_2";
1116                         Assert.AreEqual (2, eventsCalled.Count);
1117                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [1]);
1118
1119                         view.SelectMethod = "select_2";
1120                         Assert.AreEqual (2, eventsCalled.Count);
1121
1122                         view.SelectCountMethod = "selectCount_1";
1123                         Assert.AreEqual (3, eventsCalled.Count);
1124                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2]);
1125
1126                         view.SelectCountMethod = "selectCount_2";
1127                         Assert.AreEqual (4, eventsCalled.Count);
1128                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [3]);
1129
1130                         view.SelectCountMethod = "selectCount_2";
1131                         Assert.AreEqual (4, eventsCalled.Count);
1132                 }
1133
1134                 private static void InitializeView (ObjectViewPoker view, InitViewType initType, out Hashtable keys, out Hashtable old_value, out Hashtable new_value) 
1135                 {
1136                         view.TypeName = typeof (DummyDataSourceObject).AssemblyQualifiedName;
1137                         view.OldValuesParameterFormatString = "oldvalue_{0}";
1138                         view.SelectMethod = "Select";
1139                         if (initType == InitViewType.DontMatchParams) {
1140                                 view.UpdateMethod = "UpdateOther";
1141                                 view.InsertMethod = "InsertOther";
1142                                 view.DeleteMethod = "DeleteOther";
1143                         }
1144                         else {
1145                                 view.UpdateMethod = "Update";
1146                                 view.InsertMethod = "Insert";
1147                                 view.DeleteMethod = "Delete";
1148                         }
1149
1150                         Parameter selectParameter = null;
1151                         Parameter insertParameter = null;
1152                         Parameter updateParameter = null;
1153                         Parameter deleteParameter = null;
1154
1155                         selectParameter = new AlwaysChangingParameter ("filter", TypeCode.String, "p_ValueSelect");
1156                         view.SelectParameters.Add (selectParameter);
1157
1158                         switch (initType) {
1159                         case InitViewType.MatchParamsToOldValues:
1160                                 insertParameter = new AlwaysChangingParameter ("oldvalue_ID", TypeCode.String, "p_OldValueInsert");
1161                                 view.InsertParameters.Add (insertParameter);
1162                                 updateParameter = new AlwaysChangingParameter ("oldvalue_ID", TypeCode.String, "p_OldValueUpdate");
1163                                 view.UpdateParameters.Add (updateParameter);
1164                                 deleteParameter = new AlwaysChangingParameter ("oldvalue_ID", TypeCode.String, "p_OldValueDelete");
1165                                 view.DeleteParameters.Add (deleteParameter);
1166                                 break;
1167
1168                         case InitViewType.MatchParamsToValues:
1169                                 insertParameter = new AlwaysChangingParameter ("ID", TypeCode.String, "p_ValueInsert");
1170                                 view.InsertParameters.Add (insertParameter);
1171                                 updateParameter = new AlwaysChangingParameter ("ID", TypeCode.String, "p_ValueUpdate");
1172                                 view.UpdateParameters.Add (updateParameter);
1173                                 deleteParameter = new AlwaysChangingParameter ("ID", TypeCode.String, "p_ValueDelete");
1174                                 view.DeleteParameters.Add (deleteParameter);
1175                                 break;
1176
1177                         case InitViewType.DontMatchParams:
1178                                 insertParameter = new AlwaysChangingParameter ("OtherValue", TypeCode.String, "p_OtherValueInsert");
1179                                 view.InsertParameters.Add (insertParameter);
1180                                 updateParameter = new AlwaysChangingParameter ("OtherValue", TypeCode.String, "p_OtherValueUpdate");
1181                                 view.UpdateParameters.Add (updateParameter);
1182                                 deleteParameter = new AlwaysChangingParameter ("OtherValue", TypeCode.String, "p_OtherValueDelete");
1183                                 view.DeleteParameters.Add (deleteParameter);
1184                                 break;
1185                         }
1186
1187                         view.SelectParameters.ParametersChanged += new EventHandler (SelectParameters_ParametersChanged);
1188                         view.InsertParameters.ParametersChanged += new EventHandler (InsertParameters_ParametersChanged);
1189                         view.UpdateParameters.ParametersChanged += new EventHandler (UpdateParameters_ParametersChanged);
1190                         view.DeleteParameters.ParametersChanged += new EventHandler (DeleteParameters_ParametersChanged);
1191
1192                         keys = new Hashtable ();
1193                         keys.Add ("ID", "k_1001");
1194
1195                         old_value = new Hashtable ();
1196                         old_value.Add ("ID", "ov_1001");
1197
1198                         new_value = new Hashtable ();
1199                         new_value.Add ("ID", "n_1001");
1200
1201                         view.DataSourceViewChanged += new EventHandler (view_DataSourceViewChanged);
1202                 }
1203
1204                 private static IList eventsCalled;
1205
1206                 static void view_DataSourceViewChanged (object sender, EventArgs e) 
1207                 {
1208                         if (eventsCalled == null) {
1209                                 eventsCalled = new ArrayList ();
1210                         }
1211                         eventsCalled.Add ("view_DataSourceViewChanged");
1212                 }
1213
1214                 static void SelectParameters_ParametersChanged (object sender, EventArgs e) 
1215                 {
1216                         if (eventsCalled == null) {
1217                                 eventsCalled = new ArrayList ();
1218                         }
1219                         eventsCalled.Add ("SelectParameters_ParametersChanged");
1220                 }
1221
1222                 static void InsertParameters_ParametersChanged (object sender, EventArgs e) 
1223                 {
1224                         if (eventsCalled == null) {
1225                                 eventsCalled = new ArrayList ();
1226                         }
1227                         eventsCalled.Add ("InsertParameters_ParametersChanged");
1228                 }
1229
1230                 static void UpdateParameters_ParametersChanged (object sender, EventArgs e) 
1231                 {
1232                         if (eventsCalled == null) {
1233                                 eventsCalled = new ArrayList ();
1234                         }
1235                         eventsCalled.Add ("UpdateParameters_ParametersChanged");
1236                 }
1237
1238                 static void DeleteParameters_ParametersChanged (object sender, EventArgs e) 
1239                 {
1240                         if (eventsCalled == null) {
1241                                 eventsCalled = new ArrayList ();
1242                         }
1243                         eventsCalled.Add ("DeleteParameters_ParametersChanged");
1244                 }
1245
1246                 [Test]
1247                 public void ParametersAndViewChangedEvent_Select () 
1248                 {
1249                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1250                         Hashtable keys = null;
1251                         Hashtable old_values = null;
1252                         Hashtable new_values = null;
1253                         InitializeView (view, InitViewType.MatchParamsToValues, out keys, out old_values, out new_values);
1254
1255                         view.Select (DataSourceSelectArguments.Empty);
1256
1257                         Assert.IsNotNull (eventsCalled, "Events not raized");
1258                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1259                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [0], "view_DataSourceViewChanged");
1260                         Assert.AreEqual ("SelectParameters_ParametersChanged", eventsCalled [1], "SelectParameters_ParametersChanged");
1261                         Assert.AreEqual ("Select(filter = p_ValueSelect1)", eventsCalled [2], "DataSource Method params");
1262                 }
1263
1264                 [Test]
1265                 public void ParametersAndViewChangedEvent_MatchInsert () 
1266                 {
1267                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1268                         Hashtable keys = null;
1269                         Hashtable old_values = null;
1270                         Hashtable new_values = null;
1271                         InitializeView (view, InitViewType.MatchParamsToValues, out keys, out old_values, out new_values);
1272
1273                         view.Insert (new_values);
1274
1275                         Assert.IsNotNull (eventsCalled, "Events not raized");
1276                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1277                         Assert.AreEqual ("InsertParameters_ParametersChanged", eventsCalled [0], "InsertParameters_ParametersChanged");
1278                         Assert.AreEqual ("Insert(ID = n_1001)", eventsCalled [1], "DataSource Method params");
1279                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1280                 }
1281
1282                 [Test]
1283                 public void ParametersAndViewChangedEvent_MatchOldInsert () 
1284                 {
1285                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1286                         Hashtable keys = null;
1287                         Hashtable old_values = null;
1288                         Hashtable new_values = null;
1289                         InitializeView (view, InitViewType.MatchParamsToOldValues, out keys, out old_values, out new_values);
1290
1291                         view.Insert (new_values);
1292
1293                         Assert.IsNotNull (eventsCalled, "Events not raized");
1294                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1295                         Assert.AreEqual ("InsertParameters_ParametersChanged", eventsCalled [0], "InsertParameters_ParametersChanged");
1296                         Assert.AreEqual ("Insert(ID = n_1001, oldvalue_ID = p_OldValueInsert1)", eventsCalled [1], "DataSource Method params");
1297                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1298                 }
1299
1300                 [Test]
1301                 public void ParametersAndViewChangedEvent_DontMatchInsert () 
1302                 {
1303                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1304                         Hashtable keys = null;
1305                         Hashtable old_values = null;
1306                         Hashtable new_values = null;
1307                         InitializeView (view, InitViewType.DontMatchParams, out keys, out old_values, out new_values);
1308
1309                         view.Insert (new_values);
1310
1311                         Assert.IsNotNull (eventsCalled, "Events not raized");
1312                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1313                         Assert.AreEqual ("InsertParameters_ParametersChanged", eventsCalled [0], "InsertParameters_ParametersChanged");
1314                         Assert.AreEqual ("InsertOther(ID = n_1001, OtherValue = p_OtherValueInsert1)", eventsCalled [1], "DataSource Method params");
1315                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1316                 }
1317
1318                 [Test]
1319                 public void ParametersAndViewChangedEvent_MatchUpdate () 
1320                 {
1321                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1322                         Hashtable keys = null;
1323                         Hashtable old_values = null;
1324                         Hashtable new_values = null;
1325                         InitializeView (view, InitViewType.MatchParamsToValues, out keys, out old_values, out new_values);
1326
1327                         view.Update (keys, new_values, old_values);
1328
1329                         Assert.IsNotNull (eventsCalled, "Events not raized");
1330                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1331                         Assert.AreEqual ("UpdateParameters_ParametersChanged", eventsCalled [0], "UpdateParameters_ParametersChanged");
1332                         Assert.AreEqual ("Update(ID = n_1001, oldvalue_ID = k_1001)", eventsCalled [1], "DataSource Method params");
1333                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1334                 }
1335
1336                 [Test]
1337                 public void ParametersAndViewChangedEvent_MatchOldUpdate () 
1338                 {
1339                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1340                         Hashtable keys = null;
1341                         Hashtable old_values = null;
1342                         Hashtable new_values = null;
1343                         InitializeView (view, InitViewType.MatchParamsToOldValues, out keys, out old_values, out new_values);
1344
1345                         view.Update (keys, new_values, old_values);
1346
1347                         Assert.IsNotNull (eventsCalled, "Events not raized");
1348                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1349                         Assert.AreEqual ("UpdateParameters_ParametersChanged", eventsCalled [0], "UpdateParameters_ParametersChanged");
1350                         Assert.AreEqual ("Update(ID = n_1001, oldvalue_ID = k_1001)", eventsCalled [1], "DataSource Method params");
1351                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1352                 }
1353
1354                 [Test]
1355                 public void ParametersAndViewChangedEvent_DontMatchUpdate () 
1356                 {
1357                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1358                         Hashtable keys = null;
1359                         Hashtable old_values = null;
1360                         Hashtable new_values = null;
1361                         InitializeView (view, InitViewType.DontMatchParams, out keys, out old_values, out new_values);
1362
1363                         view.Update (keys, new_values, old_values);
1364
1365                         Assert.IsNotNull (eventsCalled, "Events not raized");
1366                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1367                         Assert.AreEqual ("UpdateParameters_ParametersChanged", eventsCalled [0], "UpdateParameters_ParametersChanged");
1368                         Assert.AreEqual ("UpdateOther(ID = n_1001, OtherValue = p_OtherValueUpdate1, oldvalue_ID = k_1001)", eventsCalled [1], "DataSource Method params");
1369                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1370                 }
1371
1372                 [Test]
1373                 public void ParametersAndViewChangedEvent_MatchDelete () 
1374                 {
1375                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1376                         Hashtable keys = null;
1377                         Hashtable old_values = null;
1378                         Hashtable new_values = null;
1379                         InitializeView (view, InitViewType.MatchParamsToValues, out keys, out old_values, out new_values);
1380
1381                         view.Delete (keys, old_values);
1382
1383                         Assert.IsNotNull (eventsCalled, "Events not raized");
1384                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1385                         Assert.AreEqual ("DeleteParameters_ParametersChanged", eventsCalled [0], "DeleteParameters_ParametersChanged");
1386                         Assert.AreEqual ("Delete(ID = p_ValueDelete1, oldvalue_ID = k_1001)", eventsCalled [1], "DataSource Method params");
1387                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1388                 }
1389
1390                 [Test]
1391                 public void ParametersAndViewChangedEvent_MatchOldDelete () 
1392                 {
1393                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1394                         Hashtable keys = null;
1395                         Hashtable old_values = null;
1396                         Hashtable new_values = null;
1397                         InitializeView (view, InitViewType.MatchParamsToOldValues, out keys, out old_values, out new_values);
1398
1399                         view.Delete (keys, old_values);
1400
1401                         Assert.IsNotNull (eventsCalled, "Events not raized");
1402                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1403                         Assert.AreEqual ("DeleteParameters_ParametersChanged", eventsCalled [0], "DeleteParameters_ParametersChanged");
1404                         Assert.AreEqual ("Delete(oldvalue_ID = k_1001)", eventsCalled [1], "DataSource Method params");
1405                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1406                 }
1407
1408                 [Test]
1409                 public void ParametersAndViewChangedEvent_DontMatchDelete () 
1410                 {
1411                         ObjectViewPoker view = new ObjectViewPoker (new ObjectDataSource (), "", null);
1412                         Hashtable keys = null;
1413                         Hashtable old_values = null;
1414                         Hashtable new_values = null;
1415                         InitializeView (view, InitViewType.DontMatchParams, out keys, out old_values, out new_values);
1416
1417                         view.Delete (keys, old_values);
1418
1419                         Assert.IsNotNull (eventsCalled, "Events not raized");
1420                         Assert.AreEqual (3, eventsCalled.Count, "Events Count");
1421                         Assert.AreEqual ("DeleteParameters_ParametersChanged", eventsCalled [0], "DeleteParameters_ParametersChanged");
1422                         Assert.AreEqual ("DeleteOther(oldvalue_ID = k_1001, OtherValue = p_OtherValueDelete1)", eventsCalled [1], "DataSource Method params");
1423                         Assert.AreEqual ("view_DataSourceViewChanged", eventsCalled [2], "view_DataSourceViewChanged");
1424                 }
1425
1426                 /// <summary>
1427                 /// Helper methods
1428                 /// </summary>
1429
1430                 private static bool event_checker;
1431
1432                 private static void Eventassert (string message)
1433                 {
1434                         Assert.IsTrue (ObjectDataSourceViewTest.event_checker, message);
1435                         ObjectDataSourceViewTest.event_checker = false;
1436                 }
1437
1438                 static void Event (object sender, ObjectDataSourceMethodEventArgs e)
1439                 {
1440                         ObjectDataSourceViewTest.event_checker = true;
1441                 }
1442
1443                 static void view_Selecting (object sender, ObjectDataSourceSelectingEventArgs e)
1444                 {
1445                         event_checker = true;
1446                 }
1447
1448                 void view_Filtering (object sender, ObjectDataSourceFilteringEventArgs e)
1449                 {
1450                         event_checker = true;
1451                 }
1452
1453                 void view_ObjectCreate (object sender, ObjectDataSourceEventArgs e)
1454                 {
1455                         event_checker = true;
1456                 }
1457
1458                 void view_Status (object sender, ObjectDataSourceStatusEventArgs e)
1459                 {
1460                         event_checker = true;
1461                 }
1462
1463                 void view_ObjectDisposing (object sender, ObjectDataSourceDisposingEventArgs e)
1464                 {
1465                         event_checker = true;
1466                 }
1467
1468                 private class MyDataSource : ObjectDataSource
1469                 {
1470                         public DataSourceView DoGetView (string viewName)
1471                         {
1472                                 return base.GetView (viewName);
1473                         }
1474
1475                         public void DoTrackViewState ()
1476                         {
1477                                 base.TrackViewState ();
1478                         }
1479                 }
1480         }
1481
1482         public class MyCustomDataObject
1483         {
1484         }
1485         
1486         public class DataSourceObject
1487         {
1488                 private static int maximumRows;
1489                 public static DataTable ds = CreateDataTable ();
1490                 public static void InitDS ()
1491                 {
1492                         ds = CreateDataTable ();
1493                 }
1494                 public static DataTable Select ()
1495                 {
1496                         return ds;
1497                 }
1498
1499                 public static MyCustomDataObject SelectObject ()
1500                 {
1501                         return new MyCustomDataObject ();
1502                 }
1503                 
1504                 [Sys.ComponentModel.DataObjectMethod(Sys.ComponentModel.DataObjectMethodType.Select, false)]
1505                 public static DataTable Select (int maximumRows, short startRowIndex) {
1506                         Assert.Fail ("Should not be called since not default Select DataObjectMethod");
1507                         return null;
1508                 }
1509
1510                 [Sys.ComponentModel.DataObjectMethod(Sys.ComponentModel.DataObjectMethodType.Select, true)]
1511                 public static DataTable Select (int maximumRows, int startRowIndex)
1512                 {
1513                         DataSourceObject.maximumRows = maximumRows;
1514                         if (ds.Rows.Count > maximumRows) {
1515                                 DataTable temp = ds.Clone ();
1516                                 int i = 0;
1517                                 while (i < maximumRows && startRowIndex <= ds.Rows.Count) {
1518                                         object[] o = ds.Rows[startRowIndex].ItemArray;
1519                                         temp.Rows.Add (o);
1520                                         i++;
1521                                         startRowIndex++;
1522                                 }
1523                                 return temp;
1524                         }
1525                         return ds;
1526                 }
1527
1528                 public static DataTable Delete (string ID, string FName, string LName)
1529                 {
1530                         DataRow dr = ds.Rows.Find (ID);
1531                         if (dr != null) {
1532                                 ds.Rows.Remove (dr);
1533                         }
1534                         return ds;
1535                 }
1536
1537                 public static DataTable Insert (string ID, string FName, string LName)
1538                 {
1539                         DataRow dr = ds.NewRow ();
1540                         dr["ID"] = ID;
1541                         dr["FName"] = FName;
1542                         dr["LName"] = LName;
1543                         ds.Rows.Add (dr);
1544                         return ds;
1545                 }
1546
1547                 public static DataTable Update (string ID, string FName, string LName)
1548                 {
1549                         foreach (DataRow row in ds.Rows) {
1550                                 if (row["ID"].ToString () == ID) {
1551                                         row["FName"] = FName;
1552                                         row["LName"] = LName;
1553                                 }
1554                         }
1555                         return ds;
1556                 }
1557
1558
1559                 public static int SelectCount ()
1560                 {
1561
1562                         //Note: This is return 5 only for test goal
1563                         return 5;
1564                 }
1565
1566                 public static DataTable CreateDataTable ()
1567                 {
1568                         DataTable aTable = new DataTable ("A");
1569                         DataColumn dtCol;
1570                         DataRow dtRow;
1571
1572                         // Create ID column and add to the DataTable.
1573                         dtCol = new DataColumn ();
1574                         dtCol.DataType = Type.GetType ("System.Int32");
1575                         dtCol.ColumnName = "ID";
1576                         dtCol.AutoIncrement = true;
1577                         dtCol.Caption = "ID";
1578                         dtCol.ReadOnly = true;
1579                         dtCol.Unique = true;
1580                         aTable.Columns.Add (dtCol);
1581
1582                         // Create Name column and add to the table
1583                         dtCol = new DataColumn ();
1584                         dtCol.DataType = Type.GetType ("System.String");
1585                         dtCol.ColumnName = "FName";
1586                         dtCol.AutoIncrement = false;
1587                         dtCol.Caption = "First Name";
1588                         dtCol.ReadOnly = false;
1589                         dtCol.Unique = false;
1590                         aTable.Columns.Add (dtCol);
1591
1592                         // Create Last Name column and add to the table.
1593                         dtCol = new DataColumn ();
1594                         dtCol.DataType = Type.GetType ("System.String");
1595                         dtCol.ColumnName = "LName";
1596                         dtCol.AutoIncrement = false;
1597                         dtCol.Caption = "Last Name";
1598                         dtCol.ReadOnly = false;
1599                         dtCol.Unique = false;
1600                         aTable.Columns.Add (dtCol);
1601
1602                         // Create three rows to the table
1603                         dtRow = aTable.NewRow ();
1604                         dtRow["ID"] = 1001;
1605                         dtRow["FName"] = "Mahesh";
1606                         dtRow["LName"] = "Chand";
1607                         aTable.Rows.Add (dtRow);
1608
1609                         aTable.PrimaryKey = new DataColumn[] { aTable.Columns["ID"] };
1610                         return aTable;
1611                 }
1612         }
1613 }
1614
1615 #endif