2 // System.Configuration.SettingsPropertyValueTest.cs - Unit tests for
3 // System.Configuration.SettingsPropertyValue.
6 // Chris Toshok <toshok@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Configuration;
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Runtime.Serialization.Formatters.Binary;
37 using NUnit.Framework;
39 namespace MonoTests.System.Configuration {
42 public class SettingsPropertyValueTest {
45 public void Properties ()
47 SettingsProperty p = new SettingsProperty ("property",
52 SettingsSerializeAs.String,
57 SettingsPropertyValue v = new SettingsPropertyValue (p);
59 Assert.IsFalse (v.Deserialized, "A1");
60 Assert.IsFalse (v.IsDirty, "A2");
61 Assert.AreEqual ("property", v.Name, "A3");
62 Assert.AreEqual (p, v.Property, "A4");
63 Assert.AreEqual ((object)10, v.PropertyValue, "A5");
64 Assert.AreEqual (null, v.SerializedValue, "A6");
65 Assert.IsTrue (v.UsingDefaultValue, "A7");
67 /* test that setting v.PropertyValue to
68 * something else causes SerializedValue to
70 v.PropertyValue = (object)5;
71 Assert.AreEqual ("5", v.SerializedValue, "A9");
73 /* test to see whether or not changing
74 * SerializeAs causes SerializedValue to
76 p.SerializeAs = SettingsSerializeAs.Xml;
77 Assert.AreEqual ("5", v.SerializedValue, "A11"); /* nope.. */
79 /* only changing PropertyValue does */
80 v.PropertyValue = (object)7;
81 Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>7</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A13");
87 SettingsProperty p = new SettingsProperty ("property",
92 SettingsSerializeAs.String,
97 SettingsPropertyValue v = new SettingsPropertyValue (p);
99 Assert.AreEqual (10, v.PropertyValue, "A0");
100 Assert.IsFalse (v.IsDirty, "A1");
102 /* set PropertyValue to something else */
104 Assert.IsTrue (v.IsDirty, "A2");
107 /* set PropertyValue to the same thing */
109 Assert.IsTrue (v.IsDirty, "A3");
111 /* try out a non-value type */
112 p = new SettingsProperty ("property",
113 typeof (StringWriter),
117 SettingsSerializeAs.String,
121 v = new SettingsPropertyValue (p);
123 Assert.IsNotNull (v.PropertyValue, "A5");
125 Console.WriteLine (v.PropertyValue);
126 Assert.IsTrue (v.IsDirty, "A6");
130 public void UsingDefaultValue ()
132 SettingsProperty p = new SettingsProperty ("property",
137 SettingsSerializeAs.String,
142 SettingsPropertyValue v = new SettingsPropertyValue (p);
144 Assert.AreEqual (10, v.PropertyValue, "A1");
145 Assert.IsTrue (v.UsingDefaultValue, "A2");
147 /* set PropertyValue to something else */
149 Assert.IsFalse (v.UsingDefaultValue, "A3");
151 /* set PropertyValue back to the default */
152 v.PropertyValue = 10;
153 Assert.IsFalse (v.UsingDefaultValue, "A4");
157 public void String_Deserialize ()
159 SettingsProperty p = new SettingsProperty ("property",
164 SettingsSerializeAs.String,
169 SettingsPropertyValue v = new SettingsPropertyValue (p);
170 v.SerializedValue = "123";
172 Assert.AreEqual (123, v.PropertyValue, "A1");
173 Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
174 Assert.AreEqual (false, v.UsingDefaultValue, "A3");
178 public void Xml_Deserialize ()
180 SettingsProperty p = new SettingsProperty ("property",
185 SettingsSerializeAs.Xml,
190 SettingsPropertyValue v = new SettingsPropertyValue (p);
191 v.SerializedValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>123</int>";
193 Assert.AreEqual (123, v.PropertyValue, "A1");
194 Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
195 Assert.AreEqual (false, v.UsingDefaultValue, "A3");
199 public void String_Xml_Serialize ()
201 SettingsProperty p = new SettingsProperty ("property",
206 SettingsSerializeAs.String,
211 SettingsPropertyValue v = new SettingsPropertyValue (p);
213 v.PropertyValue = 10;
214 Assert.AreEqual (10, v.PropertyValue, "A1");
215 Assert.AreEqual ("10", v.SerializedValue, "A2");
217 v.PropertyValue = 10;
218 p.SerializeAs = SettingsSerializeAs.Xml;
220 Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>10</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A3");
225 public void Binary_Serialize ()
227 SettingsProperty p = new SettingsProperty ("property",
232 SettingsSerializeAs.Binary,
237 SettingsPropertyValue v = new SettingsPropertyValue (p);
240 v.PropertyValue = 10;
242 Assert.AreEqual (typeof (byte[]), v.SerializedValue.GetType(), "A1");
243 foo = (byte[])v.SerializedValue;
246 Assert.AreEqual (5, v.PropertyValue, "A2");
249 BinaryFormatter bf = new BinaryFormatter ();
250 MemoryStream ms = new MemoryStream (foo);
251 Assert.AreEqual (10, bf.Deserialize (ms), "A3");
253 v.Deserialized = false;
254 v.SerializedValue = foo;
256 Assert.AreEqual (10, v.PropertyValue, "A4");
260 public void DefaultValueType ()
262 SettingsProperty p1 = new SettingsProperty ("property",
267 SettingsSerializeAs.String,
271 SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
272 Assert.AreEqual (typeof (int), v1.PropertyValue.GetType (), "A1");
273 Assert.AreEqual (10, v1.PropertyValue, "A2");
275 SettingsProperty p2 = new SettingsProperty ("property",
280 SettingsSerializeAs.String,
284 SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
285 Assert.AreEqual (typeof (int), v2.PropertyValue.GetType (), "A3");
286 Assert.AreEqual (10, v2.PropertyValue, "A4");
292 private int intProp = 777;
295 get { return intProp; }
296 set { intProp = value; }
301 public void DefaultValueCompexTypeEmpty ()
303 SettingsProperty p1 = new SettingsProperty ("property",
308 SettingsSerializeAs.String,
312 SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
313 Assert.IsNotNull (v1.PropertyValue, "A1");
314 Assert.AreEqual (typeof (MyData), v1.PropertyValue.GetType (), "A2");
315 MyData h = (MyData) v1.PropertyValue;
316 Assert.AreEqual (777, h.IntProp, "A3");
320 public void DefaultValueCompexType ()
322 SettingsProperty p2 = new SettingsProperty ("property",
326 @"<?xml version=""1.0"" encoding=""utf-16""?><MyData xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><IntProp>5</IntProp></MyData>",
327 SettingsSerializeAs.Xml,
331 SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
332 Assert.IsNotNull (v2.PropertyValue, "A1");
333 Assert.AreEqual (typeof (MyData), v2.PropertyValue.GetType (), "A2");
334 MyData h = (MyData) v2.PropertyValue;
335 Assert.AreEqual (5, h.IntProp, "A3");
339 public void IsDirtyAndValueDateTime ()
341 SettingsProperty sp = new SettingsProperty ("heh");
342 sp.PropertyType = typeof (DateTime);
344 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
345 Assert.IsFalse (spv.IsDirty, "A1");
346 Assert.IsNotNull (spv.PropertyValue, "A2");
347 Assert.AreEqual (typeof (DateTime), spv.PropertyValue.GetType (), "A3");
348 Assert.IsFalse (spv.IsDirty, "A4");
352 public void IsDirtyAndValuePrimitive ()
354 SettingsProperty sp = new SettingsProperty ("heh");
355 sp.PropertyType = typeof (int);
357 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
358 Assert.IsFalse (spv.IsDirty, "A1");
359 Assert.AreEqual (0, spv.PropertyValue, "A2");
360 Assert.AreEqual (typeof (int), spv.PropertyValue.GetType (), "A3");
361 Assert.IsFalse (spv.IsDirty, "A4");
365 public void IsDirtyAndValueDecimal ()
367 SettingsProperty sp = new SettingsProperty ("heh");
368 sp.PropertyType = typeof (decimal);
370 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
371 Assert.IsFalse (spv.IsDirty, "A1");
372 Assert.AreEqual (0, spv.PropertyValue, "A2");
373 Assert.AreEqual (typeof (decimal), spv.PropertyValue.GetType (), "A3");
374 Assert.IsTrue (spv.IsDirty, "A4");
378 public void IsDirtyAndValueString ()
380 SettingsProperty sp = new SettingsProperty ("heh");
381 sp.PropertyType = typeof (string);
383 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
384 Assert.IsFalse (spv.IsDirty, "A1");
385 Assert.IsNull (spv.PropertyValue, "A2");
386 Assert.IsFalse (spv.IsDirty, "A3");
388 SettingsProperty sp2 = new SettingsProperty ("heh");
389 sp2.PropertyType = typeof (string);
390 sp2.DefaultValue = "";
392 SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
393 Assert.IsFalse (spv2.IsDirty, "A4");
394 Assert.IsNotNull (spv2.PropertyValue, "A5");
395 Assert.IsFalse (spv2.IsDirty, "A6");
399 public struct MyData2
405 public void IsDirtyAndValueMyData2 ()
407 SettingsProperty sp = new SettingsProperty ("heh");
408 sp.PropertyType = typeof (MyData2);
410 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
411 Assert.IsFalse (spv.IsDirty, "A1");
412 Assert.IsNotNull (spv.PropertyValue, "A2");
413 Assert.IsTrue (spv.IsDirty, "A3");
417 public void IsDirtyAndValueArrayList ()
419 SettingsProperty sp = new SettingsProperty ("heh");
420 sp.PropertyType = typeof (ArrayList);
422 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
423 Assert.IsFalse (spv.IsDirty, "A1");
424 Assert.IsNull (spv.PropertyValue, "A2");
425 Assert.IsFalse (spv.IsDirty, "A3");
427 SettingsProperty sp2 = new SettingsProperty ("heh");
428 sp2.PropertyType = typeof (ArrayList);
429 sp2.DefaultValue = "";
431 SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
432 Assert.IsFalse (spv2.IsDirty, "A5");
433 Assert.IsNotNull (spv2.PropertyValue, "A6");
434 Assert.AreEqual (typeof (ArrayList), spv2.PropertyValue.GetType (), "A7");
435 Assert.IsTrue (spv2.IsDirty, "A8");