5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
42 namespace MonoTests.System.Configuration
45 public class SettingsBaseTest
47 class MySettings : SettingsBase
49 [UserScopedSetting] // ignored in non-ApplicationSettingsBase
51 get { return (int) this ["Foo"]; }
52 set { this ["Foo"] = value; }
55 [UserScopedSetting] // ignored in non-ApplicationSettingsBase
56 [DefaultSettingValue ("20")]
58 get { return (int) this ["Bar"]; }
59 set { this ["Bar"] = value; }
63 class MySettings2 : SettingsBase
67 [UserScopedSetting] // ignored in non-ApplicationSettingsBase
69 get { return (int) this ["Foo"]; }
70 set { this ["Foo"] = value; }
73 public override SettingsPropertyCollection Properties {
77 public SettingsPropertyCollection BaseProperties {
78 get { return base.Properties; }
83 public void PropertyDefaults ()
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);
97 public void PropertiesOverriden ()
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");
107 public void PropertyValuesInstance ()
109 SettingsPropertyCollection props = new SettingsPropertyCollection ();
110 SettingsProviderCollection provs = new SettingsProviderCollection ();
112 MyProvider p = new MyProvider ();
113 MySettings s = new MySettings ();
115 props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
118 s.Initialize (new SettingsContext (), props, provs);
119 Assert.AreEqual (s.PropertyValues, s.PropertyValues);
123 public void PropertyValuesUninitialized ()
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");
133 public void PropertyValuesInitialized ()
135 SettingsPropertyCollection props = new SettingsPropertyCollection ();
136 SettingsProviderCollection provs = new SettingsProviderCollection ();
138 MyProvider p = new MyProvider ();
139 MySettings s = new MySettings ();
144 Assert.Fail ("#1-2");
145 } catch (SettingsPropertyNotFoundException) {
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");
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");
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");
163 // initialize w/ the provider
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");
172 Assert.AreEqual (2, s.PropertyValues.Count, "#4-3-2");
173 Assert.AreEqual (0, s.Context.Count, "#4-4");
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");
186 public void AddPropertyTypeMismatch ()
188 SettingsPropertyCollection props = new SettingsPropertyCollection ();
189 SettingsProviderCollection provs = new SettingsProviderCollection ();
191 MyProvider p = new MyProvider ();
192 MySettings s = new MySettings ();
194 props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
197 s.Initialize (new SettingsContext (), props, provs);
198 int i = s.Foo; // it still works as int, regardless of the settings property type...
202 [Ignore (".NET throws NRE, which means that it is not well designed.")]
203 public void AddPropertyNoProviderButInProviders ()
205 SettingsPropertyCollection props = new SettingsPropertyCollection ();
206 SettingsProviderCollection provs = new SettingsProviderCollection ();
208 MyProvider p = new MyProvider ();
209 MySettings s = new MySettings ();
211 props.Add (new SettingsProperty ("Foo", typeof (string), null, false, 10, SettingsSerializeAs.String, null, true, true));
214 s.Initialize (new SettingsContext (), props, provs);
215 Assert.AreEqual (100, s.Foo);
219 public void ExceptionalGetPropertyValues ()
221 SettingsPropertyCollection props = new SettingsPropertyCollection ();
222 SettingsProviderCollection provs = new SettingsProviderCollection ();
224 MyProvider3 p = new MyProvider3 ();
225 MySettings s = new MySettings ();
227 props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
230 s.Initialize (new SettingsContext (), props, provs);
231 Assert.AreEqual (0, s.Context.Count, "#0");
233 Assert.AreEqual (100, s.Foo, "#1");
235 } catch (Win32Exception) {
240 [ExpectedException (typeof (ArgumentException))]
241 public void ProviderCollectionAddNameless ()
243 new SettingsProviderCollection ().Add (
244 new MyProvider (true));
248 [ExpectedException (typeof (ArgumentException))]
249 public void ProviderCollectionAddDuplicate ()
251 SettingsProviderCollection c = new SettingsProviderCollection ();
252 c.Add (new MyProvider ());
253 c.Add (new MyProvider ());
256 class MyProvider3 : MyProvider
258 public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
260 throw new Win32Exception (); // unlikely thrown otherwise.
264 class MyProvider2 : MyProvider
266 public MyProvider2 (string item, object value)
271 public override string Name {
272 get { return "MyProvider2"; }
276 class MyProvider : SettingsProvider
280 object default_value;
287 public MyProvider (bool bogus)
294 public MyProvider (string item, object value)
297 this.default_value = value;
300 public override string Name {
301 get { return bogus ? null : "MyProvider"; }
305 public override string ApplicationName {
310 public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
312 SettingsPropertyValueCollection vals =
313 new SettingsPropertyValueCollection ();
314 foreach (SettingsProperty p in props)
315 if (p.Provider == this) {
316 SettingsPropertyValue pv = new SettingsPropertyValue (p);
318 pv.PropertyValue = default_value;
324 public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
326 throw new Exception ();