1
0

ConfigurationLoaderTest.cs 6.1 KB

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