2
0

Utils.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.DynamicData;
  7. using System.Web.DynamicData.ModelProviders;
  8. using System.Web.Routing;
  9. using MonoTests.System.Web.DynamicData;
  10. namespace MonoTests.Common
  11. {
  12. static class Utils
  13. {
  14. public static MetaModel CommonInitialize ()
  15. {
  16. return CommonInitialize (false);
  17. }
  18. public static MetaModel CommonInitialize (bool myDynamicDataRoute)
  19. {
  20. MetaModel m = MetaModel.Default;
  21. var req = new FakeHttpWorkerRequest ();
  22. var ctx = new HttpContext (req);
  23. HttpContext.Current = ctx;
  24. RouteCollection routes = RouteTable.Routes;
  25. routes.Clear ();
  26. if (myDynamicDataRoute) {
  27. routes.Add (
  28. new MyDynamicDataRoute ("{table}/{action}.aspx") {
  29. Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
  30. Model = m,
  31. RouteHandler = new MyDynamicDataRouteHandler ()
  32. });
  33. } else {
  34. routes.Add (
  35. new DynamicDataRoute ("{table}/{action}.aspx") {
  36. Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
  37. Model = m,
  38. RouteHandler = new MyDynamicDataRouteHandler ()
  39. });
  40. }
  41. return m;
  42. }
  43. public static MetaModel GetModel<ContextType> ()
  44. {
  45. // This is really, really dumb but we need that since if the type has already
  46. // been registered by another test, or tests are re-ran without nunit having
  47. // reloaded the dll we'll get a duplicate entry exception.
  48. MetaModel m;
  49. try {
  50. m = MetaModel.GetModel (typeof (ContextType));
  51. } catch (InvalidOperationException) {
  52. m = new MetaModel ();
  53. m.RegisterContext (typeof (ContextType));
  54. } finally {
  55. MetaModel.ResetRegistrationException ();
  56. }
  57. return m;
  58. }
  59. public static void RegisterContext (DataModelProvider model)
  60. {
  61. RegisterContext (model, null);
  62. }
  63. public static void RegisterContext (Type contextType)
  64. {
  65. RegisterContext (contextType, null);
  66. }
  67. public static void RegisterContext (DataModelProvider model, ContextConfiguration config)
  68. {
  69. RegisterContext (model, config, true);
  70. }
  71. public static void RegisterContext (Type contextType, ContextConfiguration config)
  72. {
  73. RegisterContext (contextType, config, true);
  74. }
  75. public static void RegisterContext (DataModelProvider model, ContextConfiguration config, bool defaultModel)
  76. {
  77. // Just in case no model has been created yet
  78. MetaModel m = new MetaModel ();
  79. if (defaultModel)
  80. m = MetaModel.Default;
  81. Exception exception = null;
  82. MetaModel registered = null;
  83. try {
  84. registered = MetaModel.GetModel (model.ContextType);
  85. } catch (Exception) {
  86. // ignore
  87. }
  88. try {
  89. if (registered == null)
  90. m.RegisterContext (model, config);
  91. } catch (InvalidOperationException ex) {
  92. exception = ex;
  93. }
  94. if (exception != null) {
  95. Console.WriteLine ("RegisterContext exception:");
  96. Console.WriteLine (exception);
  97. }
  98. }
  99. public static void RegisterContext (Type contextType, ContextConfiguration config, bool defaultModel)
  100. {
  101. // Just in case no model has been created yet
  102. MetaModel m = new MetaModel ();
  103. if (defaultModel)
  104. m = MetaModel.Default;
  105. Exception exception = null;
  106. MetaModel registered = null;
  107. try {
  108. registered = MetaModel.GetModel (contextType);
  109. } catch (Exception) {
  110. // ignore
  111. }
  112. try {
  113. if (registered == null) {
  114. if (config != null)
  115. m.RegisterContext (contextType, config);
  116. else
  117. m.RegisterContext (contextType);
  118. }
  119. } catch (InvalidOperationException ex) {
  120. exception = ex;
  121. }
  122. if (exception != null) {
  123. Console.WriteLine ("RegisterContext exception:");
  124. Console.WriteLine (exception);
  125. }
  126. }
  127. public static string BuildActionName (MetaTable table, string action)
  128. {
  129. return "/" + table.Name + "/" + action + ".aspx";
  130. }
  131. public static string BuildActionName (MetaTable table, string action, string query)
  132. {
  133. string ret = "/" + table.Name + "/" + action + ".aspx";
  134. if (!String.IsNullOrEmpty (query))
  135. ret += "?" + query;
  136. return ret;
  137. }
  138. }
  139. }