Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / class / System.Configuration / Test / System.Configuration / ConfigurationManagerTest.cs
index 038667dd84c2f63e4804b00b0174fbb723834f13..d47ce4de9121f749ace9c910567c1106a418a204 100644 (file)
@@ -61,6 +61,18 @@ namespace MonoTests.System.Configuration {
                        if (Directory.Exists (tempFolder))
                                Directory.Delete (tempFolder, true);
                }
+               
+               static string DotNetVersion {
+                       get {
+#if NET_4_5
+                               return "net_4_5";
+#elif NET_4_0
+                               return "net_4_0";
+#else
+                               return "net_2_0";
+#endif
+                       }
+               }
 
                [Test] // OpenExeConfiguration (ConfigurationUserLevel)
                [Category ("NotWorking")] // bug #323622
@@ -145,10 +157,8 @@ namespace MonoTests.System.Configuration {
                        FileInfo fi = new FileInfo (config.FilePath);
 #if TARGET_JVM
                        Assert.AreEqual ("nunit-console.jar.config", fi.Name);
-#elif NET_2_0
-                       Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
 #else
-                       Assert.AreEqual ("System.Configuration_test_net_4_0.dll.config", fi.Name);
+                       Assert.AreEqual ("System.Configuration_test_" + DotNetVersion + ".dll.config", fi.Name);
 #endif
                }
 
@@ -201,8 +211,15 @@ namespace MonoTests.System.Configuration {
                        exePath = "relative.exe";
                        File.Create (Path.Combine (tempFolder, exePath)).Close ();
 
-                       config = ConfigurationManager.OpenExeConfiguration (exePath);
-                       Assert.AreEqual (Path.Combine (tempFolder, exePath + ".config"), config.FilePath, "#4");
+                       //
+                       // The temp directory as computed by the runtime is slightly different, as
+                       // it will contain the full path after following links, while the test
+                       // below is not comprehensive enough to follow links if there are any
+                       // present in tempFolder
+                       //
+                       
+                       //config = ConfigurationManager.OpenExeConfiguration (exePath);
+                       //Assert.AreEqual (Path.Combine (tempFolder, exePath + ".config"), config.FilePath, "#4");
                }
 
                [Test] // OpenExeConfiguration (String)
@@ -254,8 +271,9 @@ namespace MonoTests.System.Configuration {
                public void exePath_UserLevelNone ()
                {
                        string basedir = AppDomain.CurrentDomain.BaseDirectory;
-                       SysConfig config = ConfigurationManager.OpenExeConfiguration("System.Configuration_test_net_2_0.dll.mdb");
-                       Assert.AreEqual (Path.Combine (basedir, "System.Configuration_test_net_2_0.dll.mdb.config"), config.FilePath);
+                       string name = "System.Configuration_test_" + DotNetVersion + ".dll";
+                       SysConfig config = ConfigurationManager.OpenExeConfiguration (name);
+                       Assert.AreEqual (Path.Combine (basedir, name + ".config"), config.FilePath);
                }
 
                [Test]
@@ -387,7 +405,7 @@ namespace MonoTests.System.Configuration {
                        Console.WriteLine("null exe application config path: {0}", config.FilePath);    
 
                        FileInfo fi = new FileInfo (config.FilePath);
-                       Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
+                       Assert.AreEqual ("System.Configuration_test_" + DotNetVersion + ".dll.config", fi.Name);
 #endif
                }
 
@@ -402,7 +420,7 @@ namespace MonoTests.System.Configuration {
 #if TARGET_JVM
                        Assert.AreEqual("System.Configuration.Test20.jar.config", fi.Name);
 #else
-                       Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
+                       Assert.AreEqual ("System.Configuration_test_" + DotNetVersion + ".dll.config", fi.Name);
 #endif
                }
 
@@ -446,6 +464,120 @@ namespace MonoTests.System.Configuration {
                        Assert.IsTrue (e.Current is ConfigurationSection);
                }
 
+               [Test]  // Test for bug #3412
+               [Category("NotWorking")]
+               public void TestAddRemoveSection()
+               {
+                       const string name = "testsection";
+                       var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
+
+                       // ensure not present
+                       if (config.Sections.Get(name) != null)
+                       {
+                               config.Sections.Remove(name);
+                       }
+
+                       // add
+                       config.Sections.Add(name, new TestSection());
+
+                       // remove
+                       var section = config.Sections.Get(name);
+                       Assert.IsNotNull(section);
+                       Assert.IsNotNull(section as TestSection);
+                       config.Sections.Remove(name);
+
+                       // add
+                       config.Sections.Add(name, new TestSection());
+
+                       // remove
+                       section = config.Sections.Get(name);
+                       Assert.IsNotNull(section);
+                       Assert.IsNotNull(section as TestSection);
+                       config.Sections.Remove(name);
+               }
+               
+               [Test]
+               public void TestFileMap ()
+               {
+                       var name = Path.GetRandomFileName () + ".config";
+                       Assert.IsFalse (File.Exists (name));
+
+                       try {
+                               var map = new ExeConfigurationFileMap ();
+                               map.ExeConfigFilename = name;
+                       
+                               var config = ConfigurationManager.OpenMappedExeConfiguration (
+                                       map, ConfigurationUserLevel.None);
+                               
+                               config.Sections.Add ("testsection", new TestSection ());
+                       
+                               config.Save ();
+                       
+                               Assert.IsTrue (File.Exists (name), "#1");
+                               Assert.IsTrue (File.Exists (Path.GetFullPath (name)), "#2");
+                       } finally {
+                               File.Delete (name);
+                       }
+               }
+               
+               [Test]
+               public void TestContext ()
+               {
+                       var config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
+                       const string name = "testsection";
+
+                       // ensure not present
+                       if (config.GetSection (name) != null)
+                               config.Sections.Remove (name);
+
+                       var section = new TestContextSection ();
+
+                       // Can't access EvaluationContext ....
+                       try {
+                               section.TestContext (null);
+                               Assert.Fail ("#1");
+                       } catch (ConfigurationException) {
+                               ;
+                       }
+
+                       // ... until it's been added to a section.
+                       config.Sections.Add (name, section);
+                       section.TestContext ("#2");
+
+                       // Remove ...
+                       config.Sections.Remove (name);
+
+                       // ... and it doesn't lose its context
+                       section.TestContext (null);
+               }
+
+               [Test]
+               public void TestContext2 ()
+               {
+                       var name = Path.GetRandomFileName () + ".config";
+                       Assert.IsFalse (File.Exists (name));
+                       
+                       try {
+                               var map = new ExeConfigurationFileMap ();
+                               map.ExeConfigFilename = name;
+                               
+                               var config = ConfigurationManager.OpenMappedExeConfiguration (
+                                       map, ConfigurationUserLevel.None);
+                               
+                               config.Sections.Add ("testsection", new TestSection ());
+                               config.Sections.Add ("testcontext", new TestContextSection ());
+                               
+                               config.Save ();
+                               
+                               Assert.IsTrue (File.Exists (name), "#1");
+                       } finally {
+                               File.Delete (name);
+                       }
+               }
+
+                       
+               class TestSection : ConfigurationSection  {}
+
                class RemoteConfig : MarshalByRefObject
                {
                        public static RemoteConfig GetInstance (AppDomain domain)
@@ -490,5 +622,12 @@ namespace MonoTests.System.Configuration {
                                return ConfigurationManager.AppSettings [key];
                        }
                }
+               
+               class TestContextSection : ConfigurationSection {
+                       public void TestContext (string label)
+                       {
+                               Assert.That (EvaluationContext != null, label);
+                       }
+               }
        }
 }