1//
2// ConfigurationMapperTest.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 "ConfigurationMapperTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/ConfigurationMapper.h"
15#include "Poco/Util/MapConfiguration.h"
16#include "Poco/AutoPtr.h"
17#include "Poco/Exception.h"
18#include <algorithm>
19
20
21using Poco::Util::AbstractConfiguration;
22using Poco::Util::ConfigurationMapper;
23using Poco::Util::MapConfiguration;
24using Poco::AutoPtr;
25
26
27ConfigurationMapperTest::ConfigurationMapperTest(const std::string& name): AbstractConfigurationTest(name)
28{
29}
30
31
32ConfigurationMapperTest::~ConfigurationMapperTest()
33{
34}
35
36
37void ConfigurationMapperTest::testMapper1()
38{
39 AutoPtr<AbstractConfiguration> pConf = createConfiguration();
40 AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("", "", pConf);
41 assertTrue (pMapper->hasProperty("prop5.string1"));
42 assertTrue (pMapper->hasProperty("prop5.string1"));
43
44 AbstractConfiguration::Keys keys;
45 pMapper->keys(keys);
46 assertTrue (keys.size() == 13);
47 assertTrue (std::find(keys.begin(), keys.end(), "prop5") != keys.end());
48
49 pMapper->keys("prop5", keys);
50 assertTrue (keys.size() == 4);
51 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
52 assertTrue (std::find(keys.begin(), keys.end(), "string2") != keys.end());
53 assertTrue (std::find(keys.begin(), keys.end(), "sub1") != keys.end());
54 assertTrue (std::find(keys.begin(), keys.end(), "sub2") != keys.end());
55
56 assertTrue (pMapper->getString("prop5.string1") == "foo");
57 assertTrue (pMapper->getString("prop5.sub1.string1") == "FOO");
58
59 pMapper->setString("prop5.string3", "baz");
60 assertTrue (pMapper->getString("prop5.string3") == "baz");
61 assertTrue (pConf->getString("prop5.string3") == "baz");
62
63 pMapper->remove("prop5.string3");
64 assertTrue (!pMapper->hasProperty("prop5.string3"));
65 assertTrue (!pConf->hasProperty("prop5.string3"));
66}
67
68
69void ConfigurationMapperTest::testMapper2()
70{
71 AutoPtr<AbstractConfiguration> pConf = createConfiguration();
72 AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("prop5", "root.conf", pConf);
73
74 assertTrue (pMapper->hasProperty("root.conf.string1"));
75 assertTrue (pMapper->hasProperty("root.conf.string2"));
76
77 AbstractConfiguration::Keys keys;
78 pMapper->keys(keys);
79 assertTrue (keys.size() == 1);
80 assertTrue (std::find(keys.begin(), keys.end(), "root") != keys.end());
81
82 pMapper->keys("root", keys);
83 assertTrue (keys.size() == 1);
84 assertTrue (std::find(keys.begin(), keys.end(), "conf") != keys.end());
85
86 pMapper->keys("root.conf", keys);
87 assertTrue (keys.size() == 4);
88 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
89 assertTrue (std::find(keys.begin(), keys.end(), "string2") != keys.end());
90 assertTrue (std::find(keys.begin(), keys.end(), "sub1") != keys.end());
91 assertTrue (std::find(keys.begin(), keys.end(), "sub2") != keys.end());
92
93 assertTrue (pMapper->getString("root.conf.string1") == "foo");
94 assertTrue (pMapper->getString("root.conf.sub1.string1") == "FOO");
95
96 pMapper->setString("root.conf.string3", "baz");
97 assertTrue (pMapper->getString("root.conf.string3") == "baz");
98 assertTrue (pConf->getString("prop5.string3") == "baz");
99
100 pMapper->remove("root.conf.string3");
101 assertTrue (!pMapper->hasProperty("root.conf.string3"));
102 assertTrue (!pConf->hasProperty("prop5.string3"));
103}
104
105
106void ConfigurationMapperTest::testMapper3()
107{
108 AutoPtr<AbstractConfiguration> pConf = createConfiguration();
109 AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("", "root", pConf);
110
111 assertTrue (pMapper->hasProperty("root.prop5.string1"));
112 assertTrue (pMapper->hasProperty("root.prop5.string2"));
113
114 AbstractConfiguration::Keys keys;
115 pMapper->keys(keys);
116 assertTrue (keys.size() == 1);
117 assertTrue (std::find(keys.begin(), keys.end(), "root") != keys.end());
118
119 pMapper->keys("root", keys);
120 assertTrue (keys.size() == 13);
121 assertTrue (std::find(keys.begin(), keys.end(), "prop5") != keys.end());
122
123 pMapper->keys("root.prop5", keys);
124 assertTrue (keys.size() == 4);
125 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
126 assertTrue (std::find(keys.begin(), keys.end(), "string2") != keys.end());
127 assertTrue (std::find(keys.begin(), keys.end(), "sub1") != keys.end());
128 assertTrue (std::find(keys.begin(), keys.end(), "sub2") != keys.end());
129
130 assertTrue (pMapper->getString("root.prop5.string1") == "foo");
131 assertTrue (pMapper->getString("root.prop5.sub1.string1") == "FOO");
132
133 pMapper->setString("root.prop5.string3", "baz");
134 assertTrue (pMapper->getString("root.prop5.string3") == "baz");
135 assertTrue (pConf->getString("prop5.string3") == "baz");
136
137 pMapper->remove("root.prop5.string3");
138 assertTrue (!pMapper->hasProperty("root.prop5.string3"));
139 assertTrue (!pConf->hasProperty("prop5.string3"));
140}
141
142
143void ConfigurationMapperTest::testMapper4()
144{
145 AutoPtr<AbstractConfiguration> pConf = createConfiguration();
146 AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("prop5", "", pConf);
147
148 assertTrue (pMapper->hasProperty("string1"));
149 assertTrue (pMapper->hasProperty("string2"));
150
151 AbstractConfiguration::Keys keys;
152 pMapper->keys(keys);
153 assertTrue (keys.size() == 4);
154 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
155 assertTrue (std::find(keys.begin(), keys.end(), "string2") != keys.end());
156 assertTrue (std::find(keys.begin(), keys.end(), "sub1") != keys.end());
157 assertTrue (std::find(keys.begin(), keys.end(), "sub2") != keys.end());
158
159 assertTrue (pMapper->getString("string1") == "foo");
160 assertTrue (pMapper->getString("sub1.string1") == "FOO");
161
162 pMapper->setString("string3", "baz");
163 assertTrue (pMapper->getString("string3") == "baz");
164 assertTrue (pConf->getString("prop5.string3") == "baz");
165
166 pMapper->remove("string3");
167 assertTrue (!pMapper->hasProperty("string3"));
168 assertTrue (!pConf->hasProperty("prop5.string3"));
169}
170
171
172AbstractConfiguration::Ptr ConfigurationMapperTest::allocConfiguration() const
173{
174 return new MapConfiguration;
175}
176
177
178void ConfigurationMapperTest::setUp()
179{
180}
181
182
183void ConfigurationMapperTest::tearDown()
184{
185}
186
187
188CppUnit::Test* ConfigurationMapperTest::suite()
189{
190 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ConfigurationMapperTest");
191
192 AbstractConfigurationTest_addTests(pSuite, ConfigurationMapperTest);
193 CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper1);
194 CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper2);
195 CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper3);
196 CppUnit_addTest(pSuite, ConfigurationMapperTest, testMapper4);
197
198 return pSuite;
199}
200