Author: gert
Date: 2008-02-09 05:31:22 -0500 (Sat, 09 Feb 2008)
New Revision: 95322

Modified:
   
trunk/mcs/class/System.Web/System.Web.Compilation/AppSettingsExpressionBuilder.cs
   trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog
   
trunk/mcs/class/System.Web/Test/System.Web.Compilation/AppSettingsExpressionBuilderTest.cs
   trunk/mcs/class/System.Web/Test/System.Web.Compilation/ChangeLog
Log:
* AppSettingsExpressionBuilder.cs: Improve exception messages. Return
"raw" appsetting value if targetType is null.
* AppSettingsExpressionBuilderTest.cs: Added and improved tests for
GetAppSetting overloads.


Modified: 
trunk/mcs/class/System.Web/System.Web.Compilation/AppSettingsExpressionBuilder.cs
===================================================================
--- 
trunk/mcs/class/System.Web/System.Web.Compilation/AppSettingsExpressionBuilder.cs
   2008-02-09 09:55:18 UTC (rev 95321)
+++ 
trunk/mcs/class/System.Web/System.Web.Compilation/AppSettingsExpressionBuilder.cs
   2008-02-09 10:31:22 UTC (rev 95322)
@@ -54,25 +54,30 @@
                        string value = WebConfigurationManager.AppSettings 
[key];
 
                        if (value == null)
-                               throw new InvalidOperationException 
(String.Format ("App setting {0} not found", key));
+                               throw new InvalidOperationException 
(String.Format ("The application setting '{0}' was not found.", key));
                        return value;
                }
 
                public static object GetAppSetting (string key, Type 
targetType, string propertyName)
                {
-                       try {
-                               object value = GetAppSetting (key);
+                       object value = GetAppSetting (key);
 
-                               PropertyInfo pi = 
targetType.GetProperty(propertyName);
-                               if (pi == null)
-                                       return value.ToString ();
+                       if (targetType == null)
+                               return value.ToString ();
 
+                       PropertyInfo pi = targetType.GetProperty(propertyName);
+                       if (pi == null)
+                               return value.ToString ();
+
+                       try {
                                TypeConverter converter = 
TypeDescriptor.GetConverter (pi.PropertyType);
                                return converter.ConvertFrom (value);
+                       } catch (NotSupportedException) {
+                               throw new InvalidOperationException 
(String.Format (
+                                       "Could not convert application setting 
'{0}' " +
+                                       " to type '{1}' for property '{2}'.", 
value,
+                                       pi.PropertyType.Name, pi.Name));
                        }
-                       catch (NotSupportedException) {
-                               throw new InvalidOperationException 
(String.Format ("Could not convert app setting {0} to type {1}", key, 
targetType));
-                       }
                }
 
 
@@ -95,6 +100,4 @@
 
 }
 
-#endif
-
-
+#endif

Modified: trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog 2008-02-09 
09:55:18 UTC (rev 95321)
+++ trunk/mcs/class/System.Web/System.Web.Compilation/ChangeLog 2008-02-09 
10:31:22 UTC (rev 95322)
@@ -1,3 +1,8 @@
+2008-02-08  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * AppSettingsExpressionBuilder.cs: Improve exception messages. Return
+       "raw" appsetting value if targetType is null.
+
 2008-02-07  Marek Habersack  <[EMAIL PROTECTED]>
 
        * CompilationException.cs: added new constructor which takes

Modified: 
trunk/mcs/class/System.Web/Test/System.Web.Compilation/AppSettingsExpressionBuilderTest.cs
===================================================================
--- 
trunk/mcs/class/System.Web/Test/System.Web.Compilation/AppSettingsExpressionBuilderTest.cs
  2008-02-09 09:55:18 UTC (rev 95321)
+++ 
trunk/mcs/class/System.Web/Test/System.Web.Compilation/AppSettingsExpressionBuilderTest.cs
  2008-02-09 10:31:22 UTC (rev 95322)
