2 // ResXDataNodeSerializedGetValueTests.cs
5 // Gary Barnett (gary.barnett.mono@gmail.com)
7 // Copyright (C) Gary Barnett (2012)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Resources;
31 using System.Collections;
32 using NUnit.Framework;
33 using System.ComponentModel.Design;
34 using System.Runtime.Serialization.Formatters.Soap;
35 using System.Reflection;
37 using System.Runtime.Serialization;
39 namespace MonoTests.System.Resources {
41 public class ResXDataNodeSerializedGetValueTests : ResourcesTestHelper {
43 public void ITRSOnlyUsedFirstTimeWithNodeFromReader ()
45 ResXDataNode originalNode, returnedNode;
46 originalNode = GetNodeEmdeddedSerializable ();
47 returnedNode = GetNodeFromResXReader (originalNode);
49 Assert.IsNotNull (returnedNode, "#A1");
51 object defaultVal = returnedNode.GetValue ((ITypeResolutionService) null);
52 Assert.IsInstanceOfType (typeof (serializable), defaultVal, "#A2");
53 Assert.IsNotInstanceOfType (typeof (serializableSubClass), defaultVal, "#A3");
55 object newVal = returnedNode.GetValue (new ReturnSerializableSubClassITRS ());
56 Assert.IsNotInstanceOfType (typeof (serializableSubClass), newVal, "#A4");
57 Assert.IsInstanceOfType (typeof (serializable), newVal, "#A5");
61 public void ITRSUsedWhenNodeReturnedFromReader ()
63 ResXDataNode originalNode, returnedNode;
64 originalNode = GetNodeEmdeddedSerializable ();
65 returnedNode = GetNodeFromResXReader (originalNode);
67 Assert.IsNotNull (returnedNode, "#A1");
69 object val = returnedNode.GetValue (new ReturnSerializableSubClassITRS ());
70 Assert.IsInstanceOfType (typeof (serializableSubClass), val, "#A2");
74 public void ITRSIsIgnoredIfGetValueTypeNameAlreadyCalledWithAnotherITRS ()
76 // check that first call to GetValueTypeName sets the type for GetValue
78 ResXDataNode originalNode, returnedNode;
80 originalNode = GetNodeEmdeddedSerializable ();
81 returnedNode = GetNodeFromResXReader (originalNode);
82 Assert.IsNotNull (returnedNode, "#A1");
84 //get value type passing params
85 string newType = returnedNode.GetValueTypeName (new ReturnSerializableSubClassITRS ());
86 Assert.AreEqual ((typeof (serializableSubClass)).AssemblyQualifiedName, newType, "#A2");
87 Assert.AreNotEqual ((typeof (serializable)).AssemblyQualifiedName, newType, "#A3");
89 // get value passing null params
90 object val = returnedNode.GetValue ((ITypeResolutionService) null);
91 // Assert.IsNotInstanceOfType (typeof (serializable), val, "#A5"); this would fail as subclasses are id-ed as instances of parents
92 Assert.IsInstanceOfType (typeof (serializableSubClass), val, "#A4");
96 public void ITRSNotTouchedWhenNodeCreatedNew ()
98 // check supplied params to GetValue are not touched
99 // for an instance created manually
100 ResXDataNode node = GetNodeEmdeddedSerializable ();
102 //would raise exception if param used
103 Object obj = node.GetValue (new ExceptionalITRS ());
104 Assert.IsInstanceOfType (typeof (serializable), obj, "#A1");
107 [Test, ExpectedException (typeof (SerializationException))]
108 public void DeserializationError ()
110 ResXDataNode node = GetNodeFromResXReader (serializedResXCorruped);
111 Assert.IsNotNull (node, "#A1");
112 object val = node.GetValue ((AssemblyName []) null);
116 public void InvalidMimeTypeFromReaderReturnsNull ()
118 ResXDataNode node = GetNodeFromResXReader (serializedResXInvalidMimeType);
119 Assert.IsNotNull (node, "#A1");
120 object val = node.GetValue ((AssemblyName []) null);
121 Assert.IsNull (val, "#A2");
125 public void SoapFormattedObject ()
127 ResXDataNode node = GetNodeFromResXReader (serializedResXSOAP);
128 Assert.IsNotNull (node, "#A1");
129 // hard coded assembly name value refers to that generated under 2.0 prefix, so use compatible available class
130 object val = node.GetValue (new ReturnSerializableSubClassITRS ());
131 Assert.AreEqual ("name=aname;value=avalue", val.ToString (), "#A2");
135 public void AssemblyAutomaticallyLoaded ()
137 // DummyAssembly must be in the same directory as current assembly to work correctly
138 ResXDataNode node = GetNodeFromResXReader (anotherSerializableFromDummyAssembly);
139 Assert.IsNotNull (node, "#A1");
140 object value = node.GetValue ((AssemblyName []) null);
141 Assert.AreEqual ("DummyAssembly.AnotherSerializable, DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", value.GetType ().AssemblyQualifiedName, "#A2");
144 [Test, ExpectedException (typeof (ArgumentException))]
145 public void ErrorWhenAssemblyMissing ()
147 ResXDataNode node = GetNodeFromResXReader (missingSerializableFromMissingAssembly);
148 Assert.IsNotNull (node, "#A1");
149 object val = node.GetValue ((AssemblyName []) null);
153 public void RefToSameObjectNotHeld ()
155 ResXDataNode node = GetNodeEmdeddedSerializable ();
156 ResXDataNode returnedNode = GetNodeFromResXReader (node);
157 Assert.IsNotNull (returnedNode, "#A1");
158 serializable ser1 = (serializable) returnedNode.GetValue ((AssemblyName []) null);
159 ser1.name = "beenchanged";
160 serializable ser2 = (serializable) returnedNode.GetValue ((AssemblyName []) null);
161 Assert.AreNotSame (ser1, ser2, "#A2");
164 static string serializedResXInvalidMimeType =
165 @"<?xml version=""1.0"" encoding=""utf-8""?>
168 <resheader name=""resmimetype"">
169 <value>text/microsoft-resx</value>
171 <resheader name=""version"">
174 <resheader name=""reader"">
175 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
177 <resheader name=""writer"">
178 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
180 <data name=""test"" mimetype=""application/xxxx"">
182 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
183 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
184 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
185 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
190 static string serializedResXCorruped =
191 @"<?xml version=""1.0"" encoding=""utf-8""?>
194 <resheader name=""resmimetype"">
195 <value>text/microsoft-resx</value>
197 <resheader name=""version"">
200 <resheader name=""reader"">
201 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
203 <resheader name=""writer"">
204 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
206 <data name=""test"" mimetype=""application/x-microsoft.net.object.binary.base64"">
208 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
209 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
210 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
211 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
216 static string soapSerializedSerializable =
217 @"<SOAP-ENV:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:clr=""http://schemas.microsoft.com/soap/encoding/clr/1.0"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">
219 <a1:serializable id=""ref-1"" xmlns:a1=""http://schemas.microsoft.com/clr/nsassem/MonoTests.System.Resources/System.Windows.Forms_test_net_2_0%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"">
220 <sername id=""ref-3"">aname</sername>
221 <servalue id=""ref-4"">avalue</servalue>
224 </SOAP-ENV:Envelope>";
226 static string serializedResXSOAP =
227 @"<?xml version=""1.0"" encoding=""utf-8""?>
229 <resheader name=""resmimetype"">
230 <value>text/microsoft-resx</value>
232 <resheader name=""version"">
235 <resheader name=""reader"">
236 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
238 <resheader name=""writer"">
239 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
241 <data name=""test"" mimetype=""application/x-microsoft.net.object.soap.base64"">
243 PFNPQVAtRU5WOkVudmVsb3BlIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2Ui
244 IHhtbG5zOnhzZD0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEiIHhtbG5zOlNPQVAtRU5DPSJodHRwOi8vc2No
245 ZW1hcy54bWxzb2FwLm9yZy9zb2FwL2VuY29kaW5nLyIgeG1sbnM6U09BUC1FTlY9Imh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAu
246 b3JnL3NvYXAvZW52ZWxvcGUvIiB4bWxuczpjbHI9Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vc29hcC9lbmNvZGlu
247 Zy9jbHIvMS4wIiBTT0FQLUVOVjplbmNvZGluZ1N0eWxlPSJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy9zb2FwL2VuY29k
248 aW5nLyI+DQo8U09BUC1FTlY6Qm9keT4NCjxhMTpzZXJpYWxpemFibGUgaWQ9InJlZi0xIiB4bWxuczphMT0iaHR0cDovL3Nj
249 aGVtYXMubWljcm9zb2Z0LmNvbS9jbHIvbnNhc3NlbS9Nb25vVGVzdHMuU3lzdGVtLlJlc291cmNlcy9TeXN0ZW0uV2luZG93
250 cy5Gb3Jtc190ZXN0X25ldF8yXzAlMkMlMjBWZXJzaW9uJTNEMC4wLjAuMCUyQyUyMEN1bHR1cmUlM0RuZXV0cmFsJTJDJTIw
251 UHVibGljS2V5VG9rZW4lM0RudWxsIj4NCjxzZXJuYW1lIGlkPSJyZWYtMyI+YW5hbWU8L3Nlcm5hbWU+DQo8c2VydmFsdWUg
252 aWQ9InJlZi00Ij5hdmFsdWU8L3NlcnZhbHVlPg0KPC9hMTpzZXJpYWxpemFibGU+DQo8L1NPQVAtRU5WOkJvZHk+DQo8L1NP
253 QVAtRU5WOkVudmVsb3BlPg==
258 static string missingSerializableFromMissingAssembly =
259 @"<?xml version=""1.0"" encoding=""utf-8""?>
261 <resheader name=""resmimetype"">
262 <value>text/microsoft-resx</value>
264 <resheader name=""version"">
267 <resheader name=""reader"">
268 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
270 <resheader name=""writer"">
271 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
273 <data name=""aname"" mimetype=""application/x-microsoft.net.object.binary.base64"">
275 AAEAAAD/////AQAAAAAAAAAMAgAAAEZNaXNzaW5nQXNzZW1ibHksIFZlcnNpb249MS4wLjAuMCwgQ3Vs
276 dHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1udWxsBQEAAAAhRHVtbXlBc3NlbWJseS5NaXNzaW5n
277 U2VyaWFsaXphYmxlAgAAAAdzZXJuYW1lCHNlcnZhbHVlAQECAAAABgMAAAAFYW5hbWUGBAAAAAZhdmFs
283 static string anotherSerializableFromDummyAssembly =
284 @"<?xml version=""1.0"" encoding=""utf-8""?>
287 <resheader name=""resmimetype"">
288 <value>text/microsoft-resx</value>
290 <resheader name=""version"">
293 <resheader name=""reader"">
294 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
296 <resheader name=""writer"">
297 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
299 <data name=""aname"" mimetype=""application/x-microsoft.net.object.binary.base64"">
301 AAEAAAD/////AQAAAAAAAAAMAgAAAEREdW1teUFzc2VtYmx5LCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1
302 cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAUBAAAAIUR1bW15QXNzZW1ibHkuQW5vdGhlclNl
303 cmlhbGl6YWJsZQIAAAAHc2VybmFtZQhzZXJ2YWx1ZQEBAgAAAAYDAAAABWFuYW1lBgQAAAAGYXZhbHVl
309 static string convertableResXWithoutAssemblyName =
310 @"<?xml version=""1.0"" encoding=""utf-8""?>
313 <resheader name=""resmimetype"">
314 <value>text/microsoft-resx</value>
316 <resheader name=""version"">
319 <resheader name=""reader"">
320 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
322 <resheader name=""writer"">
323 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
326 <data name=""test"" type=""DummyAssembly.Convertable"">
327 <value>im a name im a value</value>
331 static string thisAssemblyConvertableResXWithoutAssemblyName =
332 @"<?xml version=""1.0"" encoding=""utf-8""?>
335 <resheader name=""resmimetype"">
336 <value>text/microsoft-resx</value>
338 <resheader name=""version"">
341 <resheader name=""reader"">
342 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
344 <resheader name=""writer"">
345 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
348 <data name=""test"" type=""MonoTests.System.Resources.ThisAssemblyConvertable"">
349 <value>im a name im a value</value>