PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web.DynamicData/Test/System.Web.DynamicData/MetaModelTest.cs

https://bitbucket.org/danipen/mono
C# | 335 lines | 236 code | 41 blank | 58 comment | 0 complexity | 0d3ff9fe6899f774dc958514c6ce0e23 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // MetaModelTest.cs
  3. //
  4. // Authors:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  6. // Marek Habersack <mhabersack@novell.com>
  7. //
  8. // Copyright (C) 2008-2009 Novell Inc. http://novell.com
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Collections.ObjectModel;
  34. using System.Collections.Specialized;
  35. using System.ComponentModel.DataAnnotations;
  36. using System.Data.SqlClient;
  37. using System.Data.Linq;
  38. using System.Data.Linq.Mapping;
  39. using System.Globalization;
  40. using System.Reflection;
  41. using System.Security.Permissions;
  42. using System.Security.Principal;
  43. using System.Web;
  44. using System.Web.UI;
  45. using System.Web.DynamicData;
  46. using System.Web.DynamicData.ModelProviders;
  47. using System.Web.Routing;
  48. using NUnit.Framework;
  49. using NUnit.Mocks;
  50. using MonoTests.stand_alone.WebHarness;
  51. using MonoTests.SystemWeb.Framework;
  52. using MonoTests.Common;
  53. using MetaModel = System.Web.DynamicData.MetaModel;
  54. using MetaTable = System.Web.DynamicData.MetaTable;
  55. namespace MonoTests.System.Web.DynamicData
  56. {
  57. [TestFixture]
  58. public class MetaModelTest
  59. {
  60. static MetaModel defaultModel;
  61. static MetaModelTest ()
  62. {
  63. defaultModel = new MetaModel ();
  64. }
  65. [Test]
  66. public void DefaultValues ()
  67. {
  68. var model = new MetaModel ();
  69. Assert.IsNotNull (MetaModel.Default, "#A1");
  70. // We can't be sure which model will be the default one when running under Nunit
  71. //Assert.IsTrue (MetaModel.Default == defaultModel, "#A2");
  72. Assert.IsNotNull (model.Tables, "#A3");
  73. Assert.IsNotNull (model.VisibleTables, "#A4");
  74. Assert.IsNotNull (model.FieldTemplateFactory, "#A5");
  75. Assert.AreEqual ("~/DynamicData/", model.DynamicDataFolderVirtualPath, "#A6");
  76. Assert.AreEqual (0, model.VisibleTables.Count, "#B1");
  77. Assert.AreEqual (0, model.Tables.Count, "#B2");
  78. Assert.AreEqual (typeof (FieldTemplateFactory), model.FieldTemplateFactory.GetType (), "#B3");
  79. }
  80. [Test]
  81. public void DynamicDataFolderVirtualPath ()
  82. {
  83. var model = new MetaModel ();
  84. Assert.AreEqual ("~/DynamicData/", model.DynamicDataFolderVirtualPath, "#A1");
  85. model.DynamicDataFolderVirtualPath = null;
  86. Assert.AreEqual ("~/DynamicData/", model.DynamicDataFolderVirtualPath, "#A2");
  87. model.DynamicDataFolderVirtualPath = String.Empty;
  88. Assert.AreEqual (String.Empty, model.DynamicDataFolderVirtualPath, "#A3");
  89. model.DynamicDataFolderVirtualPath = "~/FolderNoTrailingSlash";
  90. Assert.AreEqual ("~/FolderNoTrailingSlash/", model.DynamicDataFolderVirtualPath, "#A4");
  91. model.DynamicDataFolderVirtualPath = "AnotherFolder";
  92. Assert.AreEqual ("AnotherFolder/", model.DynamicDataFolderVirtualPath, "#A5");
  93. model.DynamicDataFolderVirtualPath = "/YetAnotherFolder";
  94. Assert.AreEqual ("/YetAnotherFolder/", model.DynamicDataFolderVirtualPath, "#A6");
  95. model.DynamicDataFolderVirtualPath = null;
  96. Assert.AreEqual ("~/DynamicData/", model.DynamicDataFolderVirtualPath, "#A7");
  97. }
  98. [Test]
  99. [ExpectedException (typeof (ArgumentNullException))]
  100. public void GetTableNull ()
  101. {
  102. new MetaModel ().GetTable ((Type) null);
  103. }
  104. [Test]
  105. public void RegisterContext ()
  106. {
  107. var m = new MetaModel ();
  108. try {
  109. m.RegisterContext (typeof (Foo));
  110. Assert.Fail ("#1");
  111. } catch (TargetInvocationException ex) {
  112. Assert.AreEqual ("ERROR", ex.InnerException.Message, "#2");
  113. } finally {
  114. MetaModel.ResetRegistrationException ();
  115. }
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentException))]
  119. public void RegisterContext2 ()
  120. {
  121. try {
  122. var m = new MetaModel ();
  123. m.RegisterContext (typeof (Bar)); // not supported
  124. } finally {
  125. MetaModel.ResetRegistrationException ();
  126. }
  127. }
  128. [Test]
  129. [ExpectedException (typeof (MissingMethodException))]
  130. public void RegisterContext3 ()
  131. {
  132. var m = new MetaModel ();
  133. try {
  134. // no public constructor
  135. m.RegisterContext (typeof (DataContext));
  136. } finally {
  137. MetaModel.ResetRegistrationException ();
  138. }
  139. }
  140. [Test]
  141. public void RegisterContext4 ()
  142. {
  143. MetaModel m = Utils.GetModel<MyDataContext1> ();
  144. Assert.AreEqual (0, m.Tables.Count, "#1-1");
  145. Assert.AreEqual (0, m.VisibleTables.Count, "#1-2");
  146. Assert.IsNotNull (MetaModel.GetModel (typeof (MyDataContext1)), "#2");
  147. }
  148. [Test]
  149. public void RegisterContext5 ()
  150. {
  151. // In the process of several experiments (as the docs lack any good explanation),
  152. // I determined that this test needs the following for succesful completion:
  153. //
  154. // - a worker request
  155. // - a HttpContext
  156. // - a fake route handler derived from DynamicDataRouteHandler, so that its CreateHandler
  157. // returns a handler without attempting to actually find a View for the requested action
  158. // - a route which can match table actions (taken from the skeleton DynamicData project
  159. // generated by VisualStudio)
  160. // - _empty_ query string returned from the fake worker request, or .NET will populate the
  161. // HttpRequest.QueryString collection with one null item, and .NET's DynamicData will happily
  162. // assume any entry in that collection is not null (it seems null checks aren't very popular
  163. // in DynamicData code)
  164. var req = new FakeHttpWorkerRequest ();
  165. var ctx = new HttpContext (req);
  166. HttpContext.Current = ctx;
  167. MetaModel m = Utils.GetModel<MyDataContext2> ();
  168. RouteTable.Routes.Add (
  169. new DynamicDataRoute ("{table}/{action}.aspx") {
  170. Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
  171. Model = m,
  172. RouteHandler = new MyDynamicDataRouteHandler ()
  173. });
  174. MetaTable t = m.Tables[0];
  175. Assert.AreEqual (1, m.Tables.Count, "#1-1");
  176. Assert.AreEqual (1, m.VisibleTables.Count, "#1-2");
  177. Assert.AreEqual (typeof (Foo), t.EntityType, "#1-3");
  178. // Those names are only the last part before '.' (i.e. without schema name).
  179. Assert.AreEqual ("FooTable", t.Name, "#2-1");
  180. Assert.AreEqual ("FooTable", t.DisplayName, "#2-2");
  181. Assert.AreEqual ("FooTable", t.DataContextPropertyName, "#2-3");
  182. Assert.AreEqual ("/FooTable/List.aspx", t.ListActionPath, "#2-4");
  183. Assert.AreEqual ("FooTable", t.Provider.Name, "#3-1");
  184. }
  185. [Test]
  186. public void ResetRegistrationException ()
  187. {
  188. MetaModel.ResetRegistrationException ();
  189. var m = new MetaModel ();
  190. try {
  191. m.RegisterContext (typeof (Foo));
  192. Assert.Fail ("#1");
  193. } catch (TargetInvocationException) {
  194. }
  195. try {
  196. m.RegisterContext (typeof (MyDataContext1));
  197. Assert.Fail ("#2");
  198. } catch (InvalidOperationException) {
  199. } finally {
  200. MetaModel.ResetRegistrationException ();
  201. }
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void GetTableObject ()
  206. {
  207. // entity type 'System.Object' not found.
  208. new MetaModel ().GetTable (typeof (object));
  209. }
  210. [Test]
  211. public void GetModel ()
  212. {
  213. Utils.GetModel<UseOnlyInGetModelTestDataContext> ();
  214. AssertExtensions.Throws<ArgumentNullException> (() => MetaModel.GetModel (null), "#A1");
  215. AssertExtensions.Throws<InvalidOperationException> (() => MetaModel.GetModel (typeof (object)), "#A2");
  216. Assert.IsNotNull (MetaModel.GetModel (typeof (UseOnlyInGetModelTestDataContext)));
  217. }
  218. [Test]
  219. public void TryGetTable ()
  220. {
  221. MetaModel m = Utils.GetModel<MyDataContext2> ();
  222. MetaTable t;
  223. AssertExtensions.Throws<ArgumentNullException> (() => m.TryGetTable (null, out t), "#A1");
  224. Assert.IsTrue (m.TryGetTable ("FooTable", out t), "#B1");
  225. Assert.IsNotNull (t, "#B2");
  226. Assert.AreEqual (typeof (Foo), t.EntityType, "#B3");
  227. Assert.IsFalse (m.TryGetTable (String.Empty, out t), "#C1");
  228. Assert.IsNull (t, "#C2");
  229. Assert.IsFalse (m.TryGetTable ("NoSuchTable", out t), "#C3");
  230. Assert.IsNull (t, "#C4");
  231. }
  232. [Test]
  233. public void GetTable ()
  234. {
  235. MetaModel m = Utils.GetModel<MyDataContext2> ();
  236. MetaTable t;
  237. string str = null;
  238. Type type = null;
  239. AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (str), "#A1");
  240. AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (type), "#A2");
  241. AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (null, null), "#A3");
  242. AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable (null, typeof (Foo)), "#A4");
  243. AssertExtensions.Throws<ArgumentNullException> (() => t = m.GetTable ("FooTable", null), "#A5");
  244. AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable (String.Empty), "#B1");
  245. AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("NoSuchName"), "#B2");
  246. AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable (typeof (object)), "#B3");
  247. AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("FooTable", typeof (object)), "#B4");
  248. AssertExtensions.Throws<ArgumentException> (() => t = m.GetTable ("NoSuchTable", typeof (object)), "#B5");
  249. Assert.IsNotNull (t = m.GetTable ("FooTable"), "#C1");
  250. Assert.AreEqual (typeof (Foo), t.EntityType, "#C2");
  251. Assert.IsNotNull (t = m.GetTable (typeof (Foo)), "#C3");
  252. Assert.AreEqual (typeof (Foo), t.EntityType, "#C4");
  253. Assert.IsNotNull (t = m.GetTable ("FooTable", typeof (MyDataContext2)), "#C5");
  254. Assert.AreEqual (typeof (Foo), t.EntityType, "#C6");
  255. }
  256. [Test]
  257. public void GetActionPath ()
  258. {
  259. var foo = new Foo (true);
  260. MetaModel m = Utils.GetModel<MyDataContext2> ();
  261. // None of those are thrown from GetTable - it seems this method performs NO checks at all, sigh...
  262. //
  263. //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath (null, PageAction.List, foo), "#A1");
  264. //AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath (String.Empty, PageAction.List, foo), "#A2");
  265. //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath ("FooTable", null, foo), "#A3");
  266. //AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath ("FooTable", PageAction.List, null), "#A4");
  267. //AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath ("NoSuchTable", PageAction.List, foo), "#A5");
  268. }
  269. [Test]
  270. public void GetActionPath2 ()
  271. {
  272. var foo = new Foo (true);
  273. var req = new FakeHttpWorkerRequest ();
  274. var ctx = new HttpContext (req);
  275. HttpContext.Current = ctx;
  276. MetaModel m = Utils.GetModel<MyDataContext2> ();
  277. RouteTable.Routes.Add (
  278. new DynamicDataRoute ("{table}/{action}.aspx") {
  279. Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
  280. Model = m,
  281. RouteHandler = new MyDynamicDataRouteHandler ()
  282. });
  283. // .NET stacktrace:
  284. //
  285. // at System.Web.DynamicData.MetaModel.TryGetTable(String uniqueTableName, MetaTable& table)
  286. // at System.Web.DynamicData.MetaModel.GetTable(String uniqueTableName)
  287. AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath (null, PageAction.List, foo), "#A1");
  288. Assert.AreEqual (String.Empty, m.GetActionPath ("FooTable", null, foo), "#A2");
  289. Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", PageAction.List, null), "#A3");
  290. AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath ("NoSuchTable", PageAction.List, foo), "#A4");
  291. Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", "List", foo), "#B1");
  292. }
  293. }
  294. }