diff options
author | Marck | 2010-08-11 16:31:26 +0200 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2010-08-13 23:59:33 +0100 |
commit | fcc83f2305f6da6fc93b6f94ef7dbd4a1737568a (patch) | |
tree | 32b79064d9c1062654f9b6b92ce66e4ce471b498 /OpenSim/Tests/ConfigurationLoaderTest.cs | |
parent | Configuration files can read include files from relative paths. (diff) | |
download | opensim-SC_OLD-fcc83f2305f6da6fc93b6f94ef7dbd4a1737568a.zip opensim-SC_OLD-fcc83f2305f6da6fc93b6f94ef7dbd4a1737568a.tar.gz opensim-SC_OLD-fcc83f2305f6da6fc93b6f94ef7dbd4a1737568a.tar.bz2 opensim-SC_OLD-fcc83f2305f6da6fc93b6f94ef7dbd4a1737568a.tar.xz |
Unit test for ConfigurationLoader.
Adds a new test assembly for OpenSim.
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Tests/ConfigurationLoaderTest.cs | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/OpenSim/Tests/ConfigurationLoaderTest.cs b/OpenSim/Tests/ConfigurationLoaderTest.cs new file mode 100644 index 0000000..4262c95 --- /dev/null +++ b/OpenSim/Tests/ConfigurationLoaderTest.cs | |||
@@ -0,0 +1,143 @@ | |||
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 | |||
33 | namespace OpenSim.Tests | ||
34 | { | ||
35 | [TestFixture] | ||
36 | public class ConfigurationLoaderTests | ||
37 | { | ||
38 | private const string m_testSubdirectory = "test"; | ||
39 | private string m_basePath; | ||
40 | private string m_workingDirectory; | ||
41 | private IConfigSource m_config; | ||
42 | |||
43 | /// <summary> | ||
44 | /// Set up a test directory. | ||
45 | /// </summary> | ||
46 | [SetUp] | ||
47 | public void SetUp() | ||
48 | { | ||
49 | m_basePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
50 | string path = Path.Combine(m_basePath, m_testSubdirectory); | ||
51 | Directory.CreateDirectory(path); | ||
52 | m_workingDirectory = Directory.GetCurrentDirectory(); | ||
53 | Directory.SetCurrentDirectory(path); | ||
54 | } | ||
55 | |||
56 | /// <summary> | ||
57 | /// Remove the test directory. | ||
58 | /// </summary> | ||
59 | [TearDown] | ||
60 | public void TearDown() | ||
61 | { | ||
62 | Directory.SetCurrentDirectory(m_workingDirectory); | ||
63 | Directory.Delete(m_basePath, true); | ||
64 | } | ||
65 | |||
66 | /// <summary> | ||
67 | /// Test the including of ini files with absolute and relative paths. | ||
68 | /// </summary> | ||
69 | [Test] | ||
70 | public void IncludeTests() | ||
71 | { | ||
72 | const string mainIniFile = "OpenSim.ini"; | ||
73 | m_config = new IniConfigSource(); | ||
74 | |||
75 | // Create ini files in a directory structure | ||
76 | IniConfigSource ini; | ||
77 | IConfig config; | ||
78 | |||
79 | ini = new IniConfigSource(); | ||
80 | config = ini.AddConfig("IncludeTest"); | ||
81 | config.Set("Include-absolute", "absolute/*/config/*.ini"); | ||
82 | config.Set("Include-relative", "../" + m_testSubdirectory + "/relative/*/config/*.ini"); | ||
83 | CreateIni(mainIniFile, ini); | ||
84 | |||
85 | ini = new IniConfigSource(); | ||
86 | ini.AddConfig("Absolute1").Set("name1", "value1"); | ||
87 | CreateIni("absolute/one/config/setting.ini", ini); | ||
88 | |||
89 | ini = new IniConfigSource(); | ||
90 | ini.AddConfig("Absolute2").Set("name2", 2.3); | ||
91 | CreateIni("absolute/two/config/setting1.ini", ini); | ||
92 | |||
93 | ini = new IniConfigSource(); | ||
94 | ini.AddConfig("Absolute2").Set("name3", "value3"); | ||
95 | CreateIni("absolute/two/config/setting2.ini", ini); | ||
96 | |||
97 | ini = new IniConfigSource(); | ||
98 | ini.AddConfig("Relative1").Set("name4", "value4"); | ||
99 | CreateIni("relative/one/config/setting.ini", ini); | ||
100 | |||
101 | ini = new IniConfigSource(); | ||
102 | ini.AddConfig("Relative2").Set("name5", true); | ||
103 | CreateIni("relative/two/config/setting1.ini", ini); | ||
104 | |||
105 | ini = new IniConfigSource(); | ||
106 | ini.AddConfig("Relative2").Set("name6", 6); | ||
107 | CreateIni("relative/two/config/setting2.ini", ini); | ||
108 | |||
109 | // Prepare call to ConfigurationLoader.LoadConfigSettings() | ||
110 | ConfigurationLoader cl = new ConfigurationLoader(); | ||
111 | IConfigSource argvSource = new IniConfigSource(); | ||
112 | argvSource.AddConfig("Startup").Set("inifile", mainIniFile); | ||
113 | ConfigSettings configSettings; | ||
114 | NetworkServersInfo networkInfo; | ||
115 | |||
116 | OpenSimConfigSource source = cl.LoadConfigSettings(argvSource, out configSettings, out networkInfo); | ||
117 | |||
118 | // Remove default config | ||
119 | config = source.Source.Configs["Startup"]; | ||
120 | source.Source.Configs.Remove(config); | ||
121 | config = source.Source.Configs["Network"]; | ||
122 | source.Source.Configs.Remove(config); | ||
123 | |||
124 | // Finally, we are able to check the result | ||
125 | Assert.AreEqual(m_config.ToString(), source.Source.ToString(), | ||
126 | "Configuration with includes does not contain all settings."); | ||
127 | // The following would be preferable but fails due to a type mismatch which I am not able to resolve | ||
128 | //CollectionAssert.AreEquivalent(m_config.Configs, source.Source.Configs, | ||
129 | // String.Format("Configuration with includes does not contain all settings.\nAll settings:\n{0}\nSettings read:\n{1}", m_config, source.Source)); | ||
130 | } | ||
131 | |||
132 | private void CreateIni(string filepath, IniConfigSource source) | ||
133 | { | ||
134 | string path = Path.GetDirectoryName(filepath); | ||
135 | if (path != string.Empty) | ||
136 | { | ||
137 | Directory.CreateDirectory(path); | ||
138 | } | ||
139 | source.Save(filepath); | ||
140 | m_config.Merge(source); | ||
141 | } | ||
142 | } | ||
143 | } | ||