fixed IsDirty Property, added tests for IsDirty Property
[mono.git] / mcs / class / System / Test / System.Configuration / SettingsPropertyValueTest.cs
1 //
2 // System.Configuration.SettingsPropertyValueTest.cs - Unit tests for
3 // System.Configuration.SettingsPropertyValue.
4 //
5 // Author:
6 //      Chris Toshok  <toshok@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.IO;
34 using System.Configuration;
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Runtime.Serialization.Formatters.Binary;
38 using NUnit.Framework;
39
40 namespace MonoTests.System.Configuration {
41
42         [TestFixture]
43         public class SettingsPropertyValueTest {
44
45                 [Test]
46                 public void Properties ()
47                 {
48                         SettingsProperty p = new SettingsProperty ("property",
49                                                                    typeof (int),
50                                                                    null,
51                                                                    true,
52                                                                    10,
53                                                                    SettingsSerializeAs.String,
54                                                                    null,
55                                                                    true,
56                                                                    false);
57
58                         SettingsPropertyValue v = new SettingsPropertyValue (p);
59
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");
67
68                         /* test that setting v.PropertyValue to
69                          * something else causes SerializedValue to
70                          * become not-null */
71                         v.PropertyValue = (object)5;
72                         Assert.AreEqual ("5", v.SerializedValue, "A9");
73
74                         /* test to see whether or not changing
75                          * SerializeAs causes SerializedValue to
76                          * change */
77                         p.SerializeAs = SettingsSerializeAs.Xml;
78                         Assert.AreEqual ("5", v.SerializedValue, "A11"); /* nope.. */
79
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");
83                 }
84
85                 [Test]
86                 public void Dirty ()
87                 {
88                         SettingsProperty p = new SettingsProperty ("property",
89                                                                    typeof (int),
90                                                                    null,
91                                                                    true,
92                                                                    10,
93                                                                    SettingsSerializeAs.String,
94                                                                    null,
95                                                                    true,
96                                                                    false);
97
98                         SettingsPropertyValue v = new SettingsPropertyValue (p);
99
100                         Assert.AreEqual (10, v.PropertyValue, "A0");
101                         Assert.IsFalse (v.IsDirty, "A1");
102
103                         /* set PropertyValue to something else */
104                         v.PropertyValue = 5;
105                         Assert.IsTrue (v.IsDirty, "A2");
106                         v.IsDirty = false;
107
108                         /* set PropertyValue to the same thing */
109                         v.PropertyValue = 5;
110                         Assert.IsTrue (v.IsDirty, "A3");
111
112                         /* try out a non-value type */
113                         p = new SettingsProperty ("property",
114                                                   typeof (StringWriter),
115                                                   null,
116                                                   true,
117                                                   "",
118                                                   SettingsSerializeAs.String,
119                                                   null,
120                                                   true,
121                                                   false);
122                         v = new SettingsPropertyValue (p);
123
124                         Assert.IsNotNull (v.PropertyValue, "A5");
125
126                         Console.WriteLine (v.PropertyValue);
127                         Assert.IsTrue (v.IsDirty, "A6");
128                 }
129
130                 [Test]
131                 public void UsingDefaultValue ()
132                 {
133                         SettingsProperty p = new SettingsProperty ("property",
134                                                                    typeof (int),
135                                                                    null,
136                                                                    true,
137                                                                    10,
138                                                                    SettingsSerializeAs.String,
139                                                                    null,
140                                                                    true,
141                                                                    false);
142
143                         SettingsPropertyValue v = new SettingsPropertyValue (p);
144
145                         Assert.AreEqual (10, v.PropertyValue, "A1");
146                         Assert.IsTrue (v.UsingDefaultValue, "A2");
147
148                         /* set PropertyValue to something else */
149                         v.PropertyValue = 5;
150                         Assert.IsFalse (v.UsingDefaultValue, "A3");
151
152                         /* set PropertyValue back to the default */
153                         v.PropertyValue = 10;
154                         Assert.IsFalse (v.UsingDefaultValue, "A4");
155                 }
156
157                 [Test]
158                 public void String_Deserialize ()
159                 {
160                         SettingsProperty p = new SettingsProperty ("property",
161                                                                    typeof (int),
162                                                                    null,
163                                                                    true,
164                                                                    10,
165                                                                    SettingsSerializeAs.String,
166                                                                    null,
167                                                                    true,
168                                                                    false);
169
170                         SettingsPropertyValue v = new SettingsPropertyValue (p);
171                         v.SerializedValue = "123";
172
173                         Assert.AreEqual (123, v.PropertyValue, "A1");
174                         Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
175                         Assert.AreEqual (false, v.UsingDefaultValue, "A3");
176                 }
177
178                 [Test]
179                 public void Xml_Deserialize ()
180                 {
181                         SettingsProperty p = new SettingsProperty ("property",
182                                                                    typeof (int),
183                                                                    null,
184                                                                    true,
185                                                                    10,
186                                                                    SettingsSerializeAs.Xml,
187                                                                    null,
188                                                                    true,
189                                                                    false);
190
191                         SettingsPropertyValue v = new SettingsPropertyValue (p);
192                         v.SerializedValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>123</int>";
193
194                         Assert.AreEqual (123, v.PropertyValue, "A1");
195                         Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
196                         Assert.AreEqual (false, v.UsingDefaultValue, "A3");
197                 }
198
199                 [Test]
200                 public void String_Xml_Serialize ()
201                 {
202                         SettingsProperty p = new SettingsProperty ("property",
203                                                                    typeof (int),
204                                                                    null,
205                                                                    true,
206                                                                    10,
207                                                                    SettingsSerializeAs.String,
208                                                                    null,
209                                                                    true,
210                                                                    false);
211
212                         SettingsPropertyValue v = new SettingsPropertyValue (p);
213
214                         v.PropertyValue = 10;
215                         Assert.AreEqual (10, v.PropertyValue, "A1");
216                         Assert.AreEqual ("10", v.SerializedValue, "A2");
217
218                         v.PropertyValue = 10;
219                         p.SerializeAs = SettingsSerializeAs.Xml;
220                         
221                         Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>10</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A3");
222
223                 }
224
225                 [Test]
226                 public void Binary_Serialize ()
227                 {
228                         SettingsProperty p = new SettingsProperty ("property",
229                                                                    typeof (int),
230                                                                    null,
231                                                                    true,
232                                                                    10,
233                                                                    SettingsSerializeAs.Binary,
234                                                                    null,
235                                                                    true,
236                                                                    false);
237
238                         SettingsPropertyValue v = new SettingsPropertyValue (p);
239                         byte[] foo;
240
241                         v.PropertyValue = 10;
242
243                         Assert.AreEqual (typeof (byte[]), v.SerializedValue.GetType(), "A1");
244                         foo = (byte[])v.SerializedValue;
245
246                         v.PropertyValue = 5;
247                         Assert.AreEqual (5, v.PropertyValue, "A2");
248
249
250                         BinaryFormatter bf = new BinaryFormatter ();
251                         MemoryStream ms = new MemoryStream (foo);
252                         Assert.AreEqual (10, bf.Deserialize (ms), "A3");
253
254                         v.Deserialized = false;
255                         v.SerializedValue = foo;
256
257                         Assert.AreEqual (10, v.PropertyValue, "A4");
258                 }
259
260                 [Test]
261                 public void DefaultValueType ()
262                 {
263                         SettingsProperty p1 = new SettingsProperty ("property",
264                                                                    typeof (int),
265                                                                    null,
266                                                                    true,
267                                                                    (int) 10,
268                                                                    SettingsSerializeAs.String,
269                                                                    null,
270                                                                    true,
271                                                                    false);
272                         SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
273                         Assert.AreEqual (typeof (int), v1.PropertyValue.GetType (), "A1");
274                         Assert.AreEqual (10, v1.PropertyValue, "A2");
275
276                         SettingsProperty p2 = new SettingsProperty ("property",
277                                            typeof (int),
278                                            null,
279                                            true,
280                                            "10",
281                                            SettingsSerializeAs.String,
282                                            null,
283                                            true,
284                                            false);
285                         SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
286                         Assert.AreEqual (typeof (int), v2.PropertyValue.GetType (), "A3");
287                         Assert.AreEqual (10, v2.PropertyValue, "A4");
288                 }
289
290                 [Serializable]
291                 public class MyData
292                 {
293                         private int intProp = 777;
294                         public int IntProp
295                         {
296                                 get { return intProp; }
297                                 set { intProp = value; }
298                         }
299                 }
300
301                 [Test]
302                 public void DefaultValueCompexTypeEmpty ()
303                 {
304                         SettingsProperty p1 = new SettingsProperty ("property",
305                                                                    typeof (MyData),
306                                                                    null,
307                                                                    true,
308                                                                    "",
309                                                                    SettingsSerializeAs.String,
310                                                                    null,
311                                                                    true,
312                                                                    false);
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");
318                 }
319
320                 [Test]
321                 public void DefaultValueCompexType ()
322                 {
323                         SettingsProperty p2 = new SettingsProperty ("property",
324                                                                    typeof (MyData),
325                                                                    null,
326                                                                    true,
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,
329                                                                    null,
330                                                                    true,
331                                                                    false);
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");
337                 }
338
339                 [Test]
340                 public void IsDirtyAndValueDateTime ()
341                 {
342                         SettingsProperty sp = new SettingsProperty ("heh");
343                         sp.PropertyType = typeof (DateTime);
344
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");
350                 }
351
352                 [Test]
353                 public void IsDirtyAndValuePrimitive ()
354                 {
355                         SettingsProperty sp = new SettingsProperty ("heh");
356                         sp.PropertyType = typeof (int);
357
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");
363                 }
364
365                 [Test]
366                 public void IsDirtyAndValueDecimal ()
367                 {
368                         SettingsProperty sp = new SettingsProperty ("heh");
369                         sp.PropertyType = typeof (decimal);
370
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");
376                 }
377
378                 [Test]
379                 public void IsDirtyAndValueString ()
380                 {
381                         SettingsProperty sp = new SettingsProperty ("heh");
382                         sp.PropertyType = typeof (string);
383
384                         SettingsPropertyValue spv = new SettingsPropertyValue (sp);
385                         Assert.IsFalse (spv.IsDirty, "A1");
386                         Assert.IsNull (spv.PropertyValue, "A2");
387                         Assert.IsFalse (spv.IsDirty, "A3");
388
389                         SettingsProperty sp2 = new SettingsProperty ("heh");
390                         sp2.PropertyType = typeof (string);
391                         sp2.DefaultValue = "";
392
393                         SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
394                         Assert.IsFalse (spv2.IsDirty, "A4");
395                         Assert.IsNotNull (spv2.PropertyValue, "A5");
396                         Assert.IsFalse (spv2.IsDirty, "A6");
397                 }
398
399                 [Serializable]
400                 public struct MyData2
401                 {
402                         public int intProp;
403                 }
404
405                 [Test]
406                 public void IsDirtyAndValueMyData2 ()
407                 {
408                         SettingsProperty sp = new SettingsProperty ("heh");
409                         sp.PropertyType = typeof (MyData2);
410
411                         SettingsPropertyValue spv = new SettingsPropertyValue (sp);
412                         Assert.IsFalse (spv.IsDirty, "A1");
413                         Assert.IsNotNull (spv.PropertyValue, "A2");
414                         Assert.IsTrue (spv.IsDirty, "A3");
415                 }
416
417                 [Test]
418                 public void IsDirtyAndValueArrayList ()
419                 {
420                         SettingsProperty sp = new SettingsProperty ("heh");
421                         sp.PropertyType = typeof (ArrayList);
422
423                         SettingsPropertyValue spv = new SettingsPropertyValue (sp);
424                         Assert.IsFalse (spv.IsDirty, "A1");
425                         Assert.IsNull (spv.PropertyValue, "A2");
426                         Assert.IsFalse (spv.IsDirty, "A3");
427
428                         SettingsProperty sp2 = new SettingsProperty ("heh");
429                         sp2.PropertyType = typeof (ArrayList);
430                         sp2.DefaultValue = "";
431
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");
437                 }
438         }
439 }
440
441 #endif