@@ -50,52 +50,190 @@
        {
                private string strProp;
                private int intProp;
+               private DateTime dateTimeProp;
+               private Type typeProp;
 
-               public string StrProp
-               {
+               public string StrProp {
                        get { return strProp; }
                        set { strProp = value; }
                }
 
-               public int IntProp
-               {
+               public int IntProp {
                        get { return intProp; }
                        set { intProp = value; }
                }
+
+               public DateTime DateTimeProp {
+                       get { return dateTimeProp; }
+                       set { dateTimeProp = value; }
+               }
+
+               public Type TypeProp {
+                       get { return typeProp; }
+                       set { typeProp = value; }
+               }
        }
 
        [TestFixture]
        public class AppSettingsExpressionBuilderTest
        {
-
-               [Test]
+               [Test] // GetAppSetting (String)
                [Category ("NunitWeb")]
                public void GetAppSetting1 ()
                {
                        PageDelegates pd = new PageDelegates ();
-                       pd.Load = GetAppSetting_Load1;
+                       pd.Load = GetAppSetting1_Load;
                        WebTest test = new WebTest (new PageInvoker (pd));
-                       string html = test.Run ();
+                       test.Run ();
+               }
 
+               [Test] // GetAppSetting (String)
+               public void GetAppSetting1_Key_DoesNotExist ()
+               {
+                       try {
+                               AppSettingsExpressionBuilder.GetAppSetting 
("DoesNotExist");
+                               Assert.Fail ("#1");
+                       } catch (InvalidOperationException ex) {
+                               // The application setting 'DoesNotExist' was
+                               // not found in the applications configuration
+                               Assert.AreEqual (typeof 
(InvalidOperationException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsTrue (ex.Message.IndexOf 
("'DoesNotExist'") != -1, "#5");
+                       }
                }
 
-               public static void GetAppSetting_Load1 (Page p)
+               [Test] // GetAppSetting (String)
+               public void GetAppSetting1_Key_Null ()
                {
+                       try {
+                               AppSettingsExpressionBuilder.GetAppSetting 
((string) null);
+                               Assert.Fail ("#1");
+                       } catch (InvalidOperationException ex) {
+                               // The application setting '' was not found in
+                               // the applications configuration
+                               Assert.AreEqual (typeof 
(InvalidOperationException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsTrue (ex.Message.IndexOf ("''") != -1, 
"#5");
+                       }
+               }
+
+               [Test] // GetAppSetting (String, Type, String)
+               [Category ("NunitWeb")]
+               public void GetAppSetting2 ()
+               {
+                       PageDelegates pd = new PageDelegates ();
+                       pd.Load = GetAppSetting2_Load;
+                       WebTest test = new WebTest (new PageInvoker (pd));
+                       test.Run ();
+               }
+
+               [Test] // GetAppSetting (String, Type, String)
+               public void GetAppSetting2_Key_Null ()
+               {
+                       try {
+                               AppSettingsExpressionBuilder.GetAppSetting (
+                                       (string) null, 
+                                       typeof (SettingTestingType),
+                                       "StrProp");
+                               Assert.Fail ("#1");
+                       } catch (InvalidOperationException ex) {
+                               // The application setting '' was not found in
+                               // the applications configuration
+                               Assert.AreEqual (typeof 
(InvalidOperationException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsTrue (ex.Message.IndexOf ("''") != -1, 
"#5");
+                       }
+               }
+
+               [Test]
+               public void SupportsEvaluate ()
+               {
+                       AppSettingsExpressionBuilder aseb = new 
AppSettingsExpressionBuilder ();
+                       Assert.IsTrue (aseb.SupportsEvaluate);
+               }
+
+               public static void GetAppSetting1_Load (Page p)
+               {
+                       object o = AppSettingsExpressionBuilder.GetAppSetting 
("strvalue");
+                       Assert.AreEqual (typeof (string), o.GetType (), "#A1");
+                       Assert.AreEqual ("str", o, "#A2");
+
+                       o = AppSettingsExpressionBuilder.GetAppSetting 
("intvalue");
+                       Assert.AreEqual (typeof (string), o.GetType (), "#B1");
+                       Assert.AreEqual ("123", o, "#B2");
+               }
+
+               public static void GetAppSetting2_Load (Page p)
+               {
                        object o = AppSettingsExpressionBuilder.GetAppSetting 
("strvalue", typeof (SettingTestingType), "StrProp");
-                       Assert.AreEqual (typeof (string), o.GetType (), 
"GetAppSetting1 #1");
-                       Assert.AreEqual ("str", o, "GetAppSetting1 #2");
+                       Assert.AreEqual (typeof (string), o.GetType (), "#A1");
+                       Assert.AreEqual ("str", o, "#A2");
 
+                       // property does not exist
                        o = AppSettingsExpressionBuilder.GetAppSetting 
("strvalue", typeof (SettingTestingType), "NotExistsProp");
-                       Assert.AreEqual (typeof (string), o.GetType (), 
"GetAppSetting1 #3");
-                       Assert.AreEqual ("str", o, "GetAppSetting1 #4");
+                       Assert.AreEqual (typeof (string), o.GetType (), "#B1");
+                       Assert.AreEqual ("str", o, "#B2");
 
                        o = AppSettingsExpressionBuilder.GetAppSetting 
("intvalue", typeof (SettingTestingType), "IntProp");
-                       Assert.AreEqual (typeof (int), o.GetType (), 
"GetAppSetting1 #5");
-                       Assert.AreEqual (123, o, "GetAppSetting1 #6");
+                       Assert.AreEqual (typeof (int), o.GetType (), "#C1");
+                       Assert.AreEqual (123, o, "#C2");
 
+                       // conversion
+                       o = AppSettingsExpressionBuilder.GetAppSetting 
("intvalue", typeof (SettingTestingType), "StrProp");
+                       Assert.AreEqual (typeof (string), o.GetType (), "#D1");
+                       Assert.AreEqual ("123", o, "#D2");
+
+                       // property does not exist
                        o = AppSettingsExpressionBuilder.GetAppSetting 
("intvalue", typeof (SettingTestingType), "NotExistsProp");
-                       Assert.AreEqual (typeof (string), o.GetType (), 
"GetAppSetting1 #7");
-                       Assert.AreEqual ("123", o, "GetAppSetting1 #8");
+                       Assert.AreEqual (typeof (string), o.GetType (), "#E1");
+                       Assert.AreEqual ("123", o, "#E2");
+
+                       // targetType null
+                       o = AppSettingsExpressionBuilder.GetAppSetting 
("intvalue", (Type) null, "NotExistsProp");
+                       Assert.AreEqual (typeof (string), o.GetType (), "#F1");
+                       Assert.AreEqual ("123", o, "#F2");
+
+                       // conversion failed
+                       try {
+                               AppSettingsExpressionBuilder.GetAppSetting 
("intvalue",
+                                       typeof (SettingTestingType), 
"DateTimeProp");
+                               Assert.Fail ("#G1");
+                       } catch (FormatException ex) {
+                               // String was not recognized as a valid DateTime
+                               Assert.AreEqual (typeof (FormatException), 
ex.GetType (), "#G2");
+                               Assert.IsNotNull (ex.Message, "#G3");
+                       }
+
+                       // conversion not supported
+                       try {
+                               AppSettingsExpressionBuilder.GetAppSetting 
("intvalue",
+                                       typeof (SettingTestingType), 
"TypeProp");
+                               Assert.Fail ("#H1");
+                       } catch (InvalidOperationException ex) {
+                               // Could not convert the AppSetting '123' to the
+                               // type 'Type' on property 'TypeProp'
+                               Assert.AreEqual (typeof 
(InvalidOperationException), ex.GetType (), "#H2");
+                               Assert.IsNull (ex.InnerException, "#H3");
+                               Assert.IsNotNull (ex.Message, "#H4");
+                               Assert.IsTrue (ex.Message.IndexOf ("'123'") != 
-1, "#H5");
+                               Assert.IsTrue (ex.Message.IndexOf ("'Type'") != 
-1, "#H6");
+                               Assert.IsTrue (ex.Message.IndexOf 
("'TypeProp'") != -1, "#H7");
+                       }
+
+                       // propertyName null
+                       try {
+                               AppSettingsExpressionBuilder.GetAppSetting 
("intvalue",
+                                       typeof (SettingTestingType), (string) 
null);
+                               Assert.Fail ("#I1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#I2");
+                               Assert.IsNull (ex.InnerException, "#I3");
+                               Assert.IsNotNull (ex.Message, "#I4");
+                               //Assert.AreEqual ("key", ex.ParamName, "#I5");
+                       }
                }
        }
 }

Modified: trunk/mcs/class/System.Web/Test/System.Web.Compilation/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/Test/System.Web.Compilation/ChangeLog    
2008-02-09 09:55:18 UTC (rev 95321)
+++ trunk/mcs/class/System.Web/Test/System.Web.Compilation/ChangeLog    
2008-02-09 10:31:22 UTC (rev 95322)
@@ -1,3 +1,8 @@
+2008-02-08  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * AppSettingsExpressionBuilderTest.cs: Added and improved tests for
+       GetAppSetting overloads.
+
 2007-12-23  Vladimir Krasnov <[EMAIL PROTECTED]>
 
        * added AppSettingsExpressionBuilderTest.cs

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to