diff options
author | garyb <garyb@system1.(none)> | 2012-07-11 14:24:48 +0400 |
---|---|---|
committer | garyb <garyb@system1.(none)> | 2012-10-11 02:20:03 +0400 |
commit | 31133634658463ddf48eb65568559d716e7dddd8 (patch) | |
tree | d3da468bbb32bcb7643319d1946b12efd9a392d3 /mcs/class/Managed.Windows.Forms/Test | |
parent | 75cdf6f9d8b15bb7cf20e29a3f7595533e158b78 (diff) |
Draft Tests and Implementation of ResXDataNode Based ResX Handling
Diffstat (limited to 'mcs/class/Managed.Windows.Forms/Test')
11 files changed, 3709 insertions, 10 deletions
diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeAliasTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeAliasTests.cs new file mode 100644 index 00000000000..c68a23838ff --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeAliasTests.cs @@ -0,0 +1,719 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + +namespace MonoTests.System.Resources +{ + [TestFixture] + public class ResXDataNodeAliasTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + + [Test, ExpectedException (typeof (TypeLoadException))] + public void CantAccessValueWereOnlyFullNameInResXForEmbedded () // same as validity check in assemblynames tests + { + + string filePath = GetFileFromString ("convertableResX.resx", convertableResX); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue ((AssemblyName[]) null); + } + } + + [Test, ExpectedException (typeof (TypeLoadException))] + public void CantAccessValueWereOnlyFullNameAndAliasInResXForEmbedded () + { + + string filePath = GetFileFromString ("convertableResXAlias.resx", convertableResXAlias); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue ((AssemblyName []) null); + } + } + + [Test] + public void CanAccessValueWereOnlyFullNameAndAssemblyInResXForEmbedded () + { + + string filePath = GetFileFromString ("convertableResXAssembly.resx", convertableResXAssembly); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue ((AssemblyName []) null); + // this is the qualified name of the assembly found in dir + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, obj.GetType ().AssemblyQualifiedName, "#A1"); + } + } + + [Test] + public void CanAccessValueWereOnlyFullNameAndQualifiedAssemblyInResXForEmbedded () + { + + string filePath = GetFileFromString ("convertableResXQAN.resx", convertableResXQualifiedAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue ((AssemblyName []) null); + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, obj.GetType ().AssemblyQualifiedName, "#A1"); + } + } + + /* + [Test] + public void GetValueAssemblyNameUsedWhereOnlyFullNameInResXForEmbedded () + { + // DummyAssembly must be in the same directory as current assembly to work correctly + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string filePath = GetFileFromString ("convertableResX.resx", convertableResX); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue (assemblyNames); + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, obj.GetType ().AssemblyQualifiedName); + } + } + + [Test] + public void GetValueTypeNameReturnsFullNameWereOnlyFullNameInResXForEmbedded () + { + // just a check, if this passes other tests will give false results + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResX); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + string returnedType = node.GetValueTypeName ((AssemblyName []) null); + + Assert.AreEqual ("DummyAssembly.Convertable", returnedType); + } + } + + [Test] + public void GetValueTypeNameAssemblyNameUsedWhereOnlyFullNameInResXForEmbedded () + { + // DummyAssembly must be in the same directory as current assembly to work correctly + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResX); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + string returnedType = node.GetValueTypeName (assemblyNames); + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, returnedType); + } + } + */ + + + + static string convertableResX = +@"<?xml version=""1.0"" encoding=""utf-8""?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name=""resmimetype"">text/microsoft-resx</resheader> + <resheader name=""version"">2.0</resheader> + <resheader name=""reader"">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name=""writer"">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name=""Name1""><value>this is my long string</value><comment>this is a comment</comment></data> + <data name=""Color1"" type=""System.Drawing.Color, System.Drawing"">Blue</data> + <data name=""Bitmap1"" mimetype=""application/x-microsoft.net.object.binary.base64""> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name=""Icon1"" type=""System.Drawing.Icon, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of ""resheader"" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> + <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" /> + <xsd:element name=""root"" msdata:IsDataSet=""true""> + <xsd:complexType> + <xsd:choice maxOccurs=""unbounded""> + <xsd:element name=""metadata""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" /> + </xsd:sequence> + <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" /> + <xsd:attribute name=""type"" type=""xsd:string"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""assembly""> + <xsd:complexType> + <xsd:attribute name=""alias"" type=""xsd:string"" /> + <xsd:attribute name=""name"" type=""xsd:string"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""data""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" /> + <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""resheader""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name=""resmimetype""> + <value>text/microsoft-resx</value> + </resheader> + <resheader name=""version""> + <value>2.0</value> + </resheader> + <resheader name=""reader""> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name=""writer""> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + + <data name=""test"" type=""DummyAssembly.Convertable""> + <value>im a name im a value</value> + </data> +</root>"; + + + static string convertableResXAssembly = +@"<?xml version=""1.0"" encoding=""utf-8""?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name=""resmimetype"">text/microsoft-resx</resheader> + <resheader name=""version"">2.0</resheader> + <resheader name=""reader"">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name=""writer"">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name=""Name1""><value>this is my long string</value><comment>this is a comment</comment></data> + <data name=""Color1"" type=""System.Drawing.Color, System.Drawing"">Blue</data> + <data name=""Bitmap1"" mimetype=""application/x-microsoft.net.object.binary.base64""> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name=""Icon1"" type=""System.Drawing.Icon, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of ""resheader"" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> + <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" /> + <xsd:element name=""root"" msdata:IsDataSet=""true""> + <xsd:complexType> + <xsd:choice maxOccurs=""unbounded""> + <xsd:element name=""metadata""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" /> + </xsd:sequence> + <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" /> + <xsd:attribute name=""type"" type=""xsd:string"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""assembly""> + <xsd:complexType> + <xsd:attribute name=""alias"" type=""xsd:string"" /> + <xsd:attribute name=""name"" type=""xsd:string"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""data""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" /> + <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""resheader""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name=""resmimetype""> + <value>text/microsoft-resx</value> + </resheader> + <resheader name=""version""> + <value>2.0</value> + </resheader> + <resheader name=""reader""> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name=""writer""> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + + <data name=""test"" type=""DummyAssembly.Convertable, DummyAssembly""> + <value>im a name im a value</value> + </data> +</root>"; + + + static string convertableResXAlias = +@"<?xml version=""1.0"" encoding=""utf-8""?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name=""resmimetype"">text/microsoft-resx</resheader> + <resheader name=""version"">2.0</resheader> + <resheader name=""reader"">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name=""writer"">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name=""Name1""><value>this is my long string</value><comment>this is a comment</comment></data> + <data name=""Color1"" type=""System.Drawing.Color, System.Drawing"">Blue</data> + <data name=""Bitmap1"" mimetype=""application/x-microsoft.net.object.binary.base64""> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name=""Icon1"" type=""System.Drawing.Icon, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of ""resheader"" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> + <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" /> + <xsd:element name=""root"" msdata:IsDataSet=""true""> + <xsd:complexType> + <xsd:choice maxOccurs=""unbounded""> + <xsd:element name=""metadata""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" /> + </xsd:sequence> + <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" /> + <xsd:attribute name=""type"" type=""xsd:string"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""assembly""> + <xsd:complexType> + <xsd:attribute name=""alias"" type=""xsd:string"" /> + <xsd:attribute name=""name"" type=""xsd:string"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""data""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" /> + <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""resheader""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name=""resmimetype""> + <value>text/microsoft-resx</value> + </resheader> + <resheader name=""version""> + <value>2.0</value> + </resheader> + <resheader name=""reader""> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name=""writer""> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <assembly alias=""DummyAssembly"" name=""DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"" /> + <data name=""test"" type=""DummyAssembly.Convertable""> + <value>im a name im a value</value> + </data> +</root>"; + + static string convertableResXQualifiedAssemblyName = +@"<?xml version=""1.0"" encoding=""utf-8""?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name=""resmimetype"">text/microsoft-resx</resheader> + <resheader name=""version"">2.0</resheader> + <resheader name=""reader"">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name=""writer"">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name=""Name1""><value>this is my long string</value><comment>this is a comment</comment></data> + <data name=""Color1"" type=""System.Drawing.Color, System.Drawing"">Blue</data> + <data name=""Bitmap1"" mimetype=""application/x-microsoft.net.object.binary.base64""> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name=""Icon1"" type=""System.Drawing.Icon, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of ""resheader"" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> + <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" /> + <xsd:element name=""root"" msdata:IsDataSet=""true""> + <xsd:complexType> + <xsd:choice maxOccurs=""unbounded""> + <xsd:element name=""metadata""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" /> + </xsd:sequence> + <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" /> + <xsd:attribute name=""type"" type=""xsd:string"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""assembly""> + <xsd:complexType> + <xsd:attribute name=""alias"" type=""xsd:string"" /> + <xsd:attribute name=""name"" type=""xsd:string"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""data""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" /> + <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""resheader""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name=""resmimetype""> + <value>text/microsoft-resx</value> + </resheader> + <resheader name=""version""> + <value>2.0</value> + </resheader> + <resheader name=""reader""> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name=""writer""> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + + <data name=""test"" type=""DummyAssembly.Convertable, DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null""> + <value>im a name im a value</value> + </data> +</root>"; + + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + private string GetFileFromString (string filename, string filecontents) + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + string filepath = Path.Combine (_tempDirectory, filename); + + StreamWriter writer = new StreamWriter(filepath,false); + + writer.Write (filecontents); + writer.Close (); + + return filepath; + } + + } + +} +#endif diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeAssemblyNameTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeAssemblyNameTests.cs new file mode 100644 index 00000000000..d53af5d6c57 --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeAssemblyNameTests.cs @@ -0,0 +1,656 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; +using System.ComponentModel; +using System.Globalization; + +namespace MonoTests.System.Resources { + [TestFixture] + public class ResXDataNodeAssemblyNameTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + + /* + [Test] + public void CanPassAssemblyNameToGetValueToReturnSpecificVersionOfObjectClassInstance () + { + // tries to force use of 2.0 assembly but DOESNT WORK + + ResXDataNode originalNode, returnedNode; + originalNode = GetNodeEmdeddedIcon (); + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string aName = "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + object val = returnedNode.GetValue (assemblyNames); + + //typeof(Icon).AssemblyQualifiedName + + Assert.AreEqual ("System.Drawing.Icon, " + aName, val.GetType ().AssemblyQualifiedName, "#A2"); + + //Assert.IsInstanceOfType (typeof (serializableSubClass), val, "#A2"); + } + } + + [Test] + public void CanPassAssemblyNameToGetValueTypeNameToReturnSpecificVersionOfObject () + { + // tries to force use of 2.0 assembly DOESNT WORK - returns 4.0.0.0 + + ResXDataNode originalNode, returnedNode; + originalNode = GetNodeEmdeddedIcon (); + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string aName = "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string returnedName = returnedNode.GetValueTypeName (assemblyNames); + + //typeof(Icon).AssemblyQualifiedName + + Assert.AreEqual ("System.Drawing.Icon, " + aName, returnedName, "#A2"); + + //Assert.IsInstanceOfType (typeof (serializableSubClass), val, "#A2"); + } + } + + [Test] + public void GetValueParamIsUsedWhenFileRefCreatedNewTRYOUT () + { + // doesnt work + + ResXDataNode node; + + node = GetNodeFileRefToSerializableWithoutQualifiedTypeName ("ser.bbb"); + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + + // string aName = typeof (serializable).AssemblyQualifiedName; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + object val = node.GetValue ((AssemblyName[]) null); + + //object val = node.GetValue ((AssemblyName[]) null); + } + + */ + + [Test, ExpectedException (typeof (TypeLoadException))] + public void GetValueAssemblyNameUsedWereOnlyFullNameInResXForEmbedded_TestValidityCheck () + { + // just a check, if this passes other tests will give false results + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue ((AssemblyName []) null); + } + } + + [Test] + public void GetValueAssemblyNameUsedWhereOnlyFullNameInResXForEmbedded () + { + // DummyAssembly must be in the same directory as current assembly to work correctly + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue (assemblyNames); + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, obj.GetType ().AssemblyQualifiedName); + } + } + + [Test, ExpectedException (typeof (TypeLoadException))] + public void GetValueAssemblyNameRequiredEachTimeWhereOnlyFullNameInResXForEmbedded () + { + // DummyAssembly must be in the same directory as current assembly to work correctly + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue (assemblyNames); + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, obj.GetType ().AssemblyQualifiedName, "#A1"); + + object obj2 = node.GetValue ((AssemblyName []) null); //should cause exception here + } + } + //FIXME: does the way this test is run by NUnit affect the validity of the results showing that you need assembly name to pull type from current assembly? + [Test, ExpectedException (typeof (TypeLoadException))] + public void CantLoadTypeFromThisAssemblyWithOnlyFullName () + { + string filePath = GetFileFromString ("thisAssemblyconvertableWithOutAssembly.resx", thisAssemblyConvertableResXWithoutAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + object obj = node.GetValue ((AssemblyName []) null); + } + } + + [Test] + public void CanLoadTypeFromThisAssemblyWithOnlyFullNamePassingAssemblyNames () + { + string filePath = GetFileFromString ("thisAssemblyConvertableWithOutAssembly.resx", thisAssemblyConvertableResXWithoutAssemblyName); + + string aName = "System.Windows.Forms_test_net_2_0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + // would cause exception if couldnt find type + object obj = node.GetValue (assemblyNames); + + Assert.IsInstanceOfType (typeof (ThisAssemblyConvertable), obj, "#A1"); + } + } + + [Test] + public void GetValueTypeNameReturnsFullNameWereOnlyFullNameInResXForEmbedded () + { + // just a check, if this passes other tests will give false results + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + string returnedType = node.GetValueTypeName ((AssemblyName []) null); + + Assert.AreEqual ("DummyAssembly.Convertable", returnedType); + } + } + + [Test] + public void GetValueTypeNameAssemblyNameUsedWhereOnlyFullNameInResXForEmbedded () + { + // DummyAssembly must be in the same directory as current assembly to work correctly + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + string returnedType = node.GetValueTypeName (assemblyNames); + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, returnedType); + } + } + + [Test] + public void GetValueTypeNameAssemblyNameUsedEachTimeWhereOnlyFullNameInResXForEmbedded () + { + // DummyAssembly must be in the same directory as current assembly to work correctly + + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string filePath = GetFileFromString ("convertableWithOutAssembly.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader reader = new ResXResourceReader (filePath)) { + + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + DictionaryEntry current = (DictionaryEntry) enumerator.Current; + ResXDataNode node = (ResXDataNode) current.Value; + + string returnedName = node.GetValueTypeName (assemblyNames); + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, returnedName, "#A1"); + + string nameWithNullParam = node.GetValueTypeName ((AssemblyName []) null); + + Assert.AreEqual ("DummyAssembly.Convertable", nameWithNullParam, "#A2"); + } + } + + + static string convertableResXWithoutAssemblyName = +@"<?xml version=""1.0"" encoding=""utf-8""?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name=""resmimetype"">text/microsoft-resx</resheader> + <resheader name=""version"">2.0</resheader> + <resheader name=""reader"">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name=""writer"">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name=""Name1""><value>this is my long string</value><comment>this is a comment</comment></data> + <data name=""Color1"" type=""System.Drawing.Color, System.Drawing"">Blue</data> + <data name=""Bitmap1"" mimetype=""application/x-microsoft.net.object.binary.base64""> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name=""Icon1"" type=""System.Drawing.Icon, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of ""resheader"" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> + <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" /> + <xsd:element name=""root"" msdata:IsDataSet=""true""> + <xsd:complexType> + <xsd:choice maxOccurs=""unbounded""> + <xsd:element name=""metadata""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" /> + </xsd:sequence> + <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" /> + <xsd:attribute name=""type"" type=""xsd:string"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""assembly""> + <xsd:complexType> + <xsd:attribute name=""alias"" type=""xsd:string"" /> + <xsd:attribute name=""name"" type=""xsd:string"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""data""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" /> + <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""resheader""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name=""resmimetype""> + <value>text/microsoft-resx</value> + </resheader> + <resheader name=""version""> + <value>2.0</value> + </resheader> + <resheader name=""reader""> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name=""writer""> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + + <data name=""test"" type=""DummyAssembly.Convertable""> + <value>im a name im a value</value> + </data> +</root>"; + + static string thisAssemblyConvertableResXWithoutAssemblyName = + @"<?xml version=""1.0"" encoding=""utf-8""?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name=""resmimetype"">text/microsoft-resx</resheader> + <resheader name=""version"">2.0</resheader> + <resheader name=""reader"">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name=""writer"">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name=""Name1""><value>this is my long string</value><comment>this is a comment</comment></data> + <data name=""Color1"" type=""System.Drawing.Color, System.Drawing"">Blue</data> + <data name=""Bitmap1"" mimetype=""application/x-microsoft.net.object.binary.base64""> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name=""Icon1"" type=""System.Drawing.Icon, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of ""resheader"" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> + <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" /> + <xsd:element name=""root"" msdata:IsDataSet=""true""> + <xsd:complexType> + <xsd:choice maxOccurs=""unbounded""> + <xsd:element name=""metadata""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" /> + </xsd:sequence> + <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" /> + <xsd:attribute name=""type"" type=""xsd:string"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""assembly""> + <xsd:complexType> + <xsd:attribute name=""alias"" type=""xsd:string"" /> + <xsd:attribute name=""name"" type=""xsd:string"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""data""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" /> + <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""resheader""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name=""resmimetype""> + <value>text/microsoft-resx</value> + </resheader> + <resheader name=""version""> + <value>2.0</value> + </resheader> + <resheader name=""reader""> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name=""writer""> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + + <data name=""test"" type=""MonoTests.System.Resources.ThisAssemblyConvertable""> + <value>im a name im a value</value> + </data> +</root>"; + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + private string GetFileFromString (string filename, string filecontents) + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + string filepath = Path.Combine (_tempDirectory, filename); + + StreamWriter writer = new StreamWriter (filepath, false); + + writer.Write (filecontents); + writer.Close (); + + return filepath; + } + + } + + + [SerializableAttribute] + [TypeConverter (typeof (ThisAssemblyConvertableConverter))] + public class ThisAssemblyConvertable { + protected string name; + protected string value; + + public ThisAssemblyConvertable () + { + } + + public ThisAssemblyConvertable (string name, string value) + { + this.name = name; + this.value = value; + } + + public void GetObjectData (SerializationInfo info, StreamingContext ctxt) + { + info.AddValue ("sername", name); + info.AddValue ("servalue", value); + } + + public override string ToString () + { + return String.Format ("{0}\t{1}", name, value); + } + + public override bool Equals (object obj) + { + ThisAssemblyConvertable o = obj as ThisAssemblyConvertable; + if (o == null) + return false; + return this.name.Equals (o.name) && this.value.Equals (o.value); + } + } + + class ThisAssemblyConvertableConverter : TypeConverter { + public ThisAssemblyConvertableConverter () + { + } + + public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof (string); + } + + public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType) + { + return destinationType == typeof (string); + } + + public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) + { + if (value.GetType () != typeof (string)) + throw new Exception ("value not string"); + + string serialised = (string) value; + + string [] parts = serialised.Split ('\t'); + + if (parts.Length != 2) + throw new Exception ("string in incorrect format"); + + ThisAssemblyConvertable convertable = new ThisAssemblyConvertable (parts [0], parts [1]); + return convertable; + } + + public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (destinationType != typeof (String)) { + return base.ConvertTo (context, culture, value, destinationType); + } + + return ((ThisAssemblyConvertable) value).ToString (); + } + } + +} +#endif
\ No newline at end of file diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeByteArrayTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeByteArrayTests.cs new file mode 100644 index 00000000000..c623d82d928 --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeByteArrayTests.cs @@ -0,0 +1,242 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + +namespace MonoTests.System.Resources +{ + [TestFixture] + public class ResXDataNodeByteArrayTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + + [Test] + public void GetValueITRSNotUsedWhenNodeReturnedFromReader () + { + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedBytes (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object val = returnedNode.GetValue (new AlwaysReturnIntTypeResolutionService ()); + Assert.IsInstanceOfType (typeof (byte[]), val, "#A2"); + } + } + + [Test, ExpectedException (typeof (NotImplementedException))] + public void GetValueITRSIsTouchedWhenNodeReturnedFromReader () + { + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedBytes (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + //would raise error if touched + object val = returnedNode.GetValue (new ExceptionalTypeResolutionService ()); + + } + } + + [Test] + public void GetValueITRSNotTouchedWhenNodeCreatedNew () + { + // check supplied params to GetValue are not touched + // for an instance created manually + + ResXDataNode node; + + node = GetNodeEmdeddedBytes (); + + //would raise exception if param used + Object obj = node.GetValue (new ExceptionalTypeResolutionService ()); + Assert.IsInstanceOfType (typeof (byte[]), obj, "#A1"); + } + + [Test] + public void GetValueTypeNameITRSIsUsedWithNodeFromReader () + { + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedBytes (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string returnedType = returnedNode.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + + Assert.AreEqual ((typeof (int)).AssemblyQualifiedName, returnedType, "#A2"); + } + } + + [Test] + public void GetValueTypeNameITRSIsUsedAfterGetValueCalledWithNodeFromReader () + { + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedBytes (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object obj = returnedNode.GetValue ((ITypeResolutionService) null); + + string returnedType = returnedNode.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + + Assert.AreEqual ((typeof (int)).AssemblyQualifiedName, returnedType, "#A2"); + } + } + + [Test] + public void GetValueTypeNameITRSNotUsedWhenNodeCreatedNew () + { + // check supplying params to GetValueType of the ResXDataNode does not change the output + // of the method for an instance created manually + + ResXDataNode node; + node = GetNodeEmdeddedBytes (); + string returnedType = node.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + Assert.AreEqual ((typeof (byte[])).AssemblyQualifiedName, returnedType, "#A1"); + } + + [Test] + public void ChangesToReturnedByteArrayNotLaterWrittenBack () + { + + ResXDataNode originalNode = GetNodeEmdeddedBytes (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + string newFileName; + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + ResXDataNode returnedNode; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object val = returnedNode.GetValue ((ITypeResolutionService) null); + Assert.IsInstanceOfType (typeof (byte []), val, "#A2"); + + byte[] newBytes = (byte[]) val; + + Assert.AreEqual (1, newBytes [0], "A3"); + + newBytes [0] = 99; + + newFileName = GetResXFileWithNode (returnedNode,"another.resx"); + } + + using (ResXResourceReader reader = new ResXResourceReader (newFileName)) { + reader.UseResXDataNodes = true; + + ResXDataNode returnedNode; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A4"); + + object val = returnedNode.GetValue ((ITypeResolutionService) null); + Assert.IsInstanceOfType (typeof (byte []), val, "#A5"); + + byte [] newBytes = (byte []) val; + // would be 99 if written back + Assert.AreEqual (1,newBytes [0],"A6"); + } + } + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node, string filename) + { + string fullfileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fullfileName = Path.Combine (_tempDirectory, filename); + + using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) { + writer.AddResource (node); + } + + return fullfileName; + } + + ResXDataNode GetNodeEmdeddedBytes () + { + byte[] someBytes = new byte[] {1,2,3,4,5,6,7,8,9,10}; + ResXDataNode node = new ResXDataNode ("test", someBytes); + return node; + } + + } + + +} +#endif
\ No newline at end of file diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeFileRefBitmapTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeFileRefBitmapTests.cs new file mode 100644 index 00000000000..a068479253a --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeFileRefBitmapTests.cs @@ -0,0 +1,231 @@ + /* +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; + +namespace MonoTests.System.Resources +{ + [TestFixture] + public class ResXDataNodeFileRefBitmapTests : MonoTests.System.Windows.Forms.TestHelper + { + string _tempDirectory; + string _otherTempDirectory; + + [Test, ExpectedException (typeof (NotImplementedException))] + public void GetValueParamIsTouchedWhenFileRefReturnedFromResXResourceReader () + { + // after running the enumerator of ResXResourceReader with UseResXDataNodes set + // to true, check params supplied to GetValue method + // of ResXDataNode are used to deserialise + + // for now just throwing exception in param object to ensure its accessed + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToBitmap (); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + Bitmap ico = (Bitmap)returnedNode.GetValue (new ExceptionalTypeResolutionService ()); + + } + } + + [Test] // FIXME: i would like an valid alternative TypeResolutionService that could be used to test + public void GetValueParamIsNotUsedWhenFileRefReturnedFromResXResourceReader () + { + // after running the enumerator of ResXResourceReader with UseResXDataNodes set + // to true, check params supplied to GetValue method + // of ResXDataNode are used to deserialise + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToBitmap (); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode)((DictionaryEntry)enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object val = returnedNode.GetValue (new AlwaysReturnIntTypeResolutionService ()); + + Assert.IsInstanceOfType (typeof (Bitmap),val, "#A2"); + } + } + + [Test] + public void GetValueTypeParamIsUsedWhenFileRefReturnedFromResXResourceReader () + { + // after running the enumerator of ResXResourceReader with UseResXDataNodes set + // to true, check supplying params GetValueType of the + // ResXDataNode changes the output of the method + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToBitmap (); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode)((DictionaryEntry)enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string returnedType = returnedNode.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + + Assert.AreEqual ((typeof (Int32)).AssemblyQualifiedName, returnedType, "#A2"); + } + } + + [Test] + public void GetValueTypeParamIsUsedWhenFileRefCreatedNew () + { + // check supplying params GetValueType of the + // UseResXDataNode does not change the output of the method for an instance + // initialised by me + + ResXDataNode node; + + node = GetNodeFileRefToBitmap (); + + string returnedType = node.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + + Assert.AreEqual ((typeof (Int32)).AssemblyQualifiedName, returnedType, "#A1"); + } + + [Test, ExpectedException (typeof (NotImplementedException))] + public void GetValueParamIsTouchedWhenFileRefCreatedNew () + { + // check supplying params GetValue of the + // UseResXDataNode is ignored for an instance + // initialised by me + + ResXDataNode node; + + node = GetNodeFileRefToBitmap (); + + //raise exception if param used + Bitmap ico = (Bitmap)node.GetValue (new ExceptionalTypeResolutionService ()); + + } + + [Test] // FIXME: i would like an valid alternative TypeResolutionService that could be used to test + public void GetValueParamIsNotUsedWhenFileRefCreatedNew () + { + // after running the enumerator of ResXResourceReader with UseResXDataNodes set + // to true, check params supplied to GetValue method + // of ResXDataNode are used to deserialise + + ResXDataNode node; + + node = GetNodeFileRefToBitmap (); + + object val = node.GetValue (new AlwaysReturnIntTypeResolutionService ()); + + Assert.IsInstanceOfType (typeof (Bitmap), val, "#A2"); + + } + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node) + { + string fileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fileName = Path.Combine (_tempDirectory, "myresx.resx"); + + using (ResXResourceWriter writer = new ResXResourceWriter (fileName)) { + writer.AddResource (node); + } + + return fileName; + } + + ResXDataNode GetNodeFileRefToBitmap () + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + string refFile = Path.Combine (_tempDirectory, "a.cur"); + WriteEmbeddedResource ("a.cur", refFile); + + ResXFileRef fileRef = new ResXFileRef (refFile, typeof (Bitmap).AssemblyQualifiedName); + ResXDataNode node = new ResXDataNode ("test", fileRef); + + return node; + } + + private static void WriteEmbeddedResource (string name, string filename) + { + const int size = 512; + byte[] buffer = new byte[size]; + int count = 0; + + Stream input = typeof (ResXDataNodeTest).Assembly. + GetManifestResourceStream (name); + Stream output = File.Open (filename, FileMode.Create); + + try + { + while ((count = input.Read (buffer, 0, size)) > 0) + { + output.Write (buffer, 0, count); + } + } + finally + { + output.Close (); + } + } + + } + +} +#endif + +*/
\ No newline at end of file diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeFileRefTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeFileRefTests.cs new file mode 100644 index 00000000000..05b5215d2cd --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeFileRefTests.cs @@ -0,0 +1,442 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + +namespace MonoTests.System.Resources { + [TestFixture] + public class ResXDataNodeFileRefTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + + [Test, ExpectedException (typeof (NotImplementedException))] + public void GetValueITRSTouchedWhenNodeFromReader () + { + // for a node returned from ResXResourceReader with FileRef, + // check params supplied to GetValue method of ResXDataNode are touched + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToSerializable ("ser.bbb",true); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + // raises error if touched + Icon ico = (Icon) returnedNode.GetValue (new ExceptionalTypeResolutionService ()); + + } + } + + [Test] + public void GetValueITRSNotUsedWhenNodeFromReader () + { + // for a node returend from reader with a FileRef, + // check ITRS supplied to GetValue method not actually used + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToSerializable ("ser.bbb",true); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object val = returnedNode.GetValue (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + + Assert.IsNotInstanceOfType (typeof (serializableSubClass), val, "#A2"); + Assert.IsInstanceOfType (typeof (serializable), val, "#A3"); + } + } + + [Test, ExpectedException(typeof (TypeLoadException))] + public void CantGetValueWithOnlyFullNameAsType () + { + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToSerializable ("ser.bbb", false); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object obj = returnedNode.GetValue ((AssemblyName[]) null); + + } + } + + [Test, ExpectedException (typeof (TypeLoadException))] + public void CantGetValueWithOnlyFullNameAsTypeByProvidingAssemblyName () + { + ResXDataNode originalNode, returnedNode; + + string aName = "System.Windows.Forms_test_net_2_0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + originalNode = GetNodeFileRefToSerializable ("ser.bbb", false); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object obj = returnedNode.GetValue (assemblyNames); + } + } + + [Test] + public void CanGetStrongNameFromGetValueTypeNameWithOnlyFullNameAsTypeByProvidingAssemblyName () + { + ResXDataNode originalNode, returnedNode; + + string aName = "System.Windows.Forms_test_net_2_0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + originalNode = GetNodeFileRefToSerializable ("ser.bbb", false); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string typeName = returnedNode.GetValueTypeName (assemblyNames); + + Assert.AreEqual ("MonoTests.System.Resources.serializable, " + aName, typeName, "#A2"); + } + } + + public void CanGetValueTypeNameWithOnlyFullNameAsType () + { + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToSerializable ("ser.bbb", false); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string typeName = returnedNode.GetValueTypeName ((AssemblyName []) null); + + Assert.AreEqual ((typeof (serializable)).FullName, typeName, "#A2"); + } + } + + [Test] + public void GetValueTypeNameITRSUsedWhenNodeFromReader () + { + // for a node returned from ResXResourceReader, check supplying params to + // GetValueTypeName changes the output of the method + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeFileRefToSerializable ("ser.bbb",true); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string returnedType = returnedNode.GetValueTypeName (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + + Assert.AreEqual ((typeof (serializableSubClass)).AssemblyQualifiedName, returnedType, "#A2"); + } + } + + [Test] + public void GetValueTypeNameITRSUsedWhenNodeCreatedNew () + { + // check supplying params GetValueTypeName of the + // UseResXDataNode does the output of the method for an instance + // initialised manually + + ResXDataNode node; + + node = GetNodeFileRefToSerializable ("ser.bbb",true); + + string returnedType = node.GetValueTypeName (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + + Assert.AreEqual ((typeof (serializableSubClass)).AssemblyQualifiedName, returnedType, "#A1"); + } + + [Test, ExpectedException (typeof (NotImplementedException))] + public void GetValueITRSTouchedWhenNodeCreatedNew () + { + // check supplyied params to GetValue of the ResXDataNode are touched for + // an instance initialised by me + + ResXDataNode node; + + node = GetNodeFileRefToSerializable ("ser.bbb",true); + + //raises exception if param touched + Object obj = node.GetValue (new ExceptionalTypeResolutionService ()); + } + + [Test] + public void GetValueITRSNotUsedWhenNodeCreatedNew () + { + // check supplyied params to GetValue of the ResXDataNode are not used for + // an instance initialised by me + + ResXDataNode node; + + node = GetNodeFileRefToSerializable ("ser.bbb",true); + + object val = node.GetValue (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + + Assert.IsNotInstanceOfType (typeof (serializableSubClass), val, "#A1"); + Assert.IsInstanceOfType (typeof (serializable), val, "#A2"); + } + + #region Initial Exploratory Tests + + [Test] + public void GetValueResXFileRefNullAssemblyNames () + { + ResXDataNode node = GetNodeFileRefToIcon (); + + Object ico = node.GetValue ((AssemblyName []) null); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + [Test] + public void GetValueResXFileRefNullITypeResolutionService () + { + ResXDataNode node = GetNodeFileRefToIcon (); + + Object ico = node.GetValue ((ITypeResolutionService) null); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + [Test] + public void GetValueTypeNameResXFileRefNullAssemblyNames () + { + ResXDataNode node = GetNodeFileRefToIcon (); + + string name = node.GetValueTypeName ((AssemblyName []) null); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueTypeNameResXFileRefNullITypeResolutionService () + { + + ResXDataNode node = GetNodeFileRefToIcon (); + + string name = node.GetValueTypeName ((ITypeResolutionService) null); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueTypeNameResXFileRefWrongITypeResolutionService () + { + + ResXDataNode node = GetNodeFileRefToIcon (); + + string name = node.GetValueTypeName (new DummyTypeResolutionService ()); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueResXFileRefWrongITypeResolutionService () + { + ResXDataNode node = GetNodeFileRefToIcon (); + + Object ico = node.GetValue (new DummyTypeResolutionService ()); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + [Test] + public void GetValueTypeNameResXFileRefWrongAssemblyNames () + { + + ResXDataNode node = GetNodeFileRefToIcon (); + + AssemblyName [] ass = new AssemblyName [1]; + + ass [0] = new AssemblyName ("System.Design"); + + string name = node.GetValueTypeName (ass); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueResXFileRefWrongAssemblyNames () + { + ResXDataNode node = GetNodeFileRefToIcon (); + + AssemblyName [] ass = new AssemblyName [1]; + + ass [0] = new AssemblyName ("System.Design"); + + Object ico = node.GetValue (ass); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + #endregion + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node) + { + string fileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fileName = Path.Combine (_tempDirectory, "myresx.resx"); + + using (ResXResourceWriter writer = new ResXResourceWriter (fileName)) { + writer.AddResource (node); + } + + return fileName; + } + + ResXDataNode GetNodeFileRefToSerializable (string filename, bool assemblyQualifiedName) + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + string refFile = Path.Combine (_tempDirectory, filename); + + serializable ser = new serializable ("name", "value"); + + WriteToFile (refFile, ser); + + string typeName; + + if (assemblyQualifiedName) + typeName = typeof (serializable).AssemblyQualifiedName; + else + typeName = typeof (serializable).FullName; + + ResXFileRef fileRef = new ResXFileRef (refFile, typeName); + + ResXDataNode node = new ResXDataNode ("test", fileRef); + + return node; + } + + private static void WriteToFile (string filepath, serializable ser) + { + + Stream stream = File.Open (filepath, FileMode.Create); + BinaryFormatter bFormatter = new BinaryFormatter (); + bFormatter.Serialize (stream, ser); + stream.Close (); + } + + ResXDataNode GetNodeFileRefToIcon () + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + string refFile = Path.Combine (_tempDirectory, "32x32.ico"); + WriteEmbeddedResource ("32x32.ico", refFile); + + ResXFileRef fileRef = new ResXFileRef (refFile, typeof (Icon).AssemblyQualifiedName); + ResXDataNode node = new ResXDataNode ("test", fileRef); + + return node; + } + + private static void WriteEmbeddedResource (string name, string filename) + { + const int size = 512; + byte [] buffer = new byte [size]; + int count = 0; + + Stream input = typeof (ResXDataNodeTest).Assembly. + GetManifestResourceStream (name); + Stream output = File.Open (filename, FileMode.Create); + + try { + while ((count = input.Read (buffer, 0, size)) > 0) { + output.Write (buffer, 0, count); + } + } finally { + output.Close (); + } + } + + } + +} +#endif
\ No newline at end of file diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeSerialisedGetValueTypeNameTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeSerialisedGetValueTypeNameTests.cs new file mode 100644 index 00000000000..c788bf2f622 --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeSerialisedGetValueTypeNameTests.cs @@ -0,0 +1,314 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + +namespace MonoTests.System.Resources +{ + [TestFixture] + public class ResXDataNodeSerializedGetValueTypeNameTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + + [Test] + public void ITRSUsedWithNodeFromReader () + { + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string returnedType = returnedNode.GetValueTypeName (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + + Assert.AreEqual ((typeof (serializableSubClass)).AssemblyQualifiedName, returnedType, "#A2"); + } + } + + [Test] + public void ITRSOnlyUsedFirstTimeWithNodeFromReader () + { + // check ITRS supplied to GetValueTypeName method for a node returned from reader are used when + // retrieving the value first time and returns this same value ignoring any new ITRS passed thereafter + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string defaultType = returnedNode.GetValueTypeName ((ITypeResolutionService) null); + Assert.AreEqual ((typeof (serializable)).AssemblyQualifiedName, defaultType, "#A2"); + + string newType = returnedNode.GetValueTypeName (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + Assert.AreNotEqual ((typeof (serializableSubClass)).AssemblyQualifiedName, newType, "#A3"); + Assert.AreEqual ((typeof (serializable)).AssemblyQualifiedName, newType, "#A4"); + } + } + + [Test] + public void ITRSNotUsedWhenNodeCreatedNew () + { + // check supplying params to GetValueType of the UseResXDataNode does not change the output + // of the method for an instance created manually + + ResXDataNode node; + node = GetNodeEmdeddedSerializable (); + string returnedType = node.GetValueTypeName (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + Assert.AreEqual ((typeof (serializable)).AssemblyQualifiedName, returnedType, "#A1"); + } + + [Test] + public void ITRSNotTouchedWhenNodeCreatedNew () + { + // check supplied params to GetValueType of the UseResXDataNode are not touched + // for an instance created manually + + ResXDataNode node; + + node = GetNodeEmdeddedSerializable (); + + // would raise exception if accessed + string returnedType = node.GetValueTypeName (new ExceptionalTypeResolutionService ()); + //Assert.AreEqual ((typeof (serializable)).AssemblyQualifiedName, returnedType, "#A1"); + } + + [Test] + public void ITRSIsIgnoredIfGetValueAlreadyCalledWithAnotherITRS () + { + // check that first call to GetValue sets the type for GetValueTypeName + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + // get value passing no params + object val = returnedNode.GetValue ((ITypeResolutionService) null); + Assert.IsInstanceOfType (typeof (serializable), val, "#A2"); + Assert.IsNotInstanceOfType (typeof (serializableSubClass), val, "#A3"); + + //get value type passing different params + string newType = returnedNode.GetValueTypeName (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + Assert.AreNotEqual ((typeof (serializableSubClass)).AssemblyQualifiedName, newType, "#A4"); + Assert.AreEqual ((typeof (serializable)).AssemblyQualifiedName, newType, "#A5"); + } + } + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node, string filename) + { + string fullfileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fullfileName = Path.Combine (_tempDirectory, filename); + + using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) { + writer.AddResource (node); + } + + return fullfileName; + } + + ResXDataNode GetNodeEmdeddedSerializable () + { + serializable ser = new serializable ("testName", "testValue"); + ResXDataNode node = new ResXDataNode ("test", ser); + return node; + } + + } + + [SerializableAttribute] + public class serializableSubClass : serializable { + public serializableSubClass () + { + } + + public serializableSubClass (SerializationInfo info, StreamingContext ctxt) + : base (info, ctxt) + { + } + + public serializableSubClass (Stream stream) + { + BinaryFormatter bFormatter = new BinaryFormatter (); + serializableSubClass deser = (serializableSubClass) bFormatter.Deserialize (stream); + stream.Close (); + + name = deser.name; + value = deser.value; + } + } + + public class AlwaysReturnSerializableSubClassTypeResolutionService : ITypeResolutionService { + + public Assembly GetAssembly (AssemblyName name, bool throwOnError) + { + throw new NotImplementedException ("I was accessed"); + } + + public Assembly GetAssembly (AssemblyName name) + { + throw new NotImplementedException ("I was accessed"); + } + + public string GetPathOfAssembly (AssemblyName name) + { + throw new NotImplementedException ("I was accessed"); + } + + public Type GetType (string name, bool throwOnError, bool ignoreCase) + { + return typeof (serializableSubClass); + } + + public Type GetType (string name, bool throwOnError) + { + return typeof (serializableSubClass); + } + + public Type GetType (string name) + { + return typeof (serializableSubClass); + } + + public void ReferenceAssembly (AssemblyName name) + { + + } + + } + + public class AlwaysReturnIntTypeResolutionService : ITypeResolutionService { + + public Assembly GetAssembly (AssemblyName name, bool throwOnError) + { + throw new NotImplementedException ("I was accessed"); + } + + public Assembly GetAssembly (AssemblyName name) + { + throw new NotImplementedException ("I was accessed"); + } + + public string GetPathOfAssembly (AssemblyName name) + { + throw new NotImplementedException ("I was accessed"); + } + + public Type GetType (string name, bool throwOnError, bool ignoreCase) + { + return typeof (Int32); + } + + public Type GetType (string name, bool throwOnError) + { + return typeof (Int32); + } + + public Type GetType (string name) + { + return typeof (Int32); + } + + public void ReferenceAssembly (AssemblyName name) + { + + } + } + + public class ExceptionalTypeResolutionService : ITypeResolutionService { + + public Assembly GetAssembly (AssemblyName name, bool throwOnError) + { + throw new NotImplementedException ("I was accessed"); + } + + public Assembly GetAssembly (AssemblyName name) + { + throw new NotImplementedException ("I was accessed"); + } + + public string GetPathOfAssembly (AssemblyName name) + { + throw new NotImplementedException ("I was accessed"); + } + + public Type GetType (string name, bool throwOnError, bool ignoreCase) + { + throw new NotImplementedException ("I was accessed"); + } + + public Type GetType (string name, bool throwOnError) + { + throw new NotImplementedException ("I was accessed"); + } + + public Type GetType (string name) + { + throw new NotImplementedException ("I was accessed"); + } + + public void ReferenceAssembly (AssemblyName name) + { + + } + + } + +} +#endif + diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeSerializedGetValueTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeSerializedGetValueTests.cs new file mode 100644 index 00000000000..acdf9f7ad16 --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeSerializedGetValueTests.cs @@ -0,0 +1,258 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + +namespace MonoTests.System.Resources +{ + [TestFixture] + public class ResXDataNodeSerializedGetValueTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + + [Test] + public void ITRSOnlyUsedFirstTimeWithNodeFromReader () + { + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object defaultVal = returnedNode.GetValue ((ITypeResolutionService) null); + Assert.IsInstanceOfType (typeof (serializable), defaultVal, "#A2"); + Assert.IsNotInstanceOfType (typeof (serializableSubClass), defaultVal, "#A3"); + + object newVal = returnedNode.GetValue (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + Assert.IsNotInstanceOfType (typeof (serializableSubClass), newVal, "#A4"); + Assert.IsInstanceOfType (typeof (serializable), newVal, "#A5"); + } + } + + [Test] + public void ITRSUsedWhenNodeReturnedFromReader () + { + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object val = returnedNode.GetValue (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + Assert.IsInstanceOfType (typeof (serializableSubClass), val, "#A2"); + } + } + + [Test] + public void OriginalTypeUsedWhenWritingBackToResX () + { + // check although calls subsequent to an ITRS being supplied to GetValue return that resolved type + // when the node is written back using ResXResourceWriter it uses the original type + + ResXDataNode originalNode, returnedNode, return2; + + originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object val = returnedNode.GetValue (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + Assert.IsInstanceOfType (typeof (serializableSubClass), val, "#A2"); + + string newResXFile = GetResXFileWithNode (returnedNode, "second.resx"); + + using (ResXResourceReader read2 = new ResXResourceReader (newResXFile)) { + read2.UseResXDataNodes = true; + + IDictionaryEnumerator enum2 = read2.GetEnumerator (); + enum2.MoveNext (); + return2 = (ResXDataNode) ((DictionaryEntry) enum2.Current).Value; + + Assert.IsNotNull (return2, "#A3"); + + object value2 = return2.GetValue ((ITypeResolutionService) null); + + Assert.IsNotInstanceOfType (typeof (serializableSubClass), value2, "#A4"); + Assert.IsInstanceOfType (typeof (serializable), value2, "#A5"); + } + } + } + + [Test] + public void ITRSIsIgnoredIfGetValueTypeNameAlreadyCalledWithAnotherITRS () + { + // check that first call to GetValueTypeName sets the type for GetValue + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + //get value type passing params + string newType = returnedNode.GetValueTypeName (new AlwaysReturnSerializableSubClassTypeResolutionService ()); + Assert.AreEqual ((typeof (serializableSubClass)).AssemblyQualifiedName, newType, "#A2"); + Assert.AreNotEqual ((typeof (serializable)).AssemblyQualifiedName, newType, "#A3"); + + // get value passing null params + object val = returnedNode.GetValue ((ITypeResolutionService) null); + // Assert.IsNotInstanceOfType (typeof (serializable), val, "#A5"); this would fail as subclasses are id-ed as instances of parents + Assert.IsInstanceOfType (typeof (serializableSubClass), val, "#A4"); + } + } + + [Test] + public void ITRSNotTouchedWhenNodeCreatedNew () + { + // check supplied params to GetValue are not touched + // for an instance created manually + + ResXDataNode node; + + node = GetNodeEmdeddedSerializable (); + + //would raise exception if param used + Object obj = node.GetValue (new ExceptionalTypeResolutionService ()); + Assert.IsInstanceOfType (typeof (serializable), obj, "#A1"); + } + + [Test] + public void ChangesToReturnedObjectNotLaterWrittenBack () + { + + ResXDataNode originalNode = GetNodeEmdeddedSerializable (); + + string fileName = GetResXFileWithNode (originalNode, "test.resx"); + + string newFileName; + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + ResXDataNode returnedNode; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object val = returnedNode.GetValue ((ITypeResolutionService) null); + Assert.IsInstanceOfType (typeof (serializable), val, "#A2"); + + serializable ser = (serializable) val; + + Assert.AreEqual ("testName", ser.name, "A3"); + + ser.name = "changed"; + newFileName = GetResXFileWithNode (returnedNode, "another.resx"); + } + + using (ResXResourceReader reader = new ResXResourceReader (newFileName)) { + reader.UseResXDataNodes = true; + + ResXDataNode returnedNode; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A4"); + + object val = returnedNode.GetValue ((ITypeResolutionService) null); + Assert.IsInstanceOfType (typeof (serializable), val, "#A5"); + + serializable ser = (serializable) val; + // would be "changed" if written back + Assert.AreEqual ("testName", ser.name, "A6"); + } + } + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node, string filename) + { + string fullfileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fullfileName = Path.Combine (_tempDirectory, filename); + + using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) { + writer.AddResource (node); + } + + return fullfileName; + } + + ResXDataNode GetNodeEmdeddedSerializable () + { + serializable ser = new serializable ("testName", "testValue"); + ResXDataNode node = new ResXDataNode ("test", ser); + return node; + } + + } + + +} +#endif + diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTest.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTest.cs index 3a8216b0368..6465b395d65 100644 --- a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTest.cs +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTest.cs @@ -23,22 +23,32 @@ // // Authors: // Andreia Gaita (avidigal@novell.com) -// +// Gary Barnett (2012) -#if NET_2_0 +#if NET_2_0 using System; +using System.IO; +using System.Reflection; +using System.Drawing; using System.Resources; using System.Runtime.Serialization; +using System.Collections.Generic; using System.Collections; using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + namespace MonoTests.System.Resources { [TestFixture] public class ResXDataNodeTest : MonoTests.System.Windows.Forms.TestHelper { - [Test] + string _tempDirectory; + string _otherTempDirectory; + + [Test] [ExpectedException (typeof (ArgumentNullException))] public void ConstructorEx1 () { @@ -79,7 +89,7 @@ namespace MonoTests.System.Resources { ResXDataNode d = new ResXDataNode ("name", new notserializable ()); } - + [Test] public void WriteRead1 () { @@ -92,12 +102,17 @@ namespace MonoTests.System.Resources bool found = false; ResXResourceReader rr = new ResXResourceReader ("resx.resx"); - IDictionaryEnumerator en = rr.GetEnumerator (); + rr.UseResXDataNodes = true; + IDictionaryEnumerator en = rr.GetEnumerator (); while (en.MoveNext ()) { - serializable o = ((DictionaryEntry) en.Current).Value as serializable; + ResXDataNode node = ((DictionaryEntry)en.Current).Value as ResXDataNode; + if (node == null) + break; + serializable o = node.GetValue ((AssemblyName[]) null) as serializable; if (o != null) { found = true; Assert.AreEqual (ser, o, "#A1"); + Assert.AreEqual ("comment", node.Comment, "#A3"); } } @@ -105,8 +120,561 @@ namespace MonoTests.System.Resources Assert.IsTrue (found, "#A2 - Serialized object not found on resx"); } + + [Test] + public void ConstructorResXFileRef() + { + ResXDataNode node = GetNodeFileRefToIcon (); + Assert.AreEqual (Path.Combine (_tempDirectory, "32x32.ico") , node.FileRef.FileName, "#A1"); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, node.FileRef.TypeName, "#A2"); + Assert.AreEqual ("test", node.Name, "#B1"); + } + + + [Test] + public void GetValueEmbeddedNullAssemblyNames () + { + ResXDataNode node = GetNodeEmdeddedIcon (); + + Object ico = node.GetValue ((AssemblyName[])null); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + [Test] + public void GetValueEmbeddedNullITypeResolutionService () + { + ResXDataNode node = GetNodeEmdeddedIcon (); + + Object ico = node.GetValue ((ITypeResolutionService)null); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + [Test] + public void GetValueTypeNameEmbeddedNullAssemblyNames () + { + ResXDataNode node = GetNodeEmdeddedIcon (); + + string name = node.GetValueTypeName ((AssemblyName[])null); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueTypeNameEmbeddedNullITypeResolutionService () + { + + ResXDataNode node = GetNodeEmdeddedIcon (); + + string name = node.GetValueTypeName ((ITypeResolutionService)null); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueTypeNameEmbeddedWrongITypeResolutionService () + { + + ResXDataNode node = GetNodeEmdeddedIcon (); + + string name = node.GetValueTypeName (new DummyTypeResolutionService ()); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueEmbeddedWrongITypeResolutionService () + { + ResXDataNode node = GetNodeEmdeddedIcon (); + + Object ico = node.GetValue (new DummyTypeResolutionService ()); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + [Test] + public void GetValueTypeNameEmbeddedWrongAssemblyNames () + { + + ResXDataNode node = GetNodeEmdeddedIcon (); + + AssemblyName[] ass = new AssemblyName[1]; + + ass[0] = new AssemblyName ("System.Design"); + + string name = node.GetValueTypeName (ass); + Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, name); + } + + [Test] + public void GetValueEmbeddedWrongAssemblyNames () + { + ResXDataNode node = GetNodeEmdeddedIcon (); + + AssemblyName[] ass = new AssemblyName[1]; + + ass[0] = new AssemblyName ("System.Design"); + + Object ico = node.GetValue (ass); + Assert.IsNotNull (ico, "#A1"); + Assert.IsInstanceOfType (typeof (Icon), ico, "#A2"); + } + + [Test] + public void DoesNotRequireResXFileToBeOpen_Serializable () + { + + serializable ser = new serializable ("aaaaa", "bbbbb"); + ResXDataNode dn = new ResXDataNode ("test", ser); + + string resXFile = GetResXFileWithNode (dn,"resx.resx"); + + ResXResourceReader rr = new ResXResourceReader (resXFile); + rr.UseResXDataNodes = true; + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode; + rr.Close (); + + File.Delete ("resx.resx"); + + Assert.IsNotNull (node,"#A1"); + + serializable o = node.GetValue ((AssemblyName []) null) as serializable; + + Assert.IsNotNull (o, "#A2"); + } + + [Test] + public void DoesNotRequireResXFileToBeOpen_TypeConverter () + { + + ResXDataNode dn = new ResXDataNode ("test", 34L); + + string resXFile = GetResXFileWithNode (dn,"resx.resx"); + + ResXResourceReader rr = new ResXResourceReader (resXFile); + rr.UseResXDataNodes = true; + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode; + rr.Close (); + + File.Delete ("resx.resx"); + + Assert.IsNotNull (node, "#A1"); + + object o = node.GetValue ((AssemblyName []) null); + + Assert.IsInstanceOfType (typeof (long), o, "#A2"); + Assert.AreEqual (34L, o, "#A3"); + } + + //FIXME: should move following tests to files associated with ResXResourceReader Tests + + [Test,ExpectedException (typeof(TypeLoadException))] + public void AssemblyNamesPassedToResourceReaderDoesNotAffectResXDataNode_TypeConverter () + { + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string resXFile = GetFileFromString ("test.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader rr = new ResXResourceReader (resXFile, assemblyNames)) { + rr.UseResXDataNodes = true; + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode; + + Assert.IsNotNull (node, "#A1"); + + //should raise exception + object o = node.GetValue ((AssemblyName []) null); + } + } + + [Test] + public void AssemblyNamesPassedToResourceReaderAffectsDictionary_TypeConverter () + { + string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; + AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) }; + + string resXFile = GetFileFromString ("test.resx", convertableResXWithoutAssemblyName); + + using (ResXResourceReader rr = new ResXResourceReader (resXFile, assemblyNames)) { + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + object obj = ((DictionaryEntry) en.Current).Value; + + Assert.IsNotNull (obj, "#A1"); + + Assert.AreEqual ("DummyAssembly.Convertable, " + aName, obj.GetType ().AssemblyQualifiedName, "#A2"); + + } + } + + [Test] + public void ITRSPassedToResourceReaderDoesNotAffectResXDataNode_TypeConverter () + { + + ResXDataNode dn = new ResXDataNode ("test", 34L); + + string resXFile = GetResXFileWithNode (dn,"resx.resx"); + + ResXResourceReader rr = new ResXResourceReader (resXFile, new AlwaysReturnIntTypeResolutionService()); + rr.UseResXDataNodes = true; + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode; + + Assert.IsNotNull (node, "#A1"); + + object o = node.GetValue ((AssemblyName []) null); + + Assert.IsInstanceOfType (typeof (long), o, "#A2"); + Assert.AreEqual (34L, o, "#A3"); + + rr.Close (); + } + + [Test] + public void ITRSPassedToResourceReaderDoesNotAffectResXDataNode_Serializable () + { + + serializable ser = new serializable ("aaaaa", "bbbbb"); + ResXDataNode dn = new ResXDataNode ("test", ser); + + string resXFile = GetResXFileWithNode (dn,"resx.resx"); + + ResXResourceReader rr = new ResXResourceReader (resXFile, new AlwaysReturnSerializableSubClassTypeResolutionService ()); + rr.UseResXDataNodes = true; + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode; + + Assert.IsNotNull (node, "#A1"); + + object o = node.GetValue ((AssemblyName []) null); + + Assert.IsNotInstanceOfType (typeof (serializableSubClass), o, "#A2"); + Assert.IsInstanceOfType (typeof (serializable), o, "#A3"); + rr.Close (); + } + + [Test] + public void ITRSPassedToResourceReaderAffectsDictionary_Serializable () + { + + serializable ser = new serializable ("aaaaa", "bbbbb"); + ResXDataNode dn = new ResXDataNode ("test", ser); + + string resXFile = GetResXFileWithNode (dn,"resx.resx"); + + ResXResourceReader rr = new ResXResourceReader (resXFile, new AlwaysReturnSerializableSubClassTypeResolutionService ()); + + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + object o = ((DictionaryEntry) en.Current).Value; + + Assert.IsNotNull (o, "#A1"); + + Assert.IsInstanceOfType (typeof (serializableSubClass), o,"#A2"); + + rr.Close (); + } + + [Test] + public void ITRSPassedToResourceReaderAffectsDictionary_TypeConverter () + { + + ResXDataNode dn = new ResXDataNode ("test", 34L); + + string resXFile = GetResXFileWithNode (dn,"resx.resx"); + + ResXResourceReader rr = new ResXResourceReader (resXFile, new AlwaysReturnIntTypeResolutionService ()); + + IDictionaryEnumerator en = rr.GetEnumerator (); + en.MoveNext (); + + object o = ((DictionaryEntry) en.Current).Value; + + Assert.IsNotNull (o, "#A1"); + + Assert.IsInstanceOfType (typeof (int), o,"#A2"); + Assert.AreEqual (34, o,"#A3"); + + rr.Close (); + } + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node, string filename) + { + string fullfileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fullfileName = Path.Combine (_tempDirectory, filename); + + using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) { + writer.AddResource (node); + } + + return fullfileName; + } + + ResXDataNode GetNodeFileRefToIcon () + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) + { + Directory.CreateDirectory (_otherTempDirectory); + } + + string refFile = Path.Combine (_tempDirectory, "32x32.ico"); + WriteEmbeddedResource ("32x32.ico", refFile); + + ResXFileRef fileRef = new ResXFileRef (refFile, typeof (Icon).AssemblyQualifiedName); + ResXDataNode node = new ResXDataNode ("test", fileRef); + + return node; + } + + ResXDataNode GetNodeEmdeddedIcon () + { + + Stream input = typeof (ResXDataNodeTest).Assembly. + GetManifestResourceStream ("32x32.ico"); + + Icon ico = new Icon (input); + + ResXDataNode node = new ResXDataNode ("test", ico); + + return node; + } + + private static void WriteEmbeddedResource (string name, string filename) + { + const int size = 512; + byte [] buffer = new byte [size]; + int count = 0; + + Stream input = typeof (ResXDataNodeTest).Assembly. + GetManifestResourceStream (name); + Stream output = File.Open (filename, FileMode.Create); + + try { + while ((count = input.Read (buffer, 0, size)) > 0) { + output.Write (buffer, 0, count); + } + } finally { + output.Close (); + } + } + + private string GetFileFromString (string filename, string filecontents) + { + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + string filepath = Path.Combine (_tempDirectory, filename); + + StreamWriter writer = new StreamWriter(filepath,false); + + writer.Write (filecontents); + writer.Close (); + + return filepath; + } + + static string convertableResXWithoutAssemblyName = +@"<?xml version=""1.0"" encoding=""utf-8""?> +<root> + <!-- + Microsoft ResX Schema + + Version 2.0 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name=""resmimetype"">text/microsoft-resx</resheader> + <resheader name=""version"">2.0</resheader> + <resheader name=""reader"">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name=""writer"">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name=""Name1""><value>this is my long string</value><comment>this is a comment</comment></data> + <data name=""Color1"" type=""System.Drawing.Color, System.Drawing"">Blue</data> + <data name=""Bitmap1"" mimetype=""application/x-microsoft.net.object.binary.base64""> + <value>[base64 mime encoded serialized .NET Framework object]</value> + </data> + <data name=""Icon1"" type=""System.Drawing.Icon, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> + <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> + <comment>This is a comment</comment> + </data> + + There are any number of ""resheader"" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""> + <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" /> + <xsd:element name=""root"" msdata:IsDataSet=""true""> + <xsd:complexType> + <xsd:choice maxOccurs=""unbounded""> + <xsd:element name=""metadata""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" /> + </xsd:sequence> + <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" /> + <xsd:attribute name=""type"" type=""xsd:string"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""assembly""> + <xsd:complexType> + <xsd:attribute name=""alias"" type=""xsd:string"" /> + <xsd:attribute name=""name"" type=""xsd:string"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""data""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" /> + <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" /> + <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" /> + <xsd:attribute ref=""xml:space"" /> + </xsd:complexType> + </xsd:element> + <xsd:element name=""resheader""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" /> + </xsd:sequence> + <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name=""resmimetype""> + <value>text/microsoft-resx</value> + </resheader> + <resheader name=""version""> + <value>2.0</value> + </resheader> + <resheader name=""reader""> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name=""writer""> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + + <data name=""test"" type=""DummyAssembly.Convertable""> + <value>im a name im a value</value> + </data> +</root>"; + } + + public class DummyTypeResolutionService : ITypeResolutionService + { + + public Assembly GetAssembly (AssemblyName name, bool throwOnError) + { + return null; + } + + public Assembly GetAssembly (AssemblyName name) + { + return null; + } + public string GetPathOfAssembly (AssemblyName name) + { + return null; + } + + public Type GetType (string name, bool throwOnError, bool ignoreCase) + { + return null; + } + + public Type GetType (string name, bool throwOnError) + { + return null; + } + + public Type GetType (string name) + { + return null; + } + + public void ReferenceAssembly (AssemblyName name) + { + + } + } + class notserializable { public object test; @@ -119,8 +687,12 @@ namespace MonoTests.System.Resources [SerializableAttribute] public class serializable : ISerializable { - string name; - string value; + public string name; + public string value; + + public serializable () + { + } public serializable (string name, string value) { @@ -134,6 +706,16 @@ namespace MonoTests.System.Resources value = (String) info.GetValue ("servalue", typeof (string)); } + public serializable (Stream stream) + { + BinaryFormatter bFormatter = new BinaryFormatter (); + serializable deser = (serializable) bFormatter.Deserialize (stream); + stream.Close (); + + name = deser.name; + value = deser.value; + } + public void GetObjectData (SerializationInfo info, StreamingContext ctxt) { info.AddValue ("sername", name); diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTypeConverterGetValueTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTypeConverterGetValueTests.cs new file mode 100644 index 00000000000..b07d58967de --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTypeConverterGetValueTests.cs @@ -0,0 +1,127 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + +namespace MonoTests.System.Resources +{ + [TestFixture] + public class ResXDataNodeTypeConverterGetValueTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + /* + [Test, ExpectedException (typeof (NotImplementedException))] + public void GetValueParamIsTouchedWhenEmbeddedReturnedFromResXResourceReader () + { + // after running the enumerator of ResXResourceReader with UseResXDataNodes set + // to true, check params supplied to GetValue method + // of ResXDataNode are used to deserialise + + // for now just throwing exception in param object to ensure its accessed + + ResXDataNode originalNode, returnedNode; + + originalNode = GetNodeEmdeddedIcon (); + + string fileName = GetResXFileWithNode (originalNode); + + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + Icon ico = (Icon)returnedNode.GetValue (new ExceptionalTypeResolutionService ()); + } + } + */ + + [Test] + public void ITRSNotUsedWhenCreatedNew () + { + // check supplying params to GetValue of the UseResXDataNode does not change the output + // of the method for an instance created manually + + ResXDataNode node; + node = new ResXDataNode ("along", 34L); + object obj = node.GetValue (new AlwaysReturnIntTypeResolutionService ()); + Assert.IsInstanceOfType (typeof(long), obj, "#A1"); + } + + [Test] + public void ITRSUsedEachTimeWithNodeFromReader () + { + // check GetValue uses ITRS param each time its called for a node from a ResXResourceReader + // for an object stored by means of a typeconverter, + + ResXDataNode returnedNode, originalNode; + + originalNode = new ResXDataNode ("aNumber", 23L); + + string fileName = GetResXFileWithNode (originalNode, "long.resx"); + + // should load assembly referenced in file + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + object newVal = returnedNode.GetValue (new AlwaysReturnIntTypeResolutionService ()); + + Assert.AreEqual (typeof (int).AssemblyQualifiedName, newVal.GetType ().AssemblyQualifiedName, "#A2"); + + object origVal = returnedNode.GetValue ((ITypeResolutionService) null); + + Assert.AreEqual (typeof (long).AssemblyQualifiedName, origVal.GetType().AssemblyQualifiedName, "#A3"); + } + } + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node, string filename) + { + string fullfileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fullfileName = Path.Combine (_tempDirectory, filename); + + using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) { + writer.AddResource (node); + } + + return fullfileName; + } + + + } +} +#endif diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTypeConverterGetValueTypeNameTests.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTypeConverterGetValueTypeNameTests.cs new file mode 100644 index 00000000000..7ee55e44d33 --- /dev/null +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXDataNodeTypeConverterGetValueTypeNameTests.cs @@ -0,0 +1,128 @@ +#if NET_2_0 +using System; +using System.IO; +using System.Reflection; +using System.Drawing; +using System.Resources; +using System.Runtime.Serialization; +using System.Collections.Generic; +using System.Collections; + +using NUnit.Framework; +using System.ComponentModel.Design; +using System.Runtime.Serialization.Formatters.Binary; + +namespace MonoTests.System.Resources +{ + [TestFixture] + public class ResXDataNodeTypeConverterGetValueTypeNameTests : MonoTests.System.Windows.Forms.TestHelper { + string _tempDirectory; + string _otherTempDirectory; + + [Test] + public void ITRSUsedWithNodeFromReader () + { + // for node returned from ResXResourceReader for an object stored by means of a typeconverter, + // check supplying ITRS changes output of method + + ResXDataNode returnedNode, originalNode; + + originalNode = new ResXDataNode ("aNumber", 23L); + + string fileName = GetResXFileWithNode (originalNode, "long.resx"); + + // should load assembly referenced in file + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string returnedType = returnedNode.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + + Assert.AreEqual ((typeof (Int32)).AssemblyQualifiedName, returnedType, "#A2"); + } + } + + [Test] + public void ITRSUsedEachTimeWhenNodeFromReader () + { + // for node returned from ResXResourceReader for an object stored by means of a typeconverter, + // check supplied ITRS changes output each time + + ResXDataNode returnedNode, originalNode; + + originalNode = new ResXDataNode ("aNumber", 23L); + + string fileName = GetResXFileWithNode (originalNode, "long.resx"); + + // should load assembly referenced in file + using (ResXResourceReader reader = new ResXResourceReader (fileName)) { + reader.UseResXDataNodes = true; + + IDictionaryEnumerator enumerator = reader.GetEnumerator (); + enumerator.MoveNext (); + returnedNode = (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value; + + Assert.IsNotNull (returnedNode, "#A1"); + + string newType = returnedNode.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + + Assert.AreEqual (typeof (int).AssemblyQualifiedName, newType, "#A2"); + + string origType = returnedNode.GetValueTypeName ((ITypeResolutionService) null); + + Assert.AreEqual (typeof (long).AssemblyQualifiedName, origType, "#A3"); + } + } + + [Test] + public void ITRSNotUsedWhenNodeCreatedNew () + { + // check supplying params to GetValueTypeName of the UseResXDataNode does not change the output + // of the method for an instance created manually + + ResXDataNode node; + node = new ResXDataNode ("along", 34L); + string returnedType = node.GetValueTypeName (new AlwaysReturnIntTypeResolutionService ()); + Assert.AreEqual ((typeof (long)).AssemblyQualifiedName, returnedType, "#A1"); + } + + [TearDown] + protected override void TearDown () + { + //teardown + if (Directory.Exists (_tempDirectory)) + Directory.Delete (_tempDirectory, true); + + base.TearDown (); + } + + string GetResXFileWithNode (ResXDataNode node, string filename) + { + string fullfileName; + + _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest"); + _otherTempDirectory = Path.Combine (_tempDirectory, "in"); + if (!Directory.Exists (_otherTempDirectory)) { + Directory.CreateDirectory (_otherTempDirectory); + } + + fullfileName = Path.Combine (_tempDirectory, filename); + + using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) { + writer.AddResource (node); + } + + return fullfileName; + } + + } + + +} +#endif + diff --git a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXFileRefTest.cs b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXFileRefTest.cs index 52b1951ca24..c4301a4f7d6 100644 --- a/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXFileRefTest.cs +++ b/mcs/class/Managed.Windows.Forms/Test/System.Resources/ResXFileRefTest.cs @@ -182,7 +182,7 @@ namespace MonoTests.System.Resources try { _converter.ConvertFrom (fileRef); Assert.Fail ("#A1"); - } catch (FileNotFoundException ex) { + } catch (FileNotFoundException ex) { //FIXME: GB: surely this should be Exception ex Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2"); Assert.IsNull (ex.InnerException, "#A3"); Assert.IsNotNull (ex.FileName, "#A4"); @@ -199,7 +199,7 @@ namespace MonoTests.System.Resources _converter.ConvertFrom (fileRef); Assert.Fail ("#B1"); #if NET_2_0 - } catch (ArgumentException ex) { + } catch (ArgumentException ex) { //FIXME: GB: surely this should be Exception ex Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2"); Assert.IsNull (ex.InnerException, "#B3"); Assert.IsNotNull (ex.Message, "#B4"); |