2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / Utils.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web;
6 using System.Web.DynamicData;
7 using System.Web.DynamicData.ModelProviders;
8 using System.Web.Routing;
9
10 using MonoTests.System.Web.DynamicData;
11
12 namespace MonoTests.Common
13 {
14         public static class Utils
15         {
16                 public static MetaModel CommonInitialize ()
17                 {
18                         return CommonInitialize (false);
19                 }
20
21                 public static MetaModel CommonInitialize (bool myDynamicDataRoute)
22                 {
23                         MetaModel m = MetaModel.Default;
24
25                         var req = new FakeHttpWorkerRequest ();
26                         var ctx = new HttpContext (req);
27                         HttpContext.Current = ctx;
28
29                         RouteCollection routes = RouteTable.Routes;
30                         routes.Clear ();
31                         if (myDynamicDataRoute) {
32                                 routes.Add (
33                                         new MyDynamicDataRoute ("{table}/{action}.aspx") {
34                                                 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
35                                                 Model = m,
36                                                 RouteHandler = new MyDynamicDataRouteHandler ()
37                                         });
38                         } else {
39                                 routes.Add (
40                                         new DynamicDataRoute ("{table}/{action}.aspx") {
41                                                 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
42                                                 Model = m,
43                                                 RouteHandler = new MyDynamicDataRouteHandler ()
44                                         });
45                         }
46
47                         return m;
48                 }
49
50                 public static MetaModel GetModel<ContextType> ()
51                 {
52                         // This is really, really dumb but we need that since if the type has already
53                         // been registered by another test, or tests are re-ran without nunit having
54                         // reloaded the dll we'll get a duplicate entry exception.
55                         MetaModel m;
56                         try {
57                                 m = MetaModel.GetModel (typeof (ContextType));
58                         } catch (InvalidOperationException) {
59                                 m = new MetaModel ();
60                                 m.RegisterContext (typeof (ContextType));
61                         } finally {
62                                 MetaModel.ResetRegistrationException ();
63                         }
64
65                         return m;
66                 }
67
68                 public static void RegisterContext (DataModelProvider model)
69                 {
70                         RegisterContext (model, null);
71                 }
72
73                 public static void RegisterContext (Type contextType)
74                 {
75                         RegisterContext (contextType, null);
76                 }
77
78                 public static void RegisterContext (DataModelProvider model, ContextConfiguration config)
79                 {
80                         RegisterContext (model, config, true);
81                 }
82
83                 public static void RegisterContext (Type contextType, ContextConfiguration config)
84                 {
85                         RegisterContext (contextType, config, true);
86                 }
87                 
88                 public static void RegisterContext (DataModelProvider model, ContextConfiguration config, bool defaultModel)
89                 {
90                         // Just in case no model has been created yet
91                         MetaModel m = new MetaModel ();
92
93                         if (defaultModel)
94                                 m = MetaModel.Default;
95
96                         Exception exception = null;
97                         MetaModel registered = null;
98
99                         try {
100                                 registered = MetaModel.GetModel (model.ContextType);
101                         } catch (Exception) {
102                                 // ignore
103                         }
104
105                         try {
106                                 if (registered == null)
107                                         m.RegisterContext (model, config);
108                         } catch (InvalidOperationException ex) {
109                                 exception = ex;
110                         }
111
112                         if (exception != null) {
113                                 Console.WriteLine ("RegisterContext exception:");
114                                 Console.WriteLine (exception);
115                         }
116                 }
117
118                 public static void RegisterContext (Type contextType, ContextConfiguration config, bool defaultModel)
119                 {
120                         // Just in case no model has been created yet
121                         MetaModel m = new MetaModel ();
122
123                         if (defaultModel)
124                                 m = MetaModel.Default;
125
126                         Exception exception = null;
127                         MetaModel registered = null;
128
129                         try {
130                                 registered = MetaModel.GetModel (contextType);
131                         } catch (Exception) {
132                                 // ignore
133                         }
134
135                         try {
136                                 if (registered == null) {
137                                         if (config != null)
138                                                 m.RegisterContext (contextType, config);
139                                         else
140                                                 m.RegisterContext (contextType);
141                                 }
142                         } catch (InvalidOperationException ex) {
143                                 exception = ex;
144                         }
145
146                         if (exception != null) {
147                                 Console.WriteLine ("RegisterContext exception:");
148                                 Console.WriteLine (exception);
149                         }
150                 }
151
152                 public static string BuildActionName (MetaTable table, string action)
153                 {
154                         return "/" + table.Name + "/" + action + ".aspx";
155                 }
156
157                 public static string BuildActionName (MetaTable table, string action, string query)
158                 {
159                         string ret = "/" + table.Name + "/" + action + ".aspx";
160                         if (!String.IsNullOrEmpty (query))
161                                 ret += "?" + query;
162                         return ret;
163                 }
164         }
165 }