fix bug with deleting and recreating entries in wxFileConfig (patch 1796866)
[wxWidgets.git] / tests / config / fileconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/fileconf/fileconf.cpp
3 // Purpose: wxFileConf unit test
4 // Author: Vadim Zeitlin
5 // Created: 2004-09-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_FILECONFIG
21
22 #ifndef WX_PRECOMP
23 #endif // WX_PRECOMP
24
25 #include "wx/fileconf.h"
26 #include "wx/sstream.h"
27 #include "wx/log.h"
28
29 static const wxChar *testconfig =
30 _T("[root]\n")
31 _T("entry=value\n")
32 _T("[root/group1]\n")
33 _T("[root/group1/subgroup]\n")
34 _T("subentry=subvalue\n")
35 _T("subentry2=subvalue2\n")
36 _T("[root/group2]\n")
37 ;
38
39 // ----------------------------------------------------------------------------
40 // local functions
41 // ----------------------------------------------------------------------------
42
43 static wxString Dump(wxFileConfig& fc)
44 {
45 wxStringOutputStream sos;
46 fc.Save(sos);
47 return wxTextFile::Translate(sos.GetString(), wxTextFileType_Unix);
48 }
49
50 // helper macro to test wxFileConfig contents
51 #define wxVERIFY_FILECONFIG(t, fc) CPPUNIT_ASSERT_EQUAL(wxString(t), Dump(fc))
52
53 // ----------------------------------------------------------------------------
54 // test class
55 // ----------------------------------------------------------------------------
56
57 class FileConfigTestCase : public CppUnit::TestCase
58 {
59 public:
60 FileConfigTestCase() { }
61
62 private:
63 CPPUNIT_TEST_SUITE( FileConfigTestCase );
64 CPPUNIT_TEST( Path );
65 CPPUNIT_TEST( AddEntries );
66 CPPUNIT_TEST( GetEntries );
67 CPPUNIT_TEST( GetGroups );
68 CPPUNIT_TEST( HasEntry );
69 CPPUNIT_TEST( HasGroup );
70 CPPUNIT_TEST( Binary );
71 CPPUNIT_TEST( Save );
72 CPPUNIT_TEST( DeleteEntry );
73 CPPUNIT_TEST( DeleteAndWriteEntry );
74 CPPUNIT_TEST( DeleteGroup );
75 CPPUNIT_TEST( DeleteAll );
76 CPPUNIT_TEST( RenameEntry );
77 CPPUNIT_TEST( RenameGroup );
78 CPPUNIT_TEST( CreateEntriesAndSubgroup );
79 CPPUNIT_TEST( CreateSubgroupAndEntries );
80 CPPUNIT_TEST( DeleteLastGroup );
81 CPPUNIT_TEST( DeleteAndRecreateGroup );
82 CPPUNIT_TEST_SUITE_END();
83
84 void Path();
85 void AddEntries();
86 void GetEntries();
87 void GetGroups();
88 void HasEntry();
89 void HasGroup();
90 void Binary();
91 void Save();
92 void DeleteEntry();
93 void DeleteAndWriteEntry();
94 void DeleteGroup();
95 void DeleteAll();
96 void RenameEntry();
97 void RenameGroup();
98 void CreateEntriesAndSubgroup();
99 void CreateSubgroupAndEntries();
100 void DeleteLastGroup();
101 void DeleteAndRecreateGroup();
102
103 static wxString ChangePath(wxFileConfig& fc, const wxChar *path)
104 {
105 fc.SetPath(path);
106
107 return fc.GetPath();
108 }
109
110 void CheckGroupEntries(const wxFileConfig& fc,
111 const wxChar *path,
112 size_t nEntries,
113 ...);
114 void CheckGroupSubgroups(const wxFileConfig& fc,
115 const wxChar *path,
116 size_t nGroups,
117 ...);
118
119 DECLARE_NO_COPY_CLASS(FileConfigTestCase)
120 };
121
122 // register in the unnamed registry so that these tests are run by default
123 CPPUNIT_TEST_SUITE_REGISTRATION( FileConfigTestCase );
124
125 // also include in it's own registry so that these tests can be run alone
126 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileConfigTestCase, "FileConfigTestCase" );
127
128 void FileConfigTestCase::Path()
129 {
130 wxStringInputStream sis(testconfig);
131 wxFileConfig fc(sis);
132
133 CPPUNIT_ASSERT( ChangePath(fc, _T("")) == _T("") );
134 CPPUNIT_ASSERT( ChangePath(fc, _T("/")) == _T("") );
135 CPPUNIT_ASSERT( ChangePath(fc, _T("root")) == _T("/root") );
136 CPPUNIT_ASSERT( ChangePath(fc, _T("/root")) == _T("/root") );
137 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group1/subgroup")) == _T("/root/group1/subgroup") );
138 CPPUNIT_ASSERT( ChangePath(fc, _T("/root/group2")) == _T("/root/group2") );
139 }
140
141 void FileConfigTestCase::AddEntries()
142 {
143 wxFileConfig fc;
144
145 wxVERIFY_FILECONFIG( _T(""), fc );
146
147 fc.Write(_T("/Foo"), _T("foo"));
148 wxVERIFY_FILECONFIG( _T("Foo=foo\n"), fc );
149
150 fc.Write(_T("/Bar/Baz"), _T("baz"));
151 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
152
153 fc.DeleteAll();
154 wxVERIFY_FILECONFIG( _T(""), fc );
155
156 fc.Write(_T("/Bar/Baz"), _T("baz"));
157 wxVERIFY_FILECONFIG( _T("[Bar]\nBaz=baz\n"), fc );
158
159 fc.Write(_T("/Foo"), _T("foo"));
160 wxVERIFY_FILECONFIG( _T("Foo=foo\n[Bar]\nBaz=baz\n"), fc );
161 }
162
163 void
164 FileConfigTestCase::CheckGroupEntries(const wxFileConfig& fc,
165 const wxChar *path,
166 size_t nEntries,
167 ...)
168 {
169 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
170
171 CPPUNIT_ASSERT( fc.GetNumberOfEntries() == nEntries );
172
173 va_list ap;
174 va_start(ap, nEntries);
175
176 long cookie;
177 wxString name;
178 for ( bool cont = fc.GetFirstEntry(name, cookie);
179 cont;
180 cont = fc.GetNextEntry(name, cookie), nEntries-- )
181 {
182 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
183 }
184
185 CPPUNIT_ASSERT( nEntries == 0 );
186
187 va_end(ap);
188 }
189
190 void
191 FileConfigTestCase::CheckGroupSubgroups(const wxFileConfig& fc,
192 const wxChar *path,
193 size_t nGroups,
194 ...)
195 {
196 wxConfigPathChanger change(&fc, wxString(path) + _T("/"));
197
198 CPPUNIT_ASSERT( fc.GetNumberOfGroups() == nGroups );
199
200 va_list ap;
201 va_start(ap, nGroups);
202
203 long cookie;
204 wxString name;
205 for ( bool cont = fc.GetFirstGroup(name, cookie);
206 cont;
207 cont = fc.GetNextGroup(name, cookie), nGroups-- )
208 {
209 CPPUNIT_ASSERT( name == va_arg(ap, wxChar *) );
210 }
211
212 CPPUNIT_ASSERT( nGroups == 0 );
213
214 va_end(ap);
215 }
216
217 void FileConfigTestCase::GetEntries()
218 {
219 wxStringInputStream sis(testconfig);
220 wxFileConfig fc(sis);
221
222 CheckGroupEntries(fc, _T(""), 0);
223 CheckGroupEntries(fc, _T("/root"), 1, _T("entry"));
224 CheckGroupEntries(fc, _T("/root/group1"), 0);
225 CheckGroupEntries(fc, _T("/root/group1/subgroup"),
226 2, _T("subentry"), _T("subentry2"));
227 }
228
229 void FileConfigTestCase::GetGroups()
230 {
231 wxStringInputStream sis(testconfig);
232 wxFileConfig fc(sis);
233
234 CheckGroupSubgroups(fc, _T(""), 1, _T("root"));
235 CheckGroupSubgroups(fc, _T("/root"), 2, _T("group1"), _T("group2"));
236 CheckGroupSubgroups(fc, _T("/root/group1"), 1, _T("subgroup"));
237 CheckGroupSubgroups(fc, _T("/root/group2"), 0);
238 }
239
240 void FileConfigTestCase::HasEntry()
241 {
242 wxStringInputStream sis(testconfig);
243 wxFileConfig fc(sis);
244
245 CPPUNIT_ASSERT( !fc.HasEntry(_T("root")) );
246 CPPUNIT_ASSERT( fc.HasEntry(_T("root/entry")) );
247 CPPUNIT_ASSERT( fc.HasEntry(_T("/root/entry")) );
248 CPPUNIT_ASSERT( fc.HasEntry(_T("root/group1/subgroup/subentry")) );
249 CPPUNIT_ASSERT( !fc.HasEntry(_T("")) );
250 CPPUNIT_ASSERT( !fc.HasEntry(_T("root/group1")) );
251 CPPUNIT_ASSERT( !fc.HasEntry(_T("subgroup/subentry")) );
252 CPPUNIT_ASSERT( !fc.HasEntry(_T("/root/no_such_group/entry")) );
253 CPPUNIT_ASSERT( !fc.HasGroup(_T("/root/no_such_group")) );
254 }
255
256 void FileConfigTestCase::HasGroup()
257 {
258 wxStringInputStream sis(testconfig);
259 wxFileConfig fc(sis);
260
261 CPPUNIT_ASSERT( fc.HasGroup(_T("root")) );
262 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1")) );
263 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group1/subgroup")) );
264 CPPUNIT_ASSERT( fc.HasGroup(_T("root/group2")) );
265 CPPUNIT_ASSERT( !fc.HasGroup(_T("")) );
266 CPPUNIT_ASSERT( !fc.HasGroup(_T("root/group")) );
267 CPPUNIT_ASSERT( !fc.HasGroup(_T("root//subgroup")) );
268 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot/subgroup")) );
269 CPPUNIT_ASSERT( !fc.HasGroup(_T("foot")) );
270 }
271
272 void FileConfigTestCase::Binary()
273 {
274 wxStringInputStream sis(
275 "[root]\n"
276 "binary=Zm9vCg==\n"
277 );
278 wxFileConfig fc(sis);
279
280 wxMemoryBuffer buf;
281 fc.Read("/root/binary", &buf);
282
283 CPPUNIT_ASSERT( memcmp("foo\n", buf.GetData(), buf.GetDataLen()) == 0 );
284
285 buf.SetDataLen(0);
286 buf.AppendData("\0\1\2", 3);
287 fc.Write("/root/012", buf);
288 wxVERIFY_FILECONFIG(
289 "[root]\n"
290 "binary=Zm9vCg==\n"
291 "012=AAEC\n",
292 fc
293 );
294 }
295
296 void FileConfigTestCase::Save()
297 {
298 wxStringInputStream sis(testconfig);
299 wxFileConfig fc(sis);
300 wxVERIFY_FILECONFIG( testconfig, fc );
301 }
302
303 void FileConfigTestCase::DeleteEntry()
304 {
305 wxStringInputStream sis(testconfig);
306 wxFileConfig fc(sis);
307
308 CPPUNIT_ASSERT( !fc.DeleteEntry(_T("foo")) );
309
310 CPPUNIT_ASSERT( fc.DeleteEntry(_T("root/group1/subgroup/subentry")) );
311 wxVERIFY_FILECONFIG( _T("[root]\n")
312 _T("entry=value\n")
313 _T("[root/group1]\n")
314 _T("[root/group1/subgroup]\n")
315 _T("subentry2=subvalue2\n")
316 _T("[root/group2]\n"),
317 fc );
318
319 // group should be deleted now as well as it became empty
320 wxConfigPathChanger change(&fc, _T("root/group1/subgroup/subentry2"));
321 CPPUNIT_ASSERT( fc.DeleteEntry(_T("subentry2")) );
322 wxVERIFY_FILECONFIG( _T("[root]\n")
323 _T("entry=value\n")
324 _T("[root/group1]\n")
325 _T("[root/group2]\n"),
326 fc );
327 }
328
329 void FileConfigTestCase::DeleteAndWriteEntry()
330 {
331 wxStringInputStream sis(
332 "[root/group1]\n"
333 "subentry=subvalue\n"
334 "subentry2=subvalue2\n"
335 "subentry3=subvalue3\n"
336 );
337
338 wxFileConfig fc(sis);
339
340 fc.DeleteEntry("/root/group1/subentry2");
341 fc.Write("/root/group1/subentry2", "testvalue");
342 fc.DeleteEntry("/root/group2/subentry2");
343 fc.Write("/root/group2/subentry2", "testvalue2");
344 fc.DeleteEntry("/root/group1/subentry2");
345 fc.Write("/root/group1/subentry2", "testvalue");
346 fc.DeleteEntry("/root/group2/subentry2");
347 fc.Write("/root/group2/subentry2", "testvalue2");
348
349 wxVERIFY_FILECONFIG( "[root/group1]\n"
350 "subentry=subvalue\n"
351 "subentry3=subvalue3\n"
352 "subentry2=testvalue\n"
353 "[root/group2]\n"
354 "subentry2=testvalue2\n",
355 fc );
356
357 fc.DeleteEntry("/root/group2/subentry2");
358 wxVERIFY_FILECONFIG( "[root/group1]\n"
359 "subentry=subvalue\n"
360 "subentry3=subvalue3\n"
361 "subentry2=testvalue\n",
362 fc );
363
364 fc.DeleteEntry("/root/group1/subentry2");
365 fc.DeleteEntry("/root/group1/subentry");
366 fc.DeleteEntry("/root/group1/subentry3");
367 wxVERIFY_FILECONFIG( "", fc );
368 }
369
370 void FileConfigTestCase::DeleteGroup()
371 {
372 wxStringInputStream sis(testconfig);
373 wxFileConfig fc(sis);
374
375 CPPUNIT_ASSERT( !fc.DeleteGroup(_T("foo")) );
376
377 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group1")) );
378 wxVERIFY_FILECONFIG( _T("[root]\n")
379 _T("entry=value\n")
380 _T("[root/group2]\n"),
381 fc );
382
383 // notice trailing slash: it should be ignored
384 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root/group2/")) );
385 wxVERIFY_FILECONFIG( _T("[root]\n")
386 _T("entry=value\n"),
387 fc );
388
389 CPPUNIT_ASSERT( fc.DeleteGroup(_T("root")) );
390 CPPUNIT_ASSERT( Dump(fc).empty() );
391 }
392
393 void FileConfigTestCase::DeleteAll()
394 {
395 wxStringInputStream sis(testconfig);
396 wxFileConfig fc(sis);
397
398 CPPUNIT_ASSERT( fc.DeleteAll() );
399 CPPUNIT_ASSERT( Dump(fc).empty() );
400 }
401
402 void FileConfigTestCase::RenameEntry()
403 {
404 wxStringInputStream sis(testconfig);
405 wxFileConfig fc(sis);
406
407 fc.SetPath(_T("root"));
408 CPPUNIT_ASSERT( fc.RenameEntry(_T("entry"), _T("newname")) );
409 wxVERIFY_FILECONFIG( _T("[root]\n")
410 _T("newname=value\n")
411 _T("[root/group1]\n")
412 _T("[root/group1/subgroup]\n")
413 _T("subentry=subvalue\n")
414 _T("subentry2=subvalue2\n")
415 _T("[root/group2]\n"),
416 fc );
417
418 fc.SetPath(_T("group1/subgroup"));
419 CPPUNIT_ASSERT( !fc.RenameEntry(_T("entry"), _T("newname")) );
420 CPPUNIT_ASSERT( !fc.RenameEntry(_T("subentry"), _T("subentry2")) );
421
422 CPPUNIT_ASSERT( fc.RenameEntry(_T("subentry"), _T("subentry1")) );
423 wxVERIFY_FILECONFIG( _T("[root]\n")
424 _T("newname=value\n")
425 _T("[root/group1]\n")
426 _T("[root/group1/subgroup]\n")
427 _T("subentry2=subvalue2\n")
428 _T("subentry1=subvalue\n")
429 _T("[root/group2]\n"),
430 fc );
431 }
432
433 void FileConfigTestCase::RenameGroup()
434 {
435 wxStringInputStream sis(testconfig);
436 wxFileConfig fc(sis);
437
438 CPPUNIT_ASSERT( fc.RenameGroup(_T("root"), _T("foot")) );
439 wxVERIFY_FILECONFIG( _T("[foot]\n")
440 _T("entry=value\n")
441 _T("[foot/group1]\n")
442 _T("[foot/group1/subgroup]\n")
443 _T("subentry=subvalue\n")
444 _T("subentry2=subvalue2\n")
445 _T("[foot/group2]\n"),
446 fc );
447
448 // renaming a path doesn't work, it must be the immediate group
449 CPPUNIT_ASSERT( !fc.RenameGroup(_T("foot/group1"), _T("group2")) );
450
451
452 fc.SetPath(_T("foot"));
453
454 // renaming to a name of existing group doesn't work
455 CPPUNIT_ASSERT( !fc.RenameGroup(_T("group1"), _T("group2")) );
456
457 // try exchanging the groups names and then restore them back
458 CPPUNIT_ASSERT( fc.RenameGroup(_T("group1"), _T("groupTmp")) );
459 wxVERIFY_FILECONFIG( _T("[foot]\n")
460 _T("entry=value\n")
461 _T("[foot/groupTmp]\n")
462 _T("[foot/groupTmp/subgroup]\n")
463 _T("subentry=subvalue\n")
464 _T("subentry2=subvalue2\n")
465 _T("[foot/group2]\n"),
466 fc );
467
468 CPPUNIT_ASSERT( fc.RenameGroup(_T("group2"), _T("group1")) );
469 wxVERIFY_FILECONFIG( _T("[foot]\n")
470 _T("entry=value\n")
471 _T("[foot/groupTmp]\n")
472 _T("[foot/groupTmp/subgroup]\n")
473 _T("subentry=subvalue\n")
474 _T("subentry2=subvalue2\n")
475 _T("[foot/group1]\n"),
476 fc );
477
478 CPPUNIT_ASSERT( fc.RenameGroup(_T("groupTmp"), _T("group2")) );
479 wxVERIFY_FILECONFIG( _T("[foot]\n")
480 _T("entry=value\n")
481 _T("[foot/group2]\n")
482 _T("[foot/group2/subgroup]\n")
483 _T("subentry=subvalue\n")
484 _T("subentry2=subvalue2\n")
485 _T("[foot/group1]\n"),
486 fc );
487
488 CPPUNIT_ASSERT( fc.RenameGroup(_T("group1"), _T("groupTmp")) );
489 wxVERIFY_FILECONFIG( _T("[foot]\n")
490 _T("entry=value\n")
491 _T("[foot/group2]\n")
492 _T("[foot/group2/subgroup]\n")
493 _T("subentry=subvalue\n")
494 _T("subentry2=subvalue2\n")
495 _T("[foot/groupTmp]\n"),
496 fc );
497
498 CPPUNIT_ASSERT( fc.RenameGroup(_T("group2"), _T("group1")) );
499 wxVERIFY_FILECONFIG( _T("[foot]\n")
500 _T("entry=value\n")
501 _T("[foot/group1]\n")
502 _T("[foot/group1/subgroup]\n")
503 _T("subentry=subvalue\n")
504 _T("subentry2=subvalue2\n")
505 _T("[foot/groupTmp]\n"),
506 fc );
507
508 CPPUNIT_ASSERT( fc.RenameGroup(_T("groupTmp"), _T("group2")) );
509 wxVERIFY_FILECONFIG( _T("[foot]\n")
510 _T("entry=value\n")
511 _T("[foot/group1]\n")
512 _T("[foot/group1/subgroup]\n")
513 _T("subentry=subvalue\n")
514 _T("subentry2=subvalue2\n")
515 _T("[foot/group2]\n"),
516 fc );
517 }
518
519 void FileConfigTestCase::CreateSubgroupAndEntries()
520 {
521 wxFileConfig fc;
522 fc.Write(_T("sub/sub_first"), _T("sub_one"));
523 fc.Write(_T("first"), _T("one"));
524
525 wxVERIFY_FILECONFIG( _T("first=one\n")
526 _T("[sub]\n")
527 _T("sub_first=sub_one\n"),
528 fc );
529 }
530
531 void FileConfigTestCase::CreateEntriesAndSubgroup()
532 {
533 wxFileConfig fc;
534 fc.Write(_T("first"), _T("one"));
535 fc.Write(_T("second"), _T("two"));
536 fc.Write(_T("sub/sub_first"), _T("sub_one"));
537
538 wxVERIFY_FILECONFIG( _T("first=one\n")
539 _T("second=two\n")
540 _T("[sub]\n")
541 _T("sub_first=sub_one\n"),
542 fc );
543 }
544
545 static void EmptyConfigAndWriteKey()
546 {
547 wxFileConfig fc(_T("deleteconftest"));
548
549 const wxString groupPath = _T("/root");
550
551 if ( fc.Exists(groupPath) )
552 {
553 // using DeleteGroup exposes the problem, using DeleteAll doesn't
554 CPPUNIT_ASSERT( fc.DeleteGroup(groupPath) );
555 }
556
557 // the config must be empty for the problem to arise
558 CPPUNIT_ASSERT( !fc.GetNumberOfEntries(true) );
559 CPPUNIT_ASSERT( !fc.GetNumberOfGroups(true) );
560
561
562 // this crashes on second call of this function
563 CPPUNIT_ASSERT( fc.Write(groupPath + _T("/entry"), _T("value")) );
564 }
565
566 void FileConfigTestCase::DeleteLastGroup()
567 {
568 /*
569 We make 2 of the same calls, first to create a file config with a single
570 group and key...
571 */
572 ::EmptyConfigAndWriteKey();
573
574 /*
575 ... then the same but this time the key's group is deleted before the
576 key is written again. This causes a crash.
577 */
578 ::EmptyConfigAndWriteKey();
579
580
581 // clean up
582 wxLogNull noLogging;
583 (void) ::wxRemoveFile(wxFileConfig::GetLocalFileName(_T("deleteconftest")));
584 }
585
586 void FileConfigTestCase::DeleteAndRecreateGroup()
587 {
588 static const wxChar *confInitial =
589 _T("[First]\n")
590 _T("Value1=Foo\n")
591 _T("[Second]\n")
592 _T("Value2=Bar\n");
593
594 wxStringInputStream sis(confInitial);
595 wxFileConfig fc(sis);
596
597 fc.DeleteGroup(_T("Second"));
598 wxVERIFY_FILECONFIG( _T("[First]\n")
599 _T("Value1=Foo\n"),
600 fc );
601
602 fc.Write(_T("Second/Value2"), _T("New"));
603 wxVERIFY_FILECONFIG( _T("[First]\n")
604 _T("Value1=Foo\n")
605 _T("[Second]\n")
606 _T("Value2=New\n"),
607 fc );
608 }
609
610 #endif // wxUSE_FILECONFIG
611