1//
2// PropertyFileConfigurationTest.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 "PropertyFileConfigurationTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/PropertyFileConfiguration.h"
15#include "Poco/AutoPtr.h"
16#include "Poco/Exception.h"
17#include <sstream>
18#include <algorithm>
19
20
21using Poco::Util::PropertyFileConfiguration;
22using Poco::Util::AbstractConfiguration;
23using Poco::AutoPtr;
24using Poco::NotFoundException;
25
26
27PropertyFileConfigurationTest::PropertyFileConfigurationTest(const std::string& name): AbstractConfigurationTest(name)
28{
29}
30
31
32PropertyFileConfigurationTest::~PropertyFileConfigurationTest()
33{
34}
35
36
37void PropertyFileConfigurationTest::testLoad()
38{
39 testLoad(false);
40}
41
42
43void PropertyFileConfigurationTest::testLoadEmpty()
44{
45 static const std::string propFile = " ";
46
47 std::istringstream istr(propFile);
48 AutoPtr<PropertyFileConfiguration> pConf = new PropertyFileConfiguration(istr);
49
50 AbstractConfiguration::Keys keys;
51 pConf->keys(keys);
52 assertTrue (keys.size() == 0);
53}
54
55
56void PropertyFileConfigurationTest::testLoadWithPreserveComment()
57{
58 testLoad(true);
59}
60
61
62void PropertyFileConfigurationTest::testLoad(bool preserveComment)
63{
64 static const std::string propFile =
65 "! comment\n"
66 "! comment\n"
67 "prop1=value1\n"
68 "prop2 = value2 \n"
69 "prop3.prop31: value3\n"
70 "# comment\n"
71 " prop3.prop32: value 4\n"
72 "prop3.prop33: value5, value6, \\\n"
73 "value7, value8, \\\r\n"
74 "value9\n"
75 "prop4 = escaped[\\t\\r\\n\\f]\n"
76 "#prop4 = foo\n"
77 "prop5:foo";
78
79 std::istringstream istr(propFile);
80 AutoPtr<PropertyFileConfiguration> pConf = new PropertyFileConfiguration(istr, preserveComment);
81
82 assertTrue (pConf->getString("prop1") == "value1");
83 assertTrue (pConf->getString("prop2") == "value2");
84 assertTrue (pConf->getString("prop3.prop31") == "value3");
85 assertTrue (pConf->getString("prop3.prop32") == "value 4");
86 assertTrue (pConf->getString("prop3.prop33") == "value5, value6, value7, value8, value9");
87 assertTrue (pConf->getString("prop4") == "escaped[\t\r\n\f]");
88 assertTrue (pConf->getString("prop5") == "foo");
89
90 AbstractConfiguration::Keys keys;
91 pConf->keys(keys);
92 assertTrue (keys.size() == 5);
93 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
94 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
95 assertTrue (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
96 assertTrue (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
97 assertTrue (std::find(keys.begin(), keys.end(), "prop5") != keys.end());
98
99 pConf->keys("prop3", keys);
100 assertTrue (keys.size() == 3);
101 assertTrue (std::find(keys.begin(), keys.end(), "prop31") != keys.end());
102 assertTrue (std::find(keys.begin(), keys.end(), "prop32") != keys.end());
103 assertTrue (std::find(keys.begin(), keys.end(), "prop33") != keys.end());
104
105 try
106 {
107 std::string s = pConf->getString("foo");
108 fail("No property - must throw");
109 }
110 catch (NotFoundException&)
111 {
112 }
113}
114
115
116void PropertyFileConfigurationTest::testSave()
117{
118 AutoPtr<PropertyFileConfiguration> pConf = new PropertyFileConfiguration;
119
120 pConf->setString("prop1", "value1");
121 pConf->setInt("prop2", 42);
122 pConf->setString("prop3", "value\\1\txxx");
123
124 std::ostringstream ostr;
125 pConf->save(ostr);
126 std::string propFile = ostr.str();
127 assertTrue (propFile == "prop1: value1\n"
128 "prop2: 42\n"
129 "prop3: value\\\\1\\txxx\n");
130}
131
132void PropertyFileConfigurationTest::testLoadSaveWithPreserveComment()
133{
134 std::string propFile =
135 "! comment #\n"
136 "prop1=value1\n"
137 "# comment #\n"
138 "# comment !\n"
139 "prop2 = value2 \n"
140 "! comment !\n"
141 "prop3:foo\n"
142 "prop4";
143
144 std::istringstream istr(propFile);
145 AutoPtr<PropertyFileConfiguration> pConf = new PropertyFileConfiguration(istr, true);
146
147 std::ostringstream ostr;
148 pConf->save(ostr);
149 assertEqual ("! comment #\n"
150 "prop1: value1\n"
151 "# comment #\n"
152 "# comment !\n"
153 "prop2: value2\n"
154 "! comment !\n"
155 "prop3: foo\n"
156 "prop4: \n",
157 ostr.str());
158
159 pConf->setString("prop4", "value4");
160 ostr.clear();
161 ostr.str("");
162 pConf->save(ostr);
163 assertEqual ("! comment #\n"
164 "prop1: value1\n"
165 "# comment #\n"
166 "# comment !\n"
167 "prop2: value2\n"
168 "! comment !\n"
169 "prop3: foo\n"
170 "prop4: value4\n",
171 ostr.str());
172
173 pConf->remove("prop2");
174 ostr.clear();
175 ostr.str("");
176 pConf->save(ostr);
177 assertEqual ("! comment #\n"
178 "prop1: value1\n"
179 "# comment #\n"
180 "# comment !\n"
181 "! comment !\n"
182 "prop3: foo\n"
183 "prop4: value4\n",
184 ostr.str());
185
186 pConf->setString("prop4", "value5");
187 ostr.clear();
188 ostr.str("");
189 pConf->save(ostr);
190 assertEqual ("! comment #\n"
191 "prop1: value1\n"
192 "# comment #\n"
193 "# comment !\n"
194 "! comment !\n"
195 "prop3: foo\n"
196 "prop4: value5\n",
197 ostr.str());
198}
199
200
201AbstractConfiguration::Ptr PropertyFileConfigurationTest::allocConfiguration() const
202{
203 return new PropertyFileConfiguration;
204}
205
206
207void PropertyFileConfigurationTest::setUp()
208{
209}
210
211
212void PropertyFileConfigurationTest::tearDown()
213{
214}
215
216
217CppUnit::Test* PropertyFileConfigurationTest::suite()
218{
219 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PropertyFileConfigurationTest");
220
221 AbstractConfigurationTest_addTests(pSuite, PropertyFileConfigurationTest);
222 CppUnit_addTest(pSuite, PropertyFileConfigurationTest, testLoad);
223 CppUnit_addTest(pSuite, PropertyFileConfigurationTest, testLoadEmpty);
224 CppUnit_addTest(pSuite, PropertyFileConfigurationTest, testSave);
225 CppUnit_addTest(pSuite, PropertyFileConfigurationTest, testLoadWithPreserveComment);
226 CppUnit_addTest(pSuite, PropertyFileConfigurationTest, testLoadSaveWithPreserveComment);
227
228 return pSuite;
229}
230