1
0

ConfigurationLoaderTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. using OpenSim.Tests.Common;
  32. namespace OpenSim.Tests
  33. {
  34. [TestFixture]
  35. public class ConfigurationLoaderTests : OpenSimTestCase
  36. {
  37. private const string m_testSubdirectory = "test";
  38. private string m_basePath;
  39. private string m_workingDirectory;
  40. private IConfigSource m_config;
  41. /// <summary>
  42. /// Set up a test directory.
  43. /// </summary>
  44. [SetUp]
  45. public void SetUp()
  46. {
  47. m_basePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
  48. string path = Path.Combine(m_basePath, m_testSubdirectory);
  49. Directory.CreateDirectory(path);
  50. m_workingDirectory = Directory.GetCurrentDirectory();
  51. Directory.SetCurrentDirectory(path);
  52. }
  53. /// <summary>
  54. /// Remove the test directory.
  55. /// </summary>
  56. [TearDown]
  57. public void TearDown()
  58. {
  59. Directory.SetCurrentDirectory(m_workingDirectory);
  60. Directory.Delete(m_basePath, true);
  61. }
  62. /// <summary>
  63. /// Test the including of ini files with absolute and relative paths.
  64. /// </summary>
  65. [Test]
  66. public void IncludeTests()
  67. {
  68. const string mainIniFile = "OpenSimDefaults.ini";
  69. m_config = new IniConfigSource();
  70. // Create ini files in a directory structure
  71. IniConfigSource ini;
  72. IConfig config;
  73. ini = new IniConfigSource();
  74. config = ini.AddConfig("IncludeTest");
  75. config.Set("Include-absolute", "absolute/*/config/*.ini");
  76. config.Set("Include-relative", "../" + m_testSubdirectory + "/relative/*/config/*.ini");
  77. CreateIni(mainIniFile, ini);
  78. ini = new IniConfigSource();
  79. ini.AddConfig("Absolute1").Set("name1", "value1");
  80. CreateIni("absolute/one/config/setting.ini", ini);
  81. ini = new IniConfigSource();
  82. ini.AddConfig("Absolute2").Set("name2", 2.3);
  83. CreateIni("absolute/two/config/setting1.ini", ini);
  84. ini = new IniConfigSource();
  85. ini.AddConfig("Absolute2").Set("name3", "value3");
  86. CreateIni("absolute/two/config/setting2.ini", ini);
  87. ini = new IniConfigSource();
  88. ini.AddConfig("Relative1").Set("name4", "value4");
  89. CreateIni("relative/one/config/setting.ini", ini);
  90. ini = new IniConfigSource();
  91. ini.AddConfig("Relative2").Set("name5", true);
  92. CreateIni("relative/two/config/setting1.ini", ini);
  93. ini = new IniConfigSource();
  94. ini.AddConfig("Relative2").Set("name6", 6);
  95. CreateIni("relative/two/config/setting2.ini", ini);
  96. // Prepare call to ConfigurationLoader.LoadConfigSettings()
  97. ConfigurationLoader cl = new ConfigurationLoader();
  98. IConfigSource argvSource = new IniConfigSource();
  99. EnvConfigSource envConfigSource = new EnvConfigSource();
  100. argvSource.AddConfig("Startup").Set("inifile", mainIniFile);
  101. ConfigSettings configSettings;
  102. NetworkServersInfo networkInfo;
  103. OpenSimConfigSource source = cl.LoadConfigSettings(argvSource, envConfigSource,
  104. out configSettings, out networkInfo);
  105. // Remove default config
  106. config = source.Source.Configs["Startup"];
  107. source.Source.Configs.Remove(config);
  108. config = source.Source.Configs["Network"];
  109. source.Source.Configs.Remove(config);
  110. // Finally, we are able to check the result
  111. Assert.AreEqual(m_config.ToString(), source.Source.ToString(),
  112. "Configuration with includes does not contain all settings.");
  113. // The following would be preferable but fails due to a type mismatch which I am not able to resolve
  114. //CollectionAssert.AreEquivalent(m_config.Configs, source.Source.Configs,
  115. // String.Format("Configuration with includes does not contain all settings.\nAll settings:\n{0}\nSettings read:\n{1}", m_config, source.Source));
  116. }
  117. private void CreateIni(string filepath, IniConfigSource source)
  118. {
  119. string path = Path.GetDirectoryName(filepath);
  120. if (path != string.Empty)
  121. {
  122. Directory.CreateDirectory(path);
  123. }
  124. source.Save(filepath);
  125. m_config.Merge(source);
  126. }
  127. }
  128. }