1//
2// PropertyListConfigurationTest.cpp
3//
4// Copyright (c) 2017-2018, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "PropertyListConfigurationTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Util/PropertyListConfiguration.h"
15#include "Poco/AutoPtr.h"
16#include "Poco/Exception.h"
17#include <sstream>
18
19
20using Poco::Util::PropertyListConfiguration;
21using Poco::Util::AbstractConfiguration;
22using Poco::AutoPtr;
23using Poco::NotImplementedException;
24using Poco::NotFoundException;
25
26
27PropertyListConfigurationTest::PropertyListConfigurationTest(const std::string& name): AbstractConfigurationTest(name)
28{
29}
30
31
32PropertyListConfigurationTest::~PropertyListConfigurationTest()
33{
34}
35
36
37void PropertyListConfigurationTest::testLoad()
38{
39 static const std::string pfileFile =
40 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
41 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
42 "<plist version=\"1.0\">\n"
43 "<dict>\n"
44 " <key>prop1</key>\n"
45 " <string>value1</string>\r\n"
46 " <key>prop2</key>\r\n"
47 " <integer>8080</integer>\r\n"
48 " <key>tree1</key>\n"
49 " <dict>\n"
50 " <key>date1</key>\r\n"
51 " <date>2017-06-21T09:42:33Z</date>\r\n"
52 " <key>prop3</key>\r\n"
53 " <false/>\r\n"
54 " <key>prop4</key>\r\n"
55 " <data>2g==</data>\r\n"
56 " </dict>\r\n"
57 "</dict>\r\n"
58 "</plist>\r\n";
59
60 std::istringstream istr(pfileFile);
61 AutoPtr<PropertyListConfiguration> pConf = new PropertyListConfiguration(istr);
62
63 assertTrue (pConf->getString("prop1") == "value1");
64 assertTrue (pConf->getString("prop2") == "8080");
65 assertTrue (pConf->getString("tree1.date1") == "2017-06-21T09:42:33Z");
66 assertTrue (pConf->getString("tree1.prop3") == "false");
67 std::stringstream buf;
68 pConf->getData("tree1.prop4", buf);
69 assertTrue (buf.str() == "\xda");
70
71 AbstractConfiguration::Keys keys;
72 pConf->keys(keys);
73 assertTrue (keys.size() == 3);
74 assertTrue (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
75 assertTrue (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
76 assertTrue (std::find(keys.begin(), keys.end(), "tree1") != keys.end());
77
78 pConf->keys("tree1", keys);
79 assertTrue (keys.size() == 3);
80 assertTrue (std::find(keys.begin(), keys.end(), "date1") != keys.end());
81 assertTrue (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
82 assertTrue (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
83
84 try
85 {
86 std::string s = pConf->getString("foo");
87 fail("No property - must throw");
88 }
89 catch (NotFoundException&)
90 {
91 }
92}
93
94void PropertyListConfigurationTest::testSave()
95{
96 AutoPtr<PropertyListConfiguration> pConf = new PropertyListConfiguration;
97
98 pConf->setString("prop1", "value1");
99 pConf->setInt("prop2", 42);
100 pConf->setString("prop3", "value\\1\txxx<");
101 pConf->setBool("prop4", true);
102 std::stringstream istr;
103 istr.write("#0", 2);
104 pConf->setData("prop5", istr);
105 pConf->setDouble("prop6", 3.14);
106 pConf->setString("prop7.prop8", "nestedvalue");
107
108 std::ostringstream ostr;
109 pConf->save(ostr);
110 std::string propFile = ostr.str();
111
112 assertTrue (propFile == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
113 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
114 "<plist version=\"1.0\">\n"
115 "\t<dict>\n"
116 "\t\t<key>prop1</key>\n"
117 "\t\t<string>value1</string>\n"
118 "\t\t<key>prop2</key>\n"
119 "\t\t<integer>42</integer>\n"
120 "\t\t<key>prop3</key>\n"
121 "\t\t<string>value\\1\txxx&lt;</string>\n"
122 "\t\t<key>prop4</key>\n"
123 "\t\t<true/>\n"
124 "\t\t<key>prop5</key>\n"
125 "\t\t<data>IzA=</data>\n"
126 "\t\t<key>prop6</key>\n"
127 "\t\t<real>3.14</real>\n"
128 "\t\t<key>prop7</key>\n"
129 "\t\t<dict>\n"
130 "\t\t\t<key>prop8</key>\n"
131 "\t\t\t<string>nestedvalue</string>\n"
132 "\t\t</dict>\n"
133 "\t</dict>\n"
134 "</plist>\n");
135}
136
137AbstractConfiguration::Ptr PropertyListConfigurationTest::allocConfiguration() const
138{
139 return new PropertyListConfiguration;
140}
141
142
143void PropertyListConfigurationTest::setUp()
144{
145}
146
147
148void PropertyListConfigurationTest::tearDown()
149{
150}
151
152
153CppUnit::Test* PropertyListConfigurationTest::suite()
154{
155 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PropertyListConfigurationTest");
156
157 AbstractConfigurationTest_addTests(pSuite, PropertyListConfigurationTest);
158 CppUnit_addTest(pSuite, PropertyListConfigurationTest, testLoad);
159 //CppUnit_addTest(pSuite, PropertyListConfigurationTest, testSave);
160
161 return pSuite;
162}
163