1 //
2 // JSONConfigurationTest.cpp
3 //
4 // Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH.
5 // and Contributors.
6 //
7 // SPDX-License-Identifier: BSL-1.0
8 //
9
10
11 #include "JSONConfigurationTest.h"
12 #include "CppUnit/TestCaller.h"
13 #include "CppUnit/TestSuite.h"
14 #include "Poco/Util/JSONConfiguration.h"
15 #include "Poco/JSON/JSONException.h"
16
17
18 using Poco::Util::JSONConfiguration;
19 using Poco::Util::AbstractConfiguration;
20 using Poco::AutoPtr;
21 using Poco::NotImplementedException;
22 using Poco::NotFoundException;
23 using Poco::JSON::JSONException;
24
25
JSONConfigurationTest(const std::string & name)26 JSONConfigurationTest::JSONConfigurationTest(const std::string& name) : AbstractConfigurationTest(name)
27 {
28 }
29
30
~JSONConfigurationTest()31 JSONConfigurationTest::~JSONConfigurationTest()
32 {
33 }
34
35
testLoad()36 void JSONConfigurationTest::testLoad()
37 {
38 JSONConfiguration config;
39
40 std::string json = "{ \"config\" : "
41 " { \"prop1\" : \"value1\", "
42 " \"prop2\" : 10, "
43 " \"prop3\" : [ \"element1\", \"element2\" ], "
44 " \"prop4\" : { \"prop5\" : false, "
45 " \"prop6\" : null } "
46 " }"
47 "}";
48
49 std::istringstream iss(json);
50 try
51 {
52 config.load(iss);
53 }
54 catch(JSONException jsone)
55 {
56 std::cout << jsone.message() << std::endl;
57 assertTrue (false);
58 }
59
60 std::string property1 = config.getString("config.prop1");
61 assertTrue (property1.compare("value1") == 0);
62
63 int property2 = config.getInt("config.prop2");
64 assertTrue (property2 == 10);
65
66 int nonExistingProperty = config.getInt("config.prop7", 5);
67 assertTrue (nonExistingProperty == 5);
68
69 std::string arrProperty = config.getString("config.prop3[1]");
70 assertTrue (arrProperty.compare("element2") == 0);
71
72 bool property35 = config.getBool("config.prop4.prop5");
73 assertTrue (! property35);
74
75 try
76 {
77 config.getString("propertyUnknown");
78 assertTrue (true);
79 }
80 catch(NotFoundException nfe)
81 {
82 }
83 }
84
85
testSetArrayElement()86 void JSONConfigurationTest::testSetArrayElement()
87 {
88 JSONConfiguration config;
89
90 std::string json = "{ \"config\" : "
91 " { \"prop1\" : \"value1\", "
92 " \"prop2\" : 10, "
93 " \"prop3\" : [ \"element1\", \"element2\" ], "
94 " \"prop4\" : { \"prop5\" : false, "
95 " \"prop6\" : null } "
96 " }"
97 "}";
98
99 std::istringstream iss(json);
100 config.load(iss);
101
102 // config.prop3[0] = "foo"
103 config.setString("config.prop3[0]", "foo");
104 assertTrue (config.getString("config.prop3[0]") == "foo");
105
106 // config.prop3[1] = "bar"
107 config.setString("config.prop3[1]", "bar");
108 assertTrue (config.getString("config.prop3[1]") == "bar");
109
110 // config.prop3[3] = "baz"
111 config.setString("config.prop3[3]", "baz");
112 assertTrue (config.getString("config.prop3[3]") == "baz");
113 }
114
115
allocConfiguration() const116 AbstractConfiguration::Ptr JSONConfigurationTest::allocConfiguration() const
117 {
118 return new JSONConfiguration;
119 }
120
121
setUp()122 void JSONConfigurationTest::setUp()
123 {
124 }
125
126
tearDown()127 void JSONConfigurationTest::tearDown()
128 {
129 }
130
131
suite()132 CppUnit::Test* JSONConfigurationTest::suite()
133 {
134 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONConfigurationTest");
135
136 AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest);
137 CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad);
138 CppUnit_addTest(pSuite, JSONConfigurationTest, testSetArrayElement);
139
140 return pSuite;
141 }
142