1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <test/bootstrapfixture.hxx>
12 #include <osl/file.hxx>
13 #include <rtl/ustring.hxx>
15 #include <tools/config.hxx>
17 class ToolsConfigTest
: public test::BootstrapFixture
21 : BootstrapFixture(true, false)
25 virtual void setUp() override
27 maOriginalConfigFile
= m_directories
.getURLFromSrc("/tools/qa/data/");
28 maOriginalConfigFile
+= "testconfig.ini";
30 auto const e
= osl::FileBase::getTempDirURL(maConfigFile
);
31 CPPUNIT_ASSERT_EQUAL_MESSAGE("cannot create temp folder", osl::File::RC::E_None
, e
);
32 maConfigFile
+= "/config.ini";
34 osl::File::copy(maOriginalConfigFile
, maConfigFile
);
37 virtual void tearDown() override
{ osl::File::remove(maConfigFile
); }
41 Config
aConfig(maConfigFile
);
42 CPPUNIT_ASSERT(aConfig
.HasGroup("TestGroup"));
43 CPPUNIT_ASSERT(aConfig
.HasGroup("TestGroup2"));
48 Config
aConfig(maConfigFile
);
49 CPPUNIT_ASSERT_EQUAL(OString(""), aConfig
.GetGroup());
51 CPPUNIT_ASSERT_EQUAL(OString("TestGroup"), aConfig
.GetGroupName(0));
52 CPPUNIT_ASSERT_EQUAL(OString("TestGroup2"), aConfig
.GetGroupName(1));
53 CPPUNIT_ASSERT_EQUAL(OString(""), aConfig
.GetGroupName(2));
58 Config
aConfig(maConfigFile
);
60 aConfig
.SetGroup("TestGroup");
61 CPPUNIT_ASSERT_EQUAL(OString("TestGroup"), aConfig
.GetGroup());
63 // so this is a quirk of Config - you can set the group name,
64 // but it might not exist so you really should first check if
65 // it exists via HasGroup()
66 aConfig
.SetGroup("TestGroupA");
67 CPPUNIT_ASSERT(!aConfig
.HasGroup("TestGroupA"));
68 CPPUNIT_ASSERT_EQUAL(OString("TestGroupA"), aConfig
.GetGroup());
71 void testDeleteGroup()
74 Config
aConfig(maConfigFile
);
76 aConfig
.DeleteGroup("TestGroup");
77 CPPUNIT_ASSERT(!aConfig
.HasGroup("TestGroup"));
78 CPPUNIT_ASSERT_EQUAL(OString("TestGroup2"), aConfig
.GetGroupName(0));
80 sal_uInt16 nActual
= aConfig
.GetGroupCount();
81 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), nActual
);
84 osl::File::copy(maOriginalConfigFile
, maConfigFile
);
87 Config
aConfig(maConfigFile
);
89 CPPUNIT_ASSERT(!aConfig
.HasGroup("NonExistentTestGroup"));
90 aConfig
.DeleteGroup("NonExistentTestGroup");
91 CPPUNIT_ASSERT_EQUAL(OString("TestGroup"), aConfig
.GetGroupName(0));
93 sal_uInt16 nActual
= aConfig
.GetGroupCount();
94 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), nActual
);
97 osl::File::copy(maOriginalConfigFile
, maConfigFile
);
100 void testGetGroupCount()
102 Config
aConfig(maConfigFile
);
103 sal_uInt16 nActual
= aConfig
.GetGroupCount();
104 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), nActual
);
109 Config
aConfig(maConfigFile
);
110 aConfig
.SetGroup("TestGroup");
111 CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig
.ReadKey("testkey"));
112 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("nonexistenttestkey"));
113 CPPUNIT_ASSERT_EQUAL(OString("notexists"),
114 aConfig
.ReadKey("nonexistenttestkey", "notexists"));
116 aConfig
.SetGroup("TestGroup2");
117 CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig
.ReadKey("testkey2"));
118 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("nonexistenttestkey"));
119 CPPUNIT_ASSERT_EQUAL(OString("notexists"),
120 aConfig
.ReadKey("nonexistenttestkey", "notexists"));
123 void testGetKeyName()
125 Config
aConfig(maConfigFile
);
126 aConfig
.SetGroup("TestGroup");
127 CPPUNIT_ASSERT_EQUAL(OString("testkey"), aConfig
.GetKeyName(0));
129 aConfig
.SetGroup("TestGroup2");
130 CPPUNIT_ASSERT_EQUAL(OString("testkey2"), aConfig
.GetKeyName(0));
133 void testWriteDeleteKey()
135 Config
aConfig(maConfigFile
);
136 aConfig
.SetGroup("TestGroup");
137 aConfig
.WriteKey("testkey_new", "testvalue");
139 sal_uInt16 nExpected
= 2;
140 sal_uInt16 nActual
= aConfig
.GetKeyCount();
141 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
142 CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig
.ReadKey("testkey_new"));
144 aConfig
.DeleteKey("testkey_new");
147 nActual
= aConfig
.GetKeyCount();
148 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
149 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("testkey_new"));
151 aConfig
.SetGroup("TestGroup2");
152 aConfig
.WriteKey("testkey_new", "testvalue");
154 nActual
= aConfig
.GetKeyCount();
156 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
157 CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig
.ReadKey("testkey_new"));
159 aConfig
.DeleteKey("testkey_new");
161 nActual
= aConfig
.GetKeyCount();
163 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
164 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("testkey_new"));
166 aConfig
.SetGroup("TestGroup3");
167 aConfig
.WriteKey("testkey_new_group3", "testvalue");
169 nActual
= aConfig
.GetKeyCount();
171 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
172 CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig
.ReadKey("testkey_new_group3"));
175 CPPUNIT_ASSERT_EQUAL(nExpected
, aConfig
.GetGroupCount());
177 osl::File::copy(maOriginalConfigFile
, maConfigFile
);
180 CPPUNIT_TEST_SUITE(ToolsConfigTest
);
181 CPPUNIT_TEST(testHasGroup
);
182 CPPUNIT_TEST(testGetGroup
);
183 CPPUNIT_TEST(testSetGroup
);
184 CPPUNIT_TEST(testDeleteGroup
);
185 CPPUNIT_TEST(testReadKey
);
186 CPPUNIT_TEST(testGetGroupCount
);
187 CPPUNIT_TEST(testGetKeyName
);
188 CPPUNIT_TEST(testWriteDeleteKey
);
189 CPPUNIT_TEST_SUITE_END();
192 OUString maOriginalConfigFile
;
193 OUString maConfigFile
;
196 CPPUNIT_TEST_SUITE_REGISTRATION(ToolsConfigTest
);
198 CPPUNIT_PLUGIN_IMPLEMENT();
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */