2 // System.Configuration.ApplicationsSettingsBaseTest.cs - Unit tests
3 // for System.Configuration.ApplicationSettingsBase.
6 // Chris Toshok <toshok@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
34 using System.Configuration;
35 using System.Collections;
36 using System.Collections.Specialized;
37 using NUnit.Framework;
39 namespace MonoTests.System.Configuration {
41 /* a basic settings class. just two settings, one application
42 * scoped, one user scoped */
43 class TestSettings1 : ApplicationSettingsBase
45 public TestSettings1()
46 : base ("TestSettings1")
50 [ApplicationScopedSetting]
51 [DefaultSettingValue ("root")]
52 public string Username {
54 return (string)this["Username"];
57 this["Username"] = value;
62 [DefaultSettingValue ("8 Cambridge Center")]
63 public string Address {
65 return (string)this["Address"];
68 this["Address"] = value;
73 /* an error. both ApplicationScoped and UserScoped attributes on the same property */
74 class TestSettings2 : ApplicationSettingsBase
76 public TestSettings2()
77 : base ("TestSettings2")
81 [ApplicationScopedSetting]
83 public string Username {
85 return (string)this["Username"];
88 this["Username"] = value;
95 public class ApplicationSettingsBaseTest
98 public void TestSettings1_Properties ()
100 TestSettings1 settings = new TestSettings1 ();
102 IEnumerator props = settings.Properties.GetEnumerator();
103 Assert.IsNotNull (props, "A1");
105 Assert.IsTrue (props.MoveNext(), "A2");
106 Assert.AreEqual ("Address", ((SettingsProperty)props.Current).Name, "A3");
108 Assert.IsTrue (props.MoveNext(), "A4");
109 Assert.AreEqual ("Username", ((SettingsProperty)props.Current).Name, "A5");
111 Assert.AreEqual ("root", settings.Username, "A6");
112 Assert.AreEqual ("8 Cambridge Center", settings.Address, "A7");
116 public void TestSettings1_Provider ()
118 TestSettings1 settings = new TestSettings1 ();
120 /* since we didn't specify a provider for any
121 * of them, they should all use the
122 * LocalFileSettingsProvider */
123 foreach (SettingsProperty prop in settings.Properties) {
124 Assert.AreEqual (typeof (LocalFileSettingsProvider), prop.Provider.GetType(), "A1");
125 Console.WriteLine ("'{0}'", prop.Provider.ApplicationName);
131 public void TestSettings2_Properties ()
133 TestSettings2 settings = new TestSettings2 ();
135 /* should throw ConfigurationException */
136 IEnumerator props = settings.Properties.GetEnumerator();
139 public static int Main (string[] args)
141 ApplicationSettingsBaseTest test = new ApplicationSettingsBaseTest();
142 test.TestSettings1_Properties();