1//
2// FilesystemConfigurationTest.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 "FilesystemConfigurationTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/FilesystemConfiguration.h"
15#include "Poco/AutoPtr.h"
16#include "Poco/File.h"
17#include <algorithm>
18
19
20using Poco::Util::FilesystemConfiguration;
21using Poco::Util::AbstractConfiguration;
22using Poco::AutoPtr;
23
24
25FilesystemConfigurationTest::FilesystemConfigurationTest(const std::string& name): AbstractConfigurationTest(name),
26 _path("TestConfiguration")
27{
28}
29
30
31FilesystemConfigurationTest::~FilesystemConfigurationTest()
32{
33}
34
35
36void FilesystemConfigurationTest::testFilesystemConfiguration()
37{
38 AutoPtr<FilesystemConfiguration> config = new FilesystemConfiguration(_path.toString());
39
40 config->setString("logging.loggers.root.channel.class", "ConsoleChannel");
41 config->setString("logging.loggers.app.name", "Application");
42 config->setString("logging.loggers.app.channel", "c1");
43 config->setString("logging.formatters.f1.class", "PatternFormatter");
44 config->setString("logging.formatters.f1.pattern", "[%p] %t");
45 config->setString("logging.channels.c1.class", "ConsoleChannel");
46
47 assertTrue (config->getString("logging.loggers.root.channel.class") == "ConsoleChannel");
48 assertTrue (config->getString("logging.loggers.app.name") == "Application");
49 assertTrue (config->getString("logging.loggers.app.channel") == "c1");
50 assertTrue (config->getString("logging.formatters.f1.class") == "PatternFormatter");
51 assertTrue (config->getString("logging.formatters.f1.pattern") == "[%p] %t");
52
53 config->setString("logging.loggers.app.channel", "c2");
54 assertTrue (config->getString("logging.loggers.app.channel") == "c2");
55
56 AbstractConfiguration::Keys keys;
57 config->keys(keys);
58 assertTrue (keys.size() == 1);
59 assertTrue (std::find(keys.begin(), keys.end(), "logging") != keys.end());
60
61 config->keys("logging", keys);
62 assertTrue (keys.size() == 3);
63 assertTrue (std::find(keys.begin(), keys.end(), "loggers") != keys.end());
64 assertTrue (std::find(keys.begin(), keys.end(), "formatters") != keys.end());
65 assertTrue (std::find(keys.begin(), keys.end(), "channels") != keys.end());
66
67 config->keys("logging.formatters", keys);
68 assertTrue (keys.size() == 1);
69 assertTrue (std::find(keys.begin(), keys.end(), "f1") != keys.end());
70
71 config->keys("logging.formatters.f1", keys);
72 assertTrue (keys.size() == 2);
73 assertTrue (std::find(keys.begin(), keys.end(), "class") != keys.end());
74 assertTrue (std::find(keys.begin(), keys.end(), "pattern") != keys.end());
75
76 assertTrue (config->hasProperty("logging.loggers.root.channel.class"));
77 config->clear();
78 assertTrue (!config->hasProperty("logging.loggers.root.channel.class"));
79}
80
81
82AbstractConfiguration::Ptr FilesystemConfigurationTest::allocConfiguration() const
83{
84 return new FilesystemConfiguration(_path.toString());
85}
86
87
88void FilesystemConfigurationTest::setUp()
89{
90 Poco::File dir(_path);
91 if (dir.exists()) {
92 dir.remove(true);
93 }
94}
95
96
97void FilesystemConfigurationTest::tearDown()
98{
99 Poco::File dir(_path);
100 if (dir.exists()) {
101 dir.remove(true);
102 }
103}
104
105
106CppUnit::Test* FilesystemConfigurationTest::suite()
107{
108 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FilesystemConfigurationTest");
109
110 AbstractConfigurationTest_addTests(pSuite, FilesystemConfigurationTest);
111 CppUnit_addTest(pSuite, FilesystemConfigurationTest, testFilesystemConfiguration);
112
113 return pSuite;
114}
115