Define a base TestException class 48/34048/9
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Thu, 19 Feb 2015 14:27:36 +0000 (15:27 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Tue, 17 Mar 2015 14:49:13 +0000 (15:49 +0100)
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
src/cynara-tests/common/cynara_test_commons.cpp
src/framework/config.cmake
src/framework/include/dpl/test/test_exception.h [new file with mode: 0644]
src/framework/include/dpl/test/test_failed.h [new file with mode: 0644]
src/framework/include/dpl/test/test_ignored.h [new file with mode: 0644]
src/framework/include/dpl/test/test_runner.h
src/framework/src/test_failed.cpp [new file with mode: 0644]
src/framework/src/test_runner.cpp
src/framework/src/test_runner_child.cpp
src/framework/src/test_runner_multiprocess.cpp

index 9a17083..42092cf 100644 (file)
@@ -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);
 
index e35477c..8c2f5a1 100644 (file)
@@ -20,7 +20,7 @@
 #include <cynara_test_cynara_mask.h>
 
 #include <dpl/exception.h>
-#include <dpl/test/test_runner.h>
+#include <dpl/test/test_exception.h>
 
 #include <exception>
 
@@ -43,18 +43,15 @@ void environmentWrap(const char *testName, const std::function<void(void)> &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");
index 411f09e..72b3d2c 100644 (file)
@@ -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 (file)
index 0000000..49e4739
--- /dev/null
@@ -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 <l.wojciechow@partner.samsung.com>
+ * @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 <string>
+
+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 (file)
index 0000000..f446094
--- /dev/null
@@ -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 <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       This file is the header file of TestFailed class
+ */
+
+#ifndef DPL_TEST_FAILED_H
+#define DPL_TEST_FAILED_H
+
+#include <string>
+
+#include <dpl/test/test_exception.h>
+
+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 (file)
index 0000000..17a5c0a
--- /dev/null
@@ -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 <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       This file is the header file of TestIgnored class
+ */
+
+#ifndef DPL_TEST_IGNORED_H
+#define DPL_TEST_IGNORED_H
+
+#include <string>
+
+#include <dpl/test/test_exception.h>
+
+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
index 373b319..5f070c8 100644 (file)
@@ -41,6 +41,8 @@
 #include <dpl/colors.h>
 #include <dpl/gdbbacktrace.h>
 #include <dpl/singleton.h>
+#include <dpl/test/test_failed.h>
+#include <dpl/test/test_ignored.h>
 #include <dpl/test/test_results_collector.h>
 
 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<TestRunner> 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<TestRunner> 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<TestRunner> 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 (file)
index 0000000..c8de070
--- /dev/null
@@ -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 <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       This file is the implementation file of TestFailed class
+ */
+
+#include <cstdlib>
+#include <cstring>
+#include <libgen.h>
+#include <memory>
+#include <sstream>
+
+#include <dpl/test/test_failed.h>
+
+namespace DPL {
+namespace Test {
+namespace // anonymous
+{
+std::string BaseName(const std::string &aPath)
+{
+    std::unique_ptr<char, decltype(free)*> 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
index ae9bbc5..dea2dda 100644 (file)
@@ -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 <stddef.h>
+#include <dpl/test/test_failed.h>
+#include <dpl/test/test_ignored.h>
 #include <dpl/test/test_runner.h>
 #include <dpl/test/test_results_collector.h>
 #include <dpl/exception.h>
@@ -31,9 +33,6 @@
 #include <algorithm>
 #include <cstdio>
 #include <memory.h>
-#include <libgen.h>
-#include <cstring>
-#include <cstdlib>
 
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
@@ -59,42 +58,6 @@ std::string getXMLNode(xmlNodePtr node)
 
 namespace DPL {
 namespace Test {
-namespace // anonymous
-{
-std::string BaseName(std::string aPath)
-{
-    ScopedFree<char> 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,
index 19ed08c..816316a 100644 (file)
@@ -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.
  *    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 <stddef.h>
+#include <dpl/test/test_failed.h>
+#include <dpl/test/test_ignored.h>
 #include <dpl/test/test_runner.h>
 #include <dpl/test/test_runner_child.h>
 #include <dpl/test/test_results_collector.h>
@@ -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
index 989654a..55e889b 100644 (file)
@@ -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 <sys/file.h>
 #include <dpl/exception.h>
+#include <dpl/test/test_failed.h>
+#include <dpl/test/test_ignored.h>
 #include <dpl/test/test_runner.h>
 #include <dpl/test/test_runner_child.h>
 #include <dpl/test/test_runner_multiprocess.h>
@@ -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);