1 //
2 // LayeredConfigurationTest.cpp
3 //
4 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5 // and Contributors.
6 //
7 // SPDX-License-Identifier: BSL-1.0
8 //
9
10
11 #include "LayeredConfigurationTest.h"
12 #include "CppUnit/TestCaller.h"
13 #include "CppUnit/TestSuite.h"
14 #include "Poco/Util/LayeredConfiguration.h"
15 #include "Poco/Util/MapConfiguration.h"
16 #include "Poco/AutoPtr.h"
17 #include "Poco/Exception.h"
18 #include <algorithm>
19
20
21 using Poco::Util::AbstractConfiguration;
22 using Poco::Util::LayeredConfiguration;
23 using Poco::Util::MapConfiguration;
24 using Poco::AutoPtr;
25 using Poco::NotFoundException;
26 using Poco::RuntimeException;
27
28
LayeredConfigurationTest(const std::string & name)29 LayeredConfigurationTest::LayeredConfigurationTest(const std::string& name): AbstractConfigurationTest(name)
30 {
31 }
32
33
~LayeredConfigurationTest()34 LayeredConfigurationTest::~LayeredConfigurationTest()
35 {
36 }
37
38
testEmpty()39 void LayeredConfigurationTest::testEmpty()
40 {
41 AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
42
43 AbstractConfiguration::Keys keys;
44 pLC->keys(keys);
45 assertTrue (keys.empty());
46
47 assertTrue (!pLC->hasProperty("foo"));
48 try
49 {
50 pLC->setString("foo", "bar");
51 fail("empty LayeredConfiguration - must throw");
52 }
53 catch (RuntimeException&)
54 {
55 }
56
57 try
58 {
59 std::string s = pLC->getString("foo");
60 fail("empty LayeredConfiguration - must throw");
61 }
62 catch (NotFoundException&)
63 {
64 }
65 }
66
67
testOneLayer()68 void LayeredConfigurationTest::testOneLayer()
69 {
70 AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
71 AutoPtr<MapConfiguration> pMC = new MapConfiguration;
72
73 pMC->setString("prop1", "value1");
74 pMC->setString("prop2", "value2");
75
76 pLC->addWriteable(pMC, 0);
77
78 AbstractConfiguration::Keys keys;
79 pLC->keys(keys);
80 assertTrue (keys.size() == 2);
81 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
82 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
83
84 assertTrue (pLC->getString("prop1") == "value1");
85 assertTrue (pLC->getString("prop2") == "value2");
86
87 pLC->setString("prop3", "value3");
88 assertTrue (pLC->getString("prop3") == "value3");
89
90 pLC->remove("prop3");
91 assertTrue (!pLC->hasProperty("prop3"));
92 }
93
94
testTwoLayers()95 void LayeredConfigurationTest::testTwoLayers()
96 {
97 AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
98 AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
99 AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
100
101 pMC1->setString("prop1", "value1");
102 pMC1->setString("prop2", "value2");
103 pMC2->setString("prop2", "value3");
104 pMC2->setString("prop3", "value4");
105
106 pLC->add(pMC1, 0);
107 pLC->addWriteable(pMC2, 1);
108
109 AbstractConfiguration::Keys keys;
110 pLC->keys(keys);
111 assertTrue (keys.size() == 3);
112 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
113 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
114 assertTrue (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
115
116 assertTrue (pLC->getString("prop1") == "value1");
117 assertTrue (pLC->getString("prop2") == "value2");
118 assertTrue (pLC->getString("prop3") == "value4");
119
120 pLC->setString("prop4", "value4");
121 assertTrue (pLC->getString("prop4") == "value4");
122
123 assertTrue (!pMC1->hasProperty("prop4"));
124 assertTrue (pMC2->hasProperty("prop4"));
125
126 pLC->setString("prop1", "value11");
127 assertTrue (pLC->getString("prop1") == "value1");
128 assertTrue (pMC2->getString("prop1") == "value11");
129
130 pLC->remove("prop1");
131 assertTrue (pLC->getString("prop1") == "value1");
132 assertTrue (!pMC2->hasProperty("prop1"));
133 }
134
135
testThreeLayers()136 void LayeredConfigurationTest::testThreeLayers()
137 {
138 AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
139 AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
140 AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
141 AutoPtr<MapConfiguration> pMC3 = new MapConfiguration;
142
143 pMC1->setString("prop1", "value1");
144 pMC1->setString("prop2", "value2");
145 pMC1->setString("prop3", "value3");
146 pMC2->setString("prop2", "value4");
147 pMC2->setString("prop4", "value5");
148 pMC3->setString("prop5", "value6");
149 pMC3->setString("prop1", "value7");
150
151 pLC->add(pMC1, 0);
152 pLC->add(pMC2, 1);
153 pLC->add(pMC3, -1);
154
155 assertTrue (pLC->getString("prop1") == "value7");
156 assertTrue (pLC->getString("prop2") == "value2");
157 assertTrue (pLC->getString("prop3") == "value3");
158 assertTrue (pLC->getString("prop4") == "value5");
159 assertTrue (pLC->getString("prop5") == "value6");
160 }
161
162
testRemove()163 void LayeredConfigurationTest::testRemove()
164 {
165 AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
166 AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
167 AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
168
169 pMC1->setString("prop1", "value1");
170 pMC1->setString("prop2", "value2");
171 pMC2->setString("prop2", "value3");
172 pMC2->setString("prop3", "value4");
173
174 pLC->add(pMC1, 0);
175 pLC->add(pMC2, -1);
176
177 AbstractConfiguration::Keys keys;
178 pLC->keys(keys);
179
180 assertTrue (keys.size() == 3);
181 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
182 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
183 assertTrue (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
184
185 assertTrue (pLC->getString("prop1") == "value1");
186 assertTrue (pLC->getString("prop2") == "value3");
187 assertTrue (pLC->getString("prop3") == "value4");
188
189 pLC->removeConfiguration(pMC2);
190 keys.clear();
191 pLC->keys(keys);
192
193 assertTrue (keys.size() == 2);
194 assertTrue (pLC->getString("prop1") == "value1");
195 assertTrue (pLC->getString("prop2") == "value2");
196 }
197
198
testFind()199 void LayeredConfigurationTest::testFind()
200 {
201 LayeredConfiguration::Ptr pLC = new LayeredConfiguration;
202 AbstractConfiguration::Ptr pMC1 = new MapConfiguration;
203 AbstractConfiguration::Ptr pMC2 = new MapConfiguration;
204
205 pLC->add(pMC1, 0);
206 pLC->add(pMC2, "label", -1);
207
208 AbstractConfiguration::Ptr pFound = pLC->find("label");
209 assertTrue (pFound == pMC2);
210
211 pFound = pLC->find("notfound");
212 assertTrue (pFound.isNull());
213 }
214
215
allocConfiguration() const216 AbstractConfiguration::Ptr LayeredConfigurationTest::allocConfiguration() const
217 {
218 LayeredConfiguration* pLC = new LayeredConfiguration;
219 AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
220 AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
221
222 pLC->add(pMC1, 0);
223 pLC->addWriteable(pMC2, 1);
224
225 return pLC;
226 }
227
228
setUp()229 void LayeredConfigurationTest::setUp()
230 {
231 }
232
233
tearDown()234 void LayeredConfigurationTest::tearDown()
235 {
236 }
237
238
suite()239 CppUnit::Test* LayeredConfigurationTest::suite()
240 {
241 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LayeredConfigurationTest");
242
243 AbstractConfigurationTest_addTests(pSuite, LayeredConfigurationTest);
244 CppUnit_addTest(pSuite, LayeredConfigurationTest, testEmpty);
245 CppUnit_addTest(pSuite, LayeredConfigurationTest, testOneLayer);
246 CppUnit_addTest(pSuite, LayeredConfigurationTest, testTwoLayers);
247 CppUnit_addTest(pSuite, LayeredConfigurationTest, testThreeLayers);
248 CppUnit_addTest(pSuite, LayeredConfigurationTest, testRemove);
249 CppUnit_addTest(pSuite, LayeredConfigurationTest, testFind);
250
251 return pSuite;
252 }
253