2006-02-21 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / SqlDataSourceTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.SqlDataSource
3 //
4 // Author:
5 //      Chris Toshok (toshok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Configuration;
34 using System.Data.Common;
35 using System.IO;
36 using System.Globalization;
37 using System.Web;
38 using System.Web.UI;
39 using System.Web.UI.WebControls;
40
41 namespace MonoTests.System.Web.UI.WebControls
42 {
43         class SqlPoker : SqlDataSource {
44                 public SqlPoker ()
45                 {
46                         TrackViewState ();
47                 }
48
49                 public object SaveToViewState ()
50                 {
51                         return SaveViewState ();
52                 }
53
54                 public void LoadFromViewState (object savedState)
55                 {
56                         LoadViewState (savedState);
57                 }
58         }
59
60         [TestFixture]
61         public class SqlDataSourceTest {
62                 [Test]
63                 public void Defaults ()
64                 {
65                         SqlPoker sql = new SqlPoker ();
66
67                         Assert.AreEqual ("", sql.CacheKeyDependency, "A1");
68                         Assert.IsTrue (sql.CancelSelectOnNullParameter, "A2");
69                         Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "A3");
70                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.DeleteCommandType, "A4");
71                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.InsertCommandType, "A5");
72                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.SelectCommandType, "A6");
73                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.UpdateCommandType, "A7");
74                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A8");
75                         Assert.AreEqual ("", sql.SqlCacheDependency, "A9");
76                         Assert.AreEqual ("", sql.SortParameterName, "A10");
77                         Assert.AreEqual (0, sql.CacheDuration, "A11");
78                         Assert.AreEqual (DataSourceCacheExpiry.Absolute, sql.CacheExpirationPolicy, "A12");
79                         Assert.IsFalse (sql.EnableCaching, "A13");
80                         Assert.AreEqual ("", sql.ProviderName, "A14");
81                         Assert.AreEqual ("", sql.ConnectionString, "A15");
82                         Assert.AreEqual (SqlDataSourceMode.DataSet, sql.DataSourceMode, "A16");
83                         Assert.AreEqual ("", sql.DeleteCommand, "A17");
84                         Assert.IsNotNull (sql.DeleteParameters, "A18");
85                         Assert.AreEqual (0, sql.DeleteParameters.Count, "A18.1");
86                         Assert.IsNotNull (sql.FilterParameters, "A19");
87                         Assert.AreEqual (0, sql.FilterParameters.Count, "A19.1");
88                         Assert.AreEqual ("", sql.InsertCommand, "A20");
89                         Assert.IsNotNull (sql.InsertParameters, "A21");
90                         Assert.AreEqual (0, sql.InsertParameters.Count, "A21.1");
91                         Assert.AreEqual ("", sql.SelectCommand, "A22");
92                         Assert.IsNotNull (sql.SelectParameters, "A23");
93                         Assert.AreEqual (0, sql.SelectParameters.Count, "A23.1");
94                         Assert.AreEqual ("", sql.UpdateCommand, "A24");
95                         Assert.IsNotNull (sql.UpdateParameters, "A25");
96                         Assert.AreEqual (0, sql.UpdateParameters.Count, "A25.1");
97                         Assert.AreEqual ("", sql.FilterExpression, "A26");
98                 }
99
100                 [Test]
101                 public void ViewState ()
102                 {
103                         SqlPoker sql = new SqlPoker ();
104
105                         sql.CacheKeyDependency = "hi";
106                         sql.CancelSelectOnNullParameter = false;
107                         sql.ConflictDetection = ConflictOptions.CompareAllValues;
108                         sql.DeleteCommandType = SqlDataSourceCommandType.Text;
109                         sql.InsertCommandType = SqlDataSourceCommandType.Text;
110                         sql.SelectCommandType = SqlDataSourceCommandType.Text;
111                         sql.UpdateCommandType = SqlDataSourceCommandType.Text;
112                         sql.OldValuesParameterFormatString = "{1}";
113                         sql.SqlCacheDependency = "hi";
114                         sql.SortParameterName = "hi";
115                         sql.CacheDuration = 1;
116                         sql.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
117                         sql.EnableCaching = true;
118                         sql.DataSourceMode = SqlDataSourceMode.DataReader;
119                         sql.DeleteCommand = "DELETE foo";
120                         sql.InsertCommand = "INSERT foo";
121                         sql.SelectCommand = "SELECT foo";
122                         sql.UpdateCommand = "UPDATE foo";
123                         sql.FilterExpression = "hi";
124                         
125                         Assert.AreEqual ("hi", sql.CacheKeyDependency, "A1");
126                         Assert.IsFalse (sql.CancelSelectOnNullParameter, "A2");
127                         Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "A3");
128                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "A4");
129                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "A5");
130                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "A6");
131                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "A7");
132                         Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "A8");
133                         Assert.AreEqual ("hi", sql.SqlCacheDependency, "A9");
134                         Assert.AreEqual ("hi", sql.SortParameterName, "A10");
135                         Assert.AreEqual (1, sql.CacheDuration, "A11");
136                         Assert.AreEqual (DataSourceCacheExpiry.Sliding, sql.CacheExpirationPolicy, "A12");
137                         Assert.IsTrue (sql.EnableCaching, "A13");
138                         Assert.AreEqual (SqlDataSourceMode.DataReader, sql.DataSourceMode, "A16");
139                         Assert.AreEqual ("DELETE foo", sql.DeleteCommand, "A17");
140                         Assert.AreEqual ("INSERT foo", sql.InsertCommand, "A20");
141                         Assert.AreEqual ("SELECT foo", sql.SelectCommand, "A22");
142                         Assert.AreEqual ("UPDATE foo", sql.UpdateCommand, "A24");
143                         Assert.AreEqual ("hi", sql.FilterExpression, "A26");
144
145                         object state = sql.SaveToViewState();
146
147                         sql = new SqlPoker ();
148                         sql.LoadFromViewState (state);
149
150                         Assert.AreEqual ("hi", sql.CacheKeyDependency, "B1");
151                         Assert.IsFalse  (sql.CancelSelectOnNullParameter, "B2");
152                         Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "B3");
153                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "B4");
154                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "B5");
155                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "B6");
156                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "B7");
157                         Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "B8");
158                         Assert.AreEqual ("hi", sql.SqlCacheDependency, "B9");
159                         Assert.AreEqual ("hi", sql.SortParameterName, "B10");
160                         Assert.AreEqual (1, sql.CacheDuration, "B11");
161                         Assert.AreEqual (DataSourceCacheExpiry.Sliding, sql.CacheExpirationPolicy, "B12");
162                         Assert.IsTrue   (sql.EnableCaching, "B13");
163                         Assert.AreEqual (SqlDataSourceMode.DataReader, sql.DataSourceMode, "B16");
164                         Assert.AreEqual ("DELETE foo", sql.DeleteCommand, "B17");
165                         Assert.AreEqual ("INSERT foo", sql.InsertCommand, "B20");
166                         Assert.AreEqual ("SELECT foo", sql.SelectCommand, "B22");
167                         Assert.AreEqual ("UPDATE foo", sql.UpdateCommand, "B24");
168                         Assert.AreEqual ("hi", sql.FilterExpression, "B26");
169                 }
170         }
171
172 }