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