From 90bbd6419996455c707450a416f0ffb0c8635039 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Thu, 19 Feb 2015 15:27:36 +0100 Subject: [PATCH] Define a base TestException class A new base class DPL::Test::TestException is defined as base for * DPL::Test::TestFailed (prev. DPL::Test::TestRunner::TestFailed), * DPL::Test::TestIgnored (prev. DPL::Test::TestRunner::Ignored). All three classes were moved into separate files. Common base class simplifies handling of exceptions that can be thrown during tests. Change-Id: I1fadb09b7781bf22a0090043a46ca48c55c9962b --- src/common/db_sqlite.h | 8 +-- src/cynara-tests/common/cynara_test_commons.cpp | 13 ++--- src/framework/config.cmake | 1 + src/framework/include/dpl/test/test_exception.h | 51 ++++++++++++++++ src/framework/include/dpl/test/test_failed.h | 57 ++++++++++++++++++ src/framework/include/dpl/test/test_ignored.h | 46 +++++++++++++++ src/framework/include/dpl/test/test_runner.h | 77 +++++-------------------- src/framework/src/test_failed.cpp | 64 ++++++++++++++++++++ src/framework/src/test_runner.cpp | 47 ++------------- src/framework/src/test_runner_child.cpp | 22 +++---- src/framework/src/test_runner_multiprocess.cpp | 22 +++---- 11 files changed, 273 insertions(+), 135 deletions(-) create mode 100644 src/framework/include/dpl/test/test_exception.h create mode 100644 src/framework/include/dpl/test/test_failed.h create mode 100644 src/framework/include/dpl/test/test_ignored.h create mode 100644 src/framework/src/test_failed.cpp diff --git a/src/common/db_sqlite.h b/src/common/db_sqlite.h index 9a17083..42092cf 100644 --- a/src/common/db_sqlite.h +++ b/src/common/db_sqlite.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2012-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -115,14 +115,14 @@ public: * * If database is already opened do nothing. * - * @throw DPL::Test::TestRunner::TestFailed when opening database fails + * @throw DPL::Test::TestFailed when opening database fails */ void open(void); /** * @brief Close database. * - * @throw DPL::Test::TestRunner::TestFailed when closing database fails + * @throw DPL::Test::TestFailed when closing database fails */ void close(void); @@ -140,7 +140,7 @@ public: * @param sql_query SQL query * @param result returned result * - * @throw DPL::Test::TestRunner::TestFailed when execution of query fails + * @throw DPL::Test::TestFailed when execution of query fails */ void execute(const std::string& sql_query, Sqlite3DBaseSelectResult& result); diff --git a/src/cynara-tests/common/cynara_test_commons.cpp b/src/cynara-tests/common/cynara_test_commons.cpp index e35477c..8c2f5a1 100644 --- a/src/cynara-tests/common/cynara_test_commons.cpp +++ b/src/cynara-tests/common/cynara_test_commons.cpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include @@ -43,18 +43,15 @@ void environmentWrap(const char *testName, const std::function &func try { func(); - } catch (const DPL::Test::TestRunner::TestFailed &e) { + } catch (const DPL::Test::TestException &e) { env.restore(); - throw e; - } catch (const DPL::Test::TestRunner::Ignored &e) { - env.restore(); - throw e; + throw; } catch (const DPL::Exception &e) { env.restore(); - throw e; + throw; } catch (const std::exception &e) { env.restore(); - throw e; + throw; } catch (...) { env.restore(); throw std::runtime_error("Unknown exception"); diff --git a/src/framework/config.cmake b/src/framework/config.cmake index 411f09e..72b3d2c 100644 --- a/src/framework/config.cmake +++ b/src/framework/config.cmake @@ -32,6 +32,7 @@ SET(DPL_FRAMEWORK_TEST_SOURCES ${PROJECT_SOURCE_DIR}/src/framework/src/dlog_log_provider.cpp ${PROJECT_SOURCE_DIR}/src/framework/src/log.cpp ${PROJECT_SOURCE_DIR}/src/framework/src/old_style_log_provider.cpp + ${PROJECT_SOURCE_DIR}/src/framework/src/test_failed.cpp ${PROJECT_SOURCE_DIR}/src/framework/src/test_results_collector.cpp ${PROJECT_SOURCE_DIR}/src/framework/src/test_results_collector_commons.cpp ${PROJECT_SOURCE_DIR}/src/framework/src/test_results_collector_console.cpp diff --git a/src/framework/include/dpl/test/test_exception.h b/src/framework/include/dpl/test/test_exception.h new file mode 100644 index 0000000..49e4739 --- /dev/null +++ b/src/framework/include/dpl/test/test_exception.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file test_exception.h + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file is the header file of test_exception base class + */ + +#ifndef DPL_TEST_EXCEPTION_H +#define DPL_TEST_EXCEPTION_H + +#include + +namespace DPL { +namespace Test { + +class TestException +{ + public: + std::string GetMessage() const + { + return m_message; + } + + protected: + std::string m_message; + + TestException() {} + TestException(const std::string &message) : + m_message(message) {} + +}; + +} // namespace Test +} // namespace DPL + +#endif // DPL_TEST_EXCEPTION_H diff --git a/src/framework/include/dpl/test/test_failed.h b/src/framework/include/dpl/test/test_failed.h new file mode 100644 index 0000000..f446094 --- /dev/null +++ b/src/framework/include/dpl/test/test_failed.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file test_failed.h + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file is the header file of TestFailed class + */ + +#ifndef DPL_TEST_FAILED_H +#define DPL_TEST_FAILED_H + +#include + +#include + +namespace DPL { +namespace Test { + +class TestFailed : public TestException +{ + public: + TestFailed() = default; + + //! \brief Failed test message creator + //! + //! \param[in] aTest string for tested expression + //! \param[in] aFile source file name + //! \param[in] aLine source file line + //! \param[in] aMessage error message + TestFailed(const char* aTest, + const char* aFile, + int aLine, + const std::string &aMessage); + + TestFailed(const std::string &message) : + TestException(message) + {} +}; + +} // namespace Test +} // namespace DPL + +#endif // DPL_TEST_FAILED_H diff --git a/src/framework/include/dpl/test/test_ignored.h b/src/framework/include/dpl/test/test_ignored.h new file mode 100644 index 0000000..17a5c0a --- /dev/null +++ b/src/framework/include/dpl/test/test_ignored.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file test_ignored.h + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file is the header file of TestIgnored class + */ + +#ifndef DPL_TEST_IGNORED_H +#define DPL_TEST_IGNORED_H + +#include + +#include + +namespace DPL { +namespace Test { + +class TestIgnored : public TestException +{ + public: + TestIgnored() = default; + + TestIgnored(const std::string &message) : + TestException(message) + {} +}; + +} // namespace Test +} // namespace DPL + +#endif // DPL_TEST_IGNORED_H diff --git a/src/framework/include/dpl/test/test_runner.h b/src/framework/include/dpl/test/test_runner.h index 373b319..5f070c8 100644 --- a/src/framework/include/dpl/test/test_runner.h +++ b/src/framework/include/dpl/test/test_runner.h @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include namespace DPL { @@ -152,53 +154,6 @@ class TestRunner const std::chrono::system_clock::duration& performanceMaxTime = std::chrono::microseconds::zero()); public: - class TestFailed - { - private: - std::string m_message; - - public: - TestFailed() - {} - - //! \brief Failed test message creator - //! - //! \param[in] aTest string for tested expression - //! \param[in] aFile source file name - //! \param[in] aLine source file line - //! \param[in] aMessage error message - TestFailed(const char* aTest, - const char* aFile, - int aLine, - const std::string &aMessage); - - TestFailed(const std::string &message); - - std::string GetMessage() const - { - return m_message; - } - }; - - class Ignored - { - private: - std::string m_message; - - public: - Ignored() - {} - - Ignored(const std::string &message) : - m_message(message) - {} - - std::string GetMessage() const - { - return m_message; - } - }; - void MarkAssertion(); void RegisterTest(const char *testName, TestCase proc); @@ -253,10 +208,10 @@ typedef DPL::Singleton TestRunnerSingleton; { \ std::ostringstream assertMsg; \ assertMsg << message << DPL::gdbbacktrace(); \ - DPL::Test::TestRunner::TestFailed e(#test, \ - __FILE__, \ - __LINE__, \ - assertMsg.str()); \ + DPL::Test::TestFailed e(#test, \ + __FILE__, \ + __LINE__, \ + assertMsg.str()); \ if (!std::uncaught_exception()) \ throw e; \ DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \ @@ -276,10 +231,10 @@ typedef DPL::Singleton TestRunnerSingleton; if (!assertMsg.str().empty()) \ assertMsg << ". "; \ assertMsg << err << DPL::gdbbacktrace(); \ - DPL::Test::TestRunner::TestFailed e(#test, \ - __FILE__, \ - __LINE__, \ - assertMsg.str()); \ + DPL::Test::TestFailed e(#test, \ + __FILE__, \ + __LINE__, \ + assertMsg.str()); \ if (!std::uncaught_exception()) \ throw e; \ DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \ @@ -303,12 +258,12 @@ typedef DPL::Singleton TestRunnerSingleton; * body. */ -#define RUNNER_IGNORED_MSG(message) \ - do \ - { \ - std::ostringstream assertMsg; \ - assertMsg << message; \ - throw DPL::Test::TestRunner::Ignored(assertMsg.str()); \ +#define RUNNER_IGNORED_MSG(message) \ + do \ + { \ + std::ostringstream assertMsg; \ + assertMsg << message; \ + throw DPL::Test::TestIgnored(assertMsg.str()); \ } while (0) /** diff --git a/src/framework/src/test_failed.cpp b/src/framework/src/test_failed.cpp new file mode 100644 index 0000000..c8de070 --- /dev/null +++ b/src/framework/src/test_failed.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file test_failed.cpp + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file is the implementation file of TestFailed class + */ + +#include +#include +#include +#include +#include + +#include + +namespace DPL { +namespace Test { +namespace // anonymous +{ +std::string BaseName(const std::string &aPath) +{ + std::unique_ptr path(strdup(aPath.c_str()), free); + if (!path) + throw std::bad_alloc(); + + return basename(path.get()); +} +} // namespace anonymous + +//! \brief Failed test message creator +//! +//! \param[in] aTest string for tested expression +//! \param[in] aFile source file name +//! \param[in] aLine source file line +//! \param[in] aMessage error message +TestFailed::TestFailed(const char* aTest, + const char* aFile, + int aLine, + const std::string &aMessage) +{ + std::ostringstream assertMsg; + assertMsg << "[" << BaseName(aFile) << ":" << aLine + << "] Assertion failed (" + << aTest << ") " << aMessage; + m_message = assertMsg.str(); +} + +} // namespace Test +} // namespace DPL diff --git a/src/framework/src/test_runner.cpp b/src/framework/src/test_runner.cpp index ae9bbc5..dea2dda 100644 --- a/src/framework/src/test_runner.cpp +++ b/src/framework/src/test_runner.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* +/** * @file test_runner.cpp * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) * @author Lukasz Wrzosek (l.wrzosek@samsung.com) @@ -21,6 +21,8 @@ * @brief This file is the implementation file of test runner */ #include +#include +#include #include #include #include @@ -31,9 +33,6 @@ #include #include #include -#include -#include -#include #include #include @@ -59,42 +58,6 @@ std::string getXMLNode(xmlNodePtr node) namespace DPL { namespace Test { -namespace // anonymous -{ -std::string BaseName(std::string aPath) -{ - ScopedFree path(strdup(aPath.c_str())); - if (nullptr == path.Get()) { - throw std::bad_alloc(); - } - char* baseName = basename(path.Get()); - std::string retValue = baseName; - return retValue; -} -} // namespace anonymous - -//! \brief Failed test message creator -//! -//! \param[in] aTest string for tested expression -//! \param[in] aFile source file name -//! \param[in] aLine source file line -//! \param[in] aMessage error message -TestRunner::TestFailed::TestFailed(const char* aTest, - const char* aFile, - int aLine, - const std::string &aMessage) -{ - std::ostringstream assertMsg; - assertMsg << "[" << BaseName(aFile) << ":" << aLine - << "] Assertion failed (" - << aTest << ") " << aMessage; - m_message = assertMsg.str(); -} - -TestRunner::TestFailed::TestFailed(const std::string &message) -{ - m_message = message; -} void TestRunner::RegisterTest(const char *testName, TestCase proc) { @@ -256,7 +219,7 @@ TestRunner::Status TestRunner::RunTestCase(const TestCaseStruct& testCase) setCurrentTestCase(nullptr); return FAILED; - } catch (const Ignored &e) { + } catch (const TestIgnored &e) { if (m_runIgnored) { // Simple test have to be implemented CollectResult(testCase.name, diff --git a/src/framework/src/test_runner_child.cpp b/src/framework/src/test_runner_child.cpp index 19ed08c..816316a 100644 --- a/src/framework/src/test_runner_child.cpp +++ b/src/framework/src/test_runner_child.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,13 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* +/** * @file test_runner_child.cpp * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com) * @version 1.0 * @brief This file is the implementation file of test runner */ #include +#include +#include #include #include #include @@ -297,13 +299,13 @@ void RunChildProc(TestRunner::TestCase procChild) { PipeWrapper pipe; if (!pipe.isReady()) { - throw TestRunner::TestFailed("Pipe creation failed"); + throw TestFailed("Pipe creation failed"); } pid_t pid = fork(); if (pid == -1) { - throw TestRunner::TestFailed("Child creation failed"); + throw TestFailed("Child creation failed"); } if (pid != 0) { @@ -327,11 +329,11 @@ void RunChildProc(TestRunner::TestCase procChild) waitpid(pid, &status, 0); if (pipeReturn == PipeWrapper::TIMEOUT) { - throw TestRunner::TestFailed("Timeout"); + throw TestFailed("Timeout"); } if (pipeReturn == PipeWrapper::ERROR) { - throw TestRunner::TestFailed("Reading pipe error"); + throw TestFailed("Reading pipe error"); } if (code == CHILD_TEST_PASS && msgType == MSG_TYPE_PERF_TIME) { @@ -341,9 +343,9 @@ void RunChildProc(TestRunner::TestCase procChild) } if (code == CHILD_TEST_FAIL) { - throw TestRunner::TestFailed(message); + throw TestFailed(message); } else if (code == CHILD_TEST_IGNORED) { - throw TestRunner::Ignored(message); + throw TestIgnored(message); } } else { // child code @@ -368,10 +370,10 @@ void RunChildProc(TestRunner::TestCase procChild) try { procChild(); - } catch (const DPL::Test::TestRunner::TestFailed &e) { + } catch (const DPL::Test::TestFailed &e) { msg = e.GetMessage(); code = CHILD_TEST_FAIL; - } catch (const DPL::Test::TestRunner::Ignored &e) { + } catch (const DPL::Test::TestIgnored &e) { msg = e.GetMessage(); code = CHILD_TEST_IGNORED; } catch (...) { // catch all exception generated by "user" code diff --git a/src/framework/src/test_runner_multiprocess.cpp b/src/framework/src/test_runner_multiprocess.cpp index 989654a..55e889b 100644 --- a/src/framework/src/test_runner_multiprocess.cpp +++ b/src/framework/src/test_runner_multiprocess.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* +/** * @file test_runner_multiprocess.cpp * @author Marcin Niesluchowski (m.niesluchow@samsung.com) * @version 1.0 @@ -22,6 +22,8 @@ #include #include +#include +#include #include #include #include @@ -141,16 +143,16 @@ void RunMultiProc(TestRunner::TestCase procMulti) pid_t top_pid = getpid(); if (!pipe.isReady()) { - throw TestRunner::TestFailed("Pipe creation failed"); + throw TestFailed("Pipe creation failed"); } // pipe try { procMulti(); - } catch (const TestRunner::TestFailed &e) { + } catch (const TestFailed &e) { code = MULTI_TEST_FAILED; msg = e.GetMessage(); - } catch (const TestRunner::Ignored &e) { + } catch (const TestIgnored &e) { code = MULTI_TEST_IGNORED; msg = e.GetMessage(); } catch (const std::exception &) { @@ -180,10 +182,10 @@ void RunMultiProc(TestRunner::TestCase procMulti) } if (pipeReturn == PipeWrapper::ERROR) { pipe.closeAll(); - throw TestRunner::TestFailed("Reading pipe error"); + throw TestFailed("Reading pipe error"); } else if (pipeReturn == PipeWrapper::TIMEOUT) { pipe.closeAll(); - throw TestRunner::TestFailed("Timeout error"); + throw TestFailed("Timeout error"); } msg = msg + "\n" + recMsg; } @@ -193,11 +195,11 @@ void RunMultiProc(TestRunner::TestCase procMulti) case MULTI_TEST_PASS: return; case MULTI_TEST_FAILED: - throw TestRunner::TestFailed(msg); + throw TestFailed(msg); case MULTI_TEST_IGNORED: - throw TestRunner::Ignored(msg); + throw TestIgnored(msg); default: - throw TestRunner::TestFailed(msg); + throw TestFailed(msg); } } else { pipe.setUsage(PipeWrapper::WRITEONLY); -- 2.7.4