ConfigurationLoaderTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.IO;
  28. using Nini.Config;
  29. using NUnit.Framework;
  30. using OpenSim.Framework;
  31. namespace OpenSim.Tests
  32. {
  33. [TestFixture]
  34. public class ConfigurationLoaderTests
  35. {
  36. private const string m_testSubdirectory = "test";
  37. private string m_basePath;
  38. private string m_workingDirectory;
  39. private IConfigSource m_config;
  40. /// <summary>
  41. /// Set up a test directory.
  42. /// </summary>
  43. [SetUp]
  44. public void SetUp()
  45. {
  46. m_basePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
  47. string path = Path.Combine(m_basePath, m_testSubdirectory);
  48. Directory.CreateDirectory(path);
  49. m_workingDirectory = Directory.GetCurrentDirectory();
  50. Directory.SetCurrentDirectory(path);
  51. }
  52. /// <summary>
  53. /// Remove the test directory.
  54. /// </summary>
  55. [TearDown]
  56. public void TearDown()
  57. {
  58. Directory.SetCurrentDirectory(m_workingDirectory);
  59. Directory.Delete(m_basePath, true);
  60. }
  61. /// <summary>
  62. /// Test the including of ini files with absolute and relative paths.
  63. /// </summary>
  64. [Test]
  65. public void IncludeTests()
  66. {
  67. const string mainIniFile = "OpenSimDefaults.ini";
  68. m_config = new IniConfigSource();
  69. // Create ini files in a directory structure
  70. IniConfigSource ini;
  71. IConfig config;
  72. ini = new IniConfigSource();
  73. config = ini.AddConfig("IncludeTest");
  74. config.Set("Include-absolute", "absolute/*/config/*.ini");
  75. config.Set("Include-relative", "../" + m_testSubdirectory + "/relative/*/config/*.ini");
  76. CreateIni(mainIniFile, ini);
  77. ini = new IniConfigSource();
  78. ini.AddConfig("Absolute1").Set("name1", "value1");
  79. CreateIni("absolute/one/config/setting.ini", ini);
  80. ini = new IniConfigSource();
  81. ini.AddConfig("Absolute2").Set("name2", 2.3);
  82. CreateIni("absolute/two/config/setting1.ini", ini);
  83. ini = new IniConfigSource();
  84. ini.AddConfig("Absolute2").Set("name3", "value3");
  85. CreateIni("absolute/two/config/setting2.ini", ini);
  86. ini = new IniConfigSource();
  87. ini.AddConfig("Relative1").Set("name4", "value4");
  88. CreateIni("relative/one/config/setting.ini", ini);
  89. ini = new IniConfigSource();
  90. ini.AddConfig("Relative2").Set("name5", true);
  91. CreateIni("relative/two/config/setting1.ini", ini);
  92. ini = new IniConfigSource();
  93. ini.AddConfig("Relative2").Set("name6", 6);
  94. CreateIni("relative/two/config/setting2.ini", ini);
  95. // Prepare call to ConfigurationLoader.LoadConfigSettings()
  96. ConfigurationLoader cl = new ConfigurationLoader();
  97. IConfigSource argvSource = new IniConfigSource();
  98. argvSource.AddConfig("Startup").Set("inifile", mainIniFile);
  99. ConfigSettings configSettings;
  100. NetworkServersInfo networkInfo;
  101. OpenSimConfigSource source = cl.LoadConfigSettings(argvSource, out configSettings, out networkInfo);
  102. // Remove default config
  103. config = source.Source.Configs["Startup"];
  104. source.Source.Configs.Remove(config);
  105. config = source.Source.Configs["Network"];
  106. source.Source.Configs.Remove(config);
  107. // Finally, we are able to check the result
  108. Assert.AreEqual(m_config.ToString(), source.Source.ToString(),
  109. "Configuration with includes does not contain all settings.");
  110. // The following would be preferable but fails due to a type mismatch which I am not able to resolve
  111. //CollectionAssert.AreEquivalent(m_config.Configs, source.Source.Configs,
  112. // String.Format("Configuration with includes does not contain all settings.\nAll settings:\n{0}\nSettings read:\n{1}", m_config, source.Source));
  113. }
  114. private void CreateIni(string filepath, IniConfigSource source)
  115. {
  116. string path = Path.GetDirectoryName(filepath);
  117. if (path != string.Empty)
  118. {
  119. Directory.CreateDirectory(path);
  120. }
  121. source.Save(filepath);
  122. m_config.Merge(source);
  123. }
  124. }
  125. }