2009-06-12 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web.DynamicData / Test / System.Web.DynamicData / MetaModelTest.cs
index 23129d5597e513c3662e15db0391970905078cd3..0b28dc26113917f22a7c53389917db4797a792fa 100644 (file)
@@ -58,277 +58,258 @@ using MetaTable = System.Web.DynamicData.MetaTable;
 
 namespace MonoTests.System.Web.DynamicData
 {
-    [TestFixture]
-    public class MetaModelTest
-    {
-        static MetaModel defaultModel;
-
-        static MetaModelTest ()
-        {
-            defaultModel = new MetaModel ();
-        }
-
-        [Test]
-        public void DefaultValues ()
-        {
-            var model = new MetaModel ();
-
-            Assert.IsNotNull (MetaModel.Default, "#A1");
-            Assert.IsTrue (MetaModel.Default == defaultModel, "#A2");
-            Assert.IsNotNull (model.Tables, "#A3");
-            Assert.IsNotNull (model.VisibleTables, "#A4");
-            Assert.IsNotNull (model.FieldTemplateFactory, "#A5");
-            Assert.AreEqual ("~/DynamicData/", model.DynamicDataFolderVirtualPath, "#A6");
-
-            Assert.AreEqual (0, model.VisibleTables.Count, "#B1");
-            Assert.AreEqual (0, model.Tables.Count, "#B2");
-            Assert.AreEqual (typeof (FieldTemplateFactory), model.FieldTemplateFactory.GetType (), "#B3");
-        }
-
-        [Test]
-        [ExpectedException (typeof (ArgumentNullException))]
-        public void GetTableNull ()
-        {
-            new MetaModel ().GetTable ((Type) null);
-        }
-
-        [Test]
-        public void RegisterContext ()
-        {
-            var m = new MetaModel ();
-            try {
-                m.RegisterContext (typeof (Foo));
-                Assert.Fail ("#1");
-            } catch (TargetInvocationException ex) {
-                Assert.AreEqual ("ERROR", ex.InnerException.Message, "#2");
-            } finally {
-                MetaModel.ResetRegistrationException ();
-            }
-        }
-
-        [Test]
-        [ExpectedException (typeof (ArgumentException))]
-        public void RegisterContext2 ()
-        {
-            try {
-                var m = new MetaModel ();
-                m.RegisterContext (typeof (Bar)); // not supported
-            } finally {
-                MetaModel.ResetRegistrationException ();
-            }
-        }
-
-        [Test]
-        [ExpectedException (typeof (MissingMethodException))]
-        public void RegisterContext3 ()
-        {
-            var m = new MetaModel ();
-            try {
-                // no public constructor
-                m.RegisterContext (typeof (DataContext));
-            } finally {
-                MetaModel.ResetRegistrationException ();
-            }
-        }
-
-        [Test]
-        public void RegisterContext4 ()
-        {
-            MetaModel m = GetModel<MyDataContext1> ();
-            Assert.AreEqual (0, m.Tables.Count, "#1-1");
-            Assert.AreEqual (0, m.VisibleTables.Count, "#1-2");
-            Assert.IsNotNull (MetaModel.GetModel (typeof (MyDataContext1)), "#2");
-        }
-
-        [Test]
-        public void RegisterContext5 ()
-        {
-            // In the process of several experiments (as the docs lack any good explanation),
-            // I determined that this test needs the following for succesful completion:
-            //
-            //  - a worker request
-            //  - a HttpContext
-            //  - a fake route handler derived from DynamicDataRouteHandler, so that its CreateHandler
-            //    returns a handler without attempting to actually find a View for the requested action
-            //  - a route which can match table actions (taken from the skeleton DynamicData project
-            //    generated by VisualStudio)
-            //  - _empty_ query string returned from the fake worker request, or .NET will populate the
-            //    HttpRequest.QueryString collection with one null item, and .NET's DynamicData will happily
-            //    assume any entry in that collection is not null (it seems null checks aren't very popular
-            //    in DynamicData code)
-            var req = new FakeHttpWorkerRequest ();
-            var ctx = new HttpContext (req);
-            HttpContext.Current = ctx;
-            MetaModel m = GetModel <MyDataContext2> ();
-
-            RouteTable.Routes.Add (
-                new DynamicDataRoute ("{table}/{action}.aspx") {
-                    Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
-                    Model = m,
-                    RouteHandler = new MyDynamicDataRouteHandler ()
-                });
-
-
-            MetaTable t = m.Tables[0];
-
-            Assert.AreEqual (1, m.Tables.Count, "#1-1");
-            Assert.AreEqual (1, m.VisibleTables.Count, "#1-2");
-            Assert.AreEqual (typeof (Foo), t.EntityType, "#1-3");
-
-            // Those names are only the last part before '.' (i.e. without schema name).
-            Assert.AreEqual ("FooTable", t.Name, "#2-1");
-            Assert.AreEqual ("FooTable", t.DisplayName, "#2-2");
-            Assert.AreEqual ("FooTable", t.DataContextPropertyName, "#2-3");
-            Assert.AreEqual ("/FooTable/List.aspx", t.ListActionPath, "#2-4");
-
-            Assert.AreEqual ("FooTable", t.Provider.Name, "#3-1");
-        }
-
-        [Test]
-        public void ResetRegistrationException ()
-        {
-            MetaModel.ResetRegistrationException ();
-
-            var m = new MetaModel ();
-            try {
-                m.RegisterContext (typeof (Foo));
-                Assert.Fail ("#1");
-            } catch (TargetInvocationException) {
-            }
-
-            try {
-                m.RegisterContext (typeof (MyDataContext1));
-                Assert.Fail ("#2");
-            } catch (InvalidOperationException) {
-            } finally {
-                MetaModel.ResetRegistrationException ();
-            }
-        }
-
-        [Test]
-        [ExpectedException (typeof (ArgumentException))]
-        public void GetTableObject ()
-        {
-            // entity type 'System.Object' not found.
-            new MetaModel ().GetTable (typeof (object));
-        }
-
-        [Test]
-        public void GetModel ()
-        {
-            GetModel<UseOnlyInGetModelTestDataContext> ();
-            AssertExtensions.Throws<ArgumentNullException> (() => MetaModel.GetModel (null), "#A1");
-            AssertExtensions.Throws<InvalidOperationException> (() => MetaModel.GetModel (typeof (object)), "#A2");
-            Assert.IsNotNull (MetaModel.GetModel (typeof (UseOnlyInGetModelTestDataContext)));
-        }
-
-        [Test]
-        public void TryGetTable ()
-        {
-            MetaModel m = GetModel <MyDataContext2> ();
-            MetaTable t;
-
-            AssertExtensions.Throws<ArgumentNullException> (() => m.TryGetTable (null, out t), "#A1");
-
-            Assert.IsTrue (m.TryGetTable ("FooTable", out t), "#B1");
-            Assert.IsNotNull (t, "#B2");
-            Assert.AreEqual (typeof (Foo), t.EntityType, "#B3");
-
-            Assert.IsFalse (m.TryGetTable (String.Empty, out t), "#C1");
-            Assert.IsNull (t, "#C2");
-            Assert.IsFalse (m.TryGetTable ("NoSuchTable", out t), "#C3");
-            Assert.IsNull (t, "#C4");
-        }
-
-        [Test]
-        public void GetTable ()
-        {
-            MetaModel m = GetModel<MyDataContext2> ();
-            MetaTable t;
-            string str = null;
-            Type type = null;
-
-            AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (str), "#A1");
-            AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (type), "#A2");
-            AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (null, null), "#A3");
-            AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (null, typeof (Foo)), "#A4");
-            AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable ("FooTable", null), "#A5");
-
-            AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable (String.Empty), "#B1");
-            AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("NoSuchName"), "#B2");
-            AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable (typeof (object)), "#B3");
-            AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("FooTable", typeof (object)), "#B4");
-            AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("NoSuchTable", typeof (object)), "#B5");
-
-            Assert.IsNotNull (t = m.GetTable ("FooTable"), "#C1");
-            Assert.AreEqual (typeof (Foo), t.EntityType, "#C2");
-            Assert.IsNotNull (t = m.GetTable (typeof (Foo)), "#C3");
-            Assert.AreEqual (typeof (Foo), t.EntityType, "#C4");
-            Assert.IsNotNull (t = m.GetTable ("FooTable", typeof (MyDataContext2)), "#C5");
-            Assert.AreEqual (typeof (Foo), t.EntityType, "#C6");
-        }
-
-//        [Test]
-//        public void GetActionPath ()
-//        {
-//            var foo = new Foo (true);
-//            MetaModel m = GetModel<MyDataContext2> ();
-
-            // None of those are thrown from GetTable - it seems this method performs NO checks at all, sigh...
-            //
-            //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath (null, PageAction.List, foo), "#A1");
-            //AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath (String.Empty, PageAction.List, foo), "#A2");
-            //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath ("FooTable", null, foo), "#A3");
-            //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath ("FooTable", PageAction.List, null), "#A4");
-            //AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath ("NoSuchTable", PageAction.List, foo), "#A5");
-//        }
-
-        [Test]
-        public void GetActionPath2 ()
-        {
-            var foo = new Foo (true);
-            var req = new FakeHttpWorkerRequest ();
-            var ctx = new HttpContext (req);
-            HttpContext.Current = ctx;
-            MetaModel m = GetModel<MyDataContext2> ();
-
-            RouteTable.Routes.Add (
-                new DynamicDataRoute ("{table}/{action}.aspx") {
-                    Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
-                    Model = m,
-                    RouteHandler = new MyDynamicDataRouteHandler ()
-                });
-
-            // .NET stacktrace:
-            //
-            // at System.Web.DynamicData.MetaModel.TryGetTable(String uniqueTableName, MetaTable& table)
-            // at System.Web.DynamicData.MetaModel.GetTable(String uniqueTableName)
-            AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath (null, PageAction.List, foo), "#A1");
-            Assert.AreEqual (String.Empty, m.GetActionPath ("FooTable", null, foo), "#A2");
-            Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", PageAction.List, null), "#A3");
-            AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath ("NoSuchTable", PageAction.List, foo), "#A4");
-
-            Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", "List", foo), "#B1");
-        }
-
-        #region Helper methods
-        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;
-        }
-        #endregion
-    }
+       [TestFixture]
+       public class MetaModelTest
+       {
+               static MetaModel defaultModel;
+
+               static MetaModelTest ()
+               {
+                       defaultModel = new MetaModel ();
+               }
+
+               [Test]
+               public void DefaultValues ()
+               {
+                       var model = new MetaModel ();
+
+                       Assert.IsNotNull (MetaModel.Default, "#A1");
+
+                       // We can't be sure which model will be the default one when running under Nunit
+                       //Assert.IsTrue (MetaModel.Default == defaultModel, "#A2");
+                       Assert.IsNotNull (model.Tables, "#A3");
+                       Assert.IsNotNull (model.VisibleTables, "#A4");
+                       Assert.IsNotNull (model.FieldTemplateFactory, "#A5");
+                       Assert.AreEqual ("~/DynamicData/", model.DynamicDataFolderVirtualPath, "#A6");
+
+                       Assert.AreEqual (0, model.VisibleTables.Count, "#B1");
+                       Assert.AreEqual (0, model.Tables.Count, "#B2");
+                       Assert.AreEqual (typeof (FieldTemplateFactory), model.FieldTemplateFactory.GetType (), "#B3");
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void GetTableNull ()
+               {
+                       new MetaModel ().GetTable ((Type) null);
+               }
+
+               [Test]
+               public void RegisterContext ()
+               {
+                       var m = new MetaModel ();
+                       try {
+                               m.RegisterContext (typeof (Foo));
+                               Assert.Fail ("#1");
+                       } catch (TargetInvocationException ex) {
+                               Assert.AreEqual ("ERROR", ex.InnerException.Message, "#2");
+                       } finally {
+                               MetaModel.ResetRegistrationException ();
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void RegisterContext2 ()
+               {
+                       try {
+                               var m = new MetaModel ();
+                               m.RegisterContext (typeof (Bar)); // not supported
+                       } finally {
+                               MetaModel.ResetRegistrationException ();
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (MissingMethodException))]
+               public void RegisterContext3 ()
+               {
+                       var m = new MetaModel ();
+                       try {
+                               // no public constructor
+                               m.RegisterContext (typeof (DataContext));
+                       } finally {
+                               MetaModel.ResetRegistrationException ();
+                       }
+               }
+
+               [Test]
+               public void RegisterContext4 ()
+               {
+                       MetaModel m = Utils.GetModel<MyDataContext1> ();
+                       Assert.AreEqual (0, m.Tables.Count, "#1-1");
+                       Assert.AreEqual (0, m.VisibleTables.Count, "#1-2");
+                       Assert.IsNotNull (MetaModel.GetModel (typeof (MyDataContext1)), "#2");
+               }
+
+               [Test]
+               public void RegisterContext5 ()
+               {
+                       // In the process of several experiments (as the docs lack any good explanation),
+                       // I determined that this test needs the following for succesful completion:
+                       //
+                       //  - a worker request
+                       //  - a HttpContext
+                       //  - a fake route handler derived from DynamicDataRouteHandler, so that its CreateHandler
+                       //    returns a handler without attempting to actually find a View for the requested action
+                       //  - a route which can match table actions (taken from the skeleton DynamicData project
+                       //    generated by VisualStudio)
+                       //  - _empty_ query string returned from the fake worker request, or .NET will populate the
+                       //    HttpRequest.QueryString collection with one null item, and .NET's DynamicData will happily
+                       //    assume any entry in that collection is not null (it seems null checks aren't very popular
+                       //    in DynamicData code)
+                       var req = new FakeHttpWorkerRequest ();
+                       var ctx = new HttpContext (req);
+                       HttpContext.Current = ctx;
+                       MetaModel m = Utils.GetModel<MyDataContext2> ();
+
+                       RouteTable.Routes.Add (
+                           new DynamicDataRoute ("{table}/{action}.aspx") {
+                                   Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
+                                   Model = m,
+                                   RouteHandler = new MyDynamicDataRouteHandler ()
+                           });
+
+                       MetaTable t = m.Tables[0];
+
+                       Assert.AreEqual (1, m.Tables.Count, "#1-1");
+                       Assert.AreEqual (1, m.VisibleTables.Count, "#1-2");
+                       Assert.AreEqual (typeof (Foo), t.EntityType, "#1-3");
+
+                       // Those names are only the last part before '.' (i.e. without schema name).
+                       Assert.AreEqual ("FooTable", t.Name, "#2-1");
+                       Assert.AreEqual ("FooTable", t.DisplayName, "#2-2");
+                       Assert.AreEqual ("FooTable", t.DataContextPropertyName, "#2-3");
+                       Assert.AreEqual ("/FooTable/List.aspx", t.ListActionPath, "#2-4");
+
+                       Assert.AreEqual ("FooTable", t.Provider.Name, "#3-1");
+               }
+
+               [Test]
+               public void ResetRegistrationException ()
+               {
+                       MetaModel.ResetRegistrationException ();
+
+                       var m = new MetaModel ();
+                       try {
+                               m.RegisterContext (typeof (Foo));
+                               Assert.Fail ("#1");
+                       } catch (TargetInvocationException) {
+                       }
+
+                       try {
+                               m.RegisterContext (typeof (MyDataContext1));
+                               Assert.Fail ("#2");
+                       } catch (InvalidOperationException) {
+                       } finally {
+                               MetaModel.ResetRegistrationException ();
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void GetTableObject ()
+               {
+                       // entity type 'System.Object' not found.
+                       new MetaModel ().GetTable (typeof (object));
+               }
+
+               [Test]
+               public void GetModel ()
+               {
+                       Utils.GetModel<UseOnlyInGetModelTestDataContext> ();
+                       AssertExtensions.Throws<ArgumentNullException> (() => MetaModel.GetModel (null), "#A1");
+                       AssertExtensions.Throws<InvalidOperationException> (() => MetaModel.GetModel (typeof (object)), "#A2");
+                       Assert.IsNotNull (MetaModel.GetModel (typeof (UseOnlyInGetModelTestDataContext)));
+               }
+
+               [Test]
+               public void TryGetTable ()
+               {
+                       MetaModel m = Utils.GetModel<MyDataContext2> ();
+                       MetaTable t;
+
+                       AssertExtensions.Throws<ArgumentNullException> (() => m.TryGetTable (null, out t), "#A1");
+
+                       Assert.IsTrue (m.TryGetTable ("FooTable", out t), "#B1");
+                       Assert.IsNotNull (t, "#B2");
+                       Assert.AreEqual (typeof (Foo), t.EntityType, "#B3");
+
+                       Assert.IsFalse (m.TryGetTable (String.Empty, out t), "#C1");
+                       Assert.IsNull (t, "#C2");
+                       Assert.IsFalse (m.TryGetTable ("NoSuchTable", out t), "#C3");
+                       Assert.IsNull (t, "#C4");
+               }
+
+               [Test]
+               public void GetTable ()
+               {
+                       MetaModel m = Utils.GetModel<MyDataContext2> ();
+                       MetaTable t;
+                       string str = null;
+                       Type type = null;
+
+                       AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (str), "#A1");
+                       AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (type), "#A2");
+                       AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (null, null), "#A3");
+                       AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (null, typeof (Foo)), "#A4");
+                       AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable ("FooTable", null), "#A5");
+
+                       AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable (String.Empty), "#B1");
+                       AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("NoSuchName"), "#B2");
+                       AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable (typeof (object)), "#B3");
+                       AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("FooTable", typeof (object)), "#B4");
+                       AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("NoSuchTable", typeof (object)), "#B5");
+
+                       Assert.IsNotNull (t = m.GetTable ("FooTable"), "#C1");
+                       Assert.AreEqual (typeof (Foo), t.EntityType, "#C2");
+                       Assert.IsNotNull (t = m.GetTable (typeof (Foo)), "#C3");
+                       Assert.AreEqual (typeof (Foo), t.EntityType, "#C4");
+                       Assert.IsNotNull (t = m.GetTable ("FooTable", typeof (MyDataContext2)), "#C5");
+                       Assert.AreEqual (typeof (Foo), t.EntityType, "#C6");
+               }
+
+               [Test]
+               public void GetActionPath ()
+               {
+                       var foo = new Foo (true);
+                       MetaModel m = Utils.GetModel<MyDataContext2> ();
+
+                       // None of those are thrown from GetTable - it seems this method performs NO checks at all, sigh...
+                       //
+                       //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath (null, PageAction.List, foo), "#A1");
+                       //AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath (String.Empty, PageAction.List, foo), "#A2");
+                       //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath ("FooTable", null, foo), "#A3");
+                       //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath ("FooTable", PageAction.List, null), "#A4");
+                       //AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath ("NoSuchTable", PageAction.List, foo), "#A5");
+               }
+
+               [Test]
+               public void GetActionPath2 ()
+               {
+                       var foo = new Foo (true);
+                       var req = new FakeHttpWorkerRequest ();
+                       var ctx = new HttpContext (req);
+                       HttpContext.Current = ctx;
+                       MetaModel m = Utils.GetModel<MyDataContext2> ();
+
+                       RouteTable.Routes.Add (
+                           new DynamicDataRoute ("{table}/{action}.aspx") {
+                                   Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
+                                   Model = m,
+                                   RouteHandler = new MyDynamicDataRouteHandler ()
+                           });
+
+                       // .NET stacktrace:
+                       //
+                       // at System.Web.DynamicData.MetaModel.TryGetTable(String uniqueTableName, MetaTable& table)
+                       // at System.Web.DynamicData.MetaModel.GetTable(String uniqueTableName)
+                       AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath (null, PageAction.List, foo), "#A1");
+                       Assert.AreEqual (String.Empty, m.GetActionPath ("FooTable", null, foo), "#A2");
+                       Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", PageAction.List, null), "#A3");
+                       AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath ("NoSuchTable", PageAction.List, foo), "#A4");
+
+                       Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", "List", foo), "#B1");
+               }
+       }
 }