2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / Utils.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web.DynamicData;
6 using System.Web.DynamicData.ModelProviders;
7
8 namespace MonoTests.Common
9 {
10         static class Utils
11         {
12                 public static MetaModel GetModel<ContextType> ()
13                 {
14                         // This is really, really dumb but we need that since if the type has already
15                         // been registered by another test, or tests are re-ran without nunit having
16                         // reloaded the dll we'll get a duplicate entry exception.
17                         MetaModel m;
18                         try {
19                                 m = MetaModel.GetModel (typeof (ContextType));
20                         } catch (InvalidOperationException) {
21                                 m = new MetaModel ();
22                                 m.RegisterContext (typeof (ContextType));
23                         } finally {
24                                 MetaModel.ResetRegistrationException ();
25                         }
26
27                         return m;
28                 }
29
30                 public static void RegisterContext (DataModelProvider model)
31                 {
32                         RegisterContext (model, null);
33                 }
34
35                 public static void RegisterContext (DataModelProvider model, ContextConfiguration config)
36                 {
37                         // Just in case no model has been created yet
38                         MetaModel m = new MetaModel ();
39
40                         // And get the default model instead, whatever it is
41                         m = MetaModel.Default;
42                         try {
43                                 m.RegisterContext (model, config);
44                         } catch (InvalidOperationException ex) {
45                                 Console.WriteLine ("RegisterContext exception:");
46                                 Console.WriteLine (ex);
47                         }
48                 }
49
50                 public static string BuildActionName (MetaTable table, string action)
51                 {
52                         return "/" + table.Name + "/" + action + ".aspx";
53                 }
54
55                 public static string BuildActionName (MetaTable table, string action, string query)
56                 {
57                         string ret = "/" + table.Name + "/" + action + ".aspx";
58                         if (!String.IsNullOrEmpty (query))
59                                 ret += "?" + query;
60                         return ret;
61                 }
62         }
63 }