1//
2// ConfigurationViewTest.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 "ConfigurationViewTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/MapConfiguration.h"
15#include "Poco/AutoPtr.h"
16#include "Poco/Exception.h"
17#include <algorithm>
18
19
20using Poco::Util::AbstractConfiguration;
21using Poco::Util::MapConfiguration;
22using Poco::AutoPtr;
23
24
25ConfigurationViewTest::ConfigurationViewTest(const std::string& name): AbstractConfigurationTest(name)
26{
27}
28
29
30ConfigurationViewTest::~ConfigurationViewTest()
31{
32}
33
34
35void ConfigurationViewTest::testView()
36{
37 AbstractConfiguration::Ptr pConf = createConfiguration();
38 AbstractConfiguration::Ptr pView = pConf->createView("");
39 assertTrue (pView->hasProperty("prop1"));
40 assertTrue (pView->hasProperty("prop2"));
41
42 AbstractConfiguration::Keys keys;
43 pView->keys(keys);
44 assertTrue (keys.size() == 13);
45 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
46 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
47 assertTrue (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
48 assertTrue (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
49
50 assertTrue (pView->getString("prop1") == "foo");
51 assertTrue (pView->getString("prop3.string1") == "foo");
52
53 pView->setString("prop6", "foobar");
54 assertTrue (pConf->getString("prop6") == "foobar");
55
56 pView = pConf->createView("prop1");
57 pView->keys(keys);
58 assertTrue (keys.empty());
59 assertTrue (pView->hasProperty("prop1"));
60
61 pView->setString("prop11", "foobar");
62 assertTrue (pConf->getString("prop1.prop11") == "foobar");
63
64 pView = pConf->createView("prop3");
65 pView->keys(keys);
66 assertTrue (keys.size() == 2);
67 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
68 assertTrue (std::find(keys.begin(), keys.end(), "string2") != keys.end());
69
70 assertTrue (pView->getString("string1") == "foo");
71 assertTrue (pView->getString("string2") == "bar");
72
73 pView->setString("string3", "foobar");
74 assertTrue (pConf->getString("prop3.string3") == "foobar");
75
76 pView = pConf->createView("prop5");
77 pView->keys(keys);
78 assertTrue (keys.size() == 4);
79 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
80 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
81 assertTrue (std::find(keys.begin(), keys.end(), "sub1") != keys.end());
82 assertTrue (std::find(keys.begin(), keys.end(), "sub2") != keys.end());
83
84 assertTrue (pView->getString("sub1.string1") == "FOO");
85 assertTrue (pView->getString("sub2.string2") == "Bar");
86
87 pView = pConf->createView("prop5.sub1");
88 pView->keys(keys);
89 assertTrue (keys.size() == 2);
90 assertTrue (std::find(keys.begin(), keys.end(), "string1") != keys.end());
91 assertTrue (std::find(keys.begin(), keys.end(), "string2") != keys.end());
92
93 assertTrue (pView->getString("string1") == "FOO");
94 assertTrue (pView->getString("string2") == "BAR");
95
96 pView->setString("string3", "foobar");
97 assertTrue (pConf->getString("prop5.sub1.string3") == "foobar");
98
99 pView->remove("string3");
100 assertTrue (!pConf->hasProperty("prop5.sub1.string3"));
101}
102
103
104AbstractConfiguration::Ptr ConfigurationViewTest::allocConfiguration() const
105{
106 return new MapConfiguration;
107}
108
109
110void ConfigurationViewTest::setUp()
111{
112}
113
114
115void ConfigurationViewTest::tearDown()
116{
117}
118
119
120CppUnit::Test* ConfigurationViewTest::suite()
121{
122 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ConfigurationViewTest");
123
124 AbstractConfigurationTest_addTests(pSuite, ConfigurationViewTest);
125 CppUnit_addTest(pSuite, ConfigurationViewTest, testView);
126
127 return pSuite;
128}
129