column name and ordinal fix...tested on 10.1
[mono.git] / mcs / class / System.Web.DynamicData / Test / System.Web.DynamicData / DynamicDataRouteHandlerTest.cs
1 //
2 // DynamicDataRouteHandlerTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2009 Novell Inc. http://novell.com
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.ObjectModel;
33 using System.Collections.Specialized;
34 using System.ComponentModel.DataAnnotations;
35 using System.Data.SqlClient;
36 using System.Data.Linq;
37 using System.Data.Linq.Mapping;
38 using System.Globalization;
39 using System.Linq;
40 using System.Reflection;
41 using System.Security.Permissions;
42 using System.Security.Principal;
43 using System.Web;
44 using System.Web.UI;
45 using System.Web.DynamicData;
46 using System.Web.DynamicData.ModelProviders;
47 using System.Web.Routing;
48
49 using NUnit.Framework;
50 using NUnit.Mocks;
51 using MonoTests.stand_alone.WebHarness;
52 using MonoTests.SystemWeb.Framework;
53 using MonoTests.Common;
54 using MonoTests.ModelProviders;
55
56 using MetaModel = System.Web.DynamicData.MetaModel;
57 using MetaTable = System.Web.DynamicData.MetaTable;
58
59 namespace MonoTests.System.Web.DynamicData
60 {
61         [TestFixture]
62         public class DynamicDataRouteHandlerTest
63         {
64                 DynamicDataContainerModelProvider <TestDataContext> dynamicModelProvider;
65
66                 [TestFixtureSetUp]
67                 public void SetUp ()
68                 {
69                         dynamicModelProvider = new DynamicDataContainerModelProvider <TestDataContext> ();
70                         Utils.RegisterContext (dynamicModelProvider, new ContextConfiguration () { ScaffoldAllTables = true });
71                 }
72
73                 [Test]
74                 public void Constructor ()
75                 {
76                         var ddrh = new DynamicDataRouteHandler ();
77
78                         Assert.AreEqual (null, ddrh.Model, "#A1");
79                 }
80
81                 [Test]
82                 public void CreateHandlerParams ()
83                 {
84                         MetaModel m = Utils.CommonInitialize ();
85
86                         var route = RouteTable.Routes[0] as DynamicDataRoute;
87                         MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
88                         var handler = route.RouteHandler = new DynamicDataRouteHandler ();
89
90                         // No null check is made, of course - throws from some internal method
91                         AssertExtensions.Throws<NullReferenceException> (() => {
92                                 handler.CreateHandler (null, t, PageAction.Details);
93                         }, "#A1");
94
95                         // No null check again - this time throws from GetCustomPageVirtualPath
96                         AssertExtensions.Throws<NullReferenceException> (() => {
97                                 handler.CreateHandler (route, null, PageAction.Details);
98                         }, "#A2");
99
100                         // And once again, no null check and thrown from GetCustomPageVirtualPath as well
101                         AssertExtensions.Throws<NullReferenceException> (() => {
102                                 handler.CreateHandler (route, t, null);
103                         }, "#A3");
104                 }
105
106                 [Test]
107                 [Ignore ("Throws NREX on .NET - no idea why and how to make it work. Probably needs full request emulation (using Mainsoft test suite)")]
108                 // Probably need to simulate a full reqest using similar environment as System.Web tests
109                 public void CreateHandler ()
110                 {
111                         MetaModel m = Utils.CommonInitialize ();
112
113                         var route = RouteTable.Routes[0] as DynamicDataRoute;
114                         MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
115                         Assert.IsNotNull (t, "#A1");
116
117                         var handler = route.RouteHandler = new DynamicDataRouteHandler ();
118                         var wrapper = new MyHttpContextWrapper ();
119                         var request = wrapper.Request as MyHttpRequestWrapper;
120                         request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooEmptyTable/List.aspx");
121                         request.SetProperty ("PathInfo", String.Empty);
122
123                         // This must be non-null because DynamicData doesn't care to check whether the returned
124                         // value is null or not...
125                         request.SetProperty ("QueryString", new NameValueCollection ());
126
127                         // This will assign the route handler's Model property
128                         RouteData rd = route.GetRouteData (wrapper);
129                         Assert.IsNotNull (handler.Model, "#A2");
130
131                         // Throws a NREX from some internal method - no slightest idea why, as none of the parameters
132                         // passed are null
133                         IHttpHandler h = handler.CreateHandler (route, t, PageAction.Details);
134                         Assert.IsNotNull (h, "#A3");
135                         Assert.AreEqual (typeof (Page), h.GetType (), "#A3-1");
136
137                         var page = h as Page;
138                         Assert.AreEqual (String.Empty, page.AppRelativeVirtualPath, "#A3-2");
139                 }
140
141                 [Test]
142                 public void GetCustomPageVirtualPath ()
143                 {
144                         MetaModel m = Utils.CommonInitialize ();
145                         var route = RouteTable.Routes[0] as DynamicDataRoute;
146
147                         MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
148                         Assert.IsNotNull (t, "#A1");
149
150                         // We neeed the handler to have its Model property set
151                         route.RouteHandler = new MyDynamicDataRouteHandler ();
152                         var handler = route.RouteHandler as MyDynamicDataRouteHandler;
153                         Assert.IsNotNull (handler, "#A2");
154
155                         // Lack of null check (for table)
156                         AssertExtensions.Throws<NullReferenceException> (() => {
157                                 handler.DoGetCustomPageVirtualPath (null, null);
158                         }, "#A2-1");
159
160                         // Another missing null check (this time for Model)... Are null checks
161                         // out of fashion?
162                         AssertExtensions.Throws<NullReferenceException> (() => {
163                                 handler.DoGetCustomPageVirtualPath (t, String.Empty);
164                         }, "#A2-2");
165
166                         var wrapper = new MyHttpContextWrapper ();
167                         var request = wrapper.Request as MyHttpRequestWrapper;
168                         request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooEmptyTable/List.aspx");
169                         request.SetProperty ("PathInfo", String.Empty);
170
171                         // This must be non-null because DynamicData doesn't care to check whether the returned
172                         // value is null or not...
173                         request.SetProperty ("QueryString", new NameValueCollection ());
174
175                         // This will assign the route handler's Model property
176                         RouteData rd = route.GetRouteData (wrapper);
177                         Assert.IsNotNull (handler.Model, "#A3");
178
179                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx", 
180                                 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A4");
181
182                         handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
183                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx",
184                                 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A5");
185
186                         // doh!
187                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
188                                 handler.DoGetCustomPageVirtualPath (t, null), "#A6");
189
190                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
191                                 handler.DoGetCustomPageVirtualPath (t, String.Empty), "#A7");
192                 }
193
194                 [Test]
195                 public void GetRequestContext ()
196                 {
197                         AssertExtensions.Throws<ArgumentNullException> (() => {
198                                 DynamicDataRouteHandler.GetRequestContext (null);
199                         }, "#A1");
200
201                         var req = new FakeHttpWorkerRequest ();
202                         var ctx = new HttpContext (req);
203
204                         RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx);
205                         Assert.IsNotNull (rc, "#B1");
206                         Assert.IsNotNull (rc.HttpContext, "#B1-1");
207                         Assert.IsNotNull (rc.RouteData, "#B1-2");
208
209                         Assert.IsNull (rc.RouteData.Route, "#C1");
210                         Assert.IsNull (rc.RouteData.RouteHandler, "#C1-1");
211                         Assert.IsNotNull (rc.RouteData.Values, "#C1-2");
212                         Assert.AreEqual (0, rc.RouteData.Values.Count, "#C1-3");
213                 }
214
215                 [Test]
216                 public void GetRequestMetaTable ()
217                 {
218                         MetaModel m = Utils.CommonInitialize ();
219                         var route = RouteTable.Routes[0] as DynamicDataRoute;
220                         MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
221                         Assert.IsNotNull (t, "#A1");
222
223                         // Surprise! A null check is present!
224                         AssertExtensions.Throws<ArgumentNullException> (() => {
225                                 DynamicDataRouteHandler.GetRequestMetaTable (null);
226                         }, "#A2");
227                         
228                         MetaTable t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
229                         Assert.IsNull (t2, "#A3");
230
231                         DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, t);
232                         t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
233                         Assert.IsNotNull (t2, "#A4");
234                         Assert.AreEqual (t, t2, "#A4-1");
235                 }
236
237                 [Test]
238                 public void GetScaffoldPageVirtualPath ()
239                 {
240                         MetaModel m = Utils.CommonInitialize ();
241                         var route = RouteTable.Routes[0] as DynamicDataRoute;
242
243                         MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
244                         Assert.IsNotNull (t, "#A1");
245
246                         // We neeed the handler to have its Model property set
247                         route.RouteHandler = new MyDynamicDataRouteHandler ();
248                         var handler = route.RouteHandler as MyDynamicDataRouteHandler;
249                         Assert.IsNotNull (handler, "#A2");
250
251                         // Lack of null check (for table)
252                         AssertExtensions.Throws<NullReferenceException> (() => {
253                                 handler.DoGetScaffoldPageVirtualPath (null, null);
254                         }, "#A2-1");
255
256                         // Another missing null check (this time for Model)... Are null checks
257                         // out of fashion?
258                         AssertExtensions.Throws<NullReferenceException> (() => {
259                                 handler.DoGetScaffoldPageVirtualPath (t, String.Empty);
260                         }, "#A2-2");
261
262                         var wrapper = new MyHttpContextWrapper ();
263                         var request = wrapper.Request as MyHttpRequestWrapper;
264                         request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooDisplayNameTable/List.aspx");
265                         request.SetProperty ("PathInfo", String.Empty);
266
267                         // This must be non-null because DynamicData doesn't care to check whether the returned
268                         // value is null or not...
269                         request.SetProperty ("QueryString", new NameValueCollection ());
270
271                         // This will assign the route handler's Model property
272                         RouteData rd = route.GetRouteData (wrapper);
273                         Assert.IsNotNull (handler.Model, "#A3");
274
275                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
276                                 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A4");
277
278                         handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
279                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
280                                 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A5");
281
282                         // doh!
283                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
284                                 handler.DoGetScaffoldPageVirtualPath (t, null), "#A6");
285
286                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
287                                 handler.DoGetScaffoldPageVirtualPath (t, String.Empty), "#A7");
288                 }
289
290                 [Test]
291                 public void Model ()
292                 {
293                         MetaModel m = Utils.CommonInitialize ();
294                         var route = RouteTable.Routes[0] as DynamicDataRoute;
295
296                         Assert.IsNotNull (route, "#A1");
297                         Assert.IsNotNull (route.Model, "#A1-1");
298                         var handler = route.RouteHandler;
299
300                         Assert.IsNotNull (handler, "#A2");
301                         Assert.IsTrue (handler.GetType () == typeof (MyDynamicDataRouteHandler), "#A2-1");
302                         Assert.IsNull (handler.Model, "#A2-2");
303
304                         var req = new FakeHttpWorkerRequest ();
305                         var ctx = new HttpContext (req);
306
307                         RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx);
308                         Assert.IsNotNull (rc, "#B1");
309                         Assert.IsNull (handler.Model, "#B1-2");
310
311                         var wrapper = new MyHttpContextWrapper ();
312                         var request = wrapper.Request as MyHttpRequestWrapper;
313
314                         // It appears .NET checks whether the indicated table exists - if not, GetRouteData will return
315                         // null (even though the Route class will find a match)
316                         request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/NoSuchTable/List.aspx");
317                         request.SetProperty ("PathInfo", String.Empty);
318
319                         // This must be non-null because DynamicData doesn't care to check whether the returned
320                         // value is null or not...
321                         request.SetProperty ("QueryString", new NameValueCollection ());
322                         
323                         // No table FooTable in the context - returns null
324                         RouteData rd = route.GetRouteData (wrapper);
325                         Assert.IsNull (rd, "#C1");
326
327                         // Apparently Model is set in the above call even though it returns null
328                         Assert.IsNotNull (handler.Model, "#C1-1");
329                         Assert.AreEqual (route.Model, handler.Model, "#C1-2");
330
331                         request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/BarTable/List.aspx");
332                         rd = route.GetRouteData (wrapper);
333                         Assert.IsNotNull (rd, "#D1");
334                         Assert.IsNotNull (handler.Model, "#D1-1");
335                         Assert.AreEqual (route.Model, handler.Model, "#D1-2");
336                 }
337
338                 [Test]
339                 public void SetRequestMetaTable ()
340                 {
341                         MetaModel m = Utils.CommonInitialize ();
342                         var route = RouteTable.Routes[0] as DynamicDataRoute;
343                         MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
344                         Assert.IsNotNull (t, "#A1");
345
346                         // And following the tradition... [drum roll] - NO NULL CHECK!
347                         AssertExtensions.Throws<NullReferenceException> (() => {
348                                 DynamicDataRouteHandler.SetRequestMetaTable (null, t);
349                         }, "#A2");
350
351                         DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, t);
352                         MetaTable t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
353                         Assert.IsNotNull (t2, "#A3");
354                         Assert.AreEqual (t, t2, "#A3-1");
355
356                         DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, null);
357                         t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
358                         Assert.IsNull (t2, "#A4");
359                 }
360         }
361 }