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(u
"/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(""_ostr
, aConfig
.GetGroup());
51 CPPUNIT_ASSERT_EQUAL("TestGroup"_ostr
, aConfig
.GetGroupName(0));
52 CPPUNIT_ASSERT_EQUAL("TestGroup2"_ostr
, aConfig
.GetGroupName(1));
53 CPPUNIT_ASSERT_EQUAL(""_ostr
, aConfig
.GetGroupName(2));
58 Config
aConfig(maConfigFile
);
60 aConfig
.SetGroup("TestGroup"_ostr
);
61 CPPUNIT_ASSERT_EQUAL("TestGroup"_ostr
, 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"_ostr
);
67 CPPUNIT_ASSERT(!aConfig
.HasGroup("TestGroupA"));
68 CPPUNIT_ASSERT_EQUAL("TestGroupA"_ostr
, aConfig
.GetGroup());
71 void testDeleteGroup()
74 Config
aConfig(maConfigFile
);
76 aConfig
.DeleteGroup("TestGroup");
77 CPPUNIT_ASSERT(!aConfig
.HasGroup("TestGroup"));
78 CPPUNIT_ASSERT_EQUAL("TestGroup2"_ostr
, 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("TestGroup"_ostr
, 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"_ostr
);
111 CPPUNIT_ASSERT_EQUAL("testvalue"_ostr
, aConfig
.ReadKey("testkey"_ostr
));
112 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("nonexistenttestkey"_ostr
));
113 CPPUNIT_ASSERT_EQUAL("notexists"_ostr
,
114 aConfig
.ReadKey("nonexistenttestkey"_ostr
, "notexists"_ostr
));
116 aConfig
.SetGroup("TestGroup2"_ostr
);
117 CPPUNIT_ASSERT_EQUAL("testvalue"_ostr
, aConfig
.ReadKey("testkey2"_ostr
));
118 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("nonexistenttestkey"_ostr
));
119 CPPUNIT_ASSERT_EQUAL("notexists"_ostr
,
120 aConfig
.ReadKey("nonexistenttestkey"_ostr
, "notexists"_ostr
));
123 void testGetKeyName()
125 Config
aConfig(maConfigFile
);
126 aConfig
.SetGroup("TestGroup"_ostr
);
127 CPPUNIT_ASSERT_EQUAL("testkey"_ostr
, aConfig
.GetKeyName(0));
129 aConfig
.SetGroup("TestGroup2"_ostr
);
130 CPPUNIT_ASSERT_EQUAL("testkey2"_ostr
, aConfig
.GetKeyName(0));
133 void testWriteDeleteKey()
135 Config
aConfig(maConfigFile
);
136 aConfig
.SetGroup("TestGroup"_ostr
);
137 aConfig
.WriteKey("testkey_new"_ostr
, "testvalue"_ostr
);
139 sal_uInt16 nExpected
= 2;
140 sal_uInt16 nActual
= aConfig
.GetKeyCount();
141 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
142 CPPUNIT_ASSERT_EQUAL("testvalue"_ostr
, aConfig
.ReadKey("testkey_new"_ostr
));
144 aConfig
.DeleteKey("testkey_new");
147 nActual
= aConfig
.GetKeyCount();
148 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
149 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("testkey_new"_ostr
));
151 aConfig
.SetGroup("TestGroup2"_ostr
);
152 aConfig
.WriteKey("testkey_new"_ostr
, "testvalue"_ostr
);
154 nActual
= aConfig
.GetKeyCount();
156 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
157 CPPUNIT_ASSERT_EQUAL("testvalue"_ostr
, aConfig
.ReadKey("testkey_new"_ostr
));
159 aConfig
.DeleteKey("testkey_new");
161 nActual
= aConfig
.GetKeyCount();
163 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
164 CPPUNIT_ASSERT_EQUAL(OString(), aConfig
.ReadKey("testkey_new"_ostr
));
166 aConfig
.SetGroup("TestGroup3"_ostr
);
167 aConfig
.WriteKey("testkey_new_group3"_ostr
, "testvalue"_ostr
);
169 nActual
= aConfig
.GetKeyCount();
171 CPPUNIT_ASSERT_EQUAL(nExpected
, nActual
);
172 CPPUNIT_ASSERT_EQUAL("testvalue"_ostr
, aConfig
.ReadKey("testkey_new_group3"_ostr
));
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: */