1//
2// IniFileConfigurationTest.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 "IniFileConfigurationTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/IniFileConfiguration.h"
15#include "Poco/AutoPtr.h"
16#include "Poco/Exception.h"
17#include <sstream>
18#include <algorithm>
19
20
21using Poco::Util::IniFileConfiguration;
22using Poco::Util::AbstractConfiguration;
23using Poco::AutoPtr;
24using Poco::NotImplementedException;
25using Poco::NotFoundException;
26
27
28IniFileConfigurationTest::IniFileConfigurationTest(const std::string& name): AbstractConfigurationTest(name)
29{
30}
31
32
33IniFileConfigurationTest::~IniFileConfigurationTest()
34{
35}
36
37
38void IniFileConfigurationTest::testLoad()
39{
40 static const std::string iniFile =
41 "; comment\n"
42 " ; comment \n"
43 "prop1=value1\n"
44 " prop2 = value2 \n"
45 "[section1]\n"
46 "prop1 = value3\r\n"
47 "\tprop2=value4\r\n"
48 ";prop3=value7\r\n"
49 "\n"
50 " [ section 2 ]\n"
51 "prop1 = value 5\n"
52 "\t \n"
53 "Prop2 = value6";
54
55 std::istringstream istr(iniFile);
56 AutoPtr<IniFileConfiguration> pConf = new IniFileConfiguration(istr);
57
58 assertTrue (pConf->getString("prop1") == "value1");
59 assertTrue (pConf->getString("prop2") == "value2");
60 assertTrue (pConf->getString("section1.prop1") == "value3");
61 assertTrue (pConf->getString("Section1.Prop2") == "value4");
62 assertTrue (pConf->getString("section 2.prop1") == "value 5");
63 assertTrue (pConf->getString("section 2.prop2") == "value6");
64 assertTrue (pConf->getString("SECTION 2.PROP2") == "value6");
65
66 AbstractConfiguration::Keys keys;
67 pConf->keys(keys);
68 assertTrue (keys.size() == 4);
69 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
70 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
71 assertTrue (std::find(keys.begin(), keys.end(), "section1") != keys.end());
72 assertTrue (std::find(keys.begin(), keys.end(), "section 2") != keys.end());
73
74 pConf->keys("Section1", keys);
75 assertTrue (keys.size() == 2);
76 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
77 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
78
79 pConf->setString("prop1", "value11");
80 assertTrue (pConf->getString("PROP1") == "value11");
81 pConf->setString("Prop1", "value12");
82 assertTrue (pConf->getString("prop1") == "value12");
83 pConf->keys(keys);
84 assertTrue (keys.size() == 4);
85 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
86 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
87 assertTrue (std::find(keys.begin(), keys.end(), "section1") != keys.end());
88 assertTrue (std::find(keys.begin(), keys.end(), "section 2") != keys.end());
89 assertTrue (std::find(keys.begin(), keys.end(), "Prop1") == keys.end());
90}
91
92
93void IniFileConfigurationTest::testCaseInsensitiveRemove()
94{
95 AutoPtr<AbstractConfiguration> pConf = createConfiguration();
96 AbstractConfiguration::Keys keys;
97
98 assertTrue (pConf->hasProperty("Prop1"));
99 assertTrue (pConf->hasProperty("prop4.BOOL1"));
100 assertTrue (pConf->hasProperty("Prop4.bool2"));
101 assertTrue (pConf->hasProperty("PROP4.Bool3"));
102 pConf->keys(keys);
103 assertTrue (keys.size() == 13);
104 pConf->keys("prop4", keys);
105 assertTrue (keys.size() == 17);
106
107 pConf->remove("PROP4.Bool1");
108 assertTrue (pConf->hasProperty("Prop1"));
109 assertTrue (!pConf->hasProperty("prop4.BOOL1"));
110 assertTrue (pConf->hasProperty("Prop4.bool2"));
111 assertTrue (pConf->hasProperty("PROP4.Bool3"));
112 pConf->keys(keys);
113 assertTrue (keys.size() == 13);
114 pConf->keys("PROP4", keys);
115 assertTrue (keys.size() == 16);
116
117 pConf->remove("Prop4");
118 assertTrue (pConf->hasProperty("Prop1"));
119 assertTrue (!pConf->hasProperty("prop4.BOOL1"));
120 assertTrue (!pConf->hasProperty("Prop4.bool2"));
121 assertTrue (!pConf->hasProperty("PROP4.Bool3"));
122 pConf->keys(keys);
123 assertTrue (keys.size() == 12);
124 pConf->keys("prop4", keys);
125 assertTrue (keys.size() == 0);
126}
127
128
129AbstractConfiguration::Ptr IniFileConfigurationTest::allocConfiguration() const
130{
131 return new IniFileConfiguration;
132}
133
134
135void IniFileConfigurationTest::setUp()
136{
137}
138
139
140void IniFileConfigurationTest::tearDown()
141{
142}
143
144
145CppUnit::Test* IniFileConfigurationTest::suite()
146{
147 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("IniFileConfigurationTest");
148
149 AbstractConfigurationTest_addTests(pSuite, IniFileConfigurationTest);
150 CppUnit_addTest(pSuite, IniFileConfigurationTest, testLoad);
151 CppUnit_addTest(pSuite, IniFileConfigurationTest, testCaseInsensitiveRemove);
152
153 return pSuite;
154}
155