Debian Codegraph @

olad/plugin_api/PreferencesTest.cpp

raw
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 * PreferencesTest.cpp
17 * Test fixture for the Preferences classes
18 * Copyright (C) 2005 Simon Newton
19 */
20
21 #include <cppunit/extensions/HelperMacros.h>
22 #include <set>
23 #include <string>
24 #include <vector>
25
26 #include "ola/Logging.h"
27 #include "ola/StringUtils.h"
28 #include "olad/Preferences.h"
29 #include "ola/testing/TestUtils.h"
30
31
32 using ola::BoolValidator;
33 using ola::FileBackedPreferences;
34 using ola::FileBackedPreferencesFactory;
35 using ola::IntToString;
36 using ola::IntValidator;
37 using ola::UIntValidator;
38 using ola::MemoryPreferencesFactory;
39 using ola::Preferences;
40 using ola::SetValidator;
41 using ola::StringValidator;
42 using ola::IPv4Validator;
43 using std::string;
44 using std::vector;
45
46
47 class PreferencesTest: public CppUnit::TestFixture {
48 CPPUNIT_TEST_SUITE(PreferencesTest);
49 CPPUNIT_TEST(testValidators);
50 CPPUNIT_TEST(testGetSetRemove);
51 CPPUNIT_TEST(testBool);
52 CPPUNIT_TEST(testFactory);
53 CPPUNIT_TEST(testLoad);
54 CPPUNIT_TEST(testSave);
55 CPPUNIT_TEST_SUITE_END();
56
57 public:
58 void setUp() {
59 ola::InitLogging(ola::OLA_LOG_DEBUG, ola::OLA_LOG_STDERR);
60 }
61 void testValidators();
62 void testGetSetRemove();
63 void testBool();
64 void testFactory();
65 void testLoad();
66 void testSave();
67 };
68
69
70 CPPUNIT_TEST_SUITE_REGISTRATION(PreferencesTest);
71
72 /*
73 * Check the validators work
74 */
75 void PreferencesTest::testValidators() {
76 StringValidator string_validator;
77 OLA_ASSERT(string_validator.IsValid("foo"));
78 OLA_ASSERT_FALSE(string_validator.IsValid(""));
79
80 std::set<string> values;
81 values.insert("one");
82 values.insert("two");
83 SetValidator<string> set_validator(values);
84 OLA_ASSERT(set_validator.IsValid("one"));
85 OLA_ASSERT(set_validator.IsValid("two"));
86 OLA_ASSERT_FALSE(set_validator.IsValid("zero"));
87 OLA_ASSERT_FALSE(set_validator.IsValid("three"));
88
89 std::set<unsigned int> values2;
90 values2.insert(1);
91 values2.insert(3);
92 SetValidator<unsigned int> set_validator2(values2);
93 OLA_ASSERT(set_validator2.IsValid("1"));
94 OLA_ASSERT(set_validator2.IsValid("3"));
95 OLA_ASSERT_FALSE(set_validator2.IsValid("0"));
96 OLA_ASSERT_FALSE(set_validator2.IsValid("2"));
97 OLA_ASSERT_FALSE(set_validator2.IsValid("4"));
98
99 // a string validator that allows empty strings
100 StringValidator string_validator2(true);
101 OLA_ASSERT(string_validator2.IsValid("foo"));
102 OLA_ASSERT(string_validator2.IsValid(""));
103
104 BoolValidator bool_validator;
105 OLA_ASSERT(bool_validator.IsValid("true"));
106 OLA_ASSERT(bool_validator.IsValid("false"));
107 OLA_ASSERT_FALSE(bool_validator.IsValid(""));
108
109 IntValidator int_validator(-3, 4);
110 OLA_ASSERT(int_validator.IsValid("-3"));
111 OLA_ASSERT(int_validator.IsValid("0"));
112 OLA_ASSERT(int_validator.IsValid("4"));
113 OLA_ASSERT_FALSE(int_validator.IsValid("-4"));
114 OLA_ASSERT_FALSE(int_validator.IsValid("5"));
115
116 UIntValidator uint_validator(10, 14);
117 OLA_ASSERT(uint_validator.IsValid("10"));
118 OLA_ASSERT(uint_validator.IsValid("14"));
119 OLA_ASSERT_FALSE(uint_validator.IsValid("0"));
120 OLA_ASSERT_FALSE(uint_validator.IsValid("9"));
121 OLA_ASSERT_FALSE(uint_validator.IsValid("15"));
122
123 IPv4Validator ipv4_validator; // empty ok
124 OLA_ASSERT(ipv4_validator.IsValid(""));
125 OLA_ASSERT(ipv4_validator.IsValid("1.2.3.4"));
126 OLA_ASSERT(ipv4_validator.IsValid("10.0.255.1"));
127 OLA_ASSERT_FALSE(ipv4_validator.IsValid("foo"));
128 OLA_ASSERT_FALSE(ipv4_validator.IsValid("1.2.3"));
129 OLA_ASSERT_FALSE(ipv4_validator.IsValid("1.2.3.4.5"));
130 OLA_ASSERT_FALSE(ipv4_validator.IsValid("1.f00.3.4"));
131
132 IPv4Validator ipv4_validator2(false); // empty not ok
133 OLA_ASSERT_FALSE(ipv4_validator2.IsValid(""));
134 }
135
136
137 /*
138 * Check that we can get/set the preferences
139 */
140 void PreferencesTest::testGetSetRemove() {
141 MemoryPreferencesFactory factory;
142 Preferences *preferences = factory.NewPreference("dummy");
143
144 string key1 = "foo";
145 string key2 = "bat";
146 string value1 = "bar";
147 string value2 = "baz";
148 unsigned int value3 = 1;
149 unsigned int value4 = 2;
150 int value5 = 3;
151 int value6 = 4;
152 const char value7[] = "bar";
153 const char value8[] = "baz";
154
155 // test get/set/has single values string
156 OLA_ASSERT_EQ(string(""), preferences->GetValue(key1));
157 preferences->SetValue(key1, value1);
158 OLA_ASSERT_EQ(value1, preferences->GetValue(key1));
159 OLA_ASSERT(preferences->HasKey(key1));
160 preferences->SetValue(key1, value2);
161 OLA_ASSERT_EQ(value2, preferences->GetValue(key1));
162
163 preferences->RemoveValue(key1);
164 OLA_ASSERT_EQ(string(""), preferences->GetValue(key1));
165 OLA_ASSERT_FALSE(preferences->HasKey(key1));
166
167 // test get/set/has single values uint
168 OLA_ASSERT_EQ(string(""), preferences->GetValue(key1));
169 preferences->SetValue(key1, value3);
170 OLA_ASSERT_EQ(IntToString(value3), preferences->GetValue(key1));
171 OLA_ASSERT(preferences->HasKey(key1));
172 preferences->SetValue(key1, value4);
173 OLA_ASSERT_EQ(IntToString(value4), preferences->GetValue(key1));
174
175 preferences->RemoveValue(key1);
176 OLA_ASSERT_EQ(string(""), preferences->GetValue(key1));
177 OLA_ASSERT_FALSE(preferences->HasKey(key1));
178
179 // test get/set/has single values int
180 OLA_ASSERT_EQ(string(""), preferences->GetValue(key1));
181 preferences->SetValue(key1, value5);
182 OLA_ASSERT_EQ(IntToString(value5), preferences->GetValue(key1));
183 OLA_ASSERT(preferences->HasKey(key1));
184 preferences->SetValue(key1, value6);
185 OLA_ASSERT_EQ(IntToString(value6), preferences->GetValue(key1));
186
187 preferences->RemoveValue(key1);
188 OLA_ASSERT_EQ(string(""), preferences->GetValue(key1));
189 OLA_ASSERT_FALSE(preferences->HasKey(key1));
190
191 vector<string> values;
192 // test get/set multiple value string
193 values = preferences->GetMultipleValue(key2);
194 OLA_ASSERT_EQ((size_t) 0, values.size());
195 preferences->SetMultipleValue(key2, value1);
196 values = preferences->GetMultipleValue(key2);
197 OLA_ASSERT(preferences->HasKey(key2));
198 OLA_ASSERT_EQ((size_t) 1, values.size());
199 OLA_ASSERT_EQ(value1, values.at(0));
200 preferences->SetMultipleValue(key2, value2);
201 values = preferences->GetMultipleValue(key2);
202 OLA_ASSERT_EQ((size_t) 2, values.size());
203 OLA_ASSERT_EQ(value1, values.at(0));
204 OLA_ASSERT_EQ(value2, values.at(1));
205 preferences->RemoveValue(key2);
206
207 // test get/set multiple value uint
208 values = preferences->GetMultipleValue(key2);
209 OLA_ASSERT_EQ((size_t) 0, values.size());
210 preferences->SetMultipleValue(key2, value3);
211 values = preferences->GetMultipleValue(key2);
212 OLA_ASSERT(preferences->HasKey(key2));
213 OLA_ASSERT_EQ((size_t) 1, values.size());
214 OLA_ASSERT_EQ(IntToString(value3), values.at(0));
215 preferences->SetMultipleValue(key2, value4);
216 values = preferences->GetMultipleValue(key2);
217 OLA_ASSERT_EQ((size_t) 2, values.size());
218 OLA_ASSERT_EQ(IntToString(value3), values.at(0));
219 OLA_ASSERT_EQ(IntToString(value4), values.at(1));
220 preferences->RemoveValue(key2);
221
222 // test get/set multiple value int
223 values = preferences->GetMultipleValue(key2);
224 OLA_ASSERT_EQ((size_t) 0, values.size());
225 preferences->SetMultipleValue(key2, value5);
226 values = preferences->GetMultipleValue(key2);
227 OLA_ASSERT(preferences->HasKey(key2));
228 OLA_ASSERT_EQ((size_t) 1, values.size());
229 OLA_ASSERT_EQ(IntToString(value5), values.at(0));
230 preferences->SetMultipleValue(key2, value6);
231 values = preferences->GetMultipleValue(key2);
232 OLA_ASSERT_EQ((size_t) 2, values.size());
233 OLA_ASSERT_EQ(IntToString(value5), values.at(0));
234 OLA_ASSERT_EQ(IntToString(value6), values.at(1));
235 preferences->RemoveValue(key2);
236
237 // test SetDefaultValue String
238 OLA_ASSERT(preferences->SetDefaultValue(key1, StringValidator(), value1));
239 OLA_ASSERT_EQ(value1, preferences->GetValue(key1));
240 OLA_ASSERT_FALSE(preferences->SetDefaultValue(key1, StringValidator(),
241 value2));
242 OLA_ASSERT_EQ(value1, preferences->GetValue(key1));
243 OLA_ASSERT(preferences->HasKey(key1));
244 preferences->RemoveValue(key1);
245
246 // test SetDefaultValue uint
247 UIntValidator uint_validator(0, 3);
248 OLA_ASSERT(preferences->SetDefaultValue(key1, uint_validator, value3));
249 OLA_ASSERT_EQ(IntToString(value3), preferences->GetValue(key1));
250 OLA_ASSERT_FALSE(preferences->SetDefaultValue(key1, uint_validator,
251 value4));
252 OLA_ASSERT_EQ(IntToString(value3), preferences->GetValue(key1));
253 OLA_ASSERT(preferences->HasKey(key1));
254 preferences->RemoveValue(key1);
255
256 // test SetDefaultValue int
257 IntValidator int_validator(0, 5);
258 OLA_ASSERT(preferences->SetDefaultValue(key1, int_validator, value5));
259 OLA_ASSERT_EQ(IntToString(value5), preferences->GetValue(key1));
260 OLA_ASSERT_FALSE(preferences->SetDefaultValue(key1, int_validator,
261 value6));
262 OLA_ASSERT_EQ(IntToString(value5), preferences->GetValue(key1));
263 OLA_ASSERT(preferences->HasKey(key1));
264 preferences->RemoveValue(key1);
265
266 // test SetDefaultValue char[]
267 OLA_ASSERT(preferences->SetDefaultValue(key1, StringValidator(), value7));
268 OLA_ASSERT_EQ(string(value7), preferences->GetValue(key1));
269 OLA_ASSERT_FALSE(preferences->SetDefaultValue(key1, StringValidator(),
270 value8));
271 OLA_ASSERT_EQ(string(value7), preferences->GetValue(key1));
272 OLA_ASSERT(preferences->HasKey(key1));
273 preferences->RemoveValue(key1);
274
275 // test SetDefaultValue bool
276 OLA_ASSERT(preferences->SetDefaultValue(key1, BoolValidator(), true));
277 OLA_ASSERT_EQ(string(BoolValidator::ENABLED), preferences->GetValue(key1));
278 OLA_ASSERT_FALSE(preferences->SetDefaultValue(key1, BoolValidator(),
279 false));
280 OLA_ASSERT_EQ(string(BoolValidator::ENABLED), preferences->GetValue(key1));
281 OLA_ASSERT(preferences->HasKey(key1));
282 preferences->RemoveValue(key1);
283 }
284
285
286 /*
287 * Check that we can get/set the preferences
288 */
289 void PreferencesTest::testBool() {
290 MemoryPreferencesFactory factory;
291 Preferences *preferences = factory.NewPreference("dummy");
292
293 string key1 = "foo";
294 string value1 = "bar";
295
296 // test get/set single values
297 OLA_ASSERT_EQ(false, preferences->GetValueAsBool(key1));
298 preferences->SetValueAsBool(key1, true);
299 OLA_ASSERT_EQ(true, preferences->GetValueAsBool(key1));
300 preferences->SetValueAsBool(key1, false);
301 OLA_ASSERT_EQ(false, preferences->GetValueAsBool(key1));
302 preferences->SetValue(key1, value1);
303 OLA_ASSERT_EQ(false, preferences->GetValueAsBool(key1));
304 }
305
306
307 /*
308 * Check that the factory works as intended
309 */
310 void PreferencesTest::testFactory() {
311 MemoryPreferencesFactory factory;
312 string preferences_name = "dummy";
313 Preferences *preferences = factory.NewPreference(preferences_name);
314 Preferences *preferences2 = factory.NewPreference(preferences_name);
315 OLA_ASSERT_EQ(preferences, preferences2);
316 }
317
318
319 /*
320 * Check that we can load from a file
321 */
322 void PreferencesTest::testLoad() {
323 FileBackedPreferences *preferences = new FileBackedPreferences(
324 "", "dummy", NULL);
325 preferences->Clear();
326 preferences->SetValue("foo", "bad");
327 preferences->LoadFromFile(
328 TEST_SRC_DIR "/olad/testdata/test_preferences.conf");
329
330 OLA_ASSERT_EQ(string("bar"), preferences->GetValue("foo"));
331 OLA_ASSERT(preferences->HasKey("foo"));
332 OLA_ASSERT_EQ(string("bat"), preferences->GetValue("baz"));
333 OLA_ASSERT(preferences->HasKey("baz"));
334
335 vector<string> values = preferences->GetMultipleValue("multi");
336 OLA_ASSERT_EQ((size_t) 3, values.size());
337 OLA_ASSERT_EQ(string("1"), values.at(0));
338 OLA_ASSERT_EQ(string("2"), values.at(1));
339 OLA_ASSERT_EQ(string("3"), values.at(2));
340 delete preferences;
341 }
342
343
344 void PreferencesTest::testSave() {
345 const string data_path = TEST_BUILD_DIR "/olad/ola-output.conf";
346
347 ola::FilePreferenceSaverThread saver_thread;
348 saver_thread.Start();
349 FileBackedPreferences *preferences = new FileBackedPreferences(
350 TEST_BUILD_DIR "/olad", "output", &saver_thread);
351 preferences->Clear();
352
353 unlink(data_path.c_str());
354 string key1 = "foo";
355 string key2 = "bat";
356 string key3 = "/dev/ttyUSB0";
357 string value1 = "bar";
358 string value2 = "baz";
359 string value3 = "boo";
360 string multi_key = "multi";
361 preferences->SetValue(key1, value1);
362 preferences->SetValue(key2, value2);
363 preferences->SetValue(key3, value3);
364 preferences->SetMultipleValue(multi_key, "1");
365 preferences->SetMultipleValue(multi_key, "2");
366 preferences->SetMultipleValue(multi_key, "3");
367 preferences->Save();
368
369 saver_thread.Syncronize();
370
371 FileBackedPreferences *input_preferences = new
372 FileBackedPreferences("", "input", NULL);
373 input_preferences->LoadFromFile(data_path);
374 OLA_ASSERT(*preferences == *input_preferences);
375 delete preferences;
376 delete input_preferences;
377
378 saver_thread.Join();
379 }