2
// ConfigurationMapperTest.cpp
4
// $Id: //poco/1.2/Util/testsuite/src/ConfigurationMapperTest.cpp#1 $
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 "ConfigurationMapperTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Util/ConfigurationMapper.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::ConfigurationMapper;
45
using Poco::Util::MapConfiguration;
49
ConfigurationMapperTest::ConfigurationMapperTest(const std::string& name): CppUnit::TestCase(name)
54
ConfigurationMapperTest::~ConfigurationMapperTest()
59
void ConfigurationMapperTest::testMapper1()
61
AutoPtr<AbstractConfiguration> pConf = createConfiguration();
62
AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("", "", pConf);
63
assert (pMapper->hasProperty("config.value1"));
64
assert (pMapper->hasProperty("config.value2"));
66
AbstractConfiguration::Keys keys;
68
assert (keys.size() == 1);
69
assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
71
pMapper->keys("config", keys);
72
assert (keys.size() == 3);
73
assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
74
assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
75
assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
77
assert (pMapper->getString("config.value1") == "v1");
78
assert (pMapper->getString("config.sub.value2") == "v4");
80
pMapper->setString("config.value3", "v5");
81
assert (pMapper->getString("config.value3") == "v5");
82
assert (pConf->getString("config.value3") == "v5");
86
void ConfigurationMapperTest::testMapper2()
88
AutoPtr<AbstractConfiguration> pConf = createConfiguration();
89
AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("config", "root.conf", pConf);
91
assert (pMapper->hasProperty("root.conf.value1"));
92
assert (pMapper->hasProperty("root.conf.value2"));
94
AbstractConfiguration::Keys keys;
96
assert (keys.size() == 1);
97
assert (std::find(keys.begin(), keys.end(), "root") != keys.end());
99
pMapper->keys("root", keys);
100
assert (keys.size() == 1);
101
assert (std::find(keys.begin(), keys.end(), "conf") != keys.end());
103
pMapper->keys("root.conf", keys);
104
assert (keys.size() == 3);
105
assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
106
assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
107
assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
109
assert (pMapper->getString("root.conf.value1") == "v1");
110
assert (pMapper->getString("root.conf.sub.value2") == "v4");
112
pMapper->setString("root.conf.value3", "v5");
113
assert (pMapper->getString("root.conf.value3") == "v5");
114
assert (pConf->getString("config.value3") == "v5");
118
void ConfigurationMapperTest::testMapper3()
120
AutoPtr<AbstractConfiguration> pConf = createConfiguration();
121
AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("", "root", pConf);
123
assert (pMapper->hasProperty("root.config.value1"));
124
assert (pMapper->hasProperty("root.config.value2"));
126
AbstractConfiguration::Keys keys;
128
assert (keys.size() == 1);
129
assert (std::find(keys.begin(), keys.end(), "root") != keys.end());
131
pMapper->keys("root", keys);
132
assert (keys.size() == 1);
133
assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
135
pMapper->keys("root.config", keys);
136
assert (keys.size() == 3);
137
assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
138
assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
139
assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
141
assert (pMapper->getString("root.config.value1") == "v1");
142
assert (pMapper->getString("root.config.sub.value2") == "v4");
144
pMapper->setString("root.config.value3", "v5");
145
assert (pMapper->getString("root.config.value3") == "v5");
146
assert (pConf->getString("config.value3") == "v5");
150
void ConfigurationMapperTest::testMapper4()
152
AutoPtr<AbstractConfiguration> pConf = createConfiguration();
153
AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("config", "", pConf);
155
assert (pMapper->hasProperty("value1"));
156
assert (pMapper->hasProperty("value2"));
158
AbstractConfiguration::Keys keys;
160
assert (keys.size() == 3);
161
assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
162
assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
163
assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
165
assert (pMapper->getString("value1") == "v1");
166
assert (pMapper->getString("sub.value2") == "v4");
168
pMapper->setString("value3", "v5");
169
assert (pMapper->getString("value3") == "v5");
170
assert (pConf->getString("config.value3") == "v5");
174
AbstractConfiguration* ConfigurationMapperTest::createConfiguration() const
176
AbstractConfiguration* pConfig = new MapConfiguration;
178
pConfig->setString("config.value1", "v1");
179
pConfig->setString("config.value2", "v2");
180
pConfig->setString("config.sub.value1", "v3");
181
pConfig->setString("config.sub.value2", "v4");
187
void ConfigurationMapperTest::setUp()
192
void ConfigurationMapperTest::tearDown()
197
CppUnit::Test* ConfigurationMapperTest::suite()
199
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ConfigurationMapperTest");
201
CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper1);
202
CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper2);
203
CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper3);
204
CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper4);