DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DbConfigTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using System;
17 using System.Text;
18 
19 using NUnit.Framework;
20 
21 namespace Deveel.Data.Configuration {
22  [TestFixture]
23  public class DbConfigTests {
24  [Test]
25  public void DefaultConfig() {
26  IConfiguration config = null;
27  Assert.DoesNotThrow(() => config = new Configuration());
28  Assert.IsNotNull(config);
29  Assert.IsNull(config.Parent);
30  Assert.IsNull(config.Source);
31  }
32 
33  [Test]
34  public void GetValuesFromRoot() {
35  IConfiguration config = null;
36  Assert.DoesNotThrow(() => config = new Configuration());
37  Assert.IsNotNull(config);
38  Assert.DoesNotThrow(() => config.SetValue("test.oneKey", 54));
39  Assert.DoesNotThrow(() => config.SetValue("test.twoKeys", null));
40 
41  object value1 = null;
42  object value2 = null;
43 
44  Assert.DoesNotThrow(() => value1 = config.GetValue("test.oneKey"));
45  Assert.IsNotNull(value1);
46  Assert.IsInstanceOf<int>(value1);
47  Assert.AreEqual(54, value1);
48 
49  Assert.DoesNotThrow(() => value2 = config.GetValue("test.twoKeys"));
50  Assert.IsNull(value2);
51  }
52 
53  [Test]
54  public void GetValuesFromChild() {
55  IConfiguration config = null;
56  Assert.DoesNotThrow(() => config = new Configuration());
57  Assert.IsNotNull(config);
58 
59  Assert.DoesNotThrow(() => config.SetValue("test.oneKey", "one"));
60 
61  IConfiguration child = null;
62  Assert.DoesNotThrow(() => child = new Configuration(config));
63  Assert.IsNotNull(child);
64  Assert.IsNotNull(child.Parent);
65 
66  Assert.DoesNotThrow(() => child.SetValue("test.oneKey", 45));
67 
68  object value = null;
69  Assert.DoesNotThrow(() => value = child.GetValue("test.oneKey"));
70  Assert.IsNotNull(value);
71  Assert.IsInstanceOf<int>(value);
72  Assert.AreEqual(45, value);
73 
74  Assert.DoesNotThrow(() => value = config.GetValue("test.oneKey"));
75  Assert.IsNotNull(value);
76  Assert.IsInstanceOf<string>(value);
77  Assert.AreEqual("one", value);
78  }
79 
80  [Test]
81  public void GetValueAsInt32() {
82  IConfiguration config = null;
83  Assert.DoesNotThrow(() => config = new Configuration());
84  Assert.IsNotNull(config);
85 
86  Assert.DoesNotThrow(() => config.SetValue("test.oneKey", "22"));
87 
88  object value = null;
89  Assert.DoesNotThrow(() => value = config.GetInt32("test.oneKey"));
90  Assert.IsNotNull(value);
91  Assert.IsInstanceOf<int>(value);
92  Assert.AreEqual(22, value);
93  }
94 
95  [TestCase("test","true", true)]
96  [TestCase("test","false", false)]
97  [TestCase("test", "off", false)]
98  [TestCase("test", "on", true)]
99  [TestCase("test", "enabled", true)]
100  public void GetBooleanValue(string key, string value, bool expected) {
101  IConfiguration config = null;
102  Assert.DoesNotThrow(() => config = new Configuration());
103  Assert.IsNotNull(config);
104 
105  Assert.DoesNotThrow(() => config.SetValue(key, value));
106 
107  object configValue = null;
108  Assert.DoesNotThrow(() => configValue = config.GetBoolean(key));
109  Assert.IsNotNull(configValue);
110  Assert.IsInstanceOf<bool>(configValue);
111  Assert.AreEqual(expected, configValue);
112  }
113 
114  [TestCase(1, TestEnum.One)]
115  [TestCase("one", TestEnum.One)]
116  [TestCase("TWO", TestEnum.Two)]
117  [TestCase(null, TestEnum.Default)]
118  public void GetEnumValue(object value, TestEnum expected) {
119  IConfiguration config = null;
120  Assert.DoesNotThrow(() => config = new Configuration());
121  Assert.IsNotNull(config);
122 
123  Assert.DoesNotThrow(() => config.SetValue("test", value));
124 
125  object configValue = null;
126  Assert.DoesNotThrow(() => configValue = config.GetValue<TestEnum>("test"));
127  Assert.IsInstanceOf<TestEnum>(configValue);
128  Assert.AreEqual(expected, configValue);
129  }
130 
131  public enum TestEnum {
132  One = 1,
133  Two = 2,
134  Default = 0
135  }
136 
137  [Test]
139  var properties = new StringBuilder();
140  properties.AppendLine("system.readOnly = false");
141  properties.AppendLine("caching.type = Memory");
142 
143  IConfiguration configuration = null;
144  Assert.DoesNotThrow(() => configuration = new Configuration(new StringConfigSource(properties.ToString())));
145  Assert.IsNotNull(configuration);
146  Assert.DoesNotThrow(() => configuration.Load(new PropertiesConfigFormatter()));
147 
148  Assert.DoesNotThrow(() => Assert.IsNotNull(configuration.GetValue("system.readOnly")));
149 
150  bool readOnly = true;
151  Assert.DoesNotThrow(() => readOnly = configuration.GetBoolean("system.readOnly"));
152  Assert.IsFalse(readOnly);
153  }
154  }
155 }
object GetValue(string key)
Gets a configuration setting for the given key.
IConfigSource Source
Gets or sets an optional source of the configuration object
void GetBooleanValue(string key, string value, bool expected)
void GetEnumValue(object value, TestEnum expected)
void SetValue(string key, object value)
Sets a given value for a key defined by this object.
IConfiguration Parent
Gets or sets an optional parent object of this configuration.
Defines the contract for the configuration node of a component within the system or of the system its...