2006-05-17 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System / Test / System.Configuration / ApplicationSettingsBaseTest.cs
1 //
2 // System.Configuration.ApplicationsSettingsBaseTest.cs - Unit tests
3 // for System.Configuration.ApplicationSettingsBase.
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 #define SPEW
31
32 #if NET_2_0
33
34 using System;
35 using System.Text;
36 using System.Configuration;
37 using System.ComponentModel;
38 using System.Collections;
39 using System.Collections.Specialized;
40 using NUnit.Framework;
41 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
42
43 namespace MonoTests.System.Configuration {
44         class ProviderPoker : LocalFileSettingsProvider {
45                 public override void Initialize (string name,
46                                                  NameValueCollection values)
47                 {
48 #if SPEW
49                         Console.WriteLine ("Initialize '{0}'", name);
50                         Console.WriteLine (Environment.StackTrace);
51 #endif
52                         if (name == null)
53                                 name = "ProviderPoker";
54
55                         base.Initialize (name, values);
56                 }
57
58                 public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context,
59                                                                                    SettingsPropertyCollection properties)
60                 {
61 #if SPEW
62                         Console.WriteLine (Environment.StackTrace);
63 #endif
64                         return base.GetPropertyValues (context, properties);
65                 }
66
67                 public override void SetPropertyValues (SettingsContext context,
68                                                         SettingsPropertyValueCollection values)
69                 {
70 #if SPEW
71                         Console.WriteLine (Environment.StackTrace);
72 #endif
73                         base.SetPropertyValues (context, values);
74                 }
75
76                 public override string ApplicationName {
77                         get {
78 #if SPEW
79                                 Console.WriteLine (Environment.StackTrace);
80 #endif
81                                 return base.ApplicationName;
82                         }
83                         set {
84 #if SPEW
85                                 Console.WriteLine ("ApplicationName = {0}", value);
86                                 Console.WriteLine (Environment.StackTrace);
87 #endif
88                                 base.ApplicationName = value;
89                         }
90                 }
91         }
92
93         /* a basic settings class.  just two settings, one application
94          * scoped, one user scoped */
95         class TestSettings1 : ApplicationSettingsBase
96         {
97                 public TestSettings1() : base ("TestSettings1")
98                 {
99                 }
100
101                 [ApplicationScopedSetting]
102                 [DefaultSettingValue ("root")]
103                 public string Username {
104                         get { return (string)this["Username"]; }
105                         set { this["Username"] = value; }
106                 }
107
108                 [UserScopedSetting]
109                 [DefaultSettingValue ("8 Cambridge Center")]
110                 public string Address {
111                         get { return (string)this["Address"]; }
112                         set { this["Address"] = value; }
113                 }
114         }
115
116         /* an error according to msdn2 docs.  both ApplicationScoped
117          * and UserScoped attributes on the same property */
118         class TestSettings2 : ApplicationSettingsBase
119         {
120                 public TestSettings2() : base ("TestSettings2")
121                 {
122                 }
123
124                 [ApplicationScopedSetting]
125                 [UserScopedSetting]
126                 [SettingsProvider (typeof (ProviderPoker))]
127                 public string Username {
128                         get { return (string)this["Username"]; }
129                         set { this["Username"] = value; }
130                 }
131         }
132
133         /* a custom provider for our setting */
134         class TestSettings3 : ApplicationSettingsBase
135         {
136                 public TestSettings3() : base ("TestSettings3")
137                 {
138                 }
139
140                 [ApplicationScopedSetting]
141                 [SettingsProvider (typeof (ProviderPoker))]
142                 public string Username {
143                         get { return (string)this["Username"]; }
144                         set { this["Username"] = value; }
145                 }
146         }
147
148         [TestFixture]
149         public class ApplicationSettingsBaseTest
150         {
151                 [Test]
152                 public void TestSettings1_Properties ()
153                 {
154                         TestSettings1 settings = new TestSettings1 ();
155
156                         IEnumerator props = settings.Properties.GetEnumerator();
157                         Assert.IsNotNull (props, "A1");
158                         
159                         Assert.IsTrue (props.MoveNext(), "A2");
160                         Assert.AreEqual ("Address", ((SettingsProperty)props.Current).Name, "A3");
161
162                         Assert.IsTrue (props.MoveNext(), "A4");
163                         Assert.AreEqual ("Username", ((SettingsProperty)props.Current).Name, "A5");
164
165                         Assert.AreEqual ("root", settings.Username, "A6");
166                         Assert.AreEqual ("8 Cambridge Center", settings.Address, "A7");
167                 }
168
169                 [Test]
170                 public void TestSettings1_Provider ()
171                 {
172                         TestSettings1 settings = new TestSettings1 ();
173
174                         /* since we didn't specify a provider for any
175                          * of them, they should all use the
176                          * LocalFileSettingsProvider */
177                         foreach (SettingsProperty prop in settings.Properties) {
178                                 Assert.AreEqual (typeof (LocalFileSettingsProvider), prop.Provider.GetType(), "A1");
179                                 Console.WriteLine ("'{0}': '{1}'", prop.Provider.Name, prop.Provider.ApplicationName);
180                         }
181                 }
182
183                 [Test]
184                 public void TestSettings1_SetProperty ()
185                 {
186                         TestSettings1 settings = new TestSettings1 ();
187                         bool setting_changing = false;
188                         bool setting_changed = false;
189
190                         settings.SettingChanging += delegate (object sender, SettingChangingEventArgs e) {
191                                 setting_changing = true;
192                                 Assert.AreEqual ("Username", e.SettingName, "A1");
193                                 Assert.AreEqual ("toshok", e.NewValue, "A2");
194                                 Assert.AreEqual ("TestSettings1", e.SettingKey, "A3");
195                                 Assert.AreEqual (settings.GetType().FullName, e.SettingClass, "A4");
196                         };
197
198                         settings.PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
199                                 Assert.IsTrue (setting_changing, "A5");
200                                 setting_changed = true;
201                                 Assert.AreEqual ("Username", e.PropertyName, "A6");
202                         };
203
204                         settings.Username = "toshok";
205
206                         Assert.IsTrue (setting_changing && setting_changed, "A7");
207                         Assert.AreEqual ("toshok", settings.Username, "A8");
208                 }
209
210                 [Test]
211                 public void TestSettings1_SetPropertyCancel ()
212                 {
213                         TestSettings1 settings = new TestSettings1 ();
214                         bool setting_changing = false;
215                         bool setting_changed = false;
216
217                         settings.SettingChanging += delegate (object sender, SettingChangingEventArgs e) {
218                                 setting_changing = true;
219                                 Assert.AreEqual ("Username", e.SettingName, "A1");
220                                 Assert.AreEqual ("toshok", e.NewValue, "A2");
221                                 Assert.AreEqual ("TestSettings1", e.SettingKey, "A3");
222                                 Assert.AreEqual (settings.GetType().FullName, e.SettingClass, "A4");
223                                 e.Cancel = true;
224                         };
225
226                         settings.PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
227                                 setting_changed = true;
228                                 Assert.Fail ("shouldn't reach here.", "A5");
229                         };
230
231                         settings.Username = "toshok";
232
233                         Assert.IsTrue (setting_changing, "A6");
234                         Assert.IsFalse (setting_changed, "A7");
235
236                         Assert.AreEqual ("root", settings.Username, "A8");
237                 }
238
239                 [Test]
240                 public void TestSettings1_SettingsLoaded ()
241                 {
242                         TestSettings1 settings = new TestSettings1 ();
243                         bool settings_loaded = false;
244                         SettingsProvider loaded_provider = null;
245
246                         settings.SettingsLoaded += delegate (object sender, SettingsLoadedEventArgs e) {
247                                 settings_loaded = true;
248                                 loaded_provider = e.Provider;
249                         };
250
251                         Assert.AreEqual ("root", settings.Username, "A1");
252                         Assert.IsTrue (settings_loaded, "A2");
253                         Assert.AreEqual (loaded_provider, settings.Properties ["Username"].Provider, "A3");
254                 }
255
256                 [Test]
257                 [Category ("NotWorking")]
258                 public void TestSettings1_SetPropertyReset ()
259                 {
260                         TestSettings1 settings = new TestSettings1 ();
261
262                         settings.Username = "toshok";
263
264                         Assert.AreEqual ("toshok", settings.Username, "A1");
265
266                         settings.Reset ();
267
268                         Assert.AreEqual ("root", settings.Username, "A2");
269                 }
270
271                 [Test]
272                 [Category ("NotWorking")]
273                 public void TestSettings2_Properties ()
274                 {
275                         TestSettings2 settings = new TestSettings2 ();
276
277                         /* should throw ConfigurationException */
278                         IEnumerator props = settings.Properties.GetEnumerator();
279                 }
280
281                 [Test]
282                 [Ignore ("On MS.NET it returns null ...")]
283                 public void TestSettings3_Properties ()
284                 {
285                         TestSettings3 settings = new TestSettings3 ();
286
287                         Assert.AreEqual ("root", settings.Username, "A1");
288                 }
289
290                 public static void Main (string[] args)
291                 {
292                         ApplicationSettingsBaseTest test = new ApplicationSettingsBaseTest();
293                         test.TestSettings1_Properties();
294                 }
295
296                 [Test]
297                 public void Synchronized ()
298                 {
299                         Bug78430 s = new Bug78430 ();
300                         s.Initialize (null, new SettingsPropertyCollection (),
301                                 new SettingsProviderCollection ());
302                         SettingsBase sb = SettingsBase.Synchronized (s);
303                         Assert.IsTrue (sb.IsSynchronized, "#1");
304                         Assert.IsTrue (sb is Bug78430, "#2");
305                         // these checks are so cosmetic, actually not
306                         // worthy of testing.
307                         Assert.IsTrue (Object.ReferenceEquals (s, sb), "#3");
308                         Assert.IsFalse (sb.Properties.IsSynchronized, "#4");
309                 }
310
311                 class Bug78430 : SettingsBase
312                 {
313                 }
314         }
315
316 }
317
318 #endif