1//
2// XMLConfigurationTest.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 "XMLConfigurationTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/XMLConfiguration.h"
15#include "Poco/AutoPtr.h"
16#include "Poco/Exception.h"
17#include <sstream>
18#include <algorithm>
19
20
21using Poco::Util::XMLConfiguration;
22using Poco::Util::AbstractConfiguration;
23using Poco::AutoPtr;
24using Poco::NotImplementedException;
25using Poco::NotFoundException;
26
27
28XMLConfigurationTest::XMLConfigurationTest(const std::string& name): AbstractConfigurationTest(name)
29{
30}
31
32
33XMLConfigurationTest::~XMLConfigurationTest()
34{
35}
36
37
38void XMLConfigurationTest::testLoad()
39{
40 static const std::string xmlFile =
41 "<config>"
42 " <prop1>value1</prop1>"
43 " <prop2>value2</prop2>"
44 " <prop3>"
45 " <prop4 attr='value3'/>"
46 " <prop4 attr='value4'/>"
47 " </prop3>"
48 " <prop5 id='1'>value5</prop5>"
49 " <prop5 id='2'>value6</prop5>"
50 " <prop6 id='foo'>"
51 " <prop7>value7</prop7>"
52 " </prop6>"
53 " <prop6 id='bar'>"
54 " <prop7>value8</prop7>"
55 " </prop6>"
56 "</config>";
57
58 std::istringstream istr(xmlFile);
59 AutoPtr<XMLConfiguration> pConf = new XMLConfiguration(istr);
60
61 assertTrue (pConf->getString("prop1") == "value1");
62 assertTrue (pConf->getString("prop2") == "value2");
63 assertTrue (pConf->getString("prop3.prop4[@attr]") == "value3");
64 assertTrue (pConf->getString("prop3.prop4[1][@attr]") == "value4");
65 assertTrue (pConf->getString("prop5") == "value5");
66 assertTrue (pConf->getString("prop5[0]") == "value5");
67 assertTrue (pConf->getString("prop5[1]") == "value6");
68 assertTrue (pConf->getString("prop5[@id=1]") == "value5");
69 assertTrue (pConf->getString("prop5[@id='2']") == "value6");
70 assertTrue (pConf->getString("prop6[@id=foo].prop7") == "value7");
71 assertTrue (pConf->getString("prop6[@id='bar'].prop7") == "value8");
72
73 AbstractConfiguration::Keys keys;
74 pConf->keys(keys);
75 assertTrue (keys.size() == 7);
76 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
77 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
78 assertTrue (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
79 assertTrue (std::find(keys.begin(), keys.end(), "prop5") != keys.end());
80 assertTrue (std::find(keys.begin(), keys.end(), "prop5[1]") != keys.end());
81 assertTrue (std::find(keys.begin(), keys.end(), "prop6") != keys.end());
82 assertTrue (std::find(keys.begin(), keys.end(), "prop6[1]") != keys.end());
83
84 pConf->keys("prop3", keys);
85 assertTrue (keys.size() == 2);
86 assertTrue (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
87 assertTrue (std::find(keys.begin(), keys.end(), "prop4[1]") != keys.end());
88
89 assertTrue (pConf->hasProperty("prop3.prop4[@attr]"));
90 pConf->remove("prop3.prop4[@attr]");
91 assertTrue (!pConf->hasProperty("prop3.prop4[@attr]"));
92
93 assertTrue (pConf->hasProperty("prop3"));
94 pConf->remove("prop3");
95 assertTrue (!pConf->hasProperty("prop3"));
96
97 try
98 {
99 std::string s = pConf->getString("foo");
100 fail("No property - must throw");
101 }
102 catch (NotFoundException&)
103 {
104 }
105
106 try
107 {
108 std::string s = pConf->getString("prop5[@id='3']");
109 fail("No property - must throw");
110 }
111 catch (NotFoundException&)
112 {
113 }
114}
115
116
117void XMLConfigurationTest::testSave()
118{
119 AutoPtr<XMLConfiguration> pConf = new XMLConfiguration;
120 pConf->loadEmpty("config");
121
122 std::ostringstream ostr;
123 pConf->save(ostr);
124 std::string s(ostr.str());
125 assertTrue (s == "<config/>\n");
126
127 pConf->setString("prop1", "value1");
128 assertTrue (pConf->getString("prop1") == "value1");
129
130 pConf->setString("prop2", "value2");
131 assertTrue (pConf->getString("prop2") == "value2");
132
133 pConf->setString("prop3.prop4[@attr]", "value3");
134 assertTrue (pConf->getString("prop3.prop4[@attr]") == "value3");
135
136 pConf->setString("prop3.prop4[1][@attr]", "value4");
137 assertTrue (pConf->getString("prop3.prop4[1][@attr]") == "value4");
138
139 pConf->setString("prop5", "value5a");
140 assertTrue (pConf->getString("prop5") == "value5a");
141
142 pConf->setString("prop5[0]", "value5");
143 assertTrue (pConf->getString("prop5[0]") == "value5");
144 assertTrue (pConf->getString("prop5") == "value5");
145
146 pConf->setString("prop5[1]", "value6");
147 assertTrue (pConf->getString("prop5[1]") == "value6");
148
149 try
150 {
151 pConf->setString("prop5[3]", "value7");
152 fail("bad index - must throw");
153 }
154 catch (Poco::InvalidArgumentException&)
155 {
156 }
157
158 std::ostringstream ostr2;
159 pConf->save(ostr2);
160 s = ostr2.str();
161
162 assertTrue (s ==
163 "<config>\n"
164 "\t<prop1>value1</prop1>\n"
165 "\t<prop2>value2</prop2>\n"
166 "\t<prop3>\n"
167 "\t\t<prop4 attr=\"value3\"/>\n"
168 "\t\t<prop4 attr=\"value4\"/>\n"
169 "\t</prop3>\n"
170 "\t<prop5>value5</prop5>\n"
171 "\t<prop5>value6</prop5>\n"
172 "</config>\n");
173
174 pConf->setString("prop1", "value11");
175 assertTrue (pConf->getString("prop1") == "value11");
176
177 pConf->setString("prop2", "value21");
178 assertTrue (pConf->getString("prop2") == "value21");
179
180 pConf->setString("prop3.prop4[1][@attr]", "value41");
181 assertTrue (pConf->getString("prop3.prop4[1][@attr]") == "value41");
182
183 pConf->setString("prop3.prop4[2][@attr]", "value42");
184 assertTrue (pConf->getString("prop3.prop4[2][@attr]") == "value42");
185
186 std::ostringstream ostr3;
187 pConf->save(ostr3);
188 s = ostr3.str();
189 assertTrue (s ==
190 "<config>\n"
191 "\t<prop1>value11</prop1>\n"
192 "\t<prop2>value21</prop2>\n"
193 "\t<prop3>\n"
194 "\t\t<prop4 attr=\"value3\"/>\n"
195 "\t\t<prop4 attr=\"value41\"/>\n"
196 "\t\t<prop4 attr=\"value42\"/>\n"
197 "\t</prop3>\n"
198 "\t<prop5>value5</prop5>\n"
199 "\t<prop5>value6</prop5>\n"
200 "</config>\n");
201}
202
203
204void XMLConfigurationTest::testLoadAppendSave()
205{
206 AutoPtr<XMLConfiguration> pConf = new XMLConfiguration;
207 std::istringstream istr("<config>\n"
208 "\t<prop1>value1</prop1>\n"
209 "</config>\n");
210 pConf->load(istr);
211
212 pConf->setString("prop2", "value2");
213 assertTrue (pConf->getString("prop2") == "value2");
214
215 std::ostringstream ostr;
216 pConf->save(ostr);
217 std::string s(ostr.str());
218
219 assertTrue (s ==
220 "<config>\n"
221 "\t<prop1>value1</prop1>\n"
222 "\t<prop2>value2</prop2>\n"
223 "</config>\n");
224}
225
226
227void XMLConfigurationTest::testOtherDelimiter()
228{
229 static const std::string xmlFile =
230 "<config>"
231 " <prop1>value1</prop1>"
232 " <prop2>value2</prop2>"
233 " <prop3>"
234 " <prop4 attr='value3'/>"
235 " <prop4 attr='value4'/>"
236 " </prop3>"
237 " <prop5 id='1'>value5</prop5>"
238 " <prop5 id='2'>value6</prop5>"
239 " <prop6 id='foo'>"
240 " <prop7>value7</prop7>"
241 " </prop6>"
242 " <prop6 id='bar'>"
243 " <prop7>value8</prop7>"
244 " </prop6>"
245 "</config>";
246
247 std::istringstream istr(xmlFile);
248 AutoPtr<XMLConfiguration> pConf = new XMLConfiguration(istr, '/');
249
250 assertTrue (pConf->getString("prop1") == "value1");
251 assertTrue (pConf->getString("prop2") == "value2");
252 assertTrue (pConf->getString("prop3/prop4[@attr]") == "value3");
253 assertTrue (pConf->getString("prop3/prop4[1][@attr]") == "value4");
254 assertTrue (pConf->getString("prop5") == "value5");
255 assertTrue (pConf->getString("prop5[0]") == "value5");
256 assertTrue (pConf->getString("prop5[1]") == "value6");
257 assertTrue (pConf->getString("prop5[@id=1]") == "value5");
258 assertTrue (pConf->getString("prop5[@id='2']") == "value6");
259 assertTrue (pConf->getString("prop6[@id=foo]/prop7") == "value7");
260 assertTrue (pConf->getString("prop6[@id='bar']/prop7") == "value8");
261}
262
263
264AbstractConfiguration::Ptr XMLConfigurationTest::allocConfiguration() const
265{
266 XMLConfiguration* pConfig = new XMLConfiguration();
267 pConfig->loadEmpty("TestConfiguration");
268
269 return pConfig;
270}
271
272
273void XMLConfigurationTest::testSaveEmpty()
274{
275 Poco::AutoPtr<XMLConfiguration> pConfig = new XMLConfiguration;
276 std::ostringstream ostr;
277 pConfig->save(ostr);
278 assertTrue (ostr.str() == "<config/>\n");
279}
280
281
282void XMLConfigurationTest::testFromScratch()
283{
284 Poco::AutoPtr<XMLConfiguration> pConfig = new XMLConfiguration;
285 pConfig->setString("foo", "bar");
286 std::ostringstream ostr;
287 pConfig->save(ostr);
288 assertTrue (ostr.str() == "<config>\n\t<foo>bar</foo>\n</config>\n");
289}
290
291
292void XMLConfigurationTest::testLoadEmpty()
293{
294 Poco::AutoPtr<XMLConfiguration> pConfig = new XMLConfiguration;
295 pConfig->loadEmpty("AppConfig");
296 pConfig->setString("foo", "bar");
297 std::ostringstream ostr;
298 pConfig->save(ostr);
299 assertTrue (ostr.str() == "<AppConfig>\n\t<foo>bar</foo>\n</AppConfig>\n");
300}
301
302
303void XMLConfigurationTest::setUp()
304{
305}
306
307
308void XMLConfigurationTest::tearDown()
309{
310}
311
312
313CppUnit::Test* XMLConfigurationTest::suite()
314{
315 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("XMLConfigurationTest");
316
317 AbstractConfigurationTest_addTests(pSuite, XMLConfigurationTest);
318 CppUnit_addTest(pSuite, XMLConfigurationTest, testLoad);
319 CppUnit_addTest(pSuite, XMLConfigurationTest, testSave);
320 CppUnit_addTest(pSuite, XMLConfigurationTest, testLoadAppendSave);
321 CppUnit_addTest(pSuite, XMLConfigurationTest, testOtherDelimiter);
322 CppUnit_addTest(pSuite, XMLConfigurationTest, testSaveEmpty);
323 CppUnit_addTest(pSuite, XMLConfigurationTest, testFromScratch);
324 CppUnit_addTest(pSuite, XMLConfigurationTest, testLoadEmpty);
325
326 return pSuite;
327}
328