1
0

ConfigurationLoaderTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/one/config/setting.ini");
  77. config.Set("Include-absolute1", "absolute/two/config/setting1.ini");
  78. config.Set("Include-absolute2", "absolute/two/config/setting2.ini");
  79. config.Set("Include-relative", "../" + m_testSubdirectory + "/relative/one/config/setting.ini");
  80. config.Set("Include-relative1", "../" + m_testSubdirectory + "/relative/two/config/setting1.ini");
  81. config.Set("Include-relative2", "../" + m_testSubdirectory + "/relative/two/config/setting2.ini");
  82. CreateIni(mainIniFile, ini);
  83. ini = new IniConfigSource();
  84. ini.AddConfig("Absolute1").Set("name1", "value1");
  85. CreateIni("absolute/one/config/setting.ini", ini);
  86. ini = new IniConfigSource();
  87. ini.AddConfig("Absolute2").Set("name2", 2.3);
  88. CreateIni("absolute/two/config/setting1.ini", ini);
  89. ini = new IniConfigSource();
  90. ini.AddConfig("Absolute2").Set("name3", "value3");
  91. CreateIni("absolute/two/config/setting2.ini", ini);
  92. ini = new IniConfigSource();
  93. ini.AddConfig("Relative1").Set("name4", "value4");
  94. CreateIni("relative/one/config/setting.ini", ini);
  95. ini = new IniConfigSource();
  96. ini.AddConfig("Relative2").Set("name5", true);
  97. CreateIni("relative/two/config/setting1.ini", ini);
  98. ini = new IniConfigSource();
  99. ini.AddConfig("Relative2").Set("name6", 6);
  100. CreateIni("relative/two/config/setting2.ini", ini);
  101. // Prepare call to ConfigurationLoader.LoadConfigSettings()
  102. ConfigurationLoader cl = new ConfigurationLoader();
  103. IConfigSource argvSource = new IniConfigSource();
  104. EnvConfigSource envConfigSource = new EnvConfigSource();
  105. argvSource.AddConfig("Startup").Set("inifile", mainIniFile);
  106. argvSource.AddConfig("Network");
  107. ConfigSettings configSettings;
  108. NetworkServersInfo networkInfo;
  109. OpenSimConfigSource source = cl.LoadConfigSettings(argvSource, envConfigSource,
  110. out configSettings, out networkInfo);
  111. // Remove default config
  112. config = source.Source.Configs["Startup"];
  113. source.Source.Configs.Remove(config);
  114. config = source.Source.Configs["Network"];
  115. source.Source.Configs.Remove(config);
  116. // Finally, we are able to check the result
  117. Assert.AreEqual(m_config.ToString(), source.Source.ToString(),
  118. "Configuration with includes does not contain all settings.");
  119. }
  120. private void CreateIni(string filepath, IniConfigSource source)
  121. {
  122. string path = Path.GetDirectoryName(filepath);
  123. if (path != string.Empty)
  124. {
  125. Directory.CreateDirectory(path);
  126. }
  127. source.Save(filepath);
  128. m_config.Merge(source);
  129. }
  130. }
  131. }