opensim-development – Blame information for rev 1

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