[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System / Test / System.Configuration / SettingsBaseTest.cs
1 //
2 // SettingsBaseTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 //#define SPEW
30
31 #if NET_2_0
32
33 using System;
34 using System.Text;
35 using System.Configuration;
36 using System.ComponentModel;
37 using System.Collections;
38 using System.Collections.Specialized;
39 using NUnit.Framework;
40 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
41
42 namespace MonoTests.System.Configuration
43 {
44         [TestFixture]
45         public class SettingsBaseTest
46         {
47                 class MySettings : SettingsBase
48                 {
49                         [UserScopedSetting] // ignored in non-ApplicationSettingsBase
50                         public int Foo {
51                                 get { return (int) this ["Foo"]; }
52                                 set { this ["Foo"] = value; }
53                         }
54
55                         [UserScopedSetting] // ignored in non-ApplicationSettingsBase
56                         [DefaultSettingValue ("20")]
57                         public int Bar {
58                                 get { return (int) this ["Bar"]; }
59                                 set { this ["Bar"] = value; }
60                         }
61                 }
62
63                 class MySettings2 : SettingsBase
64                 {
65                         int foo;
66
67                         [UserScopedSetting] // ignored in non-ApplicationSettingsBase
68                         public int Foo {
69                                 get { return (int) this ["Foo"]; }
70                                 set { this ["Foo"] = value; }
71                         }
72
73                         public override SettingsPropertyCollection Properties {
74                                 get { return null; }
75                         }
76
77                         public SettingsPropertyCollection BaseProperties {
78                                 get { return base.Properties; }
79                         }
80                 }
81
82                 [Test]
83                 public void PropertyDefaults ()
84                 {
85                         MySettings s = new MySettings ();
86                         Assert.IsNull (s.Properties, "#1");
87                         Assert.IsNull (s.Providers, "#2");
88                         Assert.IsNull (s.Context, "#3");
89                         Assert.AreEqual (0, s.PropertyValues.Count, "#4");
90                         Assert.IsNull (s.Properties, "#5");
91                         Assert.IsNull (s.Providers, "#6");
92                         Assert.IsNull (s.Context, "#7");
93                         s.Initialize (s.Context, s.Properties, s.Providers);
94                 }
95
96                 [Test]
97                 public void PropertiesOverriden ()
98                 {
99                         MySettings2 s = new MySettings2 ();
100                         s.Initialize (s.Context, new SettingsPropertyCollection (), s.Providers);
101                         Assert.IsNull (s.Properties, "#1");
102                         Assert.IsNotNull (s.BaseProperties, "#2");
103                         Assert.AreEqual (0, s.PropertyValues.Count, "#3");
104                 }
105
106                 [Test]
107                 public void PropertyValuesInstance ()
108                 {
109                         SettingsPropertyCollection props = new SettingsPropertyCollection ();
110                         SettingsProviderCollection provs = new SettingsProviderCollection ();
111
112                         MyProvider p = new MyProvider ();
113                         MySettings s = new MySettings ();
114
115                         props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
116                         provs.Add (p);
117
118                         s.Initialize (new SettingsContext (), props, provs);
119                         Assert.AreEqual (s.PropertyValues, s.PropertyValues);
120                 }
121
122                 [Test]
123                 public void PropertyValuesUninitialized ()
124                 {
125                         MySettings s = new MySettings ();
126                         s.Initialize (new SettingsContext (), new SettingsPropertyCollection (), new SettingsProviderCollection ());
127                         s.Properties.Add (new SettingsProperty ("Foo"));
128                         // values are filled only at initialization phase.
129                         Assert.AreEqual (0, s.PropertyValues.Count, "#1");
130                 }
131
132                 [Test]
133                 public void PropertyValuesInitialized ()
134                 {
135                         SettingsPropertyCollection props = new SettingsPropertyCollection ();
136                         SettingsProviderCollection provs = new SettingsProviderCollection ();
137
138                         MyProvider p = new MyProvider ();
139                         MySettings s = new MySettings ();
140                         int i;
141
142                         try {
143                                 i = s.Foo;
144                                 Assert.Fail ("#1-2");
145                         } catch (SettingsPropertyNotFoundException) {
146                         }
147
148                         s.Initialize (new SettingsContext (), props, provs);
149                         Assert.AreEqual (0, s.PropertyValues.Count, "#2-1");
150                         Assert.AreEqual (0, s.Context.Count, "#2-2");
151
152                         props.Add (new SettingsProperty ("Foo", typeof (int), p, false, 10, SettingsSerializeAs.String, null, true, true));
153                         // initialize w/o the provider
154                         s.Initialize (new SettingsContext (), props, provs);
155                         Assert.AreEqual (0, s.PropertyValues.Count, "#3-0");
156                         Assert.AreEqual (100, s.Foo, "#3-1");
157                         // ... !!!
158                         Assert.AreEqual (1, s.PropertyValues.Count, "#3-2");
159                         SettingsPropertyValue v = s.PropertyValues ["Foo"];
160                         Assert.AreEqual (100, v.PropertyValue, "#3-3");
161                         Assert.AreEqual (0, s.Context.Count, "#3-4");
162
163                         // initialize w/ the provider
164                         provs.Add (p);
165                         provs.Add (new MyProvider2 ("Bar", 25));
166                         props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider2"], false, 10, SettingsSerializeAs.String, null, true, true));
167                         s.Initialize (new SettingsContext (), props, provs);
168                         Assert.AreEqual (1, s.PropertyValues.Count, "#4-1");
169                         Assert.AreEqual (100, s.Foo, "#4-2");
170                         Assert.AreEqual (25, s.Bar, "#4-3");
171                         // ... !!!
172                         Assert.AreEqual (2, s.PropertyValues.Count, "#4-3-2");
173                         Assert.AreEqual (0, s.Context.Count, "#4-4");
174
175                         // wrong provider
176                         props.Remove ("Bar");
177                         props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider"], false, 10, SettingsSerializeAs.String, null, true, true));
178                         s = new MySettings ();
179                         s.Initialize (new SettingsContext (), props, provs);
180                         Assert.AreEqual (0, s.PropertyValues.Count, "#5-1");
181                         Assert.AreEqual (100, s.Foo, "#5-2");
182                         Assert.AreEqual (10, s.Bar, "#5-3");
183                 }
184
185                 [Test]
186                 public void AddPropertyTypeMismatch ()
187                 {
188                         SettingsPropertyCollection props = new SettingsPropertyCollection ();
189                         SettingsProviderCollection provs = new SettingsProviderCollection ();
190
191                         MyProvider p = new MyProvider ();
192                         MySettings s = new MySettings ();
193
194                         props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
195                         provs.Add (p);
196
197                         s.Initialize (new SettingsContext (), props, provs);
198                         int i = s.Foo; // it still works as int, regardless of the settings property type...
199                 }
200
201                 [Test]
202                 [Ignore (".NET throws NRE, which means that it is not well designed.")]
203                 public void AddPropertyNoProviderButInProviders ()
204                 {
205                         SettingsPropertyCollection props = new SettingsPropertyCollection ();
206                         SettingsProviderCollection provs = new SettingsProviderCollection ();
207
208                         MyProvider p = new MyProvider ();
209                         MySettings s = new MySettings ();
210
211                         props.Add (new SettingsProperty ("Foo", typeof (string), null, false, 10, SettingsSerializeAs.String, null, true, true));
212                         provs.Add (p);
213
214                         s.Initialize (new SettingsContext (), props, provs);
215                         Assert.AreEqual (100, s.Foo);
216                 }
217
218                 [Test]
219                 public void ExceptionalGetPropertyValues ()
220                 {
221                         SettingsPropertyCollection props = new SettingsPropertyCollection ();
222                         SettingsProviderCollection provs = new SettingsProviderCollection ();
223
224                         MyProvider3 p = new MyProvider3 ();
225                         MySettings s = new MySettings ();
226
227                         props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
228                         provs.Add (p);
229
230                         s.Initialize (new SettingsContext (), props, provs);
231                         Assert.AreEqual (0, s.Context.Count, "#0");
232                         try {
233                                 Assert.AreEqual (100, s.Foo, "#1");
234                                 Assert.Fail ("#2");
235                         } catch (Win32Exception) {
236                         }
237                 }
238
239                 [Test]
240                 [ExpectedException (typeof (ArgumentException))]
241                 public void ProviderCollectionAddNameless ()
242                 {
243                         new SettingsProviderCollection ().Add (
244                                 new MyProvider (true));
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (ArgumentException))]
249                 public void ProviderCollectionAddDuplicate ()
250                 {
251                         SettingsProviderCollection c = new SettingsProviderCollection ();
252                         c.Add (new MyProvider ());
253                         c.Add (new MyProvider ());
254                 }
255
256                 class MyProvider3 : MyProvider
257                 {
258                         public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
259                         {
260                                 throw new Win32Exception (); // unlikely thrown otherwise.
261                         }
262                 }
263
264                 class MyProvider2 : MyProvider
265                 {
266                         public MyProvider2 (string item, object value)
267                                 : base (item, value)
268                         {
269                         }
270
271                         public override string Name {
272                                 get { return "MyProvider2"; }
273                         }
274                 }
275
276                 class MyProvider : SettingsProvider
277                 {
278                         bool bogus;
279                         string item;
280                         object default_value;
281
282                         public MyProvider ()
283                                 : this (false)
284                         {
285                         }
286
287                         public MyProvider (bool bogus)
288                         {
289                                 this.item = "Foo";
290                                 default_value = 100;
291                                 this.bogus = bogus;
292                         }
293
294                         public MyProvider (string item, object value)
295                         {
296                                 this.item = item;
297                                 this.default_value = value;
298                         }
299
300                         public override string Name {
301                                 get { return bogus ? null : "MyProvider"; }
302                         }
303
304                         string app;
305                         public override string ApplicationName {
306                                 get { return app; }
307                                 set { app = value; }
308                         }
309
310                         public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
311                         {
312                                 SettingsPropertyValueCollection vals =
313                                         new SettingsPropertyValueCollection ();
314                                 foreach (SettingsProperty p in props)
315                                         if (p.Provider == this) {
316                                                 SettingsPropertyValue pv = new SettingsPropertyValue (p);
317                                                 if (pv.Name == item)
318                                                         pv.PropertyValue = default_value;
319                                                 vals.Add (pv);
320                                         }
321                                 return vals;
322                         }
323
324                         public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
325                         {
326                                 throw new Exception ();
327                         }
328                 }
329         }
330 }
331
332 #endif