2 using System.Collections.Generic;
6 using System.Web.DynamicData;
7 using System.Web.DynamicData.ModelProviders;
8 using System.Web.Routing;
10 using MonoTests.System.Web.DynamicData;
12 namespace MonoTests.Common
14 public static class Utils
16 public static MetaModel CommonInitialize ()
18 return CommonInitialize (false);
21 public static MetaModel CommonInitialize (bool myDynamicDataRoute)
23 MetaModel m = MetaModel.Default;
25 var req = new FakeHttpWorkerRequest ();
26 var ctx = new HttpContext (req);
27 HttpContext.Current = ctx;
29 RouteCollection routes = RouteTable.Routes;
31 if (myDynamicDataRoute) {
33 new MyDynamicDataRoute ("{table}/{action}.aspx") {
34 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
36 RouteHandler = new MyDynamicDataRouteHandler ()
40 new DynamicDataRoute ("{table}/{action}.aspx") {
41 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
43 RouteHandler = new MyDynamicDataRouteHandler ()
50 public static MetaModel GetModel<ContextType> ()
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.
57 m = MetaModel.GetModel (typeof (ContextType));
58 } catch (InvalidOperationException) {
60 m.RegisterContext (typeof (ContextType));
62 MetaModel.ResetRegistrationException ();
68 public static void RegisterContext (DataModelProvider model)
70 RegisterContext (model, null);
73 public static void RegisterContext (Type contextType)
75 RegisterContext (contextType, null);
78 public static void RegisterContext (DataModelProvider model, ContextConfiguration config)
80 RegisterContext (model, config, true);
83 public static void RegisterContext (Type contextType, ContextConfiguration config)
85 RegisterContext (contextType, config, true);
88 public static void RegisterContext (DataModelProvider model, ContextConfiguration config, bool defaultModel)
90 // Just in case no model has been created yet
91 MetaModel m = new MetaModel ();
94 m = MetaModel.Default;
96 Exception exception = null;
97 MetaModel registered = null;
100 registered = MetaModel.GetModel (model.ContextType);
101 } catch (Exception) {
106 if (registered == null)
107 m.RegisterContext (model, config);
108 } catch (InvalidOperationException ex) {
112 if (exception != null) {
113 Console.WriteLine ("RegisterContext exception:");
114 Console.WriteLine (exception);
118 public static void RegisterContext (Type contextType, ContextConfiguration config, bool defaultModel)
120 // Just in case no model has been created yet
121 MetaModel m = new MetaModel ();
124 m = MetaModel.Default;
126 Exception exception = null;
127 MetaModel registered = null;
130 registered = MetaModel.GetModel (contextType);
131 } catch (Exception) {
136 if (registered == null) {
138 m.RegisterContext (contextType, config);
140 m.RegisterContext (contextType);
142 } catch (InvalidOperationException ex) {
146 if (exception != null) {
147 Console.WriteLine ("RegisterContext exception:");
148 Console.WriteLine (exception);
152 public static string BuildActionName (MetaTable table, string action)
154 return "/" + table.Name + "/" + action + ".aspx";
157 public static string BuildActionName (MetaTable table, string action, string query)
159 string ret = "/" + table.Name + "/" + action + ".aspx";
160 if (!String.IsNullOrEmpty (query))