| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Web.DynamicData;
- using System.Web.DynamicData.ModelProviders;
- using System.Web.Routing;
- using MonoTests.System.Web.DynamicData;
- namespace MonoTests.Common
- {
- static class Utils
- {
- public static MetaModel CommonInitialize ()
- {
- return CommonInitialize (false);
- }
- public static MetaModel CommonInitialize (bool myDynamicDataRoute)
- {
- MetaModel m = MetaModel.Default;
- var req = new FakeHttpWorkerRequest ();
- var ctx = new HttpContext (req);
- HttpContext.Current = ctx;
- RouteCollection routes = RouteTable.Routes;
- routes.Clear ();
- if (myDynamicDataRoute) {
- routes.Add (
- new MyDynamicDataRoute ("{table}/{action}.aspx") {
- Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
- Model = m,
- RouteHandler = new MyDynamicDataRouteHandler ()
- });
- } else {
- routes.Add (
- new DynamicDataRoute ("{table}/{action}.aspx") {
- Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
- Model = m,
- RouteHandler = new MyDynamicDataRouteHandler ()
- });
- }
- return m;
- }
- public static MetaModel GetModel<ContextType> ()
- {
- // This is really, really dumb but we need that since if the type has already
- // been registered by another test, or tests are re-ran without nunit having
- // reloaded the dll we'll get a duplicate entry exception.
- MetaModel m;
- try {
- m = MetaModel.GetModel (typeof (ContextType));
- } catch (InvalidOperationException) {
- m = new MetaModel ();
- m.RegisterContext (typeof (ContextType));
- } finally {
- MetaModel.ResetRegistrationException ();
- }
- return m;
- }
- public static void RegisterContext (DataModelProvider model)
- {
- RegisterContext (model, null);
- }
- public static void RegisterContext (Type contextType)
- {
- RegisterContext (contextType, null);
- }
- public static void RegisterContext (DataModelProvider model, ContextConfiguration config)
- {
- RegisterContext (model, config, true);
- }
- public static void RegisterContext (Type contextType, ContextConfiguration config)
- {
- RegisterContext (contextType, config, true);
- }
-
- public static void RegisterContext (DataModelProvider model, ContextConfiguration config, bool defaultModel)
- {
- // Just in case no model has been created yet
- MetaModel m = new MetaModel ();
- if (defaultModel)
- m = MetaModel.Default;
- Exception exception = null;
- MetaModel registered = null;
- try {
- registered = MetaModel.GetModel (model.ContextType);
- } catch (Exception) {
- // ignore
- }
- try {
- if (registered == null)
- m.RegisterContext (model, config);
- } catch (InvalidOperationException ex) {
- exception = ex;
- }
- if (exception != null) {
- Console.WriteLine ("RegisterContext exception:");
- Console.WriteLine (exception);
- }
- }
- public static void RegisterContext (Type contextType, ContextConfiguration config, bool defaultModel)
- {
- // Just in case no model has been created yet
- MetaModel m = new MetaModel ();
- if (defaultModel)
- m = MetaModel.Default;
- Exception exception = null;
- MetaModel registered = null;
- try {
- registered = MetaModel.GetModel (contextType);
- } catch (Exception) {
- // ignore
- }
- try {
- if (registered == null) {
- if (config != null)
- m.RegisterContext (contextType, config);
- else
- m.RegisterContext (contextType);
- }
- } catch (InvalidOperationException ex) {
- exception = ex;
- }
- if (exception != null) {
- Console.WriteLine ("RegisterContext exception:");
- Console.WriteLine (exception);
- }
- }
- public static string BuildActionName (MetaTable table, string action)
- {
- return "/" + table.Name + "/" + action + ".aspx";
- }
- public static string BuildActionName (MetaTable table, string action, string query)
- {
- string ret = "/" + table.Name + "/" + action + ".aspx";
- if (!String.IsNullOrEmpty (query))
- ret += "?" + query;
- return ret;
- }
- }
- }
|