From bfedfedafe3cb86fb0431e278bb4ba0cb3b181be Mon Sep 17 00:00:00 2001 From: Chris Sherlock Date: Sun, 6 May 2018 01:32:23 +1000 Subject: [PATCH] test: create unit tests for Config Change-Id: Iaf92f93e169cf7367e3b9fc521f237413a268493 Reviewed-on: https://gerrit.libreoffice.org/53893 Tested-by: Jenkins Reviewed-by: Noel Grandin --- tools/CppunitTest_tools_config.mk | 38 ++++++ tools/Module_tools.mk | 1 + tools/qa/cppunit/test_config.cxx | 202 ++++++++++++++++++++++++++++++ tools/qa/data/testconfig.ini | 5 + 4 files changed, 246 insertions(+) create mode 100644 tools/CppunitTest_tools_config.mk create mode 100644 tools/qa/cppunit/test_config.cxx create mode 100644 tools/qa/data/testconfig.ini diff --git a/tools/CppunitTest_tools_config.mk b/tools/CppunitTest_tools_config.mk new file mode 100644 index 000000000000..79c07c7aba40 --- /dev/null +++ b/tools/CppunitTest_tools_config.mk @@ -0,0 +1,38 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +#************************************************************************* +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +#************************************************************************* + +$(eval $(call gb_CppunitTest_CppunitTest,tools_config)) + +$(eval $(call gb_CppunitTest_use_external,tools_config,boost_headers)) + +$(eval $(call gb_CppunitTest_add_exception_objects,tools_config, \ + tools/qa/cppunit/test_config \ +)) + +$(eval $(call gb_CppunitTest_use_sdk_api,tools_config)) + +$(eval $(call gb_CppunitTest_use_libraries,tools_config, \ + sal \ + tl \ + test \ + unotest \ +)) + +$(eval $(call gb_CppunitTest_use_static_libraries,tools_config, \ + ooopathutils \ +)) + +$(eval $(call gb_CppunitTest_set_include,tools_config,\ + $$(INCLUDE) \ + -I$(SRCDIR)/tools/inc \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/tools/Module_tools.mk b/tools/Module_tools.mk index 23acf677eca8..5632a5b94a2d 100644 --- a/tools/Module_tools.mk +++ b/tools/Module_tools.mk @@ -32,6 +32,7 @@ $(eval $(call gb_Module_add_targets_for_build,tools,\ $(eval $(call gb_Module_add_check_targets,tools,\ CppunitTest_tools_test \ + CppunitTest_tools_config \ )) # vim: set noet sw=4 ts=4: diff --git a/tools/qa/cppunit/test_config.cxx b/tools/qa/cppunit/test_config.cxx new file mode 100644 index 000000000000..aaf599b8fa5e --- /dev/null +++ b/tools/qa/cppunit/test_config.cxx @@ -0,0 +1,202 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include + +#include + +class ToolsConfigTest : public test::BootstrapFixture +{ +public: + ToolsConfigTest() + : BootstrapFixture(true, false) + { + } + + virtual void setUp() override + { + maOriginalConfigFile = m_directories.getURLFromSrc("/tools/qa/data/"); + maOriginalConfigFile += "testconfig.ini"; + + if (osl::FileBase::getTempDirURL(maConfigFile) == osl::File::RC::E_None) + maConfigFile += "/config.ini"; + else + assert("cannot create temp folder"); + + osl::File::copy(maOriginalConfigFile, maConfigFile); + } + + void testHasGroup() + { + Config aConfig(maConfigFile); + CPPUNIT_ASSERT(aConfig.HasGroup("TestGroup")); + CPPUNIT_ASSERT(aConfig.HasGroup("TestGroup2")); + } + + void testGetGroup() + { + Config aConfig(maConfigFile); + CPPUNIT_ASSERT_EQUAL(OString(""), aConfig.GetGroup()); + + CPPUNIT_ASSERT_EQUAL(OString("TestGroup"), aConfig.GetGroupName(0)); + CPPUNIT_ASSERT_EQUAL(OString("TestGroup2"), aConfig.GetGroupName(1)); + CPPUNIT_ASSERT_EQUAL(OString(""), aConfig.GetGroupName(2)); + } + + void testSetGroup() + { + Config aConfig(maConfigFile); + + aConfig.SetGroup(OString("TestGroup")); + CPPUNIT_ASSERT_EQUAL(OString("TestGroup"), aConfig.GetGroup()); + + // so this is a quirk of Config - you can set the group name, + // but it might not exist so you really should first check if + // it exists via HasGroup() + aConfig.SetGroup(OString("TestGroupA")); + CPPUNIT_ASSERT(!aConfig.HasGroup("TestGroupA")); + CPPUNIT_ASSERT_EQUAL(OString("TestGroupA"), aConfig.GetGroup()); + } + + void testDeleteGroup() + { + { + Config aConfig(maConfigFile); + + aConfig.DeleteGroup(OString("TestGroup")); + CPPUNIT_ASSERT(!aConfig.HasGroup("TestGroup")); + CPPUNIT_ASSERT_EQUAL(OString("TestGroup2"), aConfig.GetGroupName(0)); + + sal_uInt16 nExpected = 1; + sal_uInt16 nActual = aConfig.GetGroupCount(); + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + } + + osl::File::copy(maOriginalConfigFile, maConfigFile); + + { + Config aConfig(maConfigFile); + + CPPUNIT_ASSERT(!aConfig.HasGroup("NonExistentTestGroup")); + aConfig.DeleteGroup(OString("NonExistentTestGroup")); + CPPUNIT_ASSERT_EQUAL(OString("TestGroup"), aConfig.GetGroupName(0)); + + sal_uInt16 nExpected = 2; + sal_uInt16 nActual = aConfig.GetGroupCount(); + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + } + + osl::File::copy(maOriginalConfigFile, maConfigFile); + } + + void testGetGroupCount() + { + Config aConfig(maConfigFile); + sal_uInt16 nExpected = 2; + sal_uInt16 nActual = aConfig.GetGroupCount(); + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + } + + void testReadKey() + { + Config aConfig(maConfigFile); + aConfig.SetGroup(OString("TestGroup")); + CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig.ReadKey(OString("testkey"))); + CPPUNIT_ASSERT_EQUAL(OString(), aConfig.ReadKey(OString("nonexistenttestkey"))); + CPPUNIT_ASSERT_EQUAL(OString("notexists"), + aConfig.ReadKey(OString("nonexistenttestkey"), OString("notexists"))); + + aConfig.SetGroup(OString("TestGroup2")); + CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig.ReadKey(OString("testkey2"))); + CPPUNIT_ASSERT_EQUAL(OString(), aConfig.ReadKey(OString("nonexistenttestkey"))); + CPPUNIT_ASSERT_EQUAL(OString("notexists"), + aConfig.ReadKey(OString("nonexistenttestkey"), OString("notexists"))); + } + + void testGetKeyName() + { + Config aConfig(maConfigFile); + aConfig.SetGroup(OString("TestGroup")); + CPPUNIT_ASSERT_EQUAL(OString("testkey"), aConfig.GetKeyName(0)); + + aConfig.SetGroup(OString("TestGroup2")); + CPPUNIT_ASSERT_EQUAL(OString("testkey2"), aConfig.GetKeyName(0)); + } + + void testWriteDeleteKey() + { + Config aConfig(maConfigFile); + aConfig.SetGroup(OString("TestGroup")); + aConfig.WriteKey(OString("testkey_new"), OString("testvalue")); + + sal_uInt16 nExpected = 2; + sal_uInt16 nActual = aConfig.GetKeyCount(); + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig.ReadKey(OString("testkey_new"))); + + aConfig.DeleteKey(OString("testkey_new")); + + nExpected = 1; + nActual = aConfig.GetKeyCount(); + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + CPPUNIT_ASSERT_EQUAL(OString(), aConfig.ReadKey(OString("testkey_new"))); + + aConfig.SetGroup(OString("TestGroup2")); + aConfig.WriteKey(OString("testkey_new"), OString("testvalue")); + + nActual = aConfig.GetKeyCount(); + nExpected = 2; + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig.ReadKey(OString("testkey_new"))); + + aConfig.DeleteKey(OString("testkey_new")); + + nActual = aConfig.GetKeyCount(); + nExpected = 1; + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + CPPUNIT_ASSERT_EQUAL(OString(), aConfig.ReadKey(OString("testkey_new"))); + + aConfig.SetGroup(OString("TestGroup3")); + aConfig.WriteKey(OString("testkey_new_group3"), OString("testvalue")); + + nActual = aConfig.GetKeyCount(); + nExpected = 1; + CPPUNIT_ASSERT_EQUAL(nExpected, nActual); + CPPUNIT_ASSERT_EQUAL(OString("testvalue"), aConfig.ReadKey(OString("testkey_new_group3"))); + + nExpected = 3; + CPPUNIT_ASSERT_EQUAL(nExpected, aConfig.GetGroupCount()); + + osl::File::copy(maOriginalConfigFile, maConfigFile); + } + + CPPUNIT_TEST_SUITE(ToolsConfigTest); + CPPUNIT_TEST(testHasGroup); + CPPUNIT_TEST(testGetGroup); + CPPUNIT_TEST(testSetGroup); + CPPUNIT_TEST(testDeleteGroup); + CPPUNIT_TEST(testReadKey); + CPPUNIT_TEST(testGetGroupCount); + CPPUNIT_TEST(testGetKeyName); + CPPUNIT_TEST(testWriteDeleteKey); + CPPUNIT_TEST_SUITE_END(); + +private: + OUString maOriginalConfigFile; + OUString maConfigFile; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(ToolsConfigTest); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/qa/data/testconfig.ini b/tools/qa/data/testconfig.ini new file mode 100644 index 000000000000..630753216483 --- /dev/null +++ b/tools/qa/data/testconfig.ini @@ -0,0 +1,5 @@ +[TestGroup] +testkey=testvalue + +[TestGroup2] +testkey2=testvalue