Author: gert
Date: 2007-09-17 13:24:04 -0400 (Mon, 17 Sep 2007)
New Revision: 85910

Added:
   
trunk/mcs/class/System/Test/System.ComponentModel/ComponentResourceManagerTest.cs
Modified:
   trunk/mcs/class/System/ChangeLog
   trunk/mcs/class/System/System.ComponentModel/ChangeLog
   trunk/mcs/class/System/System.ComponentModel/ComponentResourceManager.cs
   trunk/mcs/class/System/System_test.dll.sources
   trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog
Log:
* System_test.dll.sources: Added ComponentResourceManagerTest.cs.
* ComponentResourceManager.cs: In ApplyResources, when culture is
null use CurrentUICulture. Use hashtable, which is case-insensitive
when IgnoreCase is true, to store the resources of the specified
culture and its parent(s). When IgnoreCase is set, perform a
case-insensitive lookup of property. Ignore resources that have no
value, or where the value is not an instance of the property type.
Fixes bug #82674.
* ComponentResourceManagerTest.cs: Added tests for ctors,
ApplyResources and IgnoreCase.


Modified: trunk/mcs/class/System/ChangeLog
===================================================================
--- trunk/mcs/class/System/ChangeLog    2007-09-17 17:18:02 UTC (rev 85909)
+++ trunk/mcs/class/System/ChangeLog    2007-09-17 17:24:04 UTC (rev 85910)
@@ -1,3 +1,7 @@
+2007-09-17  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * System_test.dll.sources: Added ComponentResourceManagerTest.cs.
+
 2007-09-05  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * System.dll.sources : added HandleCollector.cs.

Modified: trunk/mcs/class/System/System.ComponentModel/ChangeLog
===================================================================
--- trunk/mcs/class/System/System.ComponentModel/ChangeLog      2007-09-17 
17:18:02 UTC (rev 85909)
+++ trunk/mcs/class/System/System.ComponentModel/ChangeLog      2007-09-17 
17:24:04 UTC (rev 85910)
@@ -1,3 +1,13 @@
+2007-09-17  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * ComponentResourceManager.cs: In ApplyResources, when culture is
+       null use CurrentUICulture. Use hashtable, which is case-insensitive
+       when IgnoreCase is true, to store the resources of the specified
+       culture and its parent(s). When IgnoreCase is set, perform a
+       case-insensitive lookup of property. Ignore resources that have no
+       value, or where the value is not an instance of the property type.
+       Fixes bug #82674.
+
 2007-09-05  Gert Driesen  <[EMAIL PROTECTED]>
 
        * EnumConverter.cs: Added support for converting flag enum from

Modified: 
trunk/mcs/class/System/System.ComponentModel/ComponentResourceManager.cs
===================================================================
--- trunk/mcs/class/System/System.ComponentModel/ComponentResourceManager.cs    
2007-09-17 17:18:02 UTC (rev 85909)
+++ trunk/mcs/class/System/System.ComponentModel/ComponentResourceManager.cs    
2007-09-17 17:24:04 UTC (rev 85910)
@@ -3,11 +3,11 @@
 //
 // Author:
 //   Andreas Nahr ([EMAIL PROTECTED])
+//   Gert Driesen ([EMAIL PROTECTED])
 //
 // (C) 2003 Andreas Nahr
 //
 //
-
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -29,15 +29,15 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-
 using System;
-using System.Resources;
+using System.Collections;
+using System.Collections.Specialized;
 using System.Globalization;
-using System.Collections;
 using System.Reflection;
+using System.Resources;
 
