2009-09-10 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / Utils.cs
index 2bb4fcf9068927ea8f895e04d5790a2e3a823e59..5f0a3378ac12bf51a37993d6c90ed0bf35a4e55b 100644 (file)
@@ -2,13 +2,51 @@
 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 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
@@ -32,18 +70,83 @@ namespace MonoTests.Common
                        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 ();
 
-                       // And get the default model instead, whatever it is
-                       m = MetaModel.Default;
+                       if (defaultModel)
+                               m = MetaModel.Default;
+
+                       Exception exception = null;
+                       MetaModel registered = null;
+
                        try {
-                               m.RegisterContext (model, config);
-                       } catch (InvalidOperationException) {
+                               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)