New test.
[mono.git] / mcs / class / System.Configuration / Test / System.Configuration / ConfigurationManagerTest.cs
1 //
2 // System.Configuration.ConfigurationManagerTest.cs - Unit tests
3 // for System.Configuration.ConfigurationManager.
4 //
5 // Author:
6 //      Chris Toshok  <toshok@ximian.com>
7 //      Atsushi Enomoto  <atsushi@ximian.com>
8 //
9 // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.Collections.Specialized;
35 using System.Configuration;
36 using System.Net.Configuration;
37 using System.IO;
38 using NUnit.Framework;
39 using SysConfig = System.Configuration.Configuration;
40 using System.Runtime.InteropServices;
41
42 namespace MonoTests.System.Configuration {
43         [TestFixture]
44         public class ConfigurationManagerTest
45         {
46                 [Test]
47                 public void UserLevelNone ()
48                 {
49                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
50
51                         Console.WriteLine("application config path: {0}", config.FilePath);
52                         FileInfo fi = new FileInfo (config.FilePath);
53 #if TARGET_JVM
54                         Assert.AreEqual("System.Configuration.Test20.jar.config", fi.Name);
55 #else
56                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
57 #endif
58                 }
59
60                 [Test]
61                 [Category("NotWorking")]
62                 public void UserLevelPerRoaming ()
63                 {
64                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
65                         Console.WriteLine("roaming user config path: {0}", config.FilePath);
66
67                         FileInfo fi = new FileInfo (config.FilePath);
68                         Assert.AreEqual ("user.config", fi.Name);
69                 }
70
71                 [Test]\r
72                 [Category ("NotWorking")]
73                 public void UserLevelPerRoamingAndLocal ()
74                 {
75                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
76                         Console.WriteLine("local user config path: {0}", config.FilePath);
77
78                         FileInfo fi = new FileInfo (config.FilePath);
79                         Assert.AreEqual ("user.config", fi.Name);
80                 }
81
82                 [Test]
83                 public void exePath_UserLevelNone_absolute ()
84                 {
85 #if false
86                         string path = String.Format ("{0}hi{1}there.exe", Path.DirectorySeparatorChar, Path.DirectorySeparatorChar);
87                         SysConfig config = ConfigurationManager.OpenExeConfiguration(path);
88                         Assert.AreEqual ("", config.FilePath);
89 #endif
90                 }
91
92                 [Test]
93                 public void exePath_UserLevelNone ()
94                 {
95 #if false
96                         SysConfig config = ConfigurationManager.OpenExeConfiguration("System.Configuration_test_net_2_0.dll.mdb");
97                         Assert.AreEqual ("", config.FilePath);
98 #endif
99                 }
100
101                 [Test]
102                 public void exePath_UserLevelPerRoaming ()
103                 {
104 #if false
105                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming, "System.Configuration_test_net_2_0.dll.mdb");
106                         Assert.AreEqual ("", config.FilePath);
107 #endif
108                 }
109
110                 [Test]
111                 public void exePath_UserLevelPerRoamingAndLocal ()
112                 {
113 #if false
114                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal, "System.Configuration_test_net_2_0.dll.mdb");
115                         Assert.AreEqual ("", config.FilePath);
116 #endif
117                 }
118
119                 [Test]
120                 public void mapped_UserLevelNone ()
121                 {
122                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
123                         map.ExeConfigFilename = "execonfig";
124
125                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
126                         Console.WriteLine("mapped application config path: {0}", config.FilePath);      
127
128                         FileInfo fi = new FileInfo (config.FilePath);
129                         Assert.AreEqual ("execonfig", fi.Name);
130
131                 }
132
133                 [Test]\r
134                 [Category ("NotWorking")]
135                 public void mapped_UserLevelPerRoaming ()
136                 {
137                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
138                         map.ExeConfigFilename = "execonfig";
139                         map.RoamingUserConfigFilename = "roaminguser";
140
141                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);
142                         Console.WriteLine("mapped roaming user config path: {0}", config.FilePath);     
143
144                         FileInfo fi = new FileInfo (config.FilePath);
145                         Assert.AreEqual ("roaminguser", fi.Name);
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (ArgumentException))]
150                 public void mapped_UserLevelPerRoaming_no_execonfig ()
151                 {
152                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
153                         map.RoamingUserConfigFilename = "roaminguser";
154
155                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);
156                         Console.WriteLine("mapped roaming user config path: {0}", config.FilePath);     
157
158                         FileInfo fi = new FileInfo (config.FilePath);
159                         Assert.AreEqual ("roaminguser", fi.Name);
160                 }
161
162                 [Test]\r
163                 [Category ("NotWorking")]
164                 public void mapped_UserLevelPerRoamingAndLocal ()
165                 {
166                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
167                         map.ExeConfigFilename = "execonfig";
168                         map.RoamingUserConfigFilename = "roaminguser";
169                         map.LocalUserConfigFilename = "localuser";
170
171                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
172                         Console.WriteLine("mapped local user config path: {0}", config.FilePath);       
173
174                         FileInfo fi = new FileInfo (config.FilePath);
175                         Assert.AreEqual ("localuser", fi.Name);
176                 }
177
178                 [Test]
179                 [ExpectedException (typeof (ArgumentException))]
180                 public void mapped_UserLevelPerRoamingAndLocal_no_execonfig ()
181                 {
182                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
183                         map.RoamingUserConfigFilename = "roaminguser";
184                         map.LocalUserConfigFilename = "localuser";
185
186                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
187                         Console.WriteLine("mapped local user config path: {0}", config.FilePath);       
188
189                         FileInfo fi = new FileInfo (config.FilePath);
190                         Assert.AreEqual ("localuser", fi.Name);
191                 }
192
193                 [Test]
194                 [ExpectedException (typeof (ArgumentException))]\r
195                 [Category ("NotWorking")]
196                 public void mapped_UserLevelPerRoamingAndLocal_no_roaminguser ()
197                 {
198                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
199                         map.ExeConfigFilename = "execonfig";
200                         map.LocalUserConfigFilename = "localuser";
201
202                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
203                         Console.WriteLine("mapped local user config path: {0}", config.FilePath);       
204
205                         FileInfo fi = new FileInfo (config.FilePath);
206                         Assert.AreEqual ("localuser", fi.Name);
207                 }
208
209                 [Test]
210                 public void MachineConfig ()
211                 {
212                         SysConfig config = ConfigurationManager.OpenMachineConfiguration ();
213                         Console.WriteLine("machine config path: {0}", config.FilePath);
214
215                         FileInfo fi = new FileInfo (config.FilePath);
216                         Assert.AreEqual ("machine.config", fi.Name);
217                 }
218
219                 [Test]
220                 public void mapped_MachineConfig ()
221                 {
222                         ConfigurationFileMap map = new ConfigurationFileMap ();
223                         map.MachineConfigFilename = "machineconfig";
224
225                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (map);
226                         Console.WriteLine("mapped machine config path: {0}", config.FilePath);
227
228                         FileInfo fi = new FileInfo (config.FilePath);
229                         Assert.AreEqual ("machineconfig", fi.Name);
230                 }
231
232                 [Test]
233                 public void exePath_UserLevelNone_null ()
234                 {
235                         SysConfig config = ConfigurationManager.OpenExeConfiguration (null);
236 #if false
237                         Console.WriteLine("null exe application config path: {0}", config.FilePath);    
238
239                         FileInfo fi = new FileInfo (config.FilePath);
240                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
241 #endif
242                 }
243
244                 [Test]\r
245                 [Category ("NotWorking")]
246                 public void mapped_ExeConfiguration_null ()
247                 {
248                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(null, ConfigurationUserLevel.None);
249                         Console.WriteLine("null mapped application config path: {0}", config.FilePath); 
250
251                         FileInfo fi = new FileInfo (config.FilePath);
252 #if TARGET_JVM
253                         Assert.AreEqual("System.Configuration.Test20.jar.config", fi.Name);
254 #else
255                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
256 #endif
257                 }
258
259                 [Test]\r
260                 [Category ("NotWorking")]
261                 public void mapped_MachineConfig_null ()
262                 {
263                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (null);
264                         Console.WriteLine("null mapped machine config path: {0}", config.FilePath);
265
266                         FileInfo fi = new FileInfo (config.FilePath);
267                         Assert.AreEqual ("machine.config", fi.Name);
268                 }
269
270                 [Test]
271                 public void GetSectionReturnsNativeObject ()
272                 {
273                         Assert.IsTrue (ConfigurationManager.GetSection ("appSettings") is NameValueCollection);
274                 }
275
276                 [Test] // test for bug #78372.\r
277                 [Category ("NotWorking")]
278                 public void OpenMachineConfiguration ()
279                 {
280                         SysConfig cfg = ConfigurationManager.OpenMachineConfiguration ();
281                         Assert.IsTrue (cfg.Sections.Count > 0, "#1");
282 #if !TARGET_JVM
283                         ConfigurationSection s = cfg.Sections ["system.net/connectionManagement"];
284                         Assert.IsNotNull (s, "#2");
285                         Assert.IsTrue (s is ConnectionManagementSection, "#3");
286 #endif
287                 }
288         }
289 }
290
291 #endif