-namespace System.ComponentModel {
-
+namespace System.ComponentModel
+{
        public class ComponentResourceManager : ResourceManager
        {
                public ComponentResourceManager ()
@@ -51,31 +51,59 @@
 
                public void ApplyResources (object value, string objectName)
                {
-                       ApplyResources (value, objectName, 
System.Threading.Thread.CurrentThread.CurrentCulture);
+                       ApplyResources (value, objectName, (CultureInfo) null);
                }
 
                public virtual void ApplyResources (object value, string 
objectName, CultureInfo culture)
                {
                        if (value == null)
-                               throw new ArgumentNullException ("value is 
null.");
-                       
+                               throw new ArgumentNullException ("value");
                        if (objectName == null)
-                               throw new ArgumentNullException ("objectName is 
null.");
-                       
+                               throw new ArgumentNullException ("objectName");
+                       if (culture == null)
+                               culture = CultureInfo.CurrentUICulture;
+
+                       Hashtable resources = IgnoreCase ? 
CollectionsUtil.CreateCaseInsensitiveHashtable ()
+                               : new Hashtable ();
+                       BuildResources (culture, resources);
+
                        string objKey = objectName + ".";
+                       CompareOptions compareOptions = IgnoreCase ?
+                               CompareOptions.IgnoreCase : CompareOptions.None;
                        Type type = value.GetType ();
-                       PropertyInfo[] propinfos = type.GetProperties ();
-                       
-                       foreach(PropertyInfo pinfo in propinfos) {
-                               if (!pinfo.CanWrite)
+
+                       BindingFlags bindingFlags = BindingFlags.Public | 
BindingFlags.Instance;
+                       if (IgnoreCase)
+                               bindingFlags |= BindingFlags.IgnoreCase;
+
+                       foreach (DictionaryEntry de in resources) {
+                               string resName = (string) de.Key;
+                               if (!culture.CompareInfo.IsPrefix (resName, 
objKey, compareOptions))
                                        continue;
-                               
-                               object obj = GetObject (objKey + pinfo.Name, 
culture);
-                                       
-                               if (obj != null)
-                                       pinfo.SetValue (value, 
Convert.ChangeType (obj, pinfo.PropertyType), null);
+
+                               string propName = resName.Substring 
(objKey.Length);
+                               PropertyInfo pinfo = type.GetProperty (propName,
+                                       bindingFlags);
+                               if (pinfo == null || !pinfo.CanWrite)
+                                       continue;
+                               object resValue = de.Value;
+                               if (resValue == null || 
pinfo.PropertyType.IsInstanceOfType (resValue))
+                                       pinfo.SetValue (value, resValue, null);
                        }
                }
+
+               void BuildResources (CultureInfo culture, Hashtable resources)
+               {
+                       // first load resources for less specific culture
+                       if (culture != culture.Parent) {
+                               BuildResources (culture.Parent, resources);
+                       }
+
+                       ResourceSet rs = GetResourceSet (culture, true, false);
+                       if (rs != null) {
+                               foreach (DictionaryEntry de in rs)
+                                       resources [(string) de.Key] = de.Value;
+                       }
+               }
        }
 }
-

Modified: trunk/mcs/class/System/System_test.dll.sources
===================================================================
--- trunk/mcs/class/System/System_test.dll.sources      2007-09-17 17:18:02 UTC 
(rev 85909)
+++ trunk/mcs/class/System/System_test.dll.sources      2007-09-17 17:24:04 UTC 
(rev 85910)
@@ -113,6 +113,7 @@
 System.ComponentModel/ByteConverterTests.cs
 System.ComponentModel/CollectionConverterTest.cs
 System.ComponentModel/ComplexBindingPropertiesAttributeTest.cs
+System.ComponentModel/ComponentResourceManagerTest.cs
 System.ComponentModel/ContainerTest.cs
 System.ComponentModel/DataObjectMethodAttributeTests.cs
 System.ComponentModel/DateTimeConverterTests.cs

Modified: trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog
===================================================================
--- trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog 2007-09-17 
17:18:02 UTC (rev 85909)
+++ trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog 2007-09-17 
17:24:04 UTC (rev 85910)
@@ -1,3 +1,8 @@
+2007-09-17  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * ComponentResourceManagerTest.cs: Added tests for ctors,
+       ApplyResources and IgnoreCase.
+
 2007-09-05  Gert Driesen  <[EMAIL PROTECTED]>
 
        * EnumConverterTest.cs: Enabled test for converting flag enum from

Added: 
trunk/mcs/class/System/Test/System.ComponentModel/ComponentResourceManagerTest.cs
===================================================================
--- 
trunk/mcs/class/System/Test/System.ComponentModel/ComponentResourceManagerTest.cs
   2007-09-17 17:18:02 UTC (rev 85909)
+++ 
trunk/mcs/class/System/Test/System.ComponentModel/ComponentResourceManagerTest.cs
   2007-09-17 17:24:04 UTC (rev 85910)
@@ -0,0 +1,159 @@
+//
+// System.ComponentModel.ComponentResourceManager test cases
+//
+// Authors:
+//     Gert Driesen ([EMAIL PROTECTED])
+//
+// (c) 2007 Gert Driesen
+//
+
+using System;
+using System.Collections;
+using System.ComponentModel;
+using System.Globalization;
+using System.Reflection;
+using System.Resources;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.ComponentModel
+{
+       [TestFixture]
+       public class ComponentResourceManagerTest
+       {
+               [Test]
+               public void Constructor0 ()
+               {
+                       MockComponentResourceManager crm = new 
MockComponentResourceManager ();
+                       Assert.IsNull (crm.BaseName, "#1");
+                       Assert.IsNull (crm.BaseNameField, "#2");
+                       Assert.IsFalse (crm.IgnoreCase, "#3");
+                       Assert.IsNull (crm.MainAssembly, "#4");
+                       Assert.IsNull (crm.ResourceSets, "#5");
+                       Assert.IsNotNull (crm.ResourceSetType, "#6");
+                       Assert.IsTrue (typeof (ResourceSet).IsAssignableFrom 
(crm.ResourceSetType), "#7");
+                       //Assert.IsFalse (typeof (ResourceSet) == 
crm.ResourceSetType, "#7");
+               }
+
+               [Test]
+               public void Constructor1 ()
+               {
+                       MockComponentResourceManager crm = new 
MockComponentResourceManager (
+                               typeof (Component));
+                       Assert.IsNotNull (crm.BaseName, "#1");
+                       Assert.AreEqual ("Component", crm.BaseName, "#2");
+                       Assert.IsNotNull (crm.BaseNameField, "#3");
+                       Assert.AreEqual ("Component", crm.BaseNameField, "#4");
+                       Assert.IsFalse (crm.IgnoreCase, "#5");
+                       Assert.IsNotNull (crm.MainAssembly, "#6");
+                       Assert.AreEqual (typeof (Component).Assembly, 
crm.MainAssembly, "#7");
+                       Assert.IsNotNull (crm.ResourceSets, "#8");
+                       Assert.AreEqual (0, crm.ResourceSets.Count, "#9");
+                       Assert.IsNotNull (crm.ResourceSetType, "#10");
+                       Assert.IsTrue (typeof (ResourceSet).IsAssignableFrom 
(crm.ResourceSetType), "#11");
+                       //Assert.IsFalse (typeof (ResourceSet) == 
crm.ResourceSetType, "#12");
+               }
+
+               [Test]
+               public void Constructor1_ResourceSource_Null ()
+               {
+                       try {
+                               new ComponentResourceManager ((Type) null);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("resourceSource", 
ex.ParamName, "#6");
+                       }
+               }
+
+               [Test]
+               public void ApplyResources_ObjectName_Null ()
+               {
+                       ComponentResourceManager crm = new 
ComponentResourceManager ();
+                       try {
+                               crm.ApplyResources (new object (), (string) 
null);
+                               Assert.Fail ("#A1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsNotNull (ex.ParamName, "#A5");
+                               Assert.AreEqual ("objectName", ex.ParamName, 
"#A6");
+                       }
+
+                       try {
+                               crm.ApplyResources (new object (), (string) 
null, CultureInfo.InvariantCulture);
+                               Assert.Fail ("#B1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#B2");
+                               Assert.IsNull (ex.InnerException, "#B3");
+                               Assert.IsNotNull (ex.Message, "#B4");
+                               Assert.IsNotNull (ex.ParamName, "#B5");
+                               Assert.AreEqual ("objectName", ex.ParamName, 
"#B6");
+                       }
+               }
+
+               [Test]
+               public void ApplyResources_Value_Null ()
+               {
+                       ComponentResourceManager crm = new 
ComponentResourceManager ();
+                       try {
+                               crm.ApplyResources (null, "$this");
+                               Assert.Fail ("#A1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsNotNull (ex.ParamName, "#A5");
+                               Assert.AreEqual ("value", ex.ParamName, "#A6");
+                       }
+
+                       try {
+                               crm.ApplyResources (null, "$this", 
CultureInfo.InvariantCulture);
+                               Assert.Fail ("#B1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#B2");
+                               Assert.IsNull (ex.InnerException, "#B3");
+                               Assert.IsNotNull (ex.Message, "#B4");
+                               Assert.IsNotNull (ex.ParamName, "#B5");
+                               Assert.AreEqual ("value", ex.ParamName, "#B6");
+                       }
+               }
+
+               [Test]
+               public void IgnoreCase ()
+               {
+                       ComponentResourceManager crm = new 
ComponentResourceManager ();
+                       Assert.IsFalse (crm.IgnoreCase, "#1");
+                       crm.IgnoreCase = true;
+                       Assert.IsTrue (crm.IgnoreCase, "#2");
+               }
+
+               class MockComponentResourceManager : ComponentResourceManager
+               {
+                       public MockComponentResourceManager ()
+                       {
+                       }
+
+                       public MockComponentResourceManager (Type 
resourceSource)
+                               : base (resourceSource)
+                       {
+                       }
+
+                       public new string BaseNameField {
+                               get { return base.BaseNameField; }
+                       }
+
+                       public new Assembly MainAssembly {
+                               get { return base.MainAssembly; }
+                       }
+
+                       public new Hashtable ResourceSets {
+                               get { return base.ResourceSets; }
+                       }
+               }
+       }
+}


Property changes on: 
trunk/mcs/class/System/Test/System.ComponentModel/ComponentResourceManagerTest.cs
___________________________________________________________________
Name: svn:eol-style
   + native

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

Reply via email to