2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / TestStubTypes.cs
1 //
2 // TestStubTypes.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
9
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 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
34 using System.Collections.Specialized;
35 using System.ComponentModel.DataAnnotations;
36 using System.Data.SqlClient;
37 using System.Data.Linq;
38 using System.Data.Linq.Mapping;
39 using System.Globalization;
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 MetaModel = System.Web.DynamicData.MetaModel;
50 using MetaTable = System.Web.DynamicData.MetaTable;
51
52 using NUnit.Framework;
53 using MonoTests.DataSource;
54
55 namespace MonoTests.System.Web.DynamicData
56 {
57     class MyDynamicDataRoute : DynamicDataRoute
58     {
59         public RouteValueDictionary GetVirtualPathValues {
60             get; private set;
61         }
62
63         public bool Called {
64             get;
65             set;
66         }
67
68         public MyDynamicDataRoute (string url)
69             : base (url)
70         { }
71
72         public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
73         {
74             GetVirtualPathValues = values;
75             Called = true;
76             return base.GetVirtualPath (requestContext, values);
77         }
78     }
79
80         class MyDynamicDataRouteHandler : DynamicDataRouteHandler
81         {
82                 public override IHttpHandler CreateHandler (DynamicDataRoute route, MetaTable table, string action)
83                 {
84                         return new Page () as IHttpHandler;
85                 }
86         }
87
88         class MyDataContext1 : DataContext
89         {
90                 public MyDataContext1 ()
91                         : base (new SqlConnection ("Data Source=localhost"))
92                 {
93                 }
94         }
95
96         [Database (Name = "MyDB1")]
97         class MyDataContext2 : DataContext
98         {
99                 public MyDataContext2 ()
100                         : base (new SqlConnection ("Data Source=localhost"))
101                 {
102                 }
103
104                 public Table<Foo> FooTable { get { return GetTable<Foo> (); } }
105         }
106
107     class MyDataContext3 : MyDataContext2
108     {
109     }
110
111         class UseOnlyInGetModelTestDataContext : MyDataContext2
112         {
113         }
114
115         [Table (Name = "dbo...FooTable")]
116         partial class Foo
117         {
118                 public Foo () : this (false)
119                 {
120                 }
121
122                 public Foo (bool noThrow)
123                 {
124                         if (!noThrow)
125                                 throw new Exception ("ERROR");
126                 }
127
128                 [Column (Name = "Col1")]
129                 public string Column1 { get; set; }
130         }
131     
132         [Table (Name = "BarTable")]
133         class Bar
134         {
135                 [Column (Name = "Col1")]
136                 public string Column1 { get; set; }
137
138                 [Column (Name = "Col2")]
139                 public string Column2 { get; set; }
140         }
141
142         class HttpContextStub : HttpContextBase
143         {
144                 HttpRequestStub req;
145
146                 public HttpContextStub ()
147                         : this (null)
148                 {
149                 }
150
151                 public HttpContextStub (string dummyRequestPath)
152                         : this (dummyRequestPath, null)
153                 {
154                 }
155
156                 public HttpContextStub (string dummyRequestPath, string pathInfo)
157                         : this (dummyRequestPath, pathInfo, null)
158                 {
159                 }
160
161                 public HttpContextStub (string dummyRequestPath, string pathInfo, string method)
162                 {
163                         if (dummyRequestPath != null) {
164                                 req = new HttpRequestStub (dummyRequestPath, pathInfo);
165                                 req.Method = method;
166                         }
167                 }
168
169                 public override HttpRequestBase Request {
170                         get { return req != null ? req : base.Request; }
171                 }
172         }
173
174         class HttpRequestStub : HttpRequestBase
175         {
176                 public HttpRequestStub (string dummyRequestPath, string pathInfo)
177                 {
178                         req_path = dummyRequestPath;
179                         path_info = pathInfo;
180                 }
181
182                 string req_path, path_info;
183                 NameValueCollection query_string = new NameValueCollection ();
184
185                 public override string AppRelativeCurrentExecutionFilePath {
186                         get { return req_path ?? base.AppRelativeCurrentExecutionFilePath; }
187                 }
188
189                 public override string PathInfo {
190                         get { return path_info ?? base.PathInfo; }
191                 }
192
193                 public override NameValueCollection QueryString {
194                         get { return query_string; }
195                 }
196
197                 public override string HttpMethod {
198                         get { return Method; }
199                 }
200
201                 public string Method { get; set; }
202         }
203
204         class MyDictionary : Hashtable
205         {
206                 public override ICollection Keys {
207                         get { return null; }
208                 }
209
210                 public override object this [object key] {
211                         get {
212                                 //Console.Error.WriteLine ("Get: {0} {1}", key, key.GetHashCode ());
213                                 return base [key];
214                         }
215                         set {
216                                 //Console.Error.WriteLine ("Set: {0} {1} = {2}", key, key.GetHashCode (), value);
217                                 base [key] = value; 
218                         }
219                 }
220         }
221
222         class HttpContextStub2 : HttpContextBase
223         {
224                 public HttpContextStub2 ()
225                         : this (null, null)
226                 {
227                 }
228
229                 public HttpContextStub2 (string requestUrl, string path)
230                         : this (requestUrl, path, null)
231                 {
232                 }
233
234                 public HttpContextStub2 (string requestUrl, string path, string appPath)
235                 {
236                         request = new HttpRequestStub2 (requestUrl, path, appPath);
237                 }
238
239                 Hashtable items = new MyDictionary ();
240                 HttpRequestStub request;
241                 HttpResponseBase response;
242
243                 public override IDictionary Items {
244                         get { return items; }
245                 }
246
247                 public override HttpRequestBase Request {
248                         get { return request ?? base.Request; }
249                 }
250
251                 public override HttpResponseBase Response {
252                         get { return response ?? base.Response; }
253                 }
254
255                 public override void RewritePath (string path)
256                 {
257                         throw new ApplicationException (path);
258                 }
259
260                 public void SetResponse (HttpResponseBase response)
261                 {
262                         this.response = response;
263                 }
264         }
265
266         class HttpRequestStub2 : HttpRequestStub
267         {
268                 public HttpRequestStub2 (string dummyRequestPath, string dummyPath, string appPath)
269                         : base (dummyRequestPath, String.Empty)
270                 {
271                         path = dummyPath;
272                         app_path = appPath;
273                 }
274
275                 string path, app_path;
276
277                 public override string ApplicationPath {
278                         get { return app_path ?? base.ApplicationPath; }
279                 }
280
281                 public override string Path {
282                         get { return path ?? base.Path; }
283                 }
284         }
285
286         public class HttpResponseStub : HttpResponseBase
287         {
288                 public HttpResponseStub ()
289                         : this (0)
290                 {
291                 }
292
293                 int impl_type;
294
295                 public HttpResponseStub (int implType)
296                 {
297                         this.impl_type = implType;
298                 }
299
300                 public override string ApplyAppPathModifier (string virtualPath)
301                 {
302                         switch (impl_type) {
303                                 case 3:
304                                         return virtualPath; // pass thru
305                                 case 2:
306                                         return virtualPath + "_modified";
307                                 case 1:
308                                         throw new ApplicationException (virtualPath);
309                                 default:
310                                         return base.ApplyAppPathModifier (virtualPath);
311                         }
312                 }
313         }
314
315         class HttpContextStub3 : HttpContextStub2
316         {
317                 public HttpContextStub3 (string requestUrl, string path, string appPath, bool supportHandler)
318                         : base (requestUrl, path, appPath)
319                 {
320                         this.support_handler = supportHandler;
321                 }
322
323                 public override void RewritePath (string path)
324                 {
325                         RewrittenPath = path;
326                 }
327
328                 bool support_handler;
329                 public IHttpHandler HttpHandler { get; set; }
330
331                 public override IHttpHandler Handler {
332                         get { return support_handler ? HttpHandler : base.Handler; }
333                         set {
334                                 if (support_handler)
335                                         HttpHandler = value;
336                                 else
337                                         base.Handler = value;
338                         }
339                 }
340
341                 public string RewrittenPath { get; set; }
342         }
343
344         public class MyStopRoutingHandler : StopRoutingHandler
345         {
346                 public IHttpHandler CallGetHttpHandler (RequestContext rc)
347                 {
348                         return GetHttpHandler (rc);
349                 }
350         }
351
352         public class MyUrlRoutingHandler : UrlRoutingHandler
353         {
354                 public void DoProcessRequest (HttpContextBase httpContext)
355                 {
356                         ProcessRequest (httpContext);
357                 }
358
359                 protected override void VerifyAndProcessRequest (IHttpHandler httpHandler, HttpContextBase httpContext)
360                 {
361                         throw new ApplicationException ("MyUrlRoutingHandler");
362                 }
363         }
364
365         public class ErrorRouteHandler : IRouteHandler
366         {
367                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
368                 {
369                         throw new ApplicationException ("ErrorRouteHandler");
370                 }
371         }
372
373         public class MyRouteHandler : IRouteHandler
374         {
375                 public MyRouteHandler ()
376                         : this (new MyHttpHandler ())
377                 {
378                 }
379
380                 public MyRouteHandler (IHttpHandler handler)
381                 {
382                         this.handler = handler;
383                 }
384
385                 IHttpHandler handler;
386
387                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
388                 {
389                         return handler;
390                 }
391         }
392
393         public class MyHttpHandler : IHttpHandler
394         {
395                 public bool IsReusable {
396                         get { return true; }
397                 }
398
399                 public void ProcessRequest (HttpContext ctx)
400                 {
401                         throw new MyException ("HOGE");
402                 }
403         }
404
405         public class MyException : Exception
406         {
407                 public MyException (string msg) : base (msg) {}
408         }
409
410         public class NullRouteHandler : IRouteHandler
411         {
412                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
413                 {
414                         return null;
415                 }
416         }
417
418         public class MyRoute : Route
419         {
420                 public MyRoute (string url, IRouteHandler handler)
421                         : this (url, handler, null)
422                 {
423                 }
424
425                 public MyRoute (string url, IRouteHandler handler, Exception ex)
426                         : base (url, handler)
427                 {
428                         this.ex = ex;
429                 }
430
431                 Exception ex;
432
433                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
434                 {
435                         if (ex != null)
436                                 throw ex;
437                         return base.GetVirtualPath (requestContext, values);
438                 }
439         }
440
441         class MyHttpWorkerRequest : HttpWorkerRequest
442         {
443                 public override void EndOfRequest ()
444                 {
445                         throw new NotImplementedException ();
446                 }
447
448                 public override void FlushResponse (bool b)
449                 {
450                         throw new NotImplementedException ();
451                 }
452
453                 public override string GetHttpVerbName ()
454                 {
455                         throw new NotImplementedException ();
456                 }
457
458                 public override string GetHttpVersion ()
459                 {
460                         throw new NotImplementedException ();
461                 }
462
463                 public override string GetLocalAddress ()
464                 {
465                         throw new NotImplementedException ();
466                 }
467
468                 public override int GetLocalPort ()
469                 {
470                         throw new NotImplementedException ();
471                 }
472
473                 public override string GetQueryString ()
474                 {
475                         throw new NotImplementedException ();
476                 }
477
478                 public override string GetRawUrl ()
479                 {
480                         throw new NotImplementedException ();
481                 }
482
483                 public override string GetRemoteAddress ()
484                 {
485                         throw new NotImplementedException ();
486                 }
487
488                 public override int GetRemotePort ()
489                 {
490                         throw new NotImplementedException ();
491                 }
492
493                 public override string GetUriPath ()
494                 {
495                         return "/Foo/Bar";
496                 }
497
498                 public override void SendKnownResponseHeader (int idx, string v)
499                 {
500                         throw new NotImplementedException ();
501                 }
502
503                 public override void SendResponseFromFile (IntPtr file, long offset, long len)
504                 {
505                         throw new NotImplementedException ();
506                 }
507
508                 public override void SendResponseFromFile (string name, long offset, long len)
509                 {
510                         throw new NotImplementedException ();
511                 }
512
513                 public override void SendResponseFromMemory (byte[] buf, int index)
514                 {
515                         throw new NotImplementedException ();
516                 }
517
518                 public override void SendStatus (int i, string s)
519                 {
520                         throw new NotImplementedException ();
521                 }
522
523                 public override void SendUnknownResponseHeader (string n, string v)
524                 {
525                         throw new NotImplementedException ();
526                 }
527         }
528 }