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
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
}
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)
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]
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
}
#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
}
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)
return ConfigurationManager.AppSettings [key];
}
}
+
+ class TestContextSection : ConfigurationSection {
+ public void TestContext (string label)
+ {
+ Assert.That (EvaluationContext != null, label);
+ }
+ }
}
}