2005-09-22 Chris Toshok <toshok@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 #if NET_2_0
31
32 using System;
33 using System.Text;
34 using System.Configuration;
35 using System.Collections;
36 using System.Collections.Specialized;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Configuration {
40
41         /* a basic settings class.  just two settings, one application
42          * scoped, one user scoped */
43         class TestSettings1 : ApplicationSettingsBase
44         {
45                 public TestSettings1() 
46                         : base ("TestSettings1")
47                 {
48                 }
49
50                 [ApplicationScopedSetting]
51                 [DefaultSettingValue ("root")]
52                 public string Username {
53                         get {
54                                 return (string)this["Username"];
55                         }
56                         set {
57                                 this["Username"] = value;
58                         }
59                 }
60
61                 [UserScopedSetting]
62                 [DefaultSettingValue ("8 Cambridge Center")]
63                 public string Address {
64                         get {
65                                 return (string)this["Address"];
66                         }
67                         set {
68                                 this["Address"] = value;
69                         }
70                 }
71         }
72
73         /* an error.  both ApplicationScoped and UserScoped attributes on the same property */
74         class TestSettings2 : ApplicationSettingsBase
75         {
76                 public TestSettings2() 
77                         : base ("TestSettings2")
78                 {
79                 }
80
81                 [ApplicationScopedSetting]
82                 [UserScopedSetting]
83                 public string Username {
84                         get {
85                                 return (string)this["Username"];
86                         }
87                         set {
88                                 this["Username"] = value;
89                         }
90                 }
91         }
92
93
94         [TestFixture]
95         public class ApplicationSettingsBaseTest
96         {
97                 [Test]
98                 public void TestSettings1_Properties ()
99                 {
100                         TestSettings1 settings = new TestSettings1 ();
101
102                         IEnumerator props = settings.Properties.GetEnumerator();
103                         Assert.IsNotNull (props, "A1");
104                         
105                         Assert.IsTrue (props.MoveNext(), "A2");
106                         Assert.AreEqual ("Address", ((SettingsProperty)props.Current).Name, "A3");
107
108                         Assert.IsTrue (props.MoveNext(), "A4");
109                         Assert.AreEqual ("Username", ((SettingsProperty)props.Current).Name, "A5");
110
111                         Assert.AreEqual ("root", settings.Username, "A6");
112                         Assert.AreEqual ("8 Cambridge Center", settings.Address, "A7");
113                 }
114
115                 [Test]
116                 public void TestSettings1_Provider ()
117                 {
118                         TestSettings1 settings = new TestSettings1 ();
119
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);
126                         }
127                 }
128
129
130                 [Test]
131                 public void TestSettings2_Properties ()
132                 {
133                         TestSettings2 settings = new TestSettings2 ();
134
135                         /* should throw ConfigurationException */
136                         IEnumerator props = settings.Properties.GetEnumerator();
137                 }
138
139                 public static int Main (string[] args)
140                 {
141                         ApplicationSettingsBaseTest test = new ApplicationSettingsBaseTest();
142                         test.TestSettings1_Properties();
143                         return 0;
144                 }
145         }
146
147 }
148
149 #endif