Add NET_2_0 guards.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / SqlDataSourceViewTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.SqlDataSourceView
3 //
4 // Author:
5 //      Chris Toshok (toshok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2006 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 #if NET_2_0
32
33 using NUnit.Framework;
34 using System;
35 using System.Configuration;
36 using System.Data.Common;
37 using System.IO;
38 using System.Globalization;
39 using System.Web;
40 using System.Web.UI;
41 using System.Web.UI.WebControls;
42
43 namespace MonoTests.System.Web.UI.WebControls
44 {
45         class SqlViewPoker : SqlDataSourceView {
46                 public SqlViewPoker (SqlDataSource ds, string name, HttpContext context)
47                         : base (ds, name, context)
48                 {
49                         TrackViewState ();
50                 }
51
52                 public object SaveToViewState ()
53                 {
54                         return SaveViewState ();
55                 }
56
57                 public void LoadFromViewState (object savedState)
58                 {
59                         LoadViewState (savedState);
60                 }
61         }
62
63         [TestFixture]
64         public class SqlDataSourceViewTest {
65                 [Test]
66                 public void Defaults ()
67                 {
68                         SqlDataSource ds = new SqlDataSource ();
69                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
70
71                         Assert.IsTrue (sql.CancelSelectOnNullParameter, "A1");
72                         Assert.IsFalse (sql.CanDelete,"A2");
73                         Assert.IsFalse (sql.CanInsert,"A3");
74                         Assert.IsFalse (sql.CanPage,"A4");
75                         Assert.IsFalse (sql.CanRetrieveTotalRowCount,"A5");
76                         Assert.IsTrue (sql.CanSort,"A6");
77                         Assert.IsFalse (sql.CanUpdate,"A7");
78                         Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "A8");
79                         Assert.AreEqual ("", sql.DeleteCommand, "A9");
80                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.DeleteCommandType, "A10");
81                         Assert.IsNotNull (sql.DeleteParameters, "A11");
82                         Assert.AreEqual (0, sql.DeleteParameters.Count, "A12");
83                         Assert.AreEqual ("", sql.FilterExpression, "A13");
84                         Assert.IsNotNull (sql.FilterParameters, "A14");
85                         Assert.AreEqual (0, sql.FilterParameters.Count, "A15");
86                         Assert.AreEqual ("", sql.InsertCommand, "A16");
87                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.InsertCommandType, "A17");
88                         Assert.IsNotNull (sql.InsertParameters, "A18");
89                         Assert.AreEqual (0, sql.InsertParameters.Count, "A19");
90                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A20");
91                         Assert.AreEqual ("", sql.SelectCommand, "A21");
92                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.SelectCommandType, "A22");
93                         Assert.IsNotNull (sql.SelectParameters, "A23");
94                         Assert.AreEqual (0, sql.SelectParameters.Count, "A24");
95                         Assert.AreEqual ("", sql.SortParameterName, "A25");
96                         Assert.AreEqual ("", sql.UpdateCommand, "A26");
97                         Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.UpdateCommandType, "A27");
98                         Assert.IsNotNull (sql.UpdateParameters, "A28");
99                         Assert.AreEqual (0, sql.UpdateParameters.Count, "A29");
100                 }
101
102                 [Test]
103                 public void ViewState ()
104                 {
105                         SqlDataSource ds = new SqlDataSource ();
106                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
107
108                         /* XXX test parameters */
109
110                         sql.CancelSelectOnNullParameter = false;
111                         sql.ConflictDetection = ConflictOptions.CompareAllValues;
112                         sql.DeleteCommandType = SqlDataSourceCommandType.Text;
113                         sql.DeleteCommand = "delete command";
114                         sql.FilterExpression = "filter expression";
115                         sql.InsertCommand = "insert command";
116                         sql.InsertCommandType = SqlDataSourceCommandType.Text;
117                         sql.OldValuesParameterFormatString = "{1}";
118                         sql.SelectCommand = "select command";
119                         sql.SelectCommandType = SqlDataSourceCommandType.Text;
120                         sql.SortParameterName = "sort parameter";
121                         sql.UpdateCommand = "update command";
122                         sql.UpdateCommandType = SqlDataSourceCommandType.Text;
123
124                         Assert.IsFalse (sql.CancelSelectOnNullParameter, "A1");
125                         Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "A2");
126                         Assert.AreEqual ("delete command", sql.DeleteCommand, "A3");
127                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "A4");
128                         Assert.AreEqual ("filter expression", sql.FilterExpression, "A5");
129                         Assert.AreEqual ("insert command", sql.InsertCommand, "A6");
130                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "A7");
131                         Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "A8");
132                         Assert.AreEqual ("select command", sql.SelectCommand, "A9");
133                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "A10");
134                         Assert.AreEqual ("sort parameter", sql.SortParameterName, "A11");
135                         Assert.AreEqual ("update command", sql.UpdateCommand, "A12");
136                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "A13");
137
138                         object state = sql.SaveToViewState();
139
140                         sql = new SqlViewPoker (ds, "DefaultView", null);
141                         sql.LoadFromViewState (state);
142
143                         Assert.IsFalse (sql.CancelSelectOnNullParameter, "B1");
144                         Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "B2");
145                         Assert.AreEqual ("delete command", sql.DeleteCommand, "B3");
146                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "B4");
147                         Assert.AreEqual ("filter expression", sql.FilterExpression, "B5");
148                         Assert.AreEqual ("insert command", sql.InsertCommand, "B6");
149                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "B7");
150                         Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "B8");
151                         Assert.AreEqual ("select command", sql.SelectCommand, "B9");
152                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "B10");
153                         Assert.AreEqual ("sort parameter", sql.SortParameterName, "B11");
154                         Assert.AreEqual ("update command", sql.UpdateCommand, "B12");
155                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "B13");
156                 }
157
158                 [Test]
159                 public void CanDelete ()
160                 {
161                         SqlDataSource ds = new SqlDataSource ();
162                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
163
164                         sql.DeleteCommand = "DELETE from foo";
165                         Assert.IsTrue (sql.CanDelete, "A1");
166
167                         sql.DeleteCommand = "";
168                         Assert.IsFalse (sql.CanDelete, "A2");
169
170                         sql.DeleteCommand = null;
171                         Assert.IsFalse (sql.CanDelete, "A3");
172                 }
173
174                 [Test]
175                 public void CanInsert ()
176                 {
177                         SqlDataSource ds = new SqlDataSource ();
178                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
179
180                         sql.InsertCommand = "INSERT into foo";
181                         Assert.IsTrue (sql.CanInsert, "A1");
182
183                         sql.InsertCommand = "";
184                         Assert.IsFalse (sql.CanInsert, "A2");
185
186                         sql.InsertCommand = null;
187                         Assert.IsFalse (sql.CanInsert, "A3");
188                 }
189
190                 [Test]
191                 public void CanUpdate ()
192                 {
193                         SqlDataSource ds = new SqlDataSource ();
194                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
195
196                         sql.UpdateCommand = "UPDATE foo";
197                         Assert.IsTrue (sql.CanUpdate, "A1");
198
199                         sql.UpdateCommand = "";
200                         Assert.IsFalse (sql.CanUpdate, "A2");
201
202                         sql.UpdateCommand = null;
203                         Assert.IsFalse (sql.CanUpdate, "A3");
204                 }
205
206                 [Test]
207                 public void CanSort ()
208                 {
209                         SqlDataSource ds = new SqlDataSource ();
210                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
211
212                         sql.SortParameterName = "foo";
213                         Assert.IsTrue (sql.CanSort, "A1");
214
215                         sql.SortParameterName = null;
216                         Assert.IsTrue (sql.CanSort, "A2");
217
218                         sql.SortParameterName = "";
219                         Assert.IsTrue (sql.CanSort, "A3");
220
221                         sql.SortParameterName = "foo";
222
223                         ds.DataSourceMode = SqlDataSourceMode.DataReader;
224                         Assert.IsTrue (sql.CanSort, "A4");
225
226                         ds.DataSourceMode = SqlDataSourceMode.DataSet;
227                         Assert.IsTrue (sql.CanSort, "A5");
228
229                         sql.SortParameterName = "";
230
231                         ds.DataSourceMode = SqlDataSourceMode.DataReader;
232                         Assert.IsFalse (sql.CanSort, "A6");
233
234                         ds.DataSourceMode = SqlDataSourceMode.DataSet;
235                         Assert.IsTrue (sql.CanSort, "A7");
236                 }
237
238                 [Test]
239                 public void OldValuesParameterFormatString ()
240                 {
241                         SqlDataSource ds = new SqlDataSource ();
242                         
243                         Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A1");
244
245                         ds.OldValuesParameterFormatString = "hi {0}";
246
247                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
248
249                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A2");
250
251                         ds.OldValuesParameterFormatString = "hi {0}";
252
253                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A3");
254
255                         ds.OldValuesParameterFormatString = "{0}";
256                         sql.OldValuesParameterFormatString = "hi {0}";
257
258                         Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A4");
259                 }
260
261                 [Test]
262                 public void CancelSelectOnNullParameter ()
263                 {
264                         SqlDataSource ds = new SqlDataSource ();
265
266                         ds.CancelSelectOnNullParameter = false;
267
268                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
269
270                         Assert.IsTrue (sql.CancelSelectOnNullParameter, "A1");
271
272                         ds.CancelSelectOnNullParameter = true;
273                         sql.CancelSelectOnNullParameter = false;
274
275                         Assert.IsTrue (ds.CancelSelectOnNullParameter, "A2");
276
277                         sql.CancelSelectOnNullParameter = false;
278                         ds.CancelSelectOnNullParameter = true;
279                         Assert.IsFalse (sql.CancelSelectOnNullParameter, "A3");
280                 }
281         }
282
283 }
284
285 #endif