2
// LayeredConfigurationTest.cpp
4
// $Id: //poco/1.2/Util/testsuite/src/LayeredConfigurationTest.cpp#2 $
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "LayeredConfigurationTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Util/LayeredConfiguration.h"
37
#include "Poco/Util/MapConfiguration.h"
38
#include "Poco/AutoPtr.h"
39
#include "Poco/Exception.h"
43
using Poco::Util::AbstractConfiguration;
44
using Poco::Util::LayeredConfiguration;
45
using Poco::Util::MapConfiguration;
47
using Poco::NotFoundException;
48
using Poco::RuntimeException;
51
LayeredConfigurationTest::LayeredConfigurationTest(const std::string& name): CppUnit::TestCase(name)
56
LayeredConfigurationTest::~LayeredConfigurationTest()
61
void LayeredConfigurationTest::testEmpty()
63
AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
65
AbstractConfiguration::Keys keys;
67
assert (keys.empty());
69
assert (!pLC->hasProperty("foo"));
72
pLC->setString("foo", "bar");
73
fail("empty LayeredConfiguration - must throw");
75
catch (RuntimeException&)
81
std::string s = pLC->getString("foo");
82
fail("empty LayeredConfiguration - must throw");
84
catch (NotFoundException&)
90
void LayeredConfigurationTest::testOneLayer()
92
AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
93
AutoPtr<MapConfiguration> pMC = new MapConfiguration;
95
pMC->setString("prop1", "value1");
96
pMC->setString("prop2", "value2");
98
pLC->addWriteable(pMC, 0);
100
AbstractConfiguration::Keys keys;
102
assert (keys.size() == 2);
103
assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
104
assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
106
assert (pLC->getString("prop1") == "value1");
107
assert (pLC->getString("prop2") == "value2");
109
pLC->setString("prop3", "value3");
110
assert (pLC->getString("prop3") == "value3");
114
void LayeredConfigurationTest::testTwoLayers()
116
AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
117
AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
118
AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
120
pMC1->setString("prop1", "value1");
121
pMC1->setString("prop2", "value2");
122
pMC2->setString("prop2", "value3");
123
pMC2->setString("prop3", "value4");
126
pLC->addWriteable(pMC2, 1);
128
AbstractConfiguration::Keys keys;
130
assert (keys.size() == 3);
131
assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
132
assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
133
assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
135
assert (pLC->getString("prop1") == "value1");
136
assert (pLC->getString("prop2") == "value2");
137
assert (pLC->getString("prop3") == "value4");
139
pLC->setString("prop4", "value4");
140
assert (pLC->getString("prop4") == "value4");
142
assert (!pMC1->hasProperty("prop4"));
143
assert (pMC2->hasProperty("prop4"));
145
pLC->setString("prop1", "value11");
146
assert (pLC->getString("prop1") == "value1");
147
assert (pMC2->getString("prop1") == "value11");
151
void LayeredConfigurationTest::testThreeLayers()
153
AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
154
AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
155
AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
156
AutoPtr<MapConfiguration> pMC3 = new MapConfiguration;
158
pMC1->setString("prop1", "value1");
159
pMC1->setString("prop2", "value2");
160
pMC1->setString("prop3", "value3");
161
pMC2->setString("prop2", "value4");
162
pMC2->setString("prop4", "value5");
163
pMC3->setString("prop5", "value6");
164
pMC3->setString("prop1", "value7");
170
assert (pLC->getString("prop1") == "value7");
171
assert (pLC->getString("prop2") == "value2");
172
assert (pLC->getString("prop3") == "value3");
173
assert (pLC->getString("prop4") == "value5");
174
assert (pLC->getString("prop5") == "value6");
178
void LayeredConfigurationTest::setUp()
183
void LayeredConfigurationTest::tearDown()
188
CppUnit::Test* LayeredConfigurationTest::suite()
190
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LayeredConfigurationTest");
192
CppUnit_addTest(pSuite, LayeredConfigurationTest, testEmpty);
193
CppUnit_addTest(pSuite, LayeredConfigurationTest, testOneLayer);
194
CppUnit_addTest(pSuite, LayeredConfigurationTest, testTwoLayers);
195
CppUnit_addTest(pSuite, LayeredConfigurationTest, testThreeLayers);