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.
34 using System.Configuration;
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Runtime.Serialization.Formatters.Binary;
38 using NUnit.Framework;
40 namespace MonoTests.System.Configuration {
43 public class SettingsPropertyValueTest {
46 public void Properties ()
48 SettingsProperty p = new SettingsProperty ("property",
53 SettingsSerializeAs.String,
58 SettingsPropertyValue v = new SettingsPropertyValue (p);
60 Assert.IsFalse (v.Deserialized, "A1");
61 Assert.IsFalse (v.IsDirty, "A2");
62 Assert.AreEqual ("property", v.Name, "A3");
63 Assert.AreEqual (p, v.Property, "A4");
64 Assert.AreEqual ((object)10, v.PropertyValue, "A5");
65 Assert.AreEqual (null, v.SerializedValue, "A6");
66 Assert.IsTrue (v.UsingDefaultValue, "A7");
68 /* test that setting v.PropertyValue to
69 * something else causes SerializedValue to
71 v.PropertyValue = (object)5;
72 Assert.AreEqual ("5", v.SerializedValue, "A9");
74 /* test to see whether or not changing
75 * SerializeAs causes SerializedValue to
77 p.SerializeAs = SettingsSerializeAs.Xml;
78 Assert.AreEqual ("5", v.SerializedValue, "A11"); /* nope.. */
80 /* only changing PropertyValue does */
81 v.PropertyValue = (object)7;
82 Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>7</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A13");
88 SettingsProperty p = new SettingsProperty ("property",
93 SettingsSerializeAs.String,
98 SettingsPropertyValue v = new SettingsPropertyValue (p);
100 Assert.AreEqual (10, v.PropertyValue, "A0");
101 Assert.IsFalse (v.IsDirty, "A1");
103 /* set PropertyValue to something else */
105 Assert.IsTrue (v.IsDirty, "A2");
108 /* set PropertyValue to the same thing */
110 Assert.IsTrue (v.IsDirty, "A3");
112 /* try out a non-value type */
113 p = new SettingsProperty ("property",
114 typeof (StringWriter),
118 SettingsSerializeAs.String,
122 v = new SettingsPropertyValue (p);
124 Assert.IsNotNull (v.PropertyValue, "A5");
126 Console.WriteLine (v.PropertyValue);
127 Assert.IsTrue (v.IsDirty, "A6");
131 public void UsingDefaultValue ()
133 SettingsProperty p = new SettingsProperty ("property",
138 SettingsSerializeAs.String,
143 SettingsPropertyValue v = new SettingsPropertyValue (p);
145 Assert.AreEqual (10, v.PropertyValue, "A1");
146 Assert.IsTrue (v.UsingDefaultValue, "A2");
148 /* set PropertyValue to something else */
150 Assert.IsFalse (v.UsingDefaultValue, "A3");
152 /* set PropertyValue back to the default */
153 v.PropertyValue = 10;
154 Assert.IsFalse (v.UsingDefaultValue, "A4");
158 public void String_Deserialize ()
160 SettingsProperty p = new SettingsProperty ("property",
165 SettingsSerializeAs.String,
170 SettingsPropertyValue v = new SettingsPropertyValue (p);
171 v.SerializedValue = "123";
173 Assert.AreEqual (123, v.PropertyValue, "A1");
174 Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
175 Assert.AreEqual (false, v.UsingDefaultValue, "A3");
179 public void Xml_Deserialize ()
181 SettingsProperty p = new SettingsProperty ("property",
186 SettingsSerializeAs.Xml,
191 SettingsPropertyValue v = new SettingsPropertyValue (p);
192 v.SerializedValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>123</int>";
194 Assert.AreEqual (123, v.PropertyValue, "A1");
195 Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
196 Assert.AreEqual (false, v.UsingDefaultValue, "A3");
200 public void String_Xml_Serialize ()
202 SettingsProperty p = new SettingsProperty ("property",
207 SettingsSerializeAs.String,
212 SettingsPropertyValue v = new SettingsPropertyValue (p);
214 v.PropertyValue = 10;
215 Assert.AreEqual (10, v.PropertyValue, "A1");
216 Assert.AreEqual ("10", v.SerializedValue, "A2");
218 v.PropertyValue = 10;
219 p.SerializeAs = SettingsSerializeAs.Xml;
221 Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>10</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A3");
226 public void Binary_Serialize ()
228 SettingsProperty p = new SettingsProperty ("property",
233 SettingsSerializeAs.Binary,
238 SettingsPropertyValue v = new SettingsPropertyValue (p);
241 v.PropertyValue = 10;
243 Assert.AreEqual (typeof (byte[]), v.SerializedValue.GetType(), "A1");
244 foo = (byte[])v.SerializedValue;
247 Assert.AreEqual (5, v.PropertyValue, "A2");
250 BinaryFormatter bf = new BinaryFormatter ();
251 MemoryStream ms = new MemoryStream (foo);
252 Assert.AreEqual (10, bf.Deserialize (ms), "A3");
254 v.Deserialized = false;
255 v.SerializedValue = foo;
257 Assert.AreEqual (10, v.PropertyValue, "A4");
261 public void DefaultValueType ()
263 SettingsProperty p1 = new SettingsProperty ("property",
268 SettingsSerializeAs.String,
272 SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
273 Assert.AreEqual (typeof (int), v1.PropertyValue.GetType (), "A1");
274 Assert.AreEqual (10, v1.PropertyValue, "A2");
276 SettingsProperty p2 = new SettingsProperty ("property",
281 SettingsSerializeAs.String,
285 SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
286 Assert.AreEqual (typeof (int), v2.PropertyValue.GetType (), "A3");
287 Assert.AreEqual (10, v2.PropertyValue, "A4");
293 private int intProp = 777;
296 get { return intProp; }
297 set { intProp = value; }
302 public void DefaultValueCompexTypeEmpty ()
304 SettingsProperty p1 = new SettingsProperty ("property",
309 SettingsSerializeAs.String,
313 SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
314 Assert.IsNotNull (v1.PropertyValue, "A1");
315 Assert.AreEqual (typeof (MyData), v1.PropertyValue.GetType (), "A2");
316 MyData h = (MyData) v1.PropertyValue;
317 Assert.AreEqual (777, h.IntProp, "A3");
321 public void DefaultValueCompexType ()
323 SettingsProperty p2 = new SettingsProperty ("property",
327 @"<?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>",
328 SettingsSerializeAs.Xml,
332 SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
333 Assert.IsNotNull (v2.PropertyValue, "A1");
334 Assert.AreEqual (typeof (MyData), v2.PropertyValue.GetType (), "A2");
335 MyData h = (MyData) v2.PropertyValue;
336 Assert.AreEqual (5, h.IntProp, "A3");
340 public void IsDirtyAndValueDateTime ()
342 SettingsProperty sp = new SettingsProperty ("heh");
343 sp.PropertyType = typeof (DateTime);
345 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
346 Assert.IsFalse (spv.IsDirty, "A1");
347 Assert.IsNotNull (spv.PropertyValue, "A2");
348 Assert.AreEqual (typeof (DateTime), spv.PropertyValue.GetType (), "A3");
349 Assert.IsFalse (spv.IsDirty, "A4");
353 public void IsDirtyAndValuePrimitive ()
355 SettingsProperty sp = new SettingsProperty ("heh");
356 sp.PropertyType = typeof (int);
358 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
359 Assert.IsFalse (spv.IsDirty, "A1");
360 Assert.AreEqual (0, spv.PropertyValue, "A2");
361 Assert.AreEqual (typeof (int), spv.PropertyValue.GetType (), "A3");
362 Assert.IsFalse (spv.IsDirty, "A4");
366 public void IsDirtyAndValueDecimal ()
368 SettingsProperty sp = new SettingsProperty ("heh");
369 sp.PropertyType = typeof (decimal);
371 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
372 Assert.IsFalse (spv.IsDirty, "A1");
373 Assert.AreEqual (0, spv.PropertyValue, "A2");
374 Assert.AreEqual (typeof (decimal), spv.PropertyValue.GetType (), "A3");
375 Assert.IsTrue (spv.IsDirty, "A4");
379 public void IsDirtyAndValueString ()
381 SettingsProperty sp = new SettingsProperty ("heh");
382 sp.PropertyType = typeof (string);
384 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
385 Assert.IsFalse (spv.IsDirty, "A1");
386 Assert.IsNull (spv.PropertyValue, "A2");
387 Assert.IsFalse (spv.IsDirty, "A3");
389 SettingsProperty sp2 = new SettingsProperty ("heh");
390 sp2.PropertyType = typeof (string);
391 sp2.DefaultValue = "";
393 SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
394 Assert.IsFalse (spv2.IsDirty, "A4");
395 Assert.IsNotNull (spv2.PropertyValue, "A5");
396 Assert.IsFalse (spv2.IsDirty, "A6");
400 public struct MyData2
406 public void IsDirtyAndValueMyData2 ()
408 SettingsProperty sp = new SettingsProperty ("heh");
409 sp.PropertyType = typeof (MyData2);
411 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
412 Assert.IsFalse (spv.IsDirty, "A1");
413 Assert.IsNotNull (spv.PropertyValue, "A2");
414 Assert.IsTrue (spv.IsDirty, "A3");
418 public void IsDirtyAndValueArrayList ()
420 SettingsProperty sp = new SettingsProperty ("heh");
421 sp.PropertyType = typeof (ArrayList);
423 SettingsPropertyValue spv = new SettingsPropertyValue (sp);
424 Assert.IsFalse (spv.IsDirty, "A1");
425 Assert.IsNull (spv.PropertyValue, "A2");
426 Assert.IsFalse (spv.IsDirty, "A3");
428 SettingsProperty sp2 = new SettingsProperty ("heh");
429 sp2.PropertyType = typeof (ArrayList);
430 sp2.DefaultValue = "";
432 SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
433 Assert.IsFalse (spv2.IsDirty, "A5");
434 Assert.IsNotNull (spv2.PropertyValue, "A6");
435 Assert.AreEqual (typeof (ArrayList), spv2.PropertyValue.GetType (), "A7");
436 Assert.IsTrue (spv2.IsDirty, "A8");