column name and ordinal fix...tested on 10.1
[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 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Configuration;
35 using System.Net.Configuration;
36 using System.IO;
37 using NUnit.Framework;
38 using SysConfig = System.Configuration.Configuration;
39 using System.Runtime.InteropServices;
40 using System.Reflection;
41
42 namespace MonoTests.System.Configuration {
43         using Util;
44
45         [TestFixture]
46         public class ConfigurationManagerTest
47         {
48                 private string originalCurrentDir;
49                 private string tempFolder;
50
51                 [SetUp]
52                 public void SetUp ()
53                 {
54                         originalCurrentDir = Directory.GetCurrentDirectory ();
55                         tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
56                         if (!Directory.Exists (tempFolder))
57                                 Directory.CreateDirectory (tempFolder);
58                 }
59
60                 [TearDown]
61                 public void TearDown ()
62                 {
63                         Directory.SetCurrentDirectory (originalCurrentDir);
64                         if (Directory.Exists (tempFolder))
65                                 Directory.Delete (tempFolder, true);
66                 }
67                 
68                 [Test] // OpenExeConfiguration (ConfigurationUserLevel)
69                 [Category ("NotWorking")] // bug #323622
70                 public void OpenExeConfiguration1_Remote ()
71                 {
72                         AppDomain domain = null;
73                         string config_file;
74                         string config_xml = @"
75                                 <configuration>
76                                         <appSettings>
77                                                 <add key='anyKey' value='42' />
78                                         </appSettings>
79                                 </configuration>";
80
81                         config_file = Path.Combine (tempFolder, "otherConfig.noconfigext");
82                         File.WriteAllText (config_file, config_xml);
83
84                         try {
85                                 AppDomainSetup setup = new AppDomainSetup();
86                                 setup.ConfigurationFile = config_file;
87                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
88                                 domain = AppDomain.CreateDomain("foo", null, setup);
89
90                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
91
92                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
93                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
94                                 Assert.AreEqual ("42", config.GetSettingValue (userLevel, "anyKey"));
95                                 Assert.AreEqual ("42", config.GetSettingValue ("anyKey"));
96                         } finally {
97                                 if (domain != null)
98                                         AppDomain.Unload (domain);
99                                 File.Delete (config_file);
100                         }
101
102                         config_file = Path.Combine (tempFolder, "otherConfig.exe.config");
103                         File.WriteAllText (config_file, config_xml);
104
105                         try {
106                                 AppDomainSetup setup = new AppDomainSetup();
107                                 setup.ConfigurationFile = config_file;
108                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
109                                 domain = AppDomain.CreateDomain("foo", null, setup);
110
111                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
112
113                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
114                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
115                                 Assert.AreEqual ("42", config.GetSettingValue (userLevel, "anyKey"));
116                                 Assert.AreEqual ("42", config.GetSettingValue ("anyKey"));
117                         } finally {
118                                 if (domain != null)
119                                         AppDomain.Unload (domain);
120                                 File.Delete (config_file);
121                         }
122
123                         try {
124                                 AppDomainSetup setup = new AppDomainSetup();
125                                 setup.ConfigurationFile = config_file;
126                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
127                                 domain = AppDomain.CreateDomain("foo", null, setup);
128
129                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
130
131                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
132                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
133                                 Assert.IsNull (config.GetSettingValue (userLevel, "anyKey"));
134                                 Assert.IsNull (config.GetSettingValue ("anyKey"));
135                         } finally {
136                                 if (domain != null)
137                                         AppDomain.Unload (domain);
138                                 File.Delete (config_file);
139                         }
140                 }
141
142                 [Test] // OpenExeConfiguration (ConfigurationUserLevel)
143                 public void OpenExeConfiguration1_UserLevel_None ()
144                 {
145                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
146
147                         Console.WriteLine("application config path: {0}", config.FilePath);
148                         FileInfo fi = new FileInfo (config.FilePath);
149                         Assert.AreEqual (TestUtil.ThisConfigFileName, fi.Name);
150                 }
151
152                 [Test]
153                 public void OpenExeConfiguration1_UserLevel_PerUserRoaming ()
154                 {
155                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
156                         Console.WriteLine("roaming user config path: {0}", config.FilePath);
157
158                         FileInfo fi = new FileInfo (config.FilePath);
159                         Assert.AreEqual ("user.config", fi.Name);
160                 }
161
162                 [Test]
163                 public void OpenExeConfiguration1_UserLevel_PerUserRoamingAndLocal ()
164                 {
165                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
166                         Console.WriteLine("local user config path: {0}", config.FilePath);
167
168                         FileInfo fi = new FileInfo (config.FilePath);
169                         Assert.AreEqual ("user.config", fi.Name);
170                 }
171
172                 [Test] // OpenExeConfiguration (String)
173                 public void OpenExeConfiguration2 ()
174                 {
175                         String exePath;
176                         SysConfig config;
177
178                         exePath = Path.Combine (tempFolder, "DoesNotExist.whatever");
179                         File.Create (exePath).Close ();
180
181                         config = ConfigurationManager.OpenExeConfiguration (exePath);
182                         Assert.AreEqual (exePath + ".config", config.FilePath, "#1");
183
184                         exePath = Path.Combine (tempFolder, "SomeExecutable.exe");
185                         File.Create (exePath).Close ();
186
187                         config = ConfigurationManager.OpenExeConfiguration (exePath);
188                         Assert.AreEqual (exePath + ".config", config.FilePath, "#2");
189
190                         exePath = Path.Combine (tempFolder, "Foo.exe.config");
191                         File.Create (exePath).Close ();
192
193                         config = ConfigurationManager.OpenExeConfiguration (exePath);
194                         Assert.AreEqual (exePath + ".config", config.FilePath, "#3");
195
196                         Directory.SetCurrentDirectory (tempFolder);
197
198                         exePath = "relative.exe";
199                         File.Create (Path.Combine (tempFolder, exePath)).Close ();
200
201                         //
202                         // The temp directory as computed by the runtime is slightly different, as
203                         // it will contain the full path after following links, while the test
204                         // below is not comprehensive enough to follow links if there are any
205                         // present in tempFolder
206                         //
207                         
208                         //config = ConfigurationManager.OpenExeConfiguration (exePath);
209                         //Assert.AreEqual (Path.Combine (tempFolder, exePath + ".config"), config.FilePath, "#4");
210                 }
211
212                 [Test] // OpenExeConfiguration (String)
213                 public void OpenExeConfiguration2_ExePath_Empty ()
214                 {
215                         AppDomain domain = AppDomain.CurrentDomain;
216
217                         SysConfig config = ConfigurationManager.OpenExeConfiguration (string.Empty);
218                         Assert.AreEqual (domain.SetupInformation.ConfigurationFile, config.FilePath);
219                 }
220
221                 [Test] // OpenExeConfiguration (String)
222                 public void OpenExeConfiguration2_ExePath_Null ()
223                 {
224                         AppDomain domain = AppDomain.CurrentDomain;
225
226                         SysConfig config = ConfigurationManager.OpenExeConfiguration (string.Empty);
227                         Assert.AreEqual (domain.SetupInformation.ConfigurationFile, config.FilePath);
228                 }
229
230                 [Test] // OpenExeConfiguration (String)
231                 public void OpenExeConfiguration2_ExePath_DoesNotExist ()
232                 {
233                         String exePath = Path.Combine (tempFolder, "DoesNotExist.exe");
234
235                         try {
236                                 ConfigurationManager.OpenExeConfiguration (exePath);
237                                 Assert.Fail ("#1");
238                         } catch (ConfigurationErrorsException ex) {
239                                 // An error occurred loading a configuration file:
240                                 // The parameter 'exePath' is invalid
241                                 Assert.AreEqual (typeof (ConfigurationErrorsException), ex.GetType (), "#2");
242                                 Assert.IsNull (ex.Filename, "#3");
243                                 Assert.IsNotNull (ex.InnerException, "#4");
244                                 Assert.AreEqual (0, ex.Line, "#5");
245                                 Assert.IsNotNull (ex.Message, "#6");
246
247                                 // The parameter 'exePath' is invalid
248                                 ArgumentException inner = ex.InnerException as ArgumentException;
249                                 Assert.IsNotNull (inner, "#7");
250                                 Assert.AreEqual (typeof (ArgumentException), inner.GetType (), "#8");
251                                 Assert.IsNull (inner.InnerException, "#9");
252                                 Assert.IsNotNull (inner.Message, "#10");
253                                 Assert.AreEqual ("exePath", inner.ParamName, "#11");
254                         }
255                 }
256
257                 [Test]
258                 public void exePath_UserLevelNone ()
259                 {
260                         string basedir = AppDomain.CurrentDomain.BaseDirectory;
261                         string name = TestUtil.ThisDllName;
262                         SysConfig config = ConfigurationManager.OpenExeConfiguration (name);
263                         Assert.AreEqual (Path.Combine (basedir, name + ".config"), config.FilePath);
264                 }
265
266                 [Test]
267                 public void exePath_UserLevelPerRoaming ()
268                 {
269                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
270                         string filePath = config.FilePath;
271                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
272                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
273                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
274                 }
275
276                 [Test]
277                 public void exePath_UserLevelPerRoamingAndLocal ()
278                 {
279                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
280                         string filePath = config.FilePath;
281                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
282                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
283                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
284                 }
285
286                 [Test]
287                 public void mapped_UserLevelNone ()
288                 {
289                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
290                         map.ExeConfigFilename = "execonfig";
291
292                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
293                         Console.WriteLine("mapped application config path: {0}", config.FilePath);      
294
295                         FileInfo fi = new FileInfo (config.FilePath);
296                         Assert.AreEqual ("execonfig", fi.Name);
297
298                 }
299
300                 [Test]
301                 public void mapped_UserLevelPerRoaming ()
302                 {
303                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
304                         map.ExeConfigFilename = "execonfig";
305                         map.RoamingUserConfigFilename = "roaminguser";
306
307                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);
308                         Console.WriteLine("mapped roaming user config path: {0}", config.FilePath);     
309
310                         FileInfo fi = new FileInfo (config.FilePath);
311                         Assert.AreEqual ("roaminguser", fi.Name);
312                 }
313
314                 [Test]
315                 [ExpectedException (typeof (ArgumentException))]
316                 [Category ("NotWorking")]
317                 public void mapped_UserLevelPerRoaming_no_execonfig ()
318                 {
319                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
320                         map.RoamingUserConfigFilename = "roaminguser";
321
322                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoaming);
323                 }
324
325                 [Test]
326                 public void mapped_UserLevelPerRoamingAndLocal ()
327                 {
328                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
329                         map.ExeConfigFilename = "execonfig";
330                         map.RoamingUserConfigFilename = "roaminguser";
331                         map.LocalUserConfigFilename = "localuser";
332
333                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
334                         Console.WriteLine("mapped local user config path: {0}", config.FilePath);       
335
336                         FileInfo fi = new FileInfo (config.FilePath);
337                         Assert.AreEqual ("localuser", fi.Name);
338                 }
339
340                 [Test]
341                 [ExpectedException (typeof (ArgumentException))]
342                 [Category ("NotWorking")]
343                 public void mapped_UserLevelPerRoamingAndLocal_no_execonfig ()
344                 {
345                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
346                         map.RoamingUserConfigFilename = "roaminguser";
347                         map.LocalUserConfigFilename = "localuser";
348
349                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
350                 }
351
352                 [Test]
353                 [ExpectedException (typeof (ArgumentException))]
354                 [Category ("NotWorking")]
355                 public void mapped_UserLevelPerRoamingAndLocal_no_roaminguser ()
356                 {
357                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
358                         map.ExeConfigFilename = "execonfig";
359                         map.LocalUserConfigFilename = "localuser";
360
361                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
362                 }
363
364                 [Test]
365                 public void MachineConfig ()
366                 {
367                         SysConfig config = ConfigurationManager.OpenMachineConfiguration ();
368                         Console.WriteLine("machine config path: {0}", config.FilePath);
369
370                         FileInfo fi = new FileInfo (config.FilePath);
371                         Assert.AreEqual ("machine.config", fi.Name);
372                 }
373
374                 [Test]
375                 public void mapped_MachineConfig ()
376                 {
377                         ConfigurationFileMap map = new ConfigurationFileMap ();
378                         map.MachineConfigFilename = "machineconfig";
379
380                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (map);
381                         Console.WriteLine("mapped machine config path: {0}", config.FilePath);
382
383                         FileInfo fi = new FileInfo (config.FilePath);
384                         Assert.AreEqual ("machineconfig", fi.Name);
385                 }
386
387                 [Test]
388                 public void exePath_UserLevelNone_null ()
389                 {
390 #if false
391                         SysConfig config = ConfigurationManager.OpenExeConfiguration (null);
392                         Console.WriteLine("null exe application config path: {0}", config.FilePath);    
393
394                         FileInfo fi = new FileInfo (config.FilePath);
395                         Assert.AreEqual (TestUtil.ThisConfigFileName, fi.Name);
396 #endif
397                 }
398
399                 [Test]
400                 [Category ("NotWorking")]
401                 public void mapped_ExeConfiguration_null ()
402                 {
403                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(null, ConfigurationUserLevel.None);
404                         Console.WriteLine("null mapped application config path: {0}", config.FilePath);
405
406                         FileInfo fi = new FileInfo (config.FilePath);
407                         Assert.AreEqual (TestUtil.ThisConfigFileName, fi.Name);
408                 }
409
410                 [Test]
411                 [Category ("NotWorking")]
412                 public void mapped_MachineConfig_null ()
413                 {
414                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (null);
415                         Console.WriteLine("null mapped machine config path: {0}", config.FilePath);
416
417                         FileInfo fi = new FileInfo (config.FilePath);
418                         Assert.AreEqual ("machine.config", fi.Name);
419                 }
420
421                 [Test]
422                 public void GetSectionReturnsNativeObject ()
423                 {
424                         Assert.IsTrue (ConfigurationManager.GetSection ("appSettings") is NameValueCollection);
425                 }
426
427                 [Test] // test for bug #78372.
428                 public void OpenMachineConfiguration ()
429                 {
430                         SysConfig cfg = ConfigurationManager.OpenMachineConfiguration ();
431                         Assert.IsTrue (cfg.Sections.Count > 0, "#1");
432                         ConfigurationSection s = cfg.SectionGroups ["system.net"].Sections ["connectionManagement"];
433                         Assert.IsNotNull (s, "#2");
434                         Assert.IsTrue (s is ConnectionManagementSection, "#3");
435                 }
436
437                 [Test]
438                 public void SectionCollectionEnumerator ()
439                 {
440                         SysConfig c = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
441                         ConfigurationSectionCollection col =
442                                 c.GetSectionGroup ("system.web").Sections;
443                         IEnumerator e = col.GetEnumerator ();
444                         e.MoveNext ();
445                         Assert.IsTrue (e.Current is ConfigurationSection);
446                 }
447
448                 [Test]  // Test for bug #3412
449                 [Category("NotWorking")]
450                 public void TestAddRemoveSection()
451                 {
452                         const string name = "testsection";
453                         var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
454
455                         // ensure not present
456                         if (config.Sections.Get(name) != null)
457                         {
458                                 config.Sections.Remove(name);
459                         }
460
461                         // add
462                         config.Sections.Add(name, new TestSection());
463
464                         // remove
465                         var section = config.Sections.Get(name);
466                         Assert.IsNotNull(section);
467                         Assert.IsNotNull(section as TestSection);
468                         config.Sections.Remove(name);
469
470                         // add
471                         config.Sections.Add(name, new TestSection());
472
473                         // remove
474                         section = config.Sections.Get(name);
475                         Assert.IsNotNull(section);
476                         Assert.IsNotNull(section as TestSection);
477                         config.Sections.Remove(name);
478                 }
479                 
480                 [Test]
481                 public void TestFileMap ()
482                 {
483                         var name = Path.GetRandomFileName () + ".config";
484                         Assert.IsFalse (File.Exists (name));
485
486                         try {
487                                 var map = new ExeConfigurationFileMap ();
488                                 map.ExeConfigFilename = name;
489                         
490                                 var config = ConfigurationManager.OpenMappedExeConfiguration (
491                                         map, ConfigurationUserLevel.None);
492                                 
493                                 config.Sections.Add ("testsection", new TestSection ());
494                         
495                                 config.Save ();
496                         
497                                 Assert.IsTrue (File.Exists (name), "#1");
498                                 Assert.IsTrue (File.Exists (Path.GetFullPath (name)), "#2");
499                         } finally {
500                                 File.Delete (name);
501                         }
502                 }
503                 
504                 [Test]
505                 public void TestContext ()
506                 {
507                         var config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
508                         const string name = "testsection";
509
510                         // ensure not present
511                         if (config.GetSection (name) != null)
512                                 config.Sections.Remove (name);
513
514                         var section = new TestContextSection ();
515
516                         // Can't access EvaluationContext ....
517                         try {
518                                 section.TestContext (null);
519                                 Assert.Fail ("#1");
520                         } catch (ConfigurationException) {
521                                 ;
522                         }
523
524                         // ... until it's been added to a section.
525                         config.Sections.Add (name, section);
526                         section.TestContext ("#2");
527
528                         // Remove ...
529                         config.Sections.Remove (name);
530
531                         // ... and it doesn't lose its context
532                         section.TestContext (null);
533                 }
534
535                 [Test]
536                 public void TestContext2 ()
537                 {
538                         var name = Path.GetRandomFileName () + ".config";
539                         Assert.IsFalse (File.Exists (name));
540                         
541                         try {
542                                 var map = new ExeConfigurationFileMap ();
543                                 map.ExeConfigFilename = name;
544                                 
545                                 var config = ConfigurationManager.OpenMappedExeConfiguration (
546                                         map, ConfigurationUserLevel.None);
547                                 
548                                 config.Sections.Add ("testsection", new TestSection ());
549                                 config.Sections.Add ("testcontext", new TestContextSection ());
550                                 
551                                 config.Save ();
552                                 
553                                 Assert.IsTrue (File.Exists (name), "#1");
554                         } finally {
555                                 File.Delete (name);
556                         }
557                 }
558
559                         
560                 class TestSection : ConfigurationSection  {}
561
562                 class RemoteConfig : MarshalByRefObject
563                 {
564                         public static RemoteConfig GetInstance (AppDomain domain)
565                         {
566                                 RemoteConfig config = (RemoteConfig) domain.CreateInstanceAndUnwrap (
567                                         typeof (RemoteConfig).Assembly.FullName,
568                                         typeof (RemoteConfig).FullName, new object [0]);
569                                 return config;
570                         }
571
572                         public string GetFilePath (string exePath)
573                         {
574                                 global::System.Configuration.Configuration config =
575                                         ConfigurationManager.OpenExeConfiguration (exePath);
576                                 return config.FilePath;
577                         }
578
579                         public string GetFilePath (ConfigurationUserLevel userLevel)
580                         {
581                                 global::System.Configuration.Configuration config =
582                                         ConfigurationManager.OpenExeConfiguration (userLevel);
583                                 return config.FilePath;
584                         }
585
586                         public string GetSettingValue (string exePath, string key)
587                         {
588                                 global::System.Configuration.Configuration config =
589                                         ConfigurationManager.OpenExeConfiguration (exePath);
590                                 return config.AppSettings.Settings [key].Value;
591                         }
592
593                         public string GetSettingValue (ConfigurationUserLevel userLevel, string key)
594                         {
595                                 global::System.Configuration.Configuration config =
596                                         ConfigurationManager.OpenExeConfiguration (userLevel);
597                                 KeyValueConfigurationElement value = config.AppSettings.Settings [key];
598                                 return value != null ? value.Value : null;
599                         }
600
601                         public string GetSettingValue (string key)
602                         {
603                                 return ConfigurationManager.AppSettings [key];
604                         }
605                 }
606                 
607                 class TestContextSection : ConfigurationSection {
608                         public void TestContext (string label)
609                         {
610                                 Assert.That (EvaluationContext != null, label);
611                         }
612                 }
613
614
615                 [Test]
616                 public void TestConnectionStringRetrieval ()
617                 {
618                         var currentAssembly = Assembly.GetExecutingAssembly().Location;
619                         Assert.IsTrue (File.Exists (currentAssembly + ".config"),
620                                        String.Format ("This test cannot succeed without the .config file being in the same place as the assembly ({0})",
621                                                       currentAssembly));
622
623                         var connStringObj = ConfigurationManager.ConnectionStrings ["test-connstring"];
624                         Assert.IsNotNull (connStringObj);
625                         var connString = connStringObj.ConnectionString;
626                         Assert.IsFalse (String.IsNullOrEmpty (connString));
627                         Assert.AreEqual ("Server=(local);Initial Catalog=someDb;User Id=someUser;Password=somePassword;Application Name=someAppName;Min Pool Size=5;Max Pool Size=500;Connect Timeout=10;Connection Lifetime=29;",
628                                          connString);
629                 }
630
631                 [Test]
632                 public void BadConfig ()
633                 {
634                         string xml = @" badXml";
635
636                         var file = Path.Combine (tempFolder, "badConfig.config");
637                         File.WriteAllText (file, xml);
638
639                         try {
640                                 var fileMap = new ConfigurationFileMap (file);
641                                 var configuration = ConfigurationManager.OpenMappedMachineConfiguration (fileMap);
642                                 Assert.Fail ("Exception ConfigurationErrorsException was expected.");
643                         } catch (ConfigurationErrorsException e) {
644                                 Assert.AreEqual (file, e.Filename);
645                         }
646
647                 }
648         }
649